Skip to content

Commit 1eed2f2

Browse files
authored
[Memory64] preamble JS changes for 64-bit (#15225)
1 parent f5d9d26 commit 1eed2f2

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/preamble.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ var HEAP_DATA_VIEW;
263263
#endif
264264

265265
#if WASM_BIGINT
266-
var HEAP64;
267-
var HEAPU64;
266+
/** @type {BigInt64Array} */
267+
var HEAP64,
268+
/** @type {BigUint64Array} */
269+
HEAPU64;
268270
#endif
269271

270272
#if USE_PTHREADS
@@ -736,6 +738,7 @@ function instrumentWasmTableWithAbort() {
736738
var realGet = wasmTable.get;
737739
var wrapperCache = {};
738740
wasmTable.get = function(i) {
741+
{{{ from64('i') }}}
739742
var func = realGet.call(wasmTable, i);
740743
var cached = wrapperCache[i];
741744
if (!cached || cached.func !== func) {
@@ -749,6 +752,51 @@ function instrumentWasmTableWithAbort() {
749752
}
750753
#endif
751754

755+
#if MEMORY64
756+
// In memory64 mode wasm pointers are 64-bit. To support that in JS we must use
757+
// BigInts. For now we keep JS as much the same as it always was, that is,
758+
// stackAlloc() receives and returns a Number from the JS point of view -
759+
// we translate BigInts automatically for that.
760+
// TODO: support minified export names
761+
function instrumentWasmExportsForMemory64(exports) {
762+
var instExports = {};
763+
for (var name in exports) {
764+
(function(name) {
765+
var original = exports[name];
766+
var replacement = original;
767+
if (name === 'stackAlloc' || name === 'malloc') {
768+
// get one i64, return an i64
769+
replacement = function(x) {
770+
var r = Number(original(BigInt(x)));
771+
return r;
772+
};
773+
} else if (name === 'free') {
774+
// get one i64
775+
replacement = function(x) {
776+
original(BigInt(x));
777+
};
778+
} else if (name === 'emscripten_stack_get_end' ||
779+
name === 'emscripten_stack_get_base' ||
780+
name === 'emscripten_stack_get_current') {
781+
// return an i64
782+
replacement = function() {
783+
var r = Number(original());
784+
return r;
785+
};
786+
} else if (name === 'main') {
787+
// get a i64 as second arg
788+
replacement = function(x, y) {
789+
var r = original(x, BigInt(y));
790+
return r;
791+
};
792+
}
793+
instExports[name] = replacement;
794+
})(name);
795+
}
796+
return instExports;
797+
}
798+
#endif MEMORY64
799+
752800
var wasmBinaryFile;
753801
#if EXPORT_ES6 && USE_ES6_IMPORT_META && !SINGLE_FILE
754802
if (Module['locateFile']) {
@@ -968,6 +1016,10 @@ function createWasm() {
9681016
exports = relocateExports(exports, {{{ GLOBAL_BASE }}});
9691017
#endif
9701018

1019+
#if MEMORY64
1020+
exports = instrumentWasmExportsForMemory64(exports);
1021+
#endif
1022+
9711023
#if ASYNCIFY
9721024
exports = Asyncify.instrumentWasmExports(exports);
9731025
#endif

0 commit comments

Comments
 (0)