Skip to content

Commit

Permalink
Move the position of interpreter error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
presenthee committed Apr 28, 2024
1 parent 64ad9b8 commit 3daa1db
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions spectec/src/backend-interpreter/runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ let try_run runner target =
let result =
try
runner target
with e ->
with
| Exception.Error (at, msg, step) ->
let msg' = msg ^ " (interpreting " ^ step ^ ")" in
error_interpret at msg'
| e ->
prerr_endline (Printexc.to_string e); fail
in
result, Sys.time () -. start_time
Expand Down Expand Up @@ -110,11 +114,6 @@ let invoke module_name funcname args =
let funcaddr = get_export_addr funcname module_name in
Interpreter.invoke [funcaddr; al_of_list al_of_value args]

let try_invoke module_name funcname args =
try invoke module_name funcname args
with Exception.Error (at, msg, step) ->
let msg' = msg ^ " (interpreting " ^ step ^ ")" in
error_interpret at msg'

let get_global_value module_name globalname =
log "[Getting %s...]\n" globalname;
Expand All @@ -135,12 +134,6 @@ let instantiate module_ =

Interpreter.instantiate [ al_module; listV_of_list externvals ]

let try_instantiate module_ =
try instantiate module_
with Exception.Error (at, msg, step) ->
let msg' = msg ^ " (interpreting " ^ step ^ ")" in
error_interpret at msg'


(** Wast runner **)

Expand All @@ -153,7 +146,7 @@ let module_of_def def =
let run_action action =
match action.it with
| Invoke (var_opt, funcname, args) ->
try_invoke (Register.get_module_name var_opt) (Utf8.encode funcname) (List.map it args)
invoke (Register.get_module_name var_opt) (Utf8.encode funcname) (List.map it args)
| Get (var_opt, globalname) ->
get_global_value (Register.get_module_name var_opt) (Utf8.encode globalname)

Expand All @@ -172,7 +165,7 @@ let test_assertion assertion =
)
| AssertUninstantiable (def, re) -> (
try
def |> module_of_def |> try_instantiate |> ignore;
def |> module_of_def |> instantiate |> ignore;
Run.assert_message assertion.at "instantiation" "module instance" re;
fail
with Exception.Trap -> success
Expand All @@ -185,7 +178,7 @@ let run_command' command =
| Module (var_opt, def) ->
def
|> module_of_def
|> try_instantiate
|> instantiate
|> Register.add_with_var var_opt;
success
| Register (modulename, var_opt) ->
Expand All @@ -202,7 +195,11 @@ let run_command command =
let result =
try
run_command' command
with e ->
with
| Exception.Error (at, msg, step) ->
let msg' = msg ^ " (interpreting " ^ step ^ ")" in
error_interpret at msg'
| e ->
print_endline ("- Test failed at " ^ string_of_region command.at ^
" (" ^ Printexc.to_string e ^ ")");
fail
Expand Down Expand Up @@ -235,7 +232,7 @@ let run_wasm' args module_ =

(* Instantiate *)
module_
|> try_instantiate
|> instantiate
|> Register.add_with_var None;

(* TODO: Only Int32 arguments/results are acceptable *)
Expand All @@ -244,7 +241,7 @@ let run_wasm' args module_ =
let make_value s = Value.Num (I32 (Int32.of_string s)) in

(* Invoke *)
try_invoke (Register.get_module_name None) funcname (List.map make_value args')
invoke (Register.get_module_name None) funcname (List.map make_value args')
(* Print invocation result *)
|> al_to_list al_to_value
|> Value.string_of_values
Expand Down

0 comments on commit 3daa1db

Please sign in to comment.