From ac078dcb1c677f83527693536cb1c06157095447 Mon Sep 17 00:00:00 2001 From: jgravelle-google Date: Fri, 9 Sep 2016 10:41:00 -0700 Subject: Add flag to s2wasm to export __growWasmMemory function (#696) * Add a flag to s2wasm to export grow_memory Binaryen's wasm.js-post.js calls back in to wasm in order to grow the linear memory, via a function that asm2wasm exports called __growWasmMemory. This changes exposes that method through s2wasm when invoked with a flag. * Move AsmConstWalker from wasm-linker to wasm-emscripten * Add test for memory growth in s2wasm * Move makeDynCallThunks into wasm-emscripten module * Move mutation in getTableSegment into a separate method * Move emscripten metadata generation into wasm-emscripten Also make AsmConstWalker internal to the wasm-emscripten module, as it's only used for the metadata pass. --- src/tools/s2wasm.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/tools/s2wasm.cpp') diff --git a/src/tools/s2wasm.cpp b/src/tools/s2wasm.cpp index 2ee094af4..8c36a0d58 100644 --- a/src/tools/s2wasm.cpp +++ b/src/tools/s2wasm.cpp @@ -22,6 +22,7 @@ #include "support/command-line.h" #include "support/file.h" #include "s2wasm.h" +#include "wasm-emscripten.h" #include "wasm-linker.h" #include "wasm-printing.h" #include "wasm-validator.h" @@ -32,6 +33,7 @@ using namespace wasm; int main(int argc, const char *argv[]) { bool ignoreUnknownSymbols = false; bool generateEmscriptenGlue = false; + bool allowMemoryGrowth = false; std::string startFunction; std::vector archiveLibraries; Options options("s2wasm", "Link .s file into .wast"); @@ -73,6 +75,11 @@ int main(int argc, const char *argv[]) { [](Options *o, const std::string &argument) { o->extra["max-memory"] = argument; }) + .add("--allow-memory-growth", "", "Allow linear memory to grow at runtime", + Options::Arguments::Zero, + [&allowMemoryGrowth](Options *, const std::string &) { + allowMemoryGrowth = true; + }) .add("--emscripten-glue", "-e", "Generate emscripten glue", Options::Arguments::Zero, [&generateEmscriptenGlue](Options *, const std::string &) { @@ -98,6 +105,11 @@ int main(int argc, const char *argv[]) { }); options.parse(argc, argv); + if (allowMemoryGrowth && !generateEmscriptenGlue) { + Fatal() << "Error: adding memory growth code without Emscripten glue. " + "This doesn't do anything.\n"; + } + auto debugFlag = options.debug ? Flags::Debug : Flags::Release; auto input(read_file(options.extra["infile"], Flags::Text, debugFlag)); @@ -138,6 +150,10 @@ int main(int argc, const char *argv[]) { std::stringstream meta; if (generateEmscriptenGlue) { if (options.debug) std::cerr << "Emscripten gluing..." << std::endl; + if (allowMemoryGrowth) { + emscripten::generateMemoryGrowthFunction(linker.getOutput().wasm); + } + // dyncall thunks linker.emscriptenGlue(meta); } -- cgit v1.2.3