Skip to content

Commit 2d289c3

Browse files
dcodeIOkripken
authored andcommitted
Unified module loader support in binaryen.js (#1074)
* Unified module loader support in binaryen.js * Recompiled binaryen.js and wasm.js
1 parent d1ccc3d commit 2d289c3

6 files changed

Lines changed: 640 additions & 636 deletions

File tree

bin/binaryen.js

Lines changed: 601 additions & 590 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/wasm.js

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-js.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ echo "building wasm.js"
7171
src/passes/ExtractFunction.cpp \
7272
src/passes/FlattenControlFlow.cpp \
7373
src/passes/Inlining.cpp \
74+
src/passes/InstrumentLocals.cpp \
7475
src/passes/InstrumentMemory.cpp \
7576
src/passes/LegalizeJSInterface.cpp \
7677
src/passes/LocalCSE.cpp \
@@ -95,7 +96,9 @@ echo "building wasm.js"
9596
src/passes/ReorderFunctions.cpp \
9697
src/passes/ReorderLocals.cpp \
9798
src/passes/ReReloop.cpp \
99+
src/passes/SSAify.cpp \
98100
src/passes/SimplifyLocals.cpp \
101+
src/passes/Untee.cpp \
99102
src/passes/Vacuum.cpp \
100103
src/emscripten-optimizer/parser.cpp \
101104
src/emscripten-optimizer/simple_ast.cpp \
@@ -139,6 +142,7 @@ fi
139142
src/passes/ExtractFunction.cpp \
140143
src/passes/FlattenControlFlow.cpp \
141144
src/passes/Inlining.cpp \
145+
src/passes/InstrumentLocals.cpp \
142146
src/passes/InstrumentMemory.cpp \
143147
src/passes/LegalizeJSInterface.cpp \
144148
src/passes/LocalCSE.cpp \
@@ -163,7 +167,9 @@ fi
163167
src/passes/ReorderFunctions.cpp \
164168
src/passes/ReorderLocals.cpp \
165169
src/passes/ReReloop.cpp \
170+
src/passes/SSAify.cpp \
166171
src/passes/SimplifyLocals.cpp \
172+
src/passes/Untee.cpp \
167173
src/passes/Vacuum.cpp \
168174
src/wasm-emscripten.cpp \
169175
src/support/colors.cpp \

check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def fix(x):
511511
print '\n[ checking example testcases... ]\n'
512512

513513
if options.run_gcc_tests:
514-
print '\n[ checking native gcc testcases...]\n'
514+
print '\n[ checking native gcc testcases...]\n'
515515
if not NATIVECC or not NATIVEXX:
516516
fail_with_error('Native compiler (e.g. gcc/g++) was not found in PATH!')
517517
else:

src/js/binaryen.js-post.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -911,34 +911,19 @@
911911
return Module['_BinaryenSetAPITracing'](on);
912912
};
913913

914-
return Module;
915-
};
914+
// Support AMD-compatible loaders by defining a factory function that returns 'Module'
915+
if (typeof define === "function" && define["amd"])
916+
define(function() { return Module; });
916917

917-
if (typeof exports != 'undefined') {
918-
(function(){
919-
var a = Binaryen();
920-
if (typeof module === 'object') {
921-
module.exports = a;
922-
} else {
923-
for (var k in a) {
924-
exports[k] = a[k];
925-
}
926-
}
927-
})();
928-
}
929-
(typeof window !== 'undefined' ? window :
930-
typeof global !== 'undefined' && (
931-
typeof process === 'undefined' ||
918+
// Support CommonJS-compatible loaders by checking for 'require' and 'module.exports'
919+
else if (typeof require === "function" && typeof module !== "undefined" && module && module.exports)
920+
module.exports = Module;
932921

933-
// Note: We must export "Binaryen" even inside a CommonJS/AMD/UMD module
934-
// space because check.py generates a.js which requires Binaryen global var
935-
( process.argv &&
936-
Array.isArray(process.argv) &&
937-
process.argv[1] &&
938-
(process.argv[1].substr(-5) === '/a.js' ||
939-
process.argv[1].substr(-5) === '\\a.js')
940-
)
922+
// Otherwise expose as 'Binaryen' globally checking for common names of the global object
923+
// first (namely 'global' and 'window') and fall back to 'this' (i.e. within web workers).
924+
else
925+
(typeof global !== "undefined" && global ||
926+
typeof window !== "undefined" && window ||
927+
this)["Binaryen"] = Module;
941928

942-
) ? global :
943-
this
944-
)['Binaryen'] = Binaryen();
929+
})();

src/js/binaryen.js-pre.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
var Binaryen = function(Module) {
1+
(function() {
2+
"use strict";

0 commit comments

Comments
 (0)