diff --git a/interpreter/spec/valid.ml b/interpreter/spec/valid.ml index 5642678d8b..495a478b5f 100644 --- a/interpreter/spec/valid.ml +++ b/interpreter/spec/valid.ml @@ -365,11 +365,10 @@ let check_data (c : context) (seg : memory_segment) = check_const c offset I32Type; ignore (memory c index) -let check_global (c : context) (glob : global) : context = +let check_global (c : context) (glob : global) = let {gtype; value} = glob.it in let GlobalType (t, mut) = gtype in - check_const c value t; - {c with globals = c.globals @ [gtype]} + check_const c value t (* Modules *) @@ -413,24 +412,29 @@ let check_export (c : context) (set : NameSet.t) (ex : export) : NameSet.t = let check_module (m : module_) = let - {types; imports; tables; memories; globals; funcs; start; elems; data; - exports} = m.it in - let c = List.fold_right check_import imports {(context m) with types} in - let c' = - { (List.fold_left check_global c globals) with - funcs = c.funcs @ List.map (fun f -> type_ c f.it.ftype) funcs; - tables = c.tables @ List.map (fun tab -> tab.it.ttype) tables; - memories = c.memories @ List.map (fun mem -> mem.it.mtype) memories; + { types; imports; tables; memories; globals; funcs; start; elems; data; + exports } = m.it + in + let c0 = List.fold_right check_import imports {(context m) with types} in + let c1 = + { c0 with + funcs = c0.funcs @ List.map (fun f -> type_ c0 f.it.ftype) funcs; + tables = c0.tables @ List.map (fun tab -> tab.it.ttype) tables; + memories = c0.memories @ List.map (fun mem -> mem.it.mtype) memories; } in - require (List.length c'.tables <= 1) m.at + let c = + { c1 with globals = c1.globals @ List.map (fun g -> g.it.gtype) globals } + in + List.iter (check_global c1) globals; + List.iter (check_table c1) tables; + List.iter (check_memory c1) memories; + List.iter (check_elem c1) elems; + List.iter (check_data c1) data; + List.iter (check_func c) funcs; + check_start c start; + ignore (List.fold_left (check_export c) NameSet.empty exports); + require (List.length c.tables <= 1) m.at "multiple tables are not allowed (yet)"; - require (List.length c'.memories <= 1) m.at - "multiple memories are not allowed (yet)"; - List.iter (check_func c') funcs; - List.iter (check_table c') tables; - List.iter (check_memory c') memories; - List.iter (check_elem c') elems; - List.iter (check_data c') data; - ignore (List.fold_left (check_export c') NameSet.empty exports); - check_start c' start + require (List.length c.memories <= 1) m.at + "multiple memories are not allowed (yet)" diff --git a/interpreter/test/memory.wast b/interpreter/test/memory.wast index b9fb1bb029..d05b18fac1 100644 --- a/interpreter/test/memory.wast +++ b/interpreter/test/memory.wast @@ -10,10 +10,11 @@ (module (memory 1 2) (data (i32.const 0) "a") (data (i32.const 1) "b") (data (i32.const 2) "c") ) -(module (memory 1) (global i32 (i32.const 0)) (data (get_global 0) "a")) -(module (memory 1) (global $g i32 (i32.const 0)) (data (get_global $g) "a")) -(module (memory 1) (data (get_global 0) "a") (global i32 (i32.const 0))) -(module (memory 1) (data (get_global $g) "a") (global $g i32 (i32.const 0))) +(module (global (import "spectest" "global") i32) (memory 1) (data (get_global 0) "a")) +(module (global $g (import "spectest" "global") i32) (memory 1) (data (get_global $g) "a")) +;; Use of internal globals in constant expressions is not allowed in MVP. +;; (module (memory 1) (data (get_global 0) "a") (global i32 (i32.const 0))) +;; (module (memory 1) (data (get_global $g) "a") (global $g i32 (i32.const 0))) (module (memory (data)) (func (export "memsize") (result i32) (current_memory))) (assert_return (invoke "memsize") (i32.const 0)) @@ -38,10 +39,11 @@ (module (memory 1) (data (nop))) "constant expression required" ) -(assert_invalid - (module (memory 1) (data (get_global $g)) (global $g (mut i32) (i32.const 0))) - "constant expression required" -) +;; Use of internal globals in constant expressions is not allowed in MVP. +;; (assert_invalid +;; (module (memory 1) (data (get_global $g)) (global $g (mut i32) (i32.const 0))) +;; "constant expression required" +;; ) (assert_unlinkable (module (memory 0 0) (data (i32.const 0) "a")) @@ -57,7 +59,7 @@ "" ;; either out of memory or segment does not fit ;) (assert_unlinkable - (module (memory 1) (data (get_global 0) "a") (global i32 (i32.const 0x10000))) + (module (global (import "spectest" "global") i32) (memory 0) (data (get_global 0) "a")) "data segment does not fit" )