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.
This commit is contained in:
Alex Crichton
2021-01-21 09:21:30 -06:00
committed by GitHub
parent 0085ed3ff8
commit 207f60a18e
7 changed files with 420 additions and 108 deletions

View File

@@ -0,0 +1,67 @@
(module $a
(module $m1)
(module $b
(module $m2)
(module $c
(instance (instantiate (module outer $a $m1)))
(instance (instantiate (module outer $b $m2)))
)
(instance (instantiate $c))
)
(instance (instantiate $b))
)
(module $a
(module (export "m"))
)
(module $PARENT
(import "a" "m" (module $b))
(module $c
(module $d
(instance (instantiate (module outer $PARENT $b)))
)
(instance (instantiate $d))
)
(instance (instantiate $c))
)
;; Instantiate `$b` here below twice with two different imports. Ensure the
;; exported modules close over the captured state correctly to ensure that we
;; get the right functions.
(module $a
(module $b (export "close_over_imports")
(import "m" (module $m (export "f" (func (result i32)))))
(module (export "m")
(instance $a (instantiate (module outer $b $m)))
(func (export "f") (result i32)
call (func $a "f"))
)
)
)
(module
(import "a" "close_over_imports" (module $m0
(import "m" (module (export "f" (func (result i32)))))
(export "m" (module (export "f" (func (result i32)))))
))
(module $m1
(func (export "f") (result i32)
i32.const 0))
(instance $m_g1 (instantiate $m0 "m" (module $m1)))
(instance $g1 (instantiate (module $m_g1 "m")))
(module $m2
(func (export "f") (result i32)
i32.const 1))
(instance $m_g2 (instantiate $m0 "m" (module $m2)))
(instance $g2 (instantiate (module $m_g2 "m")))
(func (export "get1") (result i32)
call (func $g1 "f"))
(func (export "get2") (result i32)
call (func $g2 "f"))
)
(assert_return (invoke "get1") (i32.const 0))
(assert_return (invoke "get2") (i32.const 1))

View File

@@ -93,8 +93,7 @@
(instance $a (instantiate $m))
)
;; alias parent -- module
(; TODO
;; alias outer -- module
(module
(module $a)
(module $m
@@ -102,7 +101,6 @@
)
(instance (instantiate $m))
)
;)
;; The alias, import, type, module, and instance sections can all be interleaved
(module $ROOT