Skip to content

Commit cbab7f7

Browse files
committed
Add set action
1 parent 87d3a37 commit cbab7f7

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed

interpreter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ module:
393393
action:
394394
( invoke <name>? <string> <arg>* ) ;; invoke function export
395395
( get <name>? <string> ) ;; get global export
396+
( set <name>? <string> <arg> ) ;; set global export
396397
397398
arg:
398399
<literal> ;; literal argument

interpreter/script/js.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ let literal lit =
296296
let get t at =
297297
[], GlobalImport t @@ at, [], [GlobalGet (subject_idx @@ at) @@ at]
298298

299+
let set t at =
300+
[], GlobalImport t @@ at, [], [GlobalSet (subject_idx @@ at) @@ at]
301+
299302
let invoke ft at =
300303
let FuncType (ts, _) = ft in
301304
[ft @@ at], FuncImport (subject_type_idx @@ at) @@ at,
@@ -583,19 +586,25 @@ let of_wrapper mods x_opt name wrap_action opds wrap_assertion at =
583586
let rec of_action mods act =
584587
match act.it with
585588
| Invoke (x_opt, name, args) ->
589+
let opds = List.map (of_argument mods) args in
586590
"call(" ^ of_var_opt mods x_opt ^ ", " ^ of_name name ^ ", " ^
587-
"[" ^ String.concat ", " (List.map (of_arg mods) args) ^ "].flat())",
591+
"[" ^ String.concat ", " opds ^ "].flat())",
588592
let FuncType (_, ts2) as ft = lookup_func mods x_opt name act.at in
589593
if is_js_func_type ft then None else
590-
let opds = List.map (of_arg mods) args in
591594
Some (of_wrapper mods x_opt name (invoke ft) opds, ts2)
592595
| Get (x_opt, name) ->
593596
"get(" ^ of_var_opt mods x_opt ^ ", " ^ of_name name ^ ")",
594597
let GlobalType (t, _) as gt = lookup_global mods x_opt name act.at in
595598
if is_js_global_type gt then None else
596599
Some (of_wrapper mods x_opt name (get gt) [], [t])
600+
| Set (x_opt, name, arg) ->
601+
let opd = of_argument mods arg in
602+
"set(" ^ of_var_opt mods x_opt ^ ", " ^ of_name name ^ ", " ^ opd ^ ")",
603+
let GlobalType (t, _) as gt = lookup_global mods x_opt name act.at in
604+
if is_js_global_type gt then None else
605+
Some (of_wrapper mods x_opt name (set gt) [opd], [t])
597606

598-
and of_arg mods arg =
607+
and of_argument mods arg =
599608
match arg.it with
600609
| LiteralArg lit -> of_literal lit
601610
| ActionArg act ->

interpreter/script/run.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ let rec run_action act : Values.value list =
349349
(match Instance.export inst name with
350350
| Some (Instance.ExternFunc f) ->
351351
let Types.FuncType (ts1, _) = Func.type_of f in
352-
let vs = List.concat_map run_arg args in
352+
let vs = List.concat_map run_argument args in
353353
if List.length vs <> List.length ts1 then
354354
Script.error act.at "wrong number of arguments";
355355
List.iteri (fun i (v, t) ->
@@ -360,7 +360,6 @@ let rec run_action act : Values.value list =
360360
| Some _ -> Assert.error act.at "export is not a function"
361361
| None -> Assert.error act.at "undefined export"
362362
)
363-
364363
| Get (x_opt, name) ->
365364
trace ("Getting global \"" ^ Ast.string_of_name name ^ "\"...");
366365
let inst = lookup_instance x_opt act.at in
@@ -369,8 +368,21 @@ let rec run_action act : Values.value list =
369368
| Some _ -> Assert.error act.at "export is not a global"
370369
| None -> Assert.error act.at "undefined export"
371370
)
371+
| Set (x_opt, name, arg) ->
372+
trace ("Setting global \"" ^ Ast.string_of_name name ^ "\"...");
373+
let inst = lookup_instance x_opt act.at in
374+
let v =
375+
match run_argument arg with
376+
| [v] -> v
377+
| _ -> Assert.error act.at "wrong number of arguments"
378+
in
379+
(match Instance.export inst name with
380+
| Some (Instance.ExternGlobal gl) -> Global.store gl v; []
381+
| Some _ -> Assert.error act.at "export is not a global"
382+
| None -> Assert.error act.at "undefined export"
383+
)
372384

373-
and run_arg arg : Values.value list =
385+
and run_argument arg : Values.value list =
374386
match arg.it with
375387
| LiteralArg lit -> [lit.it]
376388
| ActionArg act -> run_action act

interpreter/script/script.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type action = action' Source.phrase
1515
and action' =
1616
| Invoke of var option * Ast.name * arg list
1717
| Get of var option * Ast.name
18+
| Set of var option * Ast.name * arg
1819

1920
and arg = arg' Source.phrase
2021
and arg' =

interpreter/text/arrange.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,13 @@ let access x_opt n =
702702
let rec action mode act =
703703
match act.it with
704704
| Invoke (x_opt, name, args) ->
705-
Node ("invoke" ^ access x_opt name, List.map (arg mode) args)
705+
Node ("invoke" ^ access x_opt name, List.map (argument mode) args)
706706
| Get (x_opt, name) ->
707707
Node ("get" ^ access x_opt name, [])
708+
| Set (x_opt, name, arg) ->
709+
Node ("set" ^ access x_opt name, [argument mode arg])
708710

709-
and arg mode arg =
711+
and argument mode arg =
710712
match arg.it with
711713
| LiteralArg lit -> literal mode lit
712714
| ActionArg act -> action mode act

interpreter/text/lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ rule token = parse
678678
| "register" -> REGISTER
679679
| "invoke" -> INVOKE
680680
| "get" -> GET
681+
| "set" -> SET
681682
| "assert_malformed" -> ASSERT_MALFORMED
682683
| "assert_invalid" -> ASSERT_INVALID
683684
| "assert_unlinkable" -> ASSERT_UNLINKABLE

interpreter/text/parser.mly

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ let inline_type_explicit (c : context) x ft at =
232232
%token FUNC START TYPE PARAM RESULT LOCAL GLOBAL
233233
%token TABLE ELEM MEMORY DATA DECLARE OFFSET ITEM IMPORT EXPORT
234234
%token MODULE BIN QUOTE
235-
%token SCRIPT REGISTER INVOKE GET
235+
%token SCRIPT REGISTER INVOKE GET SET
236236
%token ASSERT_MALFORMED ASSERT_INVALID ASSERT_UNLINKABLE
237237
%token ASSERT_RETURN ASSERT_TRAP ASSERT_EXHAUSTION
238238
%token<Script.nan> NAN
@@ -1019,6 +1019,8 @@ action :
10191019
{ Invoke ($3, $4, $5) @@ at () }
10201020
| LPAR GET module_var_opt name RPAR
10211021
{ Get ($3, $4) @@ at() }
1022+
| LPAR SET module_var_opt name arg RPAR
1023+
{ Set ($3, $4, $5) @@ at() }
10221024

10231025
assertion :
10241026
| LPAR ASSERT_MALFORMED script_module STRING RPAR

test/core/script.wast

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88

99
(module $m2
10-
(global (export "g") i32 (i32.const 42))
10+
(global (export "g") (mut i32) (i32.const 42))
1111
(func (export "f") (result i32) (i32.const 42))
1212
(func (export "add3") (param i32 i32 i32) (result i32)
1313
(i32.add (i32.add (local.get 0) (local.get 1)) (local.get 2))
@@ -21,17 +21,25 @@
2121
(assert_return (get $m1 "g") (i32.const 41))
2222
(assert_return (get $m2 "g") (i32.const 42))
2323

24+
(set "g" (i32.const 43))
25+
(assert_return (set "g" (i32.const 43)))
26+
(assert_return (get "g") (i32.const 43))
27+
(set $m2 "g" (i32.const 44))
28+
(assert_return (get "g") (i32.const 44))
29+
(set "g" (invoke $m1 "inc" (get "g")))
30+
(assert_return (get "g") (i32.const 45))
31+
2432
(assert_return (invoke "f") (i32.const 42))
2533
(assert_return (invoke $m1 "f") (i32.const 41))
2634
(assert_return (invoke $m2 "f") (i32.const 42))
2735

2836
(assert_return (invoke $m1 "inc" (i32.const 2)) (i32.const 3))
2937
(assert_return (invoke $m1 "inc" (get $m1 "g")) (i32.const 42))
30-
(assert_return (invoke $m1 "inc" (get $m2 "g")) (i32.const 43))
31-
(assert_return (invoke $m1 "inc" (invoke $m1 "inc" (get "g"))) (i32.const 44))
38+
(assert_return (invoke $m1 "inc" (get $m2 "g")) (i32.const 46))
39+
(assert_return (invoke $m1 "inc" (invoke $m1 "inc" (get "g"))) (i32.const 47))
3240

33-
(assert_return (invoke "add3" (get $m1 "g") (invoke $m1 "inc" (get "g")) (get "g")) (i32.const 126))
34-
(assert_return (invoke "add3" (invoke "swap" (get $m1 "g") (invoke $m1 "inc" (get "g"))) (i32.const -20)) (i32.const 64))
41+
(assert_return (invoke "add3" (get $m1 "g") (invoke $m1 "inc" (get "g")) (get "g")) (i32.const 132))
42+
(assert_return (invoke "add3" (invoke "swap" (get $m1 "g") (invoke $m1 "inc" (get "g"))) (i32.const -20)) (i32.const 67))
3543

3644

3745
(module

0 commit comments

Comments
 (0)