Skip to content

Commit

Permalink
[interp] Add command to spawn threads (WebAssembly#119)
Browse files Browse the repository at this point in the history
This command is meant to execute each action on a separate thread, then
validate that each returns the expected value.

It's currently unimplemented in both `script/run.ml` and `script/js.ml`.
  • Loading branch information
binji authored Feb 5, 2019
1 parent b1dce23 commit 86ed4d0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions interpreter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ cmd:
module with given failure string
<action> ;; perform action and print results
<assertion> ;; assert result of an action
( spawn <name>? <action> ) ;; spawn a new thread and run an action
<meta> ;; meta command
module:
Expand All @@ -322,6 +323,7 @@ module:
action:
( invoke <name>? <string> <expr>* ) ;; invoke function export
( get <name>? <string> ) ;; get global export
( join <name> ) ;; join named thread and return its value
assertion:
( assert_return <action> <expr>* ) ;; assert action has expected results
Expand Down
8 changes: 8 additions & 0 deletions interpreter/script/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ let of_action mods act =
Some (of_wrapper mods x_opt name (get gt), [t])
| _ -> None
)
(* TODO(binji): *)
| Join x ->
assert false

let of_assertion' mods act name args wrapper_opt =
let act_js, act_wrapper_opt = of_action mods act in
Expand Down Expand Up @@ -389,6 +392,9 @@ let of_assertion mods ass =
| AssertExhaustion (act, _) ->
of_assertion' mods act "assert_exhaustion" [] None

(* TODO(binji): *)
let of_spawn mods x_opt act = assert false

let of_command mods cmd =
"\n// " ^ Filename.basename cmd.at.left.file ^
":" ^ string_of_int cmd.at.left.line ^ "\n" ^
Expand All @@ -409,6 +415,8 @@ let of_command mods cmd =
of_assertion' mods act "run" [] None ^ "\n"
| Assertion ass ->
of_assertion mods ass ^ "\n"
| Spawn (x_opt, act) ->
of_spawn mods x_opt act ^ "\n"
| Meta _ -> assert false

let of_script scr =
Expand Down
7 changes: 7 additions & 0 deletions interpreter/script/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ let run_action act =
| None -> Assert.error act.at "undefined export"
)

(* TODO(binji) *)
| Join x ->
assert false

let assert_result at correct got print_expect expect =
if not correct then begin
print_string "Result: "; print_result got;
Expand Down Expand Up @@ -466,6 +470,9 @@ let rec run_command cmd =
run_assertion ass
end

(* TODO(binji) *)
| Spawn (x_opt, act) -> assert false

| Meta cmd ->
run_meta cmd

Expand Down
2 changes: 2 additions & 0 deletions interpreter/script/script.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type action = action' Source.phrase
and action' =
| Invoke of var option * Ast.name * Ast.literal list
| Get of var option * Ast.name
| Join of var

type assertion = assertion' Source.phrase
and assertion' =
Expand All @@ -29,6 +30,7 @@ and command' =
| Register of Ast.name * var option
| Action of action
| Assertion of assertion
| Spawn of var option * action
| Meta of meta

and meta = meta' Source.phrase
Expand Down
3 changes: 3 additions & 0 deletions interpreter/text/arrange.ml
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ let action act =
Node ("invoke" ^ access x_opt name, List.map literal lits)
| Get (x_opt, name) ->
Node ("get" ^ access x_opt name, [])
| Join x ->
Node ("join " ^ x.it, [])

let assertion mode ass =
match ass.it with
Expand Down Expand Up @@ -496,6 +498,7 @@ let command mode cmd =
Node ("register " ^ name n ^ var_opt x_opt, [])
| Action act -> action act
| Assertion ass -> assertion mode ass
| Spawn (x_opt, act) -> Node ("spawn " ^ var_opt x_opt, [action act])
| Meta _ -> assert false

let script mode scr = List.map (command mode) scr
2 changes: 2 additions & 0 deletions interpreter/text/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ rule token = parse

| "script" { SCRIPT }
| "register" { REGISTER }
| "spawn" { SPAWN }
| "join" { JOIN }
| "invoke" { INVOKE }
| "get" { GET }
| "assert_malformed" { ASSERT_MALFORMED }
Expand Down
17 changes: 15 additions & 2 deletions interpreter/text/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ let inline_type_explicit (c : context) x ft at =
%token FUNC START TYPE PARAM RESULT LOCAL GLOBAL
%token TABLE ELEM MEMORY DATA OFFSET IMPORT EXPORT TABLE
%token MODULE BIN QUOTE
%token SCRIPT REGISTER INVOKE GET
%token SCRIPT REGISTER SPAWN JOIN INVOKE GET
%token ASSERT_MALFORMED ASSERT_INVALID ASSERT_SOFT_INVALID ASSERT_UNLINKABLE
%token ASSERT_RETURN ASSERT_RETURN_CANONICAL_NAN ASSERT_RETURN_ARITHMETIC_NAN ASSERT_TRAP ASSERT_EXHAUSTION
%token INPUT OUTPUT
Expand Down Expand Up @@ -781,9 +781,15 @@ inline_module : /* Sugar */
inline_module1 : /* Sugar */
| module_fields1 { Textual ($1 (empty_context ()) () @@ at ()) @@ at () }
/* Scripts */
thread_var_opt :
| /* empty */ { None }
| thread_var { Some $1 }
thread_var :
| VAR { $1 @@ at () }
script_var_opt :
| /* empty */ { None }
| VAR { Some ($1 @@ at ()) } /* Sugar */
Expand All @@ -800,6 +806,8 @@ action :
{ Invoke ($3, $4, $5) @@ at () }
| LPAR GET module_var_opt name RPAR
{ Get ($3, $4) @@ at() }
| LPAR JOIN thread_var RPAR
{ Join $3 @@ at () }
assertion :
| LPAR ASSERT_MALFORMED script_module STRING RPAR
Expand All @@ -816,11 +824,16 @@ assertion :
| LPAR ASSERT_TRAP action STRING RPAR { AssertTrap ($3, $4) @@ at () }
| LPAR ASSERT_EXHAUSTION action STRING RPAR { AssertExhaustion ($3, $4) @@ at () }
assertion_list :
| /* empty */ { [] }
| assertion assertion_list { $1 :: $2 }
cmd :
| action { Action $1 @@ at () }
| assertion { Assertion $1 @@ at () }
| script_module { Module (fst $1, snd $1) @@ at () }
| LPAR REGISTER name module_var_opt RPAR { Register ($3, $4) @@ at () }
| LPAR SPAWN thread_var_opt action RPAR { Spawn ($3, $4) @@ at () }
| meta { Meta $1 @@ at () }
cmd_list :
Expand Down

0 comments on commit 86ed4d0

Please sign in to comment.