Skip to content

Commit

Permalink
Merge pull request #279 from stevehalliwell/native-call-throws
Browse files Browse the repository at this point in the history
Merge native-call-throws into main
  • Loading branch information
stevehalliwell authored Feb 8, 2025
2 parents 48628e9 + 9fcf5f5 commit 483808d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ulox/ulox.core.tests/ByteCodeEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public void Engine_Assert()
Assert.AreEqual(1,2);
Assert.AreEqual(1,1);");

StringAssert.StartsWith("'1' does not equal '2' at ip:'1' in native:'AreEqual'.", testEngine.InterpreterResult);
StringAssert.Contains("'1' does not equal '2' at ip:'1' in native:'AreEqual'.", testEngine.InterpreterResult);
}

[Test]
Expand All @@ -707,7 +707,7 @@ public void Engine_Approx_Assert()
Assert.AreApproxEqual(1,1.000000001);
Assert.AreApproxEqual(1,2);");

StringAssert.StartsWith("'1' and '2' are '-1' apart.", testEngine.InterpreterResult);
StringAssert.Contains("'1' and '2' are '-1' apart.", testEngine.InterpreterResult);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion ulox/ulox.core.tests/DynamicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void Dyanic_RemoveFieldWhenReadOnly_ShouldError()
obj.RemoveField(obj, ""a"");");

StringAssert.StartsWith("Cannot remove field from read only", testEngine.InterpreterResult);
StringAssert.Contains("Cannot remove field from read only", testEngine.InterpreterResult);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion ulox/ulox.core.tests/InnerVMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fun InnerMain()
var innerVM = VM();
innerVM.Start(InnerMain);"
);
StringAssert.StartsWith("Global var of name 'a' was not found at ip:'2' in chunk:'InnerMain(test:5)'.", testEngine.InterpreterResult, testEngine.InterpreterResult);
StringAssert.Contains("Global var of name 'a' was not found at ip:'2' in chunk:'InnerMain(test:5)'.", testEngine.InterpreterResult, testEngine.InterpreterResult);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion ulox/ulox.core.tests/ListUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void NativeList_ReadOnlyThenAdd_Error()
arr.Add(1);
");

StringAssert.StartsWith("Attempted to modify a read only list at ip:'", testEngine.InterpreterResult);
StringAssert.Contains("Attempted to modify a read only list at ip:'", testEngine.InterpreterResult);
}

[Test]
Expand Down
20 changes: 20 additions & 0 deletions ulox/ulox.core.tests/NativeCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,25 @@ NativeCallResult Func(Vm vm)

Assert.AreEqual("Hello, you, me, and everybody, I'm native.", testEngine.InterpreterResult);
}

[Test]
public void CallNativeFunc_WhenThrows_ShouldThrowUloxWrapped()
{
NativeCallResult Func(Vm vm)
{
throw new System.Exception("Native exception");
return NativeCallResult.SuccessfulExpression;
}

testEngine.MyEngine.Context.Vm.Globals.AddOrSet(new HashedString("WillThrow"), Value.New(Func, 1, 0));

testEngine.Run(@"
var a = 1;
WillThrow();
");

StringAssert.StartsWith("Native call failed", testEngine.InterpreterResult);
StringAssert.Contains("ip:'4' in chunk:'root(test:3)'", testEngine.InterpreterResult);
}
}
}
2 changes: 1 addition & 1 deletion ulox/ulox.core.tests/ObjectLibraryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Freeze_WhenNumber_ShouldFail()
var foo = 7;
Object.Freeze(foo);");

StringAssert.StartsWith("Freeze attempted on unsupported type 'Double'", testEngine.InterpreterResult);
StringAssert.Contains("Freeze attempted on unsupported type 'Double'", testEngine.InterpreterResult);
}

[Test]
Expand Down
11 changes: 10 additions & 1 deletion ulox/ulox.core/Package/Runtime/Engine/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,16 @@ private void DoNativeCall(OpCode opCode)
if (_currentCallFrame.nativeFunc == null)
ThrowRuntimeException($"{opCode} without nativeFunc encountered. This is not allowed");
var argCount = _valueStack.Count - _currentCallFrame.StackStart;
var res = _currentCallFrame.nativeFunc.Invoke(this);
var res = NativeCallResult.Failure;
try
{
res = _currentCallFrame.nativeFunc.Invoke(this);
}
catch (System.Exception e)
{
ProcessReturns();
ThrowRuntimeException($"Native call failed with inner '{e}'");
}

if (res == NativeCallResult.SuccessfulExpression)
{
Expand Down

0 comments on commit 483808d

Please sign in to comment.