Files
wasmtime/tests/misc_testsuite/module-linking/alias.wast
Alex Crichton 207f60a18e module-linking: Implement outer module aliases (#2590)
This commit fully implements outer aliases of the module linking
proposal. Outer aliases can now handle multiple-level-up aliases and now
properly also handle closed-over-values of modules that are either
imported or defined.

The structure of `wasmtime::Module` was altered as part of this commit.
It is now a compiled module plus two lists of "upvars", or closed over
values used when instantiating the module. One list of upvars is
compiled artifacts which are submodules that could be used. Another is
module values that are injected via outer aliases. Serialization and
such have been updated as appropriate to handle this.
2021-01-21 09:21:30 -06:00

144 lines
3.0 KiB
Plaintext

;; functions
(module
(module $m
(func $foo (export "foo") (result i32)
i32.const 1)
)
(instance $a (instantiate $m))
(func (export "get") (result i32)
call (func $a "foo"))
)
(assert_return (invoke "get") (i32.const 1))
;; globals
(module
(module $m
(global $g (export "g") (mut i32) (i32.const 2))
)
(instance $a (instantiate $m))
(func (export "get") (result i32)
global.get (global $a "g"))
)
(assert_return (invoke "get") (i32.const 2))
;; memories
(module
(module $m
(memory $m (export "m") 1)
(data (i32.const 0) "\03\00\00\00")
)
(instance $a (instantiate $m))
(alias $m (memory $a "m"))
(func (export "get") (result i32)
i32.const 0
i32.load)
)
(assert_return (invoke "get") (i32.const 3))
;; tables
(module
(module $m
(table $t (export "t") 1 funcref)
(func (result i32)
i32.const 4)
(elem (i32.const 0) 0)
)
(instance $a (instantiate $m))
(func (export "get") (result i32)
i32.const 0
call_indirect (table $a "t") (result i32))
)
(assert_return (invoke "get") (i32.const 4))
;; modules
(module
(module $m
(module $sub (export "module")
(func $f (export "") (result i32)
i32.const 5))
)
(instance $a (instantiate $m))
(instance $b (instantiate (module $a "module")))
(func (export "get") (result i32)
call (func $b ""))
)
(assert_return (invoke "get") (i32.const 5))
;; instances
(module
(module $m
(module $sub
(func $f (export "") (result i32)
i32.const 6))
(instance $i (export "") (instantiate $sub))
)
(instance $a (instantiate $m))
(func (export "get") (result i32)
call (func $a "" ""))
)
(assert_return (invoke "get") (i32.const 6))
;; alias parent -- type
(module
(type $t (func))
(module $m
(func $f (type outer 0 $t))
)
(instance $a (instantiate $m))
)
;; alias outer -- module
(module
(module $a)
(module $m
(instance (instantiate (module outer 0 $a)))
)
(instance (instantiate $m))
)
;; The alias, import, type, module, and instance sections can all be interleaved
(module $ROOT
(module $a)
(type $t (func))
(module $m
;; alias
(alias $thunk (type outer 0 $t))
;; import
(import "" "" (func (type $thunk)))
;; module (referencing parent type)
(module
(func (type outer $m $thunk))
(func (type outer $ROOT $t))
)
;; type
(type $thunk2 (func))
;; module (referencing previous alias)
(module $m2
(func (export "") (type outer $m $thunk2))
)
;; instance
(instance $i (instantiate $m2))
;; alias that instance
(alias $my_f (func $i ""))
;; module
(module $m3
(import "" (func)))
;; use our aliased function to create the module
(instance $i2 (instantiate $m3 "" (func $my_f)))
;; module
(module $m4
(import "" (func)))
)
;; instantiate the above module
(module $smol (func $f (export "")))
(instance $smol (instantiate $smol))
(instance (instantiate $m "" (instance $smol)))
)