Skip to content

Commit

Permalink
Merge pull request #271 from vein-lang/feature/finally-update-vm-to-u…
Browse files Browse the repository at this point in the history
…nsafe

Feature/finally update vm to unsafe
  • Loading branch information
0xF6 authored Aug 12, 2024
2 parents 84c7c7b + 1f8c4c6 commit 85de4c7
Show file tree
Hide file tree
Showing 63 changed files with 1,190 additions and 1,018 deletions.
24 changes: 12 additions & 12 deletions runtime/ishtar.vm/FFI/ForeignFunctionInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ namespace ishtar;
using System.Collections.Generic;
using System.Linq;
using collections;
using ishtar.vm.__builtin.networks;
using ishtar.__builtin.networks;
using networks;
using runtime;
using runtime.gc;
using vein.runtime;

public unsafe class ForeignFunctionInterface
{
public readonly VirtualMachine vm;
public readonly VirtualMachine* vm;
public NativeDictionary<ulong, RuntimeIshtarMethod>* methods { get; }
public Dictionary<string, ulong> method_table { get; } = new();
public AtomicNativeDictionary<ulong, PInvokeInfo>* deferMethods { get; }

private ulong _index;

public ForeignFunctionInterface(VirtualMachine vm)
public ForeignFunctionInterface(VirtualMachine* vm)
{
this.vm = vm;
methods = IshtarGC.AllocateDictionary<ulong, RuntimeIshtarMethod>(vm.@ref);
deferMethods = IshtarGC.AllocateAtomicDictionary<ulong, PInvokeInfo>(vm.@ref);
methods = IshtarGC.AllocateDictionary<ulong, RuntimeIshtarMethod>(vm);
deferMethods = IshtarGC.AllocateAtomicDictionary<ulong, PInvokeInfo>(vm);
INIT();
}
public PInvokeInfo AsNative(delegate*<CallFrame*, IshtarObject**, IshtarObject*> p)
Expand Down Expand Up @@ -62,7 +62,7 @@ private void INIT()
}

public RuntimeIshtarMethod* Add(string name, MethodFlags flags, VeinTypeCode returnType, params (string name, VeinTypeCode type)[] args)
=> Add(name, flags, vm.Types->ByTypeCode(returnType), args);
=> Add(name, flags, vm->Types->ByTypeCode(returnType), args);

public RuntimeIshtarMethod* Add(string name, MethodFlags flags, RuntimeIshtarClass* returnType, params (string name, VeinTypeCode type)[] args)
{
Expand All @@ -72,10 +72,10 @@ private void INIT()
for (int index = 0; index < args.Length; index++)
{
var (_, type) = args[index];
arr[index] = vm.Types->ByTypeCode(type);
arr[index] = vm->Types->ByTypeCode(type);
}

var method = vm.CreateInternalMethod(
var method = vm->CreateInternalMethod(
RuntimeIshtarMethod.GetFullName(name, returnType, arr), flags, args);
method->Assert(method);
method_table.Add(method->Name, _index);
Expand All @@ -94,7 +94,7 @@ public void Add(string fullName, PInvokeInfo nativeInfo)
public static void StaticValidate(void* p, CallFrame* frame)
{
if (p != null) return;
frame->vm.FastFail(WNE.STATE_CORRUPT, "Null pointer state.", frame);
frame->vm->FastFail(WNE.STATE_CORRUPT, "Null pointer state.", frame);
}

[Conditional("STATIC_VALIDATE_IL")]
Expand Down Expand Up @@ -149,7 +149,7 @@ public void GetMethod(string FullName, out PInvokeInfo nativeHeader)
return;
}
}
vm.FastFail(WNE.MISSING_METHOD, $"method '{FullName}' is not found", vm.Frames->NativeLoader);
vm->FastFail(WNE.MISSING_METHOD, $"method '{FullName}' is not found", vm->Frames->NativeLoader);
throw new EntryPointNotFoundException(FullName);
}

Expand All @@ -159,7 +159,7 @@ public void GetMethod(string FullName, out PInvokeInfo nativeHeader)
public static void LinkExternalNativeLibrary(string importModule, string fnName,
RuntimeIshtarMethod* importCaller)
{
var jitter = importCaller->Owner->Owner->vm.Jitter;
var jitter = importCaller->Owner->Owner->vm->Jitter;

jitter.CompileFFI(importCaller, importModule, fnName);
}
Expand All @@ -168,7 +168,7 @@ public static void LinkExternalNativeLibrary(string importModule, string fnName,
public void DisplayDefinedMapping()
{
foreach (var (key, value) in method_table)
vm.trace.log($"ffi map '{key}' -> 'sys::FFI/{(GetMethod(value))->Name}'");
vm->trace.log($"ffi map '{key}' -> 'sys::FFI/{(GetMethod(value))->Name}'");
}
}

Expand Down
12 changes: 12 additions & 0 deletions runtime/ishtar.vm/FFI/generated/Vein_ClosureDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ishtar;

public readonly unsafe struct Vein_ClosureDelegate(IshtarObject* o)
{
public readonly IshtarObject* Object = o;

public IshtarObject* Scope => ((IshtarObject*)Object->vtable[Object->clazz->Field["_scope"]->vtable_offset]);

public rawval* Function => ((rawval*)Object->vtable[Object->clazz->Field["_fn"]->vtable_offset]);

public bool IsVolatile => Scope is null;
}
21 changes: 21 additions & 0 deletions runtime/ishtar.vm/FFI/generated/Vein_IpEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ishtar;

using ishtar.__builtin.networks;

public readonly unsafe struct Vein_IpEndpoint(IshtarObject* o)
{
public readonly IshtarObject* Object = o;


public ushort port
{
get => ((IshtarObject*)Object->vtable[Object->clazz->Field["port"]->vtable_offset])->GetUInt16();
set => ((IshtarObject*)Object->vtable[Object->clazz->Field["port"]->vtable_offset])->SetUInt16(value);
}

public Vein_Ipv4Addr address
{
get => new((IshtarObject*)Object->vtable[Object->clazz->Field["address"]->vtable_offset]);
set => Object->vtable[Object->clazz->Field["address"]->vtable_offset] = value.Object;
}
}
28 changes: 28 additions & 0 deletions runtime/ishtar.vm/FFI/generated/Vein_Ipv4Addr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ishtar;

public readonly unsafe struct Vein_Ipv4Addr(IshtarObject* o)
{
public readonly IshtarObject* Object = o;

public byte first
{
get => ((IshtarObject*)Object->vtable[Object->clazz->Field["first"]->vtable_offset])->GetUInt8();
set => ((IshtarObject*)Object->vtable[Object->clazz->Field["first"]->vtable_offset])->SetUInt8(value);
}
public byte second
{
get => ((IshtarObject*)Object->vtable[Object->clazz->Field["second"]->vtable_offset])->GetUInt8();
set => ((IshtarObject*)Object->vtable[Object->clazz->Field["second"]->vtable_offset])->SetUInt8(value);
}
public byte third
{
get => ((IshtarObject*)Object->vtable[Object->clazz->Field["third"]->vtable_offset])->GetUInt8();
set => ((IshtarObject*)Object->vtable[Object->clazz->Field["third"]->vtable_offset])->SetUInt8(value);
}
public byte fourth
{
get => ((IshtarObject*)Object->vtable[Object->clazz->Field["fourth"]->vtable_offset])->GetUInt8();
set => ((IshtarObject*)Object->vtable[Object->clazz->Field["fourth"]->vtable_offset])->SetUInt8(value);
}
}

13 changes: 13 additions & 0 deletions runtime/ishtar.vm/FFI/generated/Vein_SocketHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ishtar;

using static libuv.LibUV;

public readonly unsafe struct Vein_SocketHandle(IshtarObject* o)
{
public uv_tcp_t* server_handle
{
get => (uv_tcp_t*)o->vtable[o->clazz->Field["server_handle"]->vtable_offset];
set => o->vtable[o->clazz->Field["server_handle"]->vtable_offset] = value;
}
}

12 changes: 6 additions & 6 deletions runtime/ishtar.vm/__builtin/B_App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public static unsafe class B_App
var gc = current->GetGC();
// TODO remove using RuntimeInformation
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return gc.ToIshtarObject(0, current);
return gc->ToIshtarObject(0, current);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return gc.ToIshtarObject(1, current);
return gc->ToIshtarObject(1, current);
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return gc.ToIshtarObject(2, current);
return gc.ToIshtarObject(-1, current);
return gc->ToIshtarObject(2, current);
return gc->ToIshtarObject(-1, current);
}


Expand All @@ -31,7 +31,7 @@ public static unsafe class B_App
ForeignFunctionInterface.StaticTypeOf(current, &exitCode, TYPE_I4);
ForeignFunctionInterface.StaticValidateField(current, &exitCode, "!!value");

current->vm.halt(IshtarMarshal.ToDotnetInt32(exitCode, current));
current->vm->halt(IshtarMarshal.ToDotnetInt32(exitCode, current));

return null;
}
Expand All @@ -54,7 +54,7 @@ public static unsafe class B_App
var clr_key = IshtarMarshal.ToDotnetString(key, current);
var clr_value = IshtarMarshal.ToDotnetBoolean(value, current);

//current->vm.Config.Set(clr_key, clr_value);
//current->vm->Config.Set(clr_key, clr_value);

return null;
}
Expand Down
10 changes: 5 additions & 5 deletions runtime/ishtar.vm/__builtin/B_Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public static unsafe class B_Field
public static void InitTable(ForeignFunctionInterface ffi)
{
//var table = ffi.method_table;
//ffi.vm.CreateInternalMethod("i_call_Field_setValue", Public | Static | Extern,
// new VeinArgumentRef("targetObject", ffi.vm.Types.ObjectClass),
//ffi.vm->CreateInternalMethod("i_call_Field_setValue", Public | Static | Extern,
// new VeinArgumentRef("targetObject", ffi.vm->Types.ObjectClass),
// new VeinArgumentRef("f", ThisClass),
// new VeinArgumentRef("value", ffi.vm.Types.ObjectClass))
// new VeinArgumentRef("value", ffi.vm->Types.ObjectClass))
// .AsNative((delegate*<CallFrame, IshtarObject**, IshtarObject*>)&FieldSetValue)
// .AddInto(table, x => x.Name);
//ffi.vm.CreateInternalMethod("i_call_Field_getValue", Public | Static | Extern,
// new VeinArgumentRef("targetObject", ffi.vm.Types.ObjectClass),
//ffi.vm->CreateInternalMethod("i_call_Field_getValue", Public | Static | Extern,
// new VeinArgumentRef("targetObject", ffi.vm->Types.ObjectClass),
// new VeinArgumentRef("f", ThisClass))
// .AsNative((delegate*<CallFrame, IshtarObject**, IshtarObject*>)&FieldGetValue)
// .AddInto(table, x => x.Name);
Expand Down
14 changes: 7 additions & 7 deletions runtime/ishtar.vm/__builtin/B_Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public static unsafe class B_Function
public static void InitTable(ForeignFunctionInterface ffi)
{
//var table = ffi.method_table;
//ffi.vm.CreateInternalMethod("i_call_Function_call", Public | Static | Extern,
//ffi.vm->CreateInternalMethod("i_call_Function_call", Public | Static | Extern,
// new VeinArgumentRef("f", ThisClass),
// new VeinArgumentRef("args", ffi.vm.Types.ArrayClass))
// new VeinArgumentRef("args", ffi.vm->Types.ArrayClass))
// .AsNative((delegate*<CallFrame, IshtarObject**, IshtarObject*>)&Call)
// .AddInto(table, x => x.Name);
//ffi.vm.CreateInternalMethod("i_call_Function_create", Public | Static | Extern,
// new VeinArgumentRef("target", ffi.vm.Types.ObjectClass),
// new VeinArgumentRef("name", ffi.vm.Types.StringClass),
// new VeinArgumentRef("ignoreCase", ffi.vm.Types.BoolClass),
// new VeinArgumentRef("throwWhenFailBind", ffi.vm.Types.BoolClass)
//ffi.vm->CreateInternalMethod("i_call_Function_create", Public | Static | Extern,
// new VeinArgumentRef("target", ffi.vm->Types.ObjectClass),
// new VeinArgumentRef("name", ffi.vm->Types.StringClass),
// new VeinArgumentRef("ignoreCase", ffi.vm->Types.BoolClass),
// new VeinArgumentRef("throwWhenFailBind", ffi.vm->Types.BoolClass)
// )
// .AsNative((delegate*<CallFrame, IshtarObject**, IshtarObject*>)&Create)
// .AddInto(table, x => x.Name);
Expand Down
4 changes: 2 additions & 2 deletions runtime/ishtar.vm/__builtin/B_GC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public static unsafe class B_GC
[IshtarExport(0, "i_call_GC_get_allocated")]
[IshtarExportFlags(Public | Static)]
public static IshtarObject* GetAllocatedBytes(CallFrame* current, IshtarObject** _)
=> current->GetGC().ToIshtarObject(current->vm.GC.Stats.total_allocations, current);
=> current->GetGC()->ToIshtarObject(current->vm->gc->total_allocations, current);

[IshtarExport(0, "i_call_GC_get_alive_objects")]
[IshtarExportFlags(Public | Static)]
public static IshtarObject* GetAliveObjects(CallFrame* current, IshtarObject** _)
=> current->GetGC().ToIshtarObjectT(current->vm.GC.Stats.alive_objects, current);
=> current->GetGC()->ToIshtarObjectT(current->vm->gc->alive_objects, current);

public static void InitTable(ForeignFunctionInterface ffi)
{
Expand Down
2 changes: 1 addition & 1 deletion runtime/ishtar.vm/__builtin/B_IEEEConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static unsafe class B_IEEEConsts
[IshtarExportFlags(Public | Static)]
public static IshtarObject* getHalfNaN(CallFrame* current, IshtarObject** args)
{
current->vm.FastFail(WNE.MISSING_METHOD, "[B_IEEEConsts::getHalfNaN]", current);
current->vm->FastFail(WNE.MISSING_METHOD, "[B_IEEEConsts::getHalfNaN]", current);
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/ishtar.vm/__builtin/B_NAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static unsafe class B_NAPI
// return IshtarObject.NullPointer;
//}

//var result = current.vm.NativeStorage.TryLoad(libFile, out var h);
//var result = current.vm->NativeStorage.TryLoad(libFile, out var h);

//if (!result)
//{
Expand All @@ -42,7 +42,7 @@ public static unsafe class B_NAPI


//var handleClass = KnowTypes.VeinLang.Native.NativeHandle(current);
//var handleObj = current.vm.GC.AllocObject(handleClass, current);
//var handleObj = current.vm->gc.AllocObject(handleClass, current);

//var wrapper = new KnowTypes.WrappedTypes.S_NativeHandle(handleObj, current);

Expand All @@ -55,7 +55,7 @@ public static unsafe class B_NAPI
public static void InitTable(ForeignFunctionInterface ffi)
{
//var table = ffi.method_table;
//ffi.vm.CreateInternalMethod("i_call_NAPI_LoadNative", Public | Static | Extern)
//ffi.vm->CreateInternalMethod("i_call_NAPI_LoadNative", Public | Static | Extern)
// .AsNative((delegate*<CallFrame, IshtarObject**, IshtarObject*>)&LoadNativeLibrary)
// .AddInto(table, x => x.Name);
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/ishtar.vm/__builtin/B_Out.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public static unsafe class B_Out
IshtarMarshal.ToDotnetString(arg1, current) :
IshtarMarshal.ToDotnetString(IshtarMarshal.ToIshtarString(arg1, current), current);

current->vm.trace.console_std_write(str);
current->vm->trace.console_std_write(str);
return null;
}

[IshtarExport(0, "@_readline")]
[IshtarExportFlags(Public | Static)]
public static IshtarObject* FReadLine(CallFrame* current, IshtarObject** args)
=> current->GetGC().ToIshtarObject(In.ReadLine(), current);
=> current->GetGC()->ToIshtarObject(In.ReadLine(), current);


public static void InitTable(ForeignFunctionInterface ffi)
{
ffi.Add("@_println", Public | Static | Extern, TYPE_VOID, ("val", TYPE_OBJECT))
->AsNative((delegate*<CallFrame*, IshtarObject**, IshtarObject*>)&FPrintLn_Object);
ffi.Add("@_readline", Public | Static | Extern, TYPE_STRING.AsRuntimeClass(ffi.vm.Types))
ffi.Add("@_readline", Public | Static | Extern, TYPE_STRING.AsRuntimeClass(ffi.vm->Types))
->AsNative((delegate*<CallFrame*, IshtarObject**, IshtarObject*>)&FReadLine);
}
}
Expand Down
10 changes: 5 additions & 5 deletions runtime/ishtar.vm/__builtin/B_String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void InitTable(ForeignFunctionInterface ffi)

var result = string.Format(template, dotnet_arr);

return gc.ToIshtarObject(result, frame);
return gc->ToIshtarObject(result, frame);

}

Expand All @@ -100,7 +100,7 @@ public static void InitTable(ForeignFunctionInterface ffi)

var result = string.Concat(str1, str2);

return gc.ToIshtarObject(result, frame);
return gc->ToIshtarObject(result, frame);
}


Expand All @@ -123,7 +123,7 @@ public static void InitTable(ForeignFunctionInterface ffi)

var result = str1.Contains(str2);

return gc.ToIshtarObject(result, frame);
return gc->ToIshtarObject(result, frame);
}

[IshtarExportFlags(Private | Static)]
Expand All @@ -144,7 +144,7 @@ public static void InitTable(ForeignFunctionInterface ffi)

var result = str1.Equals(str2);

return gc.ToIshtarObject(result, frame);
return gc->ToIshtarObject(result, frame);
}

public static IshtarObject* TemplateFunctionApply(CallFrame* frame, IshtarObject** args, Func<string, string> apply)
Expand All @@ -158,7 +158,7 @@ public static void InitTable(ForeignFunctionInterface ffi)

var result = apply(clr_str);

return gc.ToIshtarObject(result, frame);
return gc->ToIshtarObject(result, frame);
}


Expand Down
Loading

0 comments on commit 85de4c7

Please sign in to comment.