diff --git a/ulox/ulox.core.tests/ByteCodeEngineTests.cs b/ulox/ulox.core.tests/ByteCodeEngineTests.cs index f8894bc1..aaec5f7a 100644 --- a/ulox/ulox.core.tests/ByteCodeEngineTests.cs +++ b/ulox/ulox.core.tests/ByteCodeEngineTests.cs @@ -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] @@ -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] diff --git a/ulox/ulox.core.tests/DynamicTests.cs b/ulox/ulox.core.tests/DynamicTests.cs index e2270302..f961b0b1 100644 --- a/ulox/ulox.core.tests/DynamicTests.cs +++ b/ulox/ulox.core.tests/DynamicTests.cs @@ -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] diff --git a/ulox/ulox.core.tests/InnerVMTests.cs b/ulox/ulox.core.tests/InnerVMTests.cs index b69e1b07..bb94e93e 100644 --- a/ulox/ulox.core.tests/InnerVMTests.cs +++ b/ulox/ulox.core.tests/InnerVMTests.cs @@ -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] diff --git a/ulox/ulox.core.tests/ListUsageTests.cs b/ulox/ulox.core.tests/ListUsageTests.cs index 6aec808b..576d01bc 100644 --- a/ulox/ulox.core.tests/ListUsageTests.cs +++ b/ulox/ulox.core.tests/ListUsageTests.cs @@ -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] diff --git a/ulox/ulox.core.tests/NativeCallTests.cs b/ulox/ulox.core.tests/NativeCallTests.cs index 3db8eb64..c85de88b 100644 --- a/ulox/ulox.core.tests/NativeCallTests.cs +++ b/ulox/ulox.core.tests/NativeCallTests.cs @@ -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); + } } } diff --git a/ulox/ulox.core.tests/ObjectLibraryTests.cs b/ulox/ulox.core.tests/ObjectLibraryTests.cs index 09037c7f..9de15a30 100644 --- a/ulox/ulox.core.tests/ObjectLibraryTests.cs +++ b/ulox/ulox.core.tests/ObjectLibraryTests.cs @@ -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] diff --git a/ulox/ulox.core/Package/Runtime/Engine/VM.cs b/ulox/ulox.core/Package/Runtime/Engine/VM.cs index 9446cb03..958dcc5e 100644 --- a/ulox/ulox.core/Package/Runtime/Engine/VM.cs +++ b/ulox/ulox.core/Package/Runtime/Engine/VM.cs @@ -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) {