Skip to content

Commit

Permalink
Make it so builtin scripts throw rather than fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Apr 1, 2024
1 parent 56d2068 commit cc5d455
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
46 changes: 41 additions & 5 deletions FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Ceres.ExecutionEngine.TypeSystem;
using Ceres.Runtime;
using Ceres.TranslationEngine;
using EVIL.CommonTypes.TypeSystem;
using EVIL.Grammar;
using EVIL.Lexical;
using Mono.Options;
Expand Down Expand Up @@ -284,9 +285,24 @@ private void CrashHandler(Fiber fiber, Exception exception)
{
var fiberArray = _vm.Scheduler.Fibers.ToArray();
var fiberIndex = Array.IndexOf(fiberArray, fiber);

var callStack = fiber.CallStack;
var top = callStack.Peek();

StackFrame[] callStack;

if (exception is UserUnhandledExceptionException uuee)
{
callStack = uuee.EvilStackTrace;
}
else
{
callStack = fiber.CallStack.ToArray();
}

TerminateWithError(fiberIndex, callStack, exception);
}

private void TerminateWithError(int fiberIndex, StackFrame[] callStack, Exception exception)
{
var top = callStack[0];
ScriptStackFrame? scriptTop = null;

var sb = new StringBuilder();
Expand Down Expand Up @@ -317,12 +333,32 @@ private void CrashHandler(Fiber fiber, Exception exception)
}
sb.AppendLine(": ");

sb.AppendLine($"{dd.DefinedInFile}:{dd.GetLineForIP((int)scriptTop.PreviousOpCodeIP)}: {exception.Message}");
sb.Append($"{dd.DefinedInFile}:{dd.GetLineForIP((int)scriptTop.PreviousOpCodeIP)}: ");

if (exception is UserUnhandledExceptionException uuee)
{
if (uuee.EvilExceptionObject.Type == DynamicValueType.Table)
{
var msg = uuee.EvilExceptionObject.Table!["msg"];
if (msg.Type == DynamicValueType.String)
{
sb.Append(msg.String);
}
}
}
else
{
sb.Append(exception.Message);
}

sb.AppendLine();
sb.AppendLine("Stack trace:");
sb.Append(fiber.StackTrace(false));
sb.Append(Fiber.StackTrace(callStack));

Terminate(sb.ToString(), exitCode: ExitCode.RuntimeError);

}


}
}
8 changes: 3 additions & 5 deletions VirtualMachine/Ceres.Runtime/ScriptBuiltins/dofile.vil
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ loc fn try_compile_source(full_path) {
val result = evil_compile(source, full_path);

if (result is Nil) {
// noret
fail("dofile: Failed to compile the file '" + full_path + ": evil.compile returned Nil.");
throw { msg: "dofile: Failed to compile the file '" + full_path + ": evil.compile returned Nil." };
}

if (!result.success) {
// noret
fail(build_error_message(full_path, result.log));
throw { msg: build_error_message(full_path, result.log) };
}

ret result.chunk;
Expand Down Expand Up @@ -102,5 +100,5 @@ fn dofile(requested_path) {
msg += " " + v + "\n";
}

fail(msg);
throw { msg: msg };
}
9 changes: 3 additions & 6 deletions VirtualMachine/Ceres.Runtime/ScriptBuiltins/require.vil
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
val fail = core.fail;
val get_text = fs.file.get_text;
val file_exists = fs.file.exists;
val dir_exists = fs.dir.exists;
Expand Down Expand Up @@ -63,13 +62,11 @@ loc fn try_compile_source(full_path) {
val result = evil_compile(source, full_path);

if (result is Nil) {
// noret
fail("Failed to compile the file '" + full_path + ": evil.compile return Nil.");
throw { msg: "Failed to compile the file '" + full_path + ": evil.compile return Nil." };
}

if (!result.success) {
// noret
fail(build_error_message(full_path, result.log));
throw { msg: build_error_message(full_path, result.log) };
}

ret result.chunk;
Expand Down Expand Up @@ -125,5 +122,5 @@ fn require(path) {
msg += " " + v + "\n";
}

fail(msg);
throw { msg: msg };
}
8 changes: 7 additions & 1 deletion VirtualMachine/Ceres/ExecutionEngine/Concurrency/Fiber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ public async Task BlockUntilFinishedAsync()
}

public string StackTrace(bool skipNativeFrames)
{
return StackTrace(
CallStack.ToArray(skipNativeFrames)
);
}

public static string StackTrace(StackFrame[] callStack)
{
var sb = new StringBuilder();

var callStack = CallStack.ToArray(skipNativeFrames);
for (var i = 0; i < callStack.Length; i++)
{
if (callStack[i] is ScriptStackFrame ssf)
Expand Down
13 changes: 12 additions & 1 deletion VirtualMachine/Tests/Ceres.LanguageTests/tests/023_try.vil
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,15 @@ fn try_native_throws() {
}

assert.equal(result, "hello world");
}
}

#[test]
fn try_require() {
rw val result = "hiii";

try { result = require("doesnt_exist"); }
catch (e) { result = e.msg; }

assert.is_true(#result > 70);
}

0 comments on commit cc5d455

Please sign in to comment.