Skip to content

Commit

Permalink
Fix emscripten build (#855)
Browse files Browse the repository at this point in the history
It's not correct to use `--pre-js` and `--post-js` to wrap the module in
a function instance; instead we're supposed to use `-s MODULARIZE=1`.

This still keeps the build as (almost) asm.js, as switching to wasm is a
bit more work (we need to preload the wasm binary module).

This fixes issue #853.
  • Loading branch information
binji committed Jun 4, 2018
1 parent a9829ff commit 2928177
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 51 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,19 @@ else ()
target_link_libraries(libwabtjs libwabt)
set_target_properties(libwabtjs PROPERTIES OUTPUT_NAME libwabt)

set(WABT_PRE_JS ${WABT_SOURCE_DIR}/src/wabt.pre.js)
set(WABT_POST_JS ${WABT_SOURCE_DIR}/src/wabt.post.js)
set(EMSCRIPTEN_EXPORTED_JSON ${WABT_SOURCE_DIR}/src/emscripten-exported.json)

set(LIBWABT_LINK_FLAGS
--memory-init-file 0
--pre-js ${WABT_PRE_JS}
--post-js ${WABT_POST_JS}
-s EXPORTED_FUNCTIONS=\"@${EMSCRIPTEN_EXPORTED_JSON}\"
-s RESERVED_FUNCTION_POINTERS=10
-s NO_EXIT_RUNTIME=1
-s ALLOW_MEMORY_GROWTH=1
-s ELIMINATE_DUPLICATE_FUNCTIONS=1
-s MODULARIZE=1
-s EXPORT_NAME=\"'WabtModule'\"
-s WASM=0
-Oz
--llvm-lto 1
)
Expand All @@ -418,6 +418,6 @@ else ()
set_target_properties(libwabtjs
PROPERTIES
LINK_FLAGS "${LIBWABT_LINK_FLAGS_STR}"
LINK_DEPENDS "${WABT_PRE_JS};${WABT_POST_JS};${EMSCRIPTEN_EXPORTED_JSON}"
LINK_DEPENDS "${WABT_POST_JS};${EMSCRIPTEN_EXPORTED_JSON}"
)
endif ()
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ src/prebuilt/wasm2c.include.c: src/wasm2c.c.tmpl
src/prebuilt/wasm2c.include.h: src/wasm2c.h.tmpl
src/wasm2c_tmpl.py -o $@ $<

.PHONY: demo
demo: emscripten-release
cp out/emscripten/Release/libwabt.js demo

# running CMake
$(foreach CONFIG,$(CONFIGS), \
$(foreach COMPILER,$(COMPILERS), \
Expand Down
42 changes: 29 additions & 13 deletions demo/libwabt.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/wasm2wat/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function compile(contents) {
var foldExprs = foldExprsEl.checked;
var inlineExport = inlineExportEl.checked;

wabt.ready.then(function() {
WabtModule().then(function(wabt) {
try {
var module = wabt.readWasm(contents, {readDebugNames: readDebugNames});
if (generateNames) {
Expand Down
2 changes: 1 addition & 1 deletion demo/wat2wasm/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Split(["#top-row", "#bottom-row"], {
direction: 'vertical'
});

wabt.ready.then(function () {
WabtModule().then(function(wabt) {

var kCompileMinMS = 100;

Expand Down
16 changes: 2 additions & 14 deletions src/wabt.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,5 @@ WasmModule.prototype.destroy = function() {
}
};

var exports = {
ready: Promise.resolve(),
parseWat: parseWat,
readWasm: readWasm,
};

if (typeof module !== "undefined" && module && module.exports)
module.exports = exports;
else if (typeof define === "function" && define.amd)
define(function() { return exports; });
else
(typeof global !== "undefined" && global || typeof window !== "undefined" && window || this).wabt = exports;

})(); // Call IIFE from wabt.pre.js.
Module['parseWat'] = parseWat;
Module['readWasm'] = readWasm;
18 changes: 0 additions & 18 deletions src/wabt.pre.js

This file was deleted.

0 comments on commit 2928177

Please sign in to comment.