Skip to content

Commit

Permalink
[interpreter] Proper fix for JS conversion (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg authored Oct 8, 2024
1 parent 2a30893 commit 29b1fd4
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions interpreter/script/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ let env () : env =
let current_mod (env : env) = "$$" ^ string_of_int env.current_mod
let of_mod_opt (env : env) = function
| None -> current_mod env
| Some x -> x.it
| Some x -> "$" ^ x.it

let current_inst (env : env) = "$" ^ string_of_int env.current_inst
let of_inst_opt (env : env) = function
Expand Down Expand Up @@ -614,24 +614,18 @@ let rec of_result res =
| EitherResult ress ->
"[" ^ String.concat ", " (List.map of_result ress) ^ "]"

let of_module bs =
"module(" ^ of_bytes bs ^ ")"

let rec of_definition def =
match def.it with
| Textual (m, _) -> of_module (Encode.encode m)
| Encoded (_, bs) -> of_module bs.it
| Textual (m, _) -> of_bytes (Encode.encode m)
| Encoded (_, bs) -> of_bytes bs.it
| Quoted (_, s) ->
try of_definition (snd (Parse.Module.parse_string ~offset:s.at s.it))
with Parse.Syntax _ | Custom.Syntax _ -> of_module "<malformed quote>"

let of_instance env x_opt =
"instance(" ^ of_mod_opt env x_opt ^ ")"
with Parse.Syntax _ | Custom.Syntax _ -> of_bytes "<malformed quote>"

let of_wrapper env x_opt name wrap_action wrap_assertion at =
let x = of_inst_opt env x_opt in
let bs = wrap name wrap_action wrap_assertion at in
"call(instance(" ^ of_module bs ^ ", " ^
"call(instance(module(" ^ of_bytes bs ^ "), " ^
"exports(" ^ x ^ ")), " ^ " \"run\", [])"

let of_action env act =
Expand Down Expand Up @@ -680,9 +674,9 @@ let of_assertion env ass =
| AssertInvalidCustom (def, _) ->
"assert_invalid_custom(" ^ of_definition def ^ ");"
| AssertUnlinkable (x_opt, _) ->
"assert_unlinkable(" ^ of_instance env x_opt ^ ");"
"assert_unlinkable(" ^ of_mod_opt env x_opt ^ ");"
| AssertUninstantiable (x_opt, _) ->
"assert_uninstantiable(" ^ of_instance env x_opt ^ ");"
"assert_uninstantiable(" ^ of_mod_opt env x_opt ^ ");"
| AssertReturn (act, ress) ->
of_assertion' env act "assert_return" (List.map of_result ress)
(Some (assert_return ress))
Expand All @@ -705,7 +699,7 @@ let of_command env cmd =
| Quoted (_, s) ->
unquote (snd (Parse.Module.parse_string ~offset:s.at s.it))
in bind_mod env x_opt (unquote def);
"let " ^ current_mod env ^ " = " ^ of_definition def ^ ";\n" ^
"let " ^ current_mod env ^ " = module(" ^ of_definition def ^ ");\n" ^
(if x_opt = None then "" else
"let " ^ of_mod_opt env x_opt ^ " = " ^ current_mod env ^ ";\n")
| Instance (x1_opt, x2_opt) ->
Expand Down

0 comments on commit 29b1fd4

Please sign in to comment.