Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,33 @@ let get_metadata_ptr = wasm_mod =>
let get_grain_imported_name = (mod_, name) => Ident.unique_name(name);

let call_panic_handler = (wasm_mod, env, args) => {
let args = [
Expression.Global_get.make(
switch (env.compilation_mode) {
| Runtime =>
Expression.Block.make(
wasm_mod,
resolve_global(~env, panic_with_exception_name),
gensym_label("runtime_panic_stub"),
List.fold_left(
(acc, arg) => [Expression.Drop.make(wasm_mod, arg), ...acc],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little weird to me. This is only the exception being passed to the panic handler, so there's no sense in keeping it around. We should be able to just emit unreachable and have it be a little cleaner.

[Expression.Unreachable.make(wasm_mod)],
args,
),
)
| _ =>
let args = [
Expression.Global_get.make(
wasm_mod,
resolve_global(~env, panic_with_exception_name),
Type.int32,
),
...args,
];
Expression.Call.make(
wasm_mod,
resolve_func(~env, panic_with_exception_name),
args,
Type.int32,
),
...args,
];
Expression.Call.make(
wasm_mod,
resolve_func(~env, panic_with_exception_name),
args,
Type.int32,
);
);
};
};

let call_malloc = (wasm_mod, env, args) => {
Expand Down
12 changes: 12 additions & 0 deletions compiler/test/suites/runtime.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ describe("runtime", ({test, testSkip}) => {
let test_or_skip =
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;

let assertRunError = makeErrorRunner(test_or_skip);
let assertRuntime = makeRuntimeRunner(test_or_skip);

assertRunError(
~config_fn=() => {Grain_utils.Config.compilation_mode := Runtime},
"runtime_mode_panics",
{|
let arr = [> 1, 2, 3]
provide let x = arr[10]
|},
"RuntimeError: unreachable",
);

assertRuntime("numbers.test");

assertRuntime("unsafe/wasmf32.test");
Expand Down
Loading