Skip to content

Commit

Permalink
Support import/export mut globals in interpreter (WebAssembly#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
binji committed Nov 16, 2017
1 parent 9f13dc1 commit 07a6fb2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
8 changes: 1 addition & 7 deletions interpreter/valid/valid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,6 @@ let check_import (im : import) (c : context) : context =
{c with memories = mt :: c.memories}
| GlobalImport gt ->
check_global_type gt idesc.at;
let GlobalType (_, mut) = gt in
require (mut = Immutable) idesc.at
"mutable globals cannot be imported (yet)";
{c with globals = gt :: c.globals}

module NameSet = Set.Make(struct type t = Ast.name let compare = compare end)
Expand All @@ -466,10 +463,7 @@ let check_export (c : context) (set : NameSet.t) (ex : export) : NameSet.t =
| FuncExport x -> ignore (func c x)
| TableExport x -> ignore (table c x)
| MemoryExport x -> ignore (memory c x)
| GlobalExport x ->
let GlobalType (_, mut) = global c x in
require (mut = Immutable) edesc.at
"mutable globals cannot be exported (yet)"
| GlobalExport x -> ignore (global c x)
);
require (not (NameSet.mem name set)) ex.at "duplicate export name";
NameSet.add name set
Expand Down
22 changes: 3 additions & 19 deletions test/core/globals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,9 @@
"global is immutable"
)

(assert_invalid
(module (import "m" "a" (global (mut i32))))
"mutable globals cannot be imported"
)

(assert_invalid
(module (global (import "m" "a") (mut i32)))
"mutable globals cannot be imported"
)

(assert_invalid
(module (global (mut f32) (f32.const 0)) (export "a" (global 0)))
"mutable globals cannot be exported"
)

(assert_invalid
(module (global (export "a") (mut f32) (f32.const 0)))
"mutable globals cannot be exported"
)
;; mutable globals can be exported
(module (global (mut f32) (f32.const 0)) (export "a" (global 0)))
(module (global (export "a") (mut f32) (f32.const 0)))

(assert_invalid
(module (global f32 (f32.neg (f32.const 0))))
Expand Down
33 changes: 33 additions & 0 deletions test/core/linking.wast
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,29 @@
(module $Mg
(global $glob (export "glob") i32 (i32.const 42))
(func (export "get") (result i32) (get_global $glob))

;; export mutable globals
(global $mut_glob (export "mut_glob") (mut i32) (i32.const 142))
(func (export "get_mut") (result i32) (get_global $mut_glob))
(func (export "set_mut") (param i32) (set_global $mut_glob (get_local 0)))
)
(register "Mg" $Mg)

(module $Ng
(global $x (import "Mg" "glob") i32)
(global $mut_glob (import "Mg" "mut_glob") (mut i32))
(func $f (import "Mg" "get") (result i32))
(func $get_mut (import "Mg" "get_mut") (result i32))
(func $set_mut (import "Mg" "set_mut") (param i32))

(export "Mg.glob" (global $x))
(export "Mg.get" (func $f))
(global $glob (export "glob") i32 (i32.const 43))
(func (export "get") (result i32) (get_global $glob))

(export "Mg.mut_glob" (global $mut_glob))
(export "Mg.get_mut" (func $get_mut))
(export "Mg.set_mut" (func $set_mut))
)

(assert_return (get $Mg "glob") (i32.const 42))
Expand All @@ -58,6 +71,26 @@
(assert_return (invoke $Ng "Mg.get") (i32.const 42))
(assert_return (invoke $Ng "get") (i32.const 43))

(assert_return (get $Mg "mut_glob") (i32.const 142))
(assert_return (get $Ng "Mg.mut_glob") (i32.const 142))
(assert_return (invoke $Mg "get_mut") (i32.const 142))
(assert_return (invoke $Ng "Mg.get_mut") (i32.const 142))

(assert_return (invoke $Mg "set_mut" (i32.const 241)))
(assert_return (get $Mg "mut_glob") (i32.const 241))
(assert_return (get $Ng "Mg.mut_glob") (i32.const 241))
(assert_return (invoke $Mg "get_mut") (i32.const 241))
(assert_return (invoke $Ng "Mg.get_mut") (i32.const 241))


(assert_unlinkable
(module (import "Mg" "mut_glob" (global i32)))
"incompatible import type"
)
(assert_unlinkable
(module (import "Mg" "glob" (global (mut i32))))
"incompatible import type"
)

;; Tables

Expand Down

0 comments on commit 07a6fb2

Please sign in to comment.