Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ var HEAP_DATA_VIEW;
#endif

#if WASM_BIGINT
var HEAP64;
var HEAPU64;
/** @type {BigInt64Array} */
var HEAP64,
/** @type {BigUint64Array} */
HEAPU64;
#endif

#if USE_PTHREADS
Expand Down Expand Up @@ -736,6 +738,7 @@ function instrumentWasmTableWithAbort() {
var realGet = wasmTable.get;
var wrapperCache = {};
wasmTable.get = function(i) {
{{{ from64('i') }}}
var func = realGet.call(wasmTable, i);
var cached = wrapperCache[i];
if (!cached || cached.func !== func) {
Expand All @@ -749,6 +752,51 @@ function instrumentWasmTableWithAbort() {
}
#endif

#if MEMORY64
// In memory64 mode wasm pointers are 64-bit. To support that in JS we must use
// BigInts. For now we keep JS as much the same as it always was, that is,
// stackAlloc() receives and returns a Number from the JS point of view -
// we translate BigInts automatically for that.
// TODO: support minified export names
function instrumentWasmExportsForMemory64(exports) {
var instExports = {};
for (var name in exports) {
(function(name) {
var original = exports[name];
var replacement = original;
if (name === 'stackAlloc' || name === 'malloc') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method won't work if we minify the names of exports, which we do in -O3 etc. For now, please add a comment // TODO: support minified export names, and in a later PR we should disable that minification for MEMORY64 for now (that would be in minify_wasm_imports_and_exports in tools/building.py)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here: #15305

// get one i64, return an i64
replacement = function(x) {
var r = Number(original(BigInt(x)));
return r;
};
} else if (name === 'free') {
// get one i64
replacement = function(x) {
original(BigInt(x));
};
} else if (name === 'emscripten_stack_get_end' ||
name === 'emscripten_stack_get_base' ||
name === 'emscripten_stack_get_current') {
// return an i64
replacement = function() {
var r = Number(original());
return r;
};
} else if (name === 'main') {
// get a i64 as second arg
replacement = function(x, y) {
var r = original(x, BigInt(y));
return r;
};
}
instExports[name] = replacement;
})(name);
}
return instExports;
}
#endif MEMORY64

var wasmBinaryFile;
#if EXPORT_ES6 && USE_ES6_IMPORT_META && !SINGLE_FILE
if (Module['locateFile']) {
Expand Down Expand Up @@ -968,6 +1016,10 @@ function createWasm() {
exports = relocateExports(exports, {{{ GLOBAL_BASE }}});
#endif

#if MEMORY64
exports = instrumentWasmExportsForMemory64(exports);
#endif

#if ASYNCIFY
exports = Asyncify.instrumentWasmExports(exports);
#endif
Expand Down