Skip to content

Commit

Permalink
Merge pull request #235 from vein-lang/feature/callframe-now-struct
Browse files Browse the repository at this point in the history
CallFrame now a struct
  • Loading branch information
0xF6 authored Jun 9, 2024
2 parents 6503da7 + 0e75623 commit 6edf728
Show file tree
Hide file tree
Showing 57 changed files with 563 additions and 637 deletions.
30 changes: 15 additions & 15 deletions runtime/ishtar.vm/DefaultWatchDog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace ishtar;
using System.Threading;

[ExcludeFromCodeCoverage]
public class DefaultWatchDog(VirtualMachine vm) : IWatchDog
public unsafe class DefaultWatchDog(VirtualMachine vm) : IWatchDog
{
private static readonly object guarder = new();

void IWatchDog.FastFail(WNE type, string msg, CallFrame frame)
void IWatchDog.FastFail(WNE type, string msg, CallFrame* frame)
{
lock (guarder)
{
Expand All @@ -21,19 +21,19 @@ void IWatchDog.ValidateLastError()
{
lock (guarder)
{
if (vm.CurrentException is not null)
{
CallFrame.FillStackTrace(vm.CurrentException.frame);
Console.ForegroundColor = ConsoleColor.Red;
var err = $"native exception was thrown.\n\t" +
$"[{vm.CurrentException.code}]\n\t" +
$"'{vm.CurrentException.msg}'";
if (vm.CurrentException?.frame?.exception is not null)
err += $"\n{vm.CurrentException.frame.exception.stack_trace}";
vm.println(err);
Console.ForegroundColor = ConsoleColor.White;
vm.halt();
}
if (vm.CurrentException is null)
return;

CallFrame.FillStackTrace(vm.CurrentException.frame);
Console.ForegroundColor = ConsoleColor.Red;
var err = $"native exception was thrown.\n\t" +
$"[{vm.CurrentException.code}]\n\t" +
$"'{vm.CurrentException.msg}'";
if (vm.CurrentException is not null && vm.CurrentException.frame is not null && !vm.CurrentException.frame->exception.IsDefault())
err += $"\n{vm.CurrentException.frame->exception.GetStackTrace()}";
vm.println(err);
Console.ForegroundColor = ConsoleColor.White;
vm.halt();
}
}
}
20 changes: 10 additions & 10 deletions runtime/ishtar.vm/FFI/ForeignFunctionInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ private void INIT()
}

[Conditional("STATIC_VALIDATE_IL")]
public static void StaticValidate(void* p, CallFrame frame)
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")]
public static void StaticValidateField(CallFrame current, IshtarObject** arg1, string name)
public static void StaticValidateField(CallFrame* current, IshtarObject** arg1, string name)
{
StaticValidate(*arg1, current);
var @class = ( *arg1)->clazz;
Expand All @@ -67,27 +67,27 @@ public static void StaticValidateField(CallFrame current, IshtarObject** arg1, s
}

[Conditional("STATIC_VALIDATE_IL")]
public static void StaticValidate(CallFrame frame, stackval* value, RuntimeIshtarClass* clazz)
public static void StaticValidate(CallFrame* frame, stackval* value, RuntimeIshtarClass* clazz)
{
frame.assert(clazz is not null);
frame.assert(value->type != VeinTypeCode.TYPE_NONE);
frame->assert(clazz is not null);
frame->assert(value->type != VeinTypeCode.TYPE_NONE);
var obj = IshtarMarshal.Boxing(frame, value);
frame.assert(obj->__gc_id != -1);
frame->assert(obj->__gc_id != -1);
var currentClass = obj->clazz;
var targetClass = clazz;
frame.assert(currentClass->ID == targetClass->ID, $"{currentClass->Name}.ID == {targetClass->Name}.ID");
frame->assert(currentClass->ID == targetClass->ID, $"{currentClass->Name}.ID == {targetClass->Name}.ID");
}

[Conditional("STATIC_VALIDATE_IL")]
public static void StaticValidate(CallFrame current, IshtarObject** arg1)
public static void StaticValidate(CallFrame* current, IshtarObject** arg1)
{
StaticValidate(*arg1, current);
var @class = (*arg1)->clazz;
VirtualMachine.Assert(@class->is_inited, WNE.TYPE_LOAD, $"Class '{@class->FullName->NameWithNS}' corrupted.", current);
VirtualMachine.Assert(!@class->IsAbstract, WNE.TYPE_LOAD, $"Class '{@class->FullName->NameWithNS}' abstract.", current);
}
[Conditional("STATIC_VALIDATE_IL")]
public static void StaticTypeOf(CallFrame current, IshtarObject** arg1, VeinTypeCode code)
public static void StaticTypeOf(CallFrame* current, IshtarObject** arg1, VeinTypeCode code)
{
StaticValidate(*arg1, current);
var @class = (*arg1)->clazz;
Expand Down
2 changes: 1 addition & 1 deletion runtime/ishtar.vm/IAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ private FileInfo FindInPaths(string name)
protected override void debug(string s) { }


public CallFrame sys_frame => Vault.vm.Frames.ModuleLoaderFrame;
public CallFrame* sys_frame => Vault.vm.Frames.ModuleLoaderFrame;
}
}
4 changes: 2 additions & 2 deletions runtime/ishtar.vm/IWatchDog.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace ishtar
{
public interface IWatchDog
public unsafe interface IWatchDog
{
void FastFail(WNE type, string msg, CallFrame frame = null);
void FastFail(WNE type, string msg, CallFrame* frame = null);
void ValidateLastError();
}
}
38 changes: 20 additions & 18 deletions runtime/ishtar.vm/NativeExports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ public static void VM_INIT()

public static stackval* VM_EXECUTE_METHOD(VirtualMachine vm, Types.FrameRef* frame)
{
var vault = vm.Vault;
var type = vault.GlobalFindType(*frame->runtime_token);
throw null;
//var vault = vm.Vault;
//var type = vault.GlobalFindType(*frame->runtime_token);

if (type is null)
return null;
//if (type is null)
// return null;

if (type->Methods->Length >= frame->index)
return null;
//if (type->Methods->Length >= frame->index)
// return null;

var method = type->Methods->Get(frame->index);
//var method = type->Methods->Get(frame->index);

if (*method is not { } or { IsExtern: true } or { IsStatic: false })
return null;
//if (*method is not { } or { IsExtern: true } or { IsStatic: false })
// return null;



var callframe = new CallFrame(vm)
{
args = frame->args,
level = 0,
method = method
};
//var callframe = CallFrame.Create(method, null)

//var callframe = new CallFrame()
//{
// args = frame->args,
// level = 0,
// method = method
//};

vm.exec_method(callframe);
//vm.exec_method(callframe);

return callframe.returnValue.Ref;
//return callframe.returnValue.Ref;
}

public static class Types
Expand Down
18 changes: 8 additions & 10 deletions runtime/ishtar.vm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,26 @@

var args_ = stackalloc stackval[1];

var frame = new CallFrame(vm)
{
args = args_,
method = entry_point,
level = 0
};


var frame = CallFrame.Create(entry_point, null);
frame->args = args_;


var watcher = Stopwatch.StartNew();
vm.exec_method(frame);

if (frame.exception is not null)
if (!frame->exception.IsDefault())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"unhandled exception '{frame.exception.value->clazz->Name}' was thrown. \n" +
$"{frame.exception.stack_trace}");
Console.WriteLine($"unhandled exception '{frame->exception.value->clazz->Name}' was thrown. \n" +
$"{frame->exception.GetStackTrace()}");
Console.ForegroundColor = ConsoleColor.White;
}

watcher.Stop();
Console.WriteLine($"Elapsed: {watcher.Elapsed}");

frame->Dispose();
vm.Dispose();

return 0;
Expand Down
11 changes: 3 additions & 8 deletions runtime/ishtar.vm/SmartPointer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
namespace ishtar;

public readonly unsafe struct SmartPointer<T>(ushort size, CallFrame frame,
delegate*<CallFrame, int, T*> allocator,
delegate*<CallFrame, T*, int, void> free) : IDisposable where T : unmanaged
public readonly unsafe struct SmartPointer<T>(ushort size, CallFrame* frame,
delegate*<CallFrame*, int, T*> allocator,
delegate*<CallFrame*, T*, int, void> free) : IDisposable where T : unmanaged
{
#if DEBUG
private readonly nint[] OriginalAddress = new nint[1];

internal void CaptureAddress() => OriginalAddress[0] = (nint)Ref;
#endif
public readonly T* Ref = allocator(frame, size);
public readonly ushort size = size;

Expand Down
12 changes: 3 additions & 9 deletions runtime/ishtar.vm/Trace.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
namespace ishtar;

internal class IshtarTrace
internal struct IshtarTrace()
{
private static bool useConsole;
private static bool useFile;

public IshtarTrace()
{
useConsole = Environment.GetCommandLineArgs().Contains("--sys::log::use-console=1");
useFile = Environment.GetCommandLineArgs().Contains("--sys::log::use-file=1"); // TODO
}
private bool useConsole = Environment.GetCommandLineArgs().Contains("--sys::log::use-console=1");
private bool useFile = Environment.GetCommandLineArgs().Contains("--sys::log::use-file=1");

public void println(string s)
{
Expand Down
Loading

0 comments on commit 6edf728

Please sign in to comment.