Skip to content
Merged
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
2 changes: 2 additions & 0 deletions interpreter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ wasm -d script.wast -o script.js
The first creates a new test scripts where all embedded modules are converted to binary, the second one where all are converted to textual.

The last invocation produces an equivalent, self-contained JavaScript test file.
The flag `-h` can be used to omit the test harness from the converted file;
it then is the client's responsibility to provide versions of the necessary functions.
By default, the generated script will require `assert_soft_invalid` (see below) to detect validation failures. Use the `-us` flag ("unchecked soft") to deactivate these assertions to run on implementations that do not validate dead code.

#### Command Line Expressions
Expand Down
1 change: 1 addition & 0 deletions interpreter/host/flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ let unchecked_soft = ref false
let print_sig = ref false
let dry = ref false
let width = ref 80
let harness = ref true
27 changes: 20 additions & 7 deletions interpreter/host/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Source

(* Harness *)

let prefix =
let harness =
"'use strict';\n" ^
"\n" ^
"let soft_validate = " ^ string_of_bool (not !Flags.unchecked_soft) ^ ";\n" ^
Expand Down Expand Up @@ -49,6 +49,18 @@ let prefix =
" return new WebAssembly.Instance(module(bytes), imports);\n" ^
"}\n" ^
"\n" ^
"function call(instance, name, args) {\n" ^
" return instance.exports[name](...args);\n" ^
"}\n" ^
"\n" ^
"function get(instance, name) {\n" ^
" return instance.exports[name];\n" ^
"}\n" ^
"\n" ^
"function exports(name, instance) {\n" ^
" return {[name]: instance.exports};\n" ^
"}\n" ^
"\n" ^
"function assert_malformed(bytes) {\n" ^
" try { module(bytes, false) } catch (e) {\n" ^
" if (e instanceof WebAssembly.CompileError) return;\n" ^
Expand Down Expand Up @@ -248,8 +260,8 @@ let of_string = of_string_with add_char
let of_wrapper x_opt name wrap_action wrap_assertion at =
let x = of_var_opt x_opt in
let bs = wrap x name wrap_action wrap_assertion at in
"instance(" ^ of_bytes bs ^ ", " ^ "{" ^ x ^ ": " ^ x ^ ".exports})" ^
".exports.run()"
"call(instance(" ^ of_bytes bs ^ ", " ^
"exports(" ^ of_string x ^ ", " ^ x ^ ")), " ^ " \"run\", [])"

let of_float z =
match string_of_float z with
Expand All @@ -276,16 +288,16 @@ let of_definition def =
let of_action mods act =
match act.it with
| Invoke (x_opt, name, lits) ->
of_var_opt x_opt ^ ".exports[" ^ of_string name ^ "]" ^
"(" ^ String.concat ", " (List.map of_literal lits) ^ ")",
"call(" ^ of_var_opt x_opt ^ ", " ^ of_string name ^ ", " ^
"[" ^ String.concat ", " (List.map of_literal lits) ^ "])",
(match lookup mods x_opt name act.at with
| ExternalFuncType ft when not (is_js_func_type ft) ->
let FuncType (_, out) = ft in
Some (of_wrapper x_opt name (invoke ft lits), out)
| _ -> None
)
| Get (x_opt, name) ->
of_var_opt x_opt ^ ".exports[" ^ of_string name ^ "]",
"get(" ^ of_var_opt x_opt ^ ", " ^ of_string name ^ ")",
(match lookup mods x_opt name act.at with
| ExternalGlobalType gt when not (is_js_global_type gt) ->
let GlobalType (t, _) = gt in
Expand Down Expand Up @@ -352,4 +364,5 @@ let of_command mods cmd =
| Meta _ -> assert false

let of_script scr =
prefix ^ String.concat "" (List.map (of_command (ref Map.empty)) scr)
(if !Flags.harness then harness else "") ^
String.concat "" (List.map (of_command (ref Map.empty)) scr)
3 changes: 2 additions & 1 deletion interpreter/host/main.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let name = "wasm"
let version = "0.6"
let version = "0.7"

let configure () =
Import.register "spectest" Spectest.lookup;
Expand Down Expand Up @@ -31,6 +31,7 @@ let argspec = Arg.align
"-s", Arg.Set Flags.print_sig, " show module signatures";
"-u", Arg.Set Flags.unchecked, " unchecked, do not perform validation";
"-us", Arg.Set Flags.unchecked_soft, " do not perform soft validation checks";
"-h", Arg.Clear Flags.harness, " exclude harness for JS convesion";
Copy link
Member

Choose a reason for hiding this comment

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

convesion => conversion

"-d", Arg.Set Flags.dry, " dry, do not run program";
"-t", Arg.Set Flags.trace, " trace execution";
"-v", Arg.Unit banner, " show version"
Expand Down