summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/js/wasm.js-post.js11
-rw-r--r--src/tools/asm2wasm.cpp24
2 files changed, 32 insertions, 3 deletions
diff --git a/src/js/wasm.js-post.js b/src/js/wasm.js-post.js
index 1c207b3f3..5a1de4c9e 100644
--- a/src/js/wasm.js-post.js
+++ b/src/js/wasm.js-post.js
@@ -29,6 +29,7 @@ function integrateWasmJS(Module) {
// inputs
var method = Module['wasmJSMethod'] || {{{ wasmJSMethod }}} || 'native-wasm,interpret-s-expr'; // by default, try native and then .wast
+ Module['wasmJSMethod'] = method;
var wasmTextFile = Module['wasmTextFile'] || {{{ wasmTextFile }}};
var wasmBinaryFile = Module['wasmBinaryFile'] || {{{ wasmBinaryFile }}};
@@ -100,10 +101,12 @@ function integrateWasmJS(Module) {
}
var oldView = new Int8Array(oldBuffer);
var newView = new Int8Array(newBuffer);
- if ({{{ WASM_BACKEND }}}) {
- // memory segments arrived in the wast, do not trample them
+
+ // If we have a mem init file, do not trample it
+ if (!memoryInitializer) {
oldView.set(newView.subarray(STATIC_BASE, STATIC_BASE + STATIC_BUMP), STATIC_BASE);
}
+
newView.set(oldView);
updateGlobalBuffer(newBuffer);
updateGlobalBufferViews();
@@ -215,6 +218,10 @@ function integrateWasmJS(Module) {
info.global = global;
info.env = env;
+ if (!('memInitBase' in env)) {
+ env['memInitBase'] = STATIC_BASE; // tell the memory segments where to place themselves
+ }
+
wasmJS['providedTotalMemory'] = Module['buffer'].byteLength;
// Prepare to generate wasm, using either asm2wasm or s-exprs
diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp
index beacc884c..af2cf6f5a 100644
--- a/src/tools/asm2wasm.cpp
+++ b/src/tools/asm2wasm.cpp
@@ -21,6 +21,7 @@
#include "support/colors.h"
#include "support/command-line.h"
#include "support/file.h"
+#include "wasm-builder.h"
#include "wasm-printing.h"
#include "asm2wasm.h"
@@ -40,10 +41,14 @@ int main(int argc, const char *argv[]) {
o->extra["output"] = argument;
Colors::disable();
})
- .add("--mapped-globals", "-m", "Mapped globals", Options::Arguments::One,
+ .add("--mapped-globals", "-n", "Mapped globals", Options::Arguments::One,
[](Options *o, const std::string &argument) {
std::cerr << "warning: the --mapped-globals/-m option is deprecated (a mapped globals file is no longer needed as we use wasm globals)" << std::endl;
})
+ .add("--mem-init", "-t", "Import a memory initialization file into the output module", Options::Arguments::One,
+ [](Options *o, const std::string &argument) {
+ o->extra["mem init"] = argument;
+ })
.add("--total-memory", "-m", "Total memory size", Options::Arguments::One,
[](Options *o, const std::string &argument) {
o->extra["total memory"] = argument;
@@ -91,6 +96,23 @@ int main(int argc, const char *argv[]) {
Asm2WasmBuilder asm2wasm(wasm, pre.memoryGrowth, options.debug, imprecise, opts);
asm2wasm.processAsm(asmjs);
+ // import mem init file, if provided
+ const auto &memInit = options.extra.find("mem init");
+ if (memInit != options.extra.end()) {
+ auto filename = memInit->second.c_str();
+ auto data(read_file<std::vector<char>>(filename, Flags::Binary, options.debug ? Flags::Debug : Flags::Release));
+ // the mem init's base is imported
+ auto* import = new Import;
+ import->name = Name("memInitBase");
+ import->module = Name("env");
+ import->base = Name("memInitBase");
+ import->kind = Import::Global;
+ import->globalType = i32;
+ wasm.addImport(import);
+ // create the memory segment
+ wasm.memory.segments.emplace_back(Builder(wasm).makeGetGlobal(import->name, import->globalType), data);
+ }
+
if (options.debug) std::cerr << "printing..." << std::endl;
Output output(options.extra["output"], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
WasmPrinter::printModule(&wasm, output.getStream());