Skip to content

Commit

Permalink
add defered logic, for bindings by full quality name
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Jul 7, 2024
1 parent 4d629c7 commit dfbd155
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions runtime/ishtar.vm/FFI/ForeignFunctionInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ public unsafe class ForeignFunctionInterface
public readonly VirtualMachine vm;
public NativeDictionary<ulong, RuntimeIshtarMethod>* methods { get; }
public Dictionary<string, ulong> method_table { get; } = new();
public AtomicNativeDictionary<ulong, PInvokeInfo>* deferMethods { get; }

Check failure on line 16 in runtime/ishtar.vm/FFI/ForeignFunctionInterface.cs

View workflow job for this annotation

GitHub Actions / build_all (macos-latest, osx-x64, false)

The type 'ishtar.PInvokeInfo' cannot be used as type parameter 'TValue' in the generic type or method 'AtomicNativeDictionary<TKey, TValue>'. There is no boxing conversion from 'ishtar.PInvokeInfo' to 'System.IEquatable<ishtar.PInvokeInfo>'.

private ulong _index;

public ForeignFunctionInterface(VirtualMachine vm)
{
this.vm = vm;
methods = IshtarGC.AllocateDictionary<ulong, RuntimeIshtarMethod>(vm.@ref);
deferMethods = IshtarGC.AllocateAtomicDictionary<ulong, PInvokeInfo>(vm.@ref);
INIT();
}
public PInvokeInfo AsNative(delegate*<CallFrame*, IshtarObject**, IshtarObject*> p)
{
var result = new PInvokeInfo
{
isInternal = true,
compiled_func_ref = (nint)p,
};
return result;
}

private void INIT()
{
Expand All @@ -38,6 +49,7 @@ private void INIT()
//B_Field.InitTable(this);
//B_Function.InitTable(this);
//B_NAPI.InitTable(this);
B_Threading.InitTable(this);
}

public RuntimeIshtarMethod* Add(string name, MethodFlags flags, VeinTypeCode returnType, params (string name, VeinTypeCode type)[] args)
Expand All @@ -62,6 +74,13 @@ private void INIT()
return method;
}

public void Add(string fullName, PInvokeInfo nativeInfo)
{
_index++;
method_table.Add(fullName, _index);
deferMethods->Add(_index, nativeInfo);
}

[Conditional("STATIC_VALIDATE_IL")]
public static void StaticValidate(void* p, CallFrame* frame)
{
Expand Down Expand Up @@ -106,10 +125,22 @@ public static void StaticTypeOf(CallFrame* current, IshtarObject** arg1, VeinTyp
VirtualMachine.Assert(@class->TypeCode == code, WNE.TYPE_MISMATCH, $"@class.{@class->TypeCode} == {code}", current);
}

public RuntimeIshtarMethod* GetMethod(string FullName)
public void GetMethod(string FullName, out PInvokeInfo nativeHeader)
{
if (method_table.TryGetValue(FullName, out var idx))
return methods->Get(idx);
{
if (methods->ContainsKey(idx))
{
nativeHeader = methods->Get(idx)->PIInfo;
return;
}
else if (deferMethods->ContainsKey(idx))
{
nativeHeader = deferMethods->Get(idx);
return;
}
}
vm.FastFail(WNE.MISSING_METHOD, $"method '{FullName}' is not found", vm.Frames->NativeLoader);
throw new EntryPointNotFoundException(FullName);
}

Expand All @@ -127,9 +158,11 @@ public static void LinkExternalNativeLibrary(string importModule, string fnName,

public void DisplayDefinedMapping()
{
if (vm.Config.HasFlag(SysFlag.DISPLAY_FFI_MAPPING)) return;
if (vm.Config.HasFlag(SysFlag.DISPLAY_FFI_MAPPING))
return;

foreach (var (key, value) in method_table) vm.trace.println($"ffi map '{key}' -> 'sys::FFI/{(GetMethod(value))->Name}'");
foreach (var (key, value) in method_table)
vm.trace.println($"ffi map '{key}' -> 'sys::FFI/{(GetMethod(value))->Name}'");
}
}

Expand Down

0 comments on commit dfbd155

Please sign in to comment.