Instantiate nested modules for module linking (#2447)

This commit implements the interpretation necessary of the instance
section of the module linking proposal. Instantiating a module which
itself has nested instantiated instances will now instantiate the nested
instances properly. This isn't all that useful without the ability to
alias exports off the result, but we can at least observe the side
effects of instantiation through the `start` function.

cc #2094
This commit is contained in:
Alex Crichton
2020-12-01 14:01:31 -06:00
committed by GitHub
parent 40ad39fee9
commit 88a8a8993a
11 changed files with 433 additions and 105 deletions

View File

@@ -13,6 +13,7 @@ fn run_wast(wast: &str, strategy: Strategy) -> anyhow::Result<()> {
let simd = wast.iter().any(|s| s == "simd");
let multi_memory = wast.iter().any(|s| s == "multi-memory");
let module_linking = wast.iter().any(|s| s == "module-linking");
let bulk_mem = multi_memory || wast.iter().any(|s| s == "bulk-memory-operations");
// Some simd tests assume support for multiple tables, which are introduced
@@ -24,6 +25,7 @@ fn run_wast(wast: &str, strategy: Strategy) -> anyhow::Result<()> {
.wasm_bulk_memory(bulk_mem)
.wasm_reference_types(reftypes)
.wasm_multi_memory(multi_memory)
.wasm_module_linking(module_linking)
.strategy(strategy)?
.cranelift_debug_verifier(true);

View File

@@ -0,0 +1,162 @@
(module
(module)
(instance $a (instantiate 0))
)
(module $a
(global (export "global") (mut i32) (i32.const 0))
(func (export "reset")
i32.const 0
global.set 0)
(func $set (export "inc")
i32.const 1
global.get 0
i32.add
global.set 0)
(func (export "get") (result i32)
global.get 0)
(func (export "load") (result i32)
i32.const 0
i32.load)
(memory (export "memory") 1)
(table (export "table") 1 funcref)
(elem (i32.const 0) $set)
)
;; Imported functions work
(module
(import "a" "inc" (func $set))
(module
(import "" (func))
(start 0))
(instance $a (instantiate 0 (func $set)))
)
(assert_return (invoke $a "get") (i32.const 1))
;; Imported globals work
(module
(import "a" "global" (global $g (mut i32)))
(module
(import "" (global (mut i32)))
(func
i32.const 2
global.set 0)
(start 0))
(instance $a (instantiate 0 (global $g)))
)
(assert_return (invoke $a "get") (i32.const 2))
;; Imported tables work
(module
(import "a" "table" (table $t 1 funcref))
(module
(import "" (table 1 funcref))
(func
i32.const 0
call_indirect)
(start 0))
(instance $a (instantiate 0 (table $t)))
)
(assert_return (invoke $a "get") (i32.const 3))
;; Imported memories work
(module
(import "a" "memory" (memory $m 1))
(module
(import "" (memory 1))
(func
i32.const 0
i32.const 4
i32.store)
(start 0))
(instance $a (instantiate 0 (memory $m)))
)
(assert_return (invoke $a "load") (i32.const 4))
;; all at once
(module
(import "a" "inc" (func $f))
(import "a" "global" (global $g (mut i32)))
(import "a" "table" (table $t 1 funcref))
(import "a" "memory" (memory $m 1))
(module
(import "" (memory 1))
(import "" (global (mut i32)))
(import "" (table 1 funcref))
(import "" (func))
(func $start
call 0
i32.const 0
i32.const 4
i32.store
i32.const 0
call_indirect
global.get 0
global.set 0)
(start $start))
(instance $a
(instantiate 0
(memory $m)
(global $g)
(table $t)
(func $f)
)
)
)
;; instantiate lots
(module
(import "a" "inc" (func $f))
(import "a" "global" (global $g (mut i32)))
(import "a" "table" (table $t 1 funcref))
(import "a" "memory" (memory $m 1))
(module $mm (import "" (memory 1)))
(module $mf (import "" (func)))
(module $mt (import "" (table 1 funcref)))
(module $mg (import "" (global (mut i32))))
(instance (instantiate $mm (memory $m)))
(instance (instantiate $mf (func $f)))
(instance (instantiate $mt (table $t)))
(instance (instantiate $mg (global $g)))
)
;; instantiate nested
(assert_return (invoke $a "reset"))
(assert_return (invoke $a "get") (i32.const 0))
(module
(import "a" "inc" (func))
(module
(import "" (func))
(module
(import "" (func))
(module
(import "" (func))
(module
(import "" (func))
(start 0)
)
(instance (instantiate 0 (func 0)))
)
(instance (instantiate 0 (func 0)))
)
(instance (instantiate 0 (func 0)))
)
(instance (instantiate 0 (func 0)))
)
(assert_return (invoke $a "get") (i32.const 1))