Skip to content

Commit 7eacfcc

Browse files
authored
Optimize code size of dynCall() when MEMORY64 is used, and add a memory size limit check. (#19288)
* Optimize code size of dynCall() when MEMORY64 is used, and add a memory size limit check. * Do not use 'let' or '...args' spread * Fix typo * Address review
1 parent f642503 commit 7eacfcc

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

emcc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,6 +2483,8 @@ def phase_linker_setup(options, state, newargs):
24832483
def check_memory_setting(setting):
24842484
if settings[setting] % webassembly.WASM_PAGE_SIZE != 0:
24852485
exit_with_error(f'{setting} must be a multiple of WebAssembly page size (64KiB), was {settings[setting]}')
2486+
if settings[setting] >= 2**53:
2487+
exit_with_error(f'{setting} must be smaller than 2^53 bytes due to JS Numbers (doubles) being used to hold pointer addresses in JS side')
24862488

24872489
check_memory_setting('INITIAL_MEMORY')
24882490
check_memory_setting('MAXIMUM_MEMORY')

src/library.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ mergeInto(LibraryManager.library, {
248248
#endif
249249
}
250250

251-
let alignUp = (x, multiple) => x + (multiple - x % multiple) % multiple;
251+
var alignUp = (x, multiple) => x + (multiple - x % multiple) % multiple;
252252

253253
// Loop through potential heap size increases. If we attempt a too eager
254254
// reservation that fails, cut down on the attempted size and reserve a
@@ -3252,27 +3252,20 @@ mergeInto(LibraryManager.library, {
32523252
assert(getWasmTableEntry(ptr), 'missing table entry in dynCall: ' + ptr);
32533253
#endif
32543254
#if MEMORY64
3255-
// With MEMORY64 we have an additional step to covert `p` arguments to
3255+
// With MEMORY64 we have an additional step to convert `p` arguments to
32563256
// bigint. This is the runtime equivalent of the wrappers we create for wasm
32573257
// exports in `emscripten.py:create_wasm64_wrappers`.
3258-
if (sig.includes('p')) {
3259-
var new_args = [];
3260-
args.forEach((arg, index) => {
3261-
if (sig[index + 1] == 'p') {
3262-
arg = BigInt(arg);
3263-
}
3264-
new_args.push(arg);
3265-
});
3266-
args = new_args;
3258+
for (var i = 1; i < sig.length; ++i) {
3259+
if (sig[i] == 'p') args[i-1] = BigInt(args[i-1]);
32673260
}
32683261
#endif
32693262
var rtn = getWasmTableEntry(ptr).apply(null, args);
32703263
#if MEMORY64
3271-
if (sig[0] == 'p') {
3272-
rtn = Number(rtn);
3273-
}
3274-
#endif
3264+
return sig[0] == 'p' ? Number(rtn) : rtn;
3265+
#else
32753266
return rtn;
3267+
#endif
3268+
32763269
#endif
32773270
},
32783271

@@ -3675,7 +3668,7 @@ mergeInto(LibraryManager.library, {
36753668
return this.allocated[id];
36763669
};
36773670
this.allocate = function(handle) {
3678-
let id = this.freelist.pop() || this.allocated.length;
3671+
var id = this.freelist.pop() || this.allocated.length;
36793672
this.allocated[id] = handle;
36803673
return id;
36813674
};

0 commit comments

Comments
 (0)