Skip to content

Commit 42ad2cd

Browse files
committed
import memory #684
1 parent 304fef1 commit 42ad2cd

35 files changed

+73
-38
lines changed

src/asm2wasm.h

+11
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,22 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
817817
wasm.addExport(export_);
818818
}
819819

820+
#if 0
821+
// export memory
820822
auto memoryExport = make_unique<Export>();
821823
memoryExport->name = MEMORY;
822824
memoryExport->value = Name::fromInt(0);
823825
memoryExport->kind = Export::Memory;
824826
wasm.addExport(memoryExport.release());
827+
#else
828+
// import memory
829+
auto memoryImport = make_unique<Import>();
830+
memoryImport->name = MEMORY;
831+
memoryImport->module = ENV;
832+
memoryImport->base = MEMORY;
833+
memoryImport->kind = Import::Memory;
834+
wasm.addImport(memoryImport.release());
835+
#endif
825836

826837
#if 0 // enable asm2wasm i64 optimizations when browsers have consistent i64 support in wasm
827838
if (udivmoddi4.is() && getTempRet0.is()) {

src/js/wasm.js-post.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function integrateWasmJS(Module) {
183183
return false;
184184
}
185185
exports = instance.exports;
186-
mergeMemory(exports.memory);
186+
if (exports.memory) mergeMemory(exports.memory);
187187

188188
Module["usingWasm"] = true;
189189

@@ -274,6 +274,11 @@ function integrateWasmJS(Module) {
274274
global = fixImports(global);
275275
env = fixImports(env);
276276

277+
// import memory
278+
if (!env['memory']) {
279+
env['memory'] = providedBuffer;
280+
}
281+
277282
// try the methods. each should return the exports if it succeeded
278283

279284
var exports;

src/wasm-js.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,36 @@ extern "C" void EMSCRIPTEN_KEEPALIVE instantiate() {
169169

170170
struct JSExternalInterface : ModuleInstance::ExternalInterface {
171171
void init(Module& wasm, ModuleInstance& instance) override {
172-
// create a new buffer here, just like native wasm support would.
173-
EM_ASM_({
174-
Module['outside']['newBuffer'] = new ArrayBuffer($0);
175-
}, wasm.memory.initial * Memory::kPageSize);
172+
// look for imported memory
173+
{
174+
bool found = false;
175+
for (auto& import : wasm.imports) {
176+
if (import->module == ENV && import->base == MEMORY) {
177+
assert(import->kind == Import::Memory);
178+
// memory is imported
179+
EM_ASM({
180+
Module['outside']['tempBuffer'] = Module['lookupImport']('env', 'memory');
181+
});
182+
found = true;
183+
}
184+
}
185+
if (!found) {
186+
// no memory import; create a new buffer here, just like native wasm support would.
187+
EM_ASM_({
188+
Module['outside']['tempBuffer'] = Module['outside']['newBuffer'] = new ArrayBuffer($0);
189+
}, wasm.memory.initial * Memory::kPageSize);
190+
}
191+
}
176192
for (auto segment : wasm.memory.segments) {
177193
EM_ASM_({
178194
var source = Module['HEAP8'].subarray($1, $1 + $2);
179-
var target = new Int8Array(Module['outside']['newBuffer']);
195+
var target = new Int8Array(Module['outside']['tempBuffer']);
180196
target.set(source, $0);
181197
}, ConstantExpressionRunner(instance.globals).visit(segment.offset).value.geti32(), &segment.data[0], segment.data.size());
182198
}
199+
EM_ASM({
200+
Module['outside']['tempBuffer'] = null;
201+
});
183202
// Table support is in a JS array. If the entry is a number, it's a function pointer. If not, it's a JS method to be called directly
184203
// TODO: make them all JS methods, wrapping a dynCall where necessary?
185204
EM_ASM_({

test/emcc_O2_hello_world.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
(import $___syscall140 "env" "___syscall140" (param i32 i32) (result i32))
3131
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
3232
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
33+
(import $memory memory "env" "memory")
3334
(import $memInitBase global "env" "memInitBase" i32)
3435
(export "_free" $_free)
3536
(export "_main" $_main)
@@ -49,7 +50,6 @@
4950
(export "dynCall_ii" $dynCall_ii)
5051
(export "dynCall_iiii" $dynCall_iiii)
5152
(export "dynCall_vi" $dynCall_vi)
52-
(export "memory" memory)
5353
(global $__THREW__ i32 (i32.const 0))
5454
(global $threwValue i32 (i32.const 0))
5555
(global $setjmpId i32 (i32.const 0))

test/emcc_O2_hello_world.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
(import $___unlock "env" "___unlock" (param i32))
2929
(import $___syscall140 "env" "___syscall140" (param i32 i32) (result i32))
3030
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
31+
(import $memory memory "env" "memory")
3132
(export "_free" $_free)
3233
(export "_main" $_main)
3334
(export "_memset" $_memset)
@@ -46,7 +47,6 @@
4647
(export "dynCall_ii" $dynCall_ii)
4748
(export "dynCall_iiii" $dynCall_iiii)
4849
(export "dynCall_vi" $dynCall_vi)
49-
(export "memory" memory)
5050
(global $__THREW__ i32 (i32.const 0))
5151
(global $threwValue i32 (i32.const 0))
5252
(global $setjmpId i32 (i32.const 0))

test/emcc_O2_hello_world.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
(import $___unlock "env" "___unlock" (param i32))
2929
(import $___syscall140 "env" "___syscall140" (param i32 i32) (result i32))
3030
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
31+
(import $memory memory "env" "memory")
3132
(export "_free" $_free)
3233
(export "_main" $_main)
3334
(export "_memset" $_memset)
@@ -46,7 +47,6 @@
4647
(export "dynCall_ii" $dynCall_ii)
4748
(export "dynCall_iiii" $dynCall_iiii)
4849
(export "dynCall_vi" $dynCall_vi)
49-
(export "memory" memory)
5050
(global $__THREW__ i32 (i32.const 0))
5151
(global $threwValue i32 (i32.const 0))
5252
(global $setjmpId i32 (i32.const 0))

test/emcc_O2_hello_world.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(import $___syscall140 "env" "___syscall140" (param i32 i32) (result i32))
3030
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
3131
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
32+
(import $memory memory "env" "memory")
3233
(export "_free" $_free)
3334
(export "_main" $_main)
3435
(export "_memset" $_memset)
@@ -47,7 +48,6 @@
4748
(export "dynCall_ii" $dynCall_ii)
4849
(export "dynCall_iiii" $dynCall_iiii)
4950
(export "dynCall_vi" $dynCall_vi)
50-
(export "memory" memory)
5151
(global $__THREW__ i32 (i32.const 0))
5252
(global $threwValue i32 (i32.const 0))
5353
(global $setjmpId i32 (i32.const 0))

test/emcc_hello_world.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
(import $i32s-rem "asm2wasm" "i32s-rem" (param i32 i32) (result i32))
4040
(import $i32u-rem "asm2wasm" "i32u-rem" (param i32 i32) (result i32))
4141
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
42+
(import $memory memory "env" "memory")
4243
(import $memInitBase global "env" "memInitBase" i32)
4344
(export "_i64Subtract" $_i64Subtract)
4445
(export "_free" $_free)
@@ -63,7 +64,6 @@
6364
(export "dynCall_iiii" $dynCall_iiii)
6465
(export "dynCall_vi" $dynCall_vi)
6566
(export "___udivmoddi4" $___udivmoddi4)
66-
(export "memory" memory)
6767
(global $__THREW__ i32 (i32.const 0))
6868
(global $threwValue i32 (i32.const 0))
6969
(global $setjmpId i32 (i32.const 0))

test/emcc_hello_world.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(import $_pthread_cleanup_push "env" "_pthread_cleanup_push" (param i32 i32))
3333
(import $_sysconf "env" "_sysconf" (param i32) (result i32))
3434
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
35+
(import $memory memory "env" "memory")
3536
(export "_i64Subtract" $_i64Subtract)
3637
(export "_free" $_free)
3738
(export "_main" $_main)
@@ -55,7 +56,6 @@
5556
(export "dynCall_iiii" $dynCall_iiii)
5657
(export "dynCall_vi" $dynCall_vi)
5758
(export "___udivmoddi4" $___udivmoddi4)
58-
(export "memory" memory)
5959
(global $__THREW__ i32 (i32.const 0))
6060
(global $threwValue i32 (i32.const 0))
6161
(global $setjmpId i32 (i32.const 0))

test/emcc_hello_world.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(import $_pthread_cleanup_push "env" "_pthread_cleanup_push" (param i32 i32))
3333
(import $_sysconf "env" "_sysconf" (param i32) (result i32))
3434
(import $___syscall146 "env" "___syscall146" (param i32 i32) (result i32))
35+
(import $memory memory "env" "memory")
3536
(export "_i64Subtract" $_i64Subtract)
3637
(export "_free" $_free)
3738
(export "_main" $_main)
@@ -55,7 +56,6 @@
5556
(export "dynCall_iiii" $dynCall_iiii)
5657
(export "dynCall_vi" $dynCall_vi)
5758
(export "___udivmoddi4" $___udivmoddi4)
58-
(export "memory" memory)
5959
(global $__THREW__ i32 (i32.const 0))
6060
(global $threwValue i32 (i32.const 0))
6161
(global $setjmpId i32 (i32.const 0))

test/emcc_hello_world.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
(import $i32s-rem "asm2wasm" "i32s-rem" (param i32 i32) (result i32))
3939
(import $i32u-rem "asm2wasm" "i32u-rem" (param i32 i32) (result i32))
4040
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
41+
(import $memory memory "env" "memory")
4142
(export "_i64Subtract" $_i64Subtract)
4243
(export "_free" $_free)
4344
(export "_main" $_main)
@@ -61,7 +62,6 @@
6162
(export "dynCall_iiii" $dynCall_iiii)
6263
(export "dynCall_vi" $dynCall_vi)
6364
(export "___udivmoddi4" $___udivmoddi4)
64-
(export "memory" memory)
6565
(global $__THREW__ i32 (i32.const 0))
6666
(global $threwValue i32 (i32.const 0))
6767
(global $setjmpId i32 (i32.const 0))

test/empty.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(memory 256 256)
33
(data (get_global $memInitBase) "empty.asm.js")
4+
(import $memory memory "env" "memory")
45
(import $memInitBase global "env" "memInitBase" i32)
5-
(export "memory" memory)
66
)

test/empty.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(module
22
(memory 256 256)
3-
(export "memory" memory)
3+
(import $memory memory "env" "memory")
44
)

test/empty.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(module
22
(memory 256 256)
3-
(export "memory" memory)
3+
(import $memory memory "env" "memory")
44
)

test/empty.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(module
22
(memory 256 256)
3-
(export "memory" memory)
3+
(import $memory memory "env" "memory")
44
)

test/hello_world.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(module
22
(memory 256 256)
33
(data (get_global $memInitBase) "hello_world.asm.js")
4+
(import $memory memory "env" "memory")
45
(import $memInitBase global "env" "memInitBase" i32)
56
(export "add" $add)
6-
(export "memory" memory)
77
(func $add (param $0 i32) (param $1 i32) (result i32)
88
(i32.add
99
(get_local $0)

test/hello_world.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(memory 256 256)
3+
(import $memory memory "env" "memory")
34
(export "add" $add)
4-
(export "memory" memory)
55
(func $add (param $0 i32) (param $1 i32) (result i32)
66
(i32.add
77
(get_local $0)

test/hello_world.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(memory 256 256)
3+
(import $memory memory "env" "memory")
34
(export "add" $add)
4-
(export "memory" memory)
55
(func $add (param $x i32) (param $y i32) (result i32)
66
(return
77
(i32.add

test/hello_world.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(memory 256 256)
3+
(import $memory memory "env" "memory")
34
(export "add" $add)
4-
(export "memory" memory)
55
(func $add (param $x i32) (param $y i32) (result i32)
66
(return
77
(i32.add

test/memorygrowth.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
(import $xa "env" "___unlock" (param i32))
2727
(import $ya "env" "___syscall146" (param i32 i32) (result i32))
2828
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
29+
(import $memory memory "env" "memory")
2930
(import $memInitBase global "env" "memInitBase" i32)
3031
(export "_free" $fb)
3132
(export "_main" $Na)
@@ -47,7 +48,6 @@
4748
(export "dynCall_iiii" $lb)
4849
(export "dynCall_vi" $mb)
4950
(export "__growWasmMemory" $__growWasmMemory)
50-
(export "memory" memory)
5151
(global $v i32 (i32.const 0))
5252
(global $w i32 (i32.const 0))
5353
(global $x i32 (i32.const 0))

test/memorygrowth.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
(import $wa "env" "___syscall54" (param i32 i32) (result i32))
2525
(import $xa "env" "___unlock" (param i32))
2626
(import $ya "env" "___syscall146" (param i32 i32) (result i32))
27+
(import $memory memory "env" "memory")
2728
(export "_free" $fb)
2829
(export "_main" $Na)
2930
(export "_pthread_self" $ib)
@@ -44,7 +45,6 @@
4445
(export "dynCall_iiii" $lb)
4546
(export "dynCall_vi" $mb)
4647
(export "__growWasmMemory" $__growWasmMemory)
47-
(export "memory" memory)
4848
(global $v i32 (i32.const 0))
4949
(global $w i32 (i32.const 0))
5050
(global $x i32 (i32.const 0))

test/memorygrowth.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
(import $wa "env" "___syscall54" (param i32 i32) (result i32))
2525
(import $xa "env" "___unlock" (param i32))
2626
(import $ya "env" "___syscall146" (param i32 i32) (result i32))
27+
(import $memory memory "env" "memory")
2728
(export "_free" $fb)
2829
(export "_main" $Na)
2930
(export "_pthread_self" $ib)
@@ -44,7 +45,6 @@
4445
(export "dynCall_iiii" $lb)
4546
(export "dynCall_vi" $mb)
4647
(export "__growWasmMemory" $__growWasmMemory)
47-
(export "memory" memory)
4848
(global $v i32 (i32.const 0))
4949
(global $w i32 (i32.const 0))
5050
(global $x i32 (i32.const 0))

test/memorygrowth.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
(import $xa "env" "___unlock" (param i32))
2626
(import $ya "env" "___syscall146" (param i32 i32) (result i32))
2727
(import $i32u-div "asm2wasm" "i32u-div" (param i32 i32) (result i32))
28+
(import $memory memory "env" "memory")
2829
(export "_free" $fb)
2930
(export "_main" $Na)
3031
(export "_pthread_self" $ib)
@@ -45,7 +46,6 @@
4546
(export "dynCall_iiii" $lb)
4647
(export "dynCall_vi" $mb)
4748
(export "__growWasmMemory" $__growWasmMemory)
48-
(export "memory" memory)
4949
(global $v i32 (i32.const 0))
5050
(global $w i32 (i32.const 0))
5151
(global $x i32 (i32.const 0))

test/min.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
(memory 256 256)
33
(data (get_global $memInitBase) "min.asm.js")
44
(import $tDP global "env" "tempDoublePtr" i32)
5+
(import $memory memory "env" "memory")
56
(import $memInitBase global "env" "memInitBase" i32)
67
(export "floats" $floats)
7-
(export "memory" memory)
88
(func $floats (param $0 f32) (result f32)
99
(local $1 f32)
1010
(f32.add

test/min.fromasm.imprecise

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(module
22
(memory 256 256)
33
(import $tDP global "env" "tempDoublePtr" i32)
4+
(import $memory memory "env" "memory")
45
(export "floats" $floats)
5-
(export "memory" memory)
66
(func $floats (param $0 f32) (result f32)
77
(local $1 f32)
88
(f32.add

test/min.fromasm.imprecise.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(module
22
(memory 256 256)
33
(import $tDP global "env" "tempDoublePtr" i32)
4+
(import $memory memory "env" "memory")
45
(export "floats" $floats)
5-
(export "memory" memory)
66
(func $floats (param $f f32) (result f32)
77
(local $t f32)
88
(return

test/min.fromasm.no-opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(module
22
(memory 256 256)
33
(import $tDP global "env" "tempDoublePtr" i32)
4+
(import $memory memory "env" "memory")
45
(export "floats" $floats)
5-
(export "memory" memory)
66
(func $floats (param $f f32) (result f32)
77
(local $t f32)
88
(return

test/two_sides.fromasm

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
(data (get_global $memInitBase) "two_sides.asm.js")
44
(type $FUNCSIG$id (func (param f64) (result i32)))
55
(import $f64-to-int "asm2wasm" "f64-to-int" (param f64) (result i32))
6+
(import $memory memory "env" "memory")
67
(import $memInitBase global "env" "memInitBase" i32)
78
(export "_test" $_test)
8-
(export "memory" memory)
99
(func $_test (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
1010
(local $5 f64)
1111
(if

0 commit comments

Comments
 (0)