Skip to content

Commit ebc7938

Browse files
authored
Remove most usage of withStackSave. NFC (#21764)
The remaining usage is all in library_wasmfs.js where it make the code a fair amount more readable to continue to use `withStackSave()` See #21763
1 parent 8fd0437 commit ebc7938

13 files changed

+141
-127
lines changed

src/library_dylink.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,15 @@ var LibraryDylink = {
340340
},
341341

342342
$dlSetError__internal: true,
343-
$dlSetError__deps: ['__dl_seterr', '$stringToUTF8OnStack', '$withStackSave'],
343+
$dlSetError__deps: ['__dl_seterr', '$stringToUTF8OnStack', '$stackSave', '$stackRestore'],
344344
$dlSetError: (msg) => {
345345
#if DYLINK_DEBUG
346346
dbg(`dlSetError: ${msg}`);
347347
#endif
348-
withStackSave(() => {
349-
var cmsg = stringToUTF8OnStack(msg);
350-
___dl_seterr(cmsg, 0);
351-
});
348+
var sp = stackSave();
349+
var cmsg = stringToUTF8OnStack(msg);
350+
___dl_seterr(cmsg, 0);
351+
stackRestore(sp);
352352
},
353353

354354
// We support some amount of allocation during startup in the case of

src/library_exceptions.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ var LibraryExceptions = {
287287

288288
#endif
289289
#if WASM_EXCEPTIONS || !DISABLE_EXCEPTION_CATCHING
290-
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$withStackSave', '$stackAlloc'],
291-
$getExceptionMessageCommon: (ptr) => withStackSave(() => {
290+
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$stackSave', '$stackRestore', '$stackAlloc'],
291+
$getExceptionMessageCommon: (ptr) => {
292+
var sp = stackSave();
292293
var type_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
293294
var message_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
294295
___get_exception_message(ptr, type_addr_addr, message_addr_addr);
@@ -301,8 +302,9 @@ var LibraryExceptions = {
301302
message = UTF8ToString(message_addr);
302303
_free(message_addr);
303304
}
305+
stackRestore(sp);
304306
return [type, message];
305-
}),
307+
},
306308
#endif
307309
#if WASM_EXCEPTIONS
308310
$getCppExceptionTag: () =>

src/library_html5.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
var LibraryHTML5 = {
8-
$JSEvents__deps: ['$withStackSave',
8+
$JSEvents__deps: [
99
#if PTHREADS
1010
'_emscripten_run_callback_on_thread',
1111
#endif
@@ -2303,7 +2303,7 @@ var LibraryHTML5 = {
23032303
#if OFFSCREENCANVAS_SUPPORT
23042304
_emscripten_set_offscreencanvas_size: 'emscripten_set_canvas_element_size',
23052305

2306-
$setOffscreenCanvasSizeOnTargetThread__deps: ['$stringToNewUTF8', '_emscripten_set_offscreencanvas_size_on_thread', '$withStackSave'],
2306+
$setOffscreenCanvasSizeOnTargetThread__deps: ['$stringToNewUTF8', '_emscripten_set_offscreencanvas_size_on_thread'],
23072307
$setOffscreenCanvasSizeOnTargetThread: (targetThread, targetCanvas, width, height) => {
23082308
targetCanvas = targetCanvas ? UTF8ToString(targetCanvas) : '';
23092309
var targetCanvasPtr = 0;
@@ -2346,7 +2346,7 @@ var LibraryHTML5 = {
23462346
},
23472347
#endif
23482348

2349-
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$withStackSave', '$stringToUTF8OnStack'],
2349+
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$stackSave', '$stackRestore', '$stringToUTF8OnStack'],
23502350
$setCanvasElementSize: (target, width, height) => {
23512351
#if GL_DEBUG
23522352
dbg(`setCanvasElementSize(target=${target},width=${width},height=${height}`);
@@ -2357,10 +2357,10 @@ var LibraryHTML5 = {
23572357
} else {
23582358
// This function is being called from high-level JavaScript code instead of asm.js/Wasm,
23592359
// and it needs to synchronously proxy over to another thread, so marshal the string onto the heap to do the call.
2360-
withStackSave(() => {
2361-
var targetInt = stringToUTF8OnStack(target.id);
2362-
_emscripten_set_canvas_element_size(targetInt, width, height);
2363-
});
2360+
var sp = stackSave();
2361+
var targetInt = stringToUTF8OnStack(target.id);
2362+
_emscripten_set_canvas_element_size(targetInt, width, height);
2363+
stackRestore(sp);
23642364
}
23652365
},
23662366

@@ -2428,16 +2428,18 @@ var LibraryHTML5 = {
24282428
#endif
24292429

24302430
// JavaScript-friendly API, returns pair [width, height]
2431-
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$withStackSave', '$stringToUTF8OnStack'],
2432-
$getCanvasElementSize: (target) => withStackSave(() => {
2431+
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$stackSave', '$stackRestore', '$stringToUTF8OnStack'],
2432+
$getCanvasElementSize: (target) => {
2433+
var sp = stackSave();
24332434
var w = stackAlloc(8);
24342435
var h = w + 4;
24352436

24362437
var targetInt = stringToUTF8OnStack(target.id);
24372438
var ret = _emscripten_get_canvas_element_size(targetInt, w, h);
24382439
var size = [{{{ makeGetValue('w', 0, 'i32')}}}, {{{ makeGetValue('h', 0, 'i32')}}}];
2440+
stackRestore(sp);
24392441
return size;
2440-
}),
2442+
},
24412443

24422444
emscripten_set_element_css_size__proxy: 'sync',
24432445
emscripten_set_element_css_size__deps: ['$JSEvents', '$findEventTarget'],

src/library_pthread.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ var LibraryPThread = {
960960
$proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
961961
#endif
962962
963-
$proxyToMainThread__deps: ['$withStackSave', '$stackAlloc', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
963+
$proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
964964
$proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
965965
$proxyToMainThread: (funcIndex, emAsmAddr, sync, ...callArgs) => {
966966
// EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
@@ -973,34 +973,36 @@ var LibraryPThread = {
973973
// all the args here.
974974
// We also pass 'sync' to C separately, since C needs to look at it.
975975
// Allocate a buffer, which will be copied by the C code.
976-
return withStackSave(() => {
977-
// First passed parameter specifies the number of arguments to the function.
978-
// When BigInt support is enabled, we must handle types in a more complex
979-
// way, detecting at runtime if a value is a BigInt or not (as we have no
980-
// type info here). To do that, add a "prefix" before each value that
981-
// indicates if it is a BigInt, which effectively doubles the number of
982-
// values we serialize for proxying. TODO: pack this?
983-
var serializedNumCallArgs = callArgs.length {{{ WASM_BIGINT ? "* 2" : "" }}};
984-
var args = stackAlloc(serializedNumCallArgs * 8);
985-
var b = {{{ getHeapOffset('args', 'i64') }}};
986-
for (var i = 0; i < callArgs.length; i++) {
987-
var arg = callArgs[i];
976+
//
977+
// First passed parameter specifies the number of arguments to the function.
978+
// When BigInt support is enabled, we must handle types in a more complex
979+
// way, detecting at runtime if a value is a BigInt or not (as we have no
980+
// type info here). To do that, add a "prefix" before each value that
981+
// indicates if it is a BigInt, which effectively doubles the number of
982+
// values we serialize for proxying. TODO: pack this?
983+
var serializedNumCallArgs = callArgs.length {{{ WASM_BIGINT ? "* 2" : "" }}};
984+
var sp = stackSave();
985+
var args = stackAlloc(serializedNumCallArgs * 8);
986+
var b = {{{ getHeapOffset('args', 'i64') }}};
987+
for (var i = 0; i < callArgs.length; i++) {
988+
var arg = callArgs[i];
988989
#if WASM_BIGINT
989-
if (typeof arg == 'bigint') {
990-
// The prefix is non-zero to indicate a bigint.
991-
HEAP64[b + 2*i] = 1n;
992-
HEAP64[b + 2*i + 1] = arg;
993-
} else {
994-
// The prefix is zero to indicate a JS Number.
995-
HEAP64[b + 2*i] = 0n;
996-
HEAPF64[b + 2*i + 1] = arg;
997-
}
990+
if (typeof arg == 'bigint') {
991+
// The prefix is non-zero to indicate a bigint.
992+
HEAP64[b + 2*i] = 1n;
993+
HEAP64[b + 2*i + 1] = arg;
994+
} else {
995+
// The prefix is zero to indicate a JS Number.
996+
HEAP64[b + 2*i] = 0n;
997+
HEAPF64[b + 2*i + 1] = arg;
998+
}
998999
#else
999-
HEAPF64[b + i] = arg;
1000+
HEAPF64[b + i] = arg;
10001001
#endif
1001-
}
1002-
return __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
1003-
});
1002+
}
1003+
var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
1004+
stackRestore(sp);
1005+
return rtn;
10041006
},
10051007
10061008
// Reuse global JS array to avoid creating JS garbage for each proxied call

src/library_sdl.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,12 +2157,14 @@ var LibrarySDL = {
21572157
// We support JPG, PNG, TIF because browsers do
21582158
IMG_Init: (flags) => flags,
21592159

2160-
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', '$withStackSave', '$stringToUTF8OnStack', '$stackAlloc'],
2160+
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', '$stackSave', '$stackRestore', '$stringToUTF8OnStack', '$stackAlloc'],
21612161
IMG_Load_RW__proxy: 'sync',
21622162
IMG_Load_RW: (rwopsID, freeSrc) => {
2163+
var sp = stackSave();
21632164
try {
21642165
// stb_image integration support
21652166
var cleanup = () => {
2167+
stackRestore(sp);
21662168
if (rwops && freeSrc) _SDL_FreeRW(rwopsID);
21672169
}
21682170
var addCleanup = (func) => {
@@ -2172,7 +2174,7 @@ var LibrarySDL = {
21722174
func();
21732175
}
21742176
}
2175-
var callStbImage = (func, params) => withStackSave(() => {
2177+
var callStbImage = (func, params) => {
21762178
var x = stackAlloc({{{ getNativeTypeSize('i32') }}});
21772179
var y = stackAlloc({{{ getNativeTypeSize('i32') }}});
21782180
var comp = stackAlloc({{{ getNativeTypeSize('i32') }}});
@@ -2187,7 +2189,7 @@ var LibrarySDL = {
21872189
size: {{{ makeGetValue('x', 0, 'i32') }}} * {{{ makeGetValue('y', 0, 'i32') }}} * {{{ makeGetValue('comp', 0, 'i32') }}},
21882190
bpp: {{{ makeGetValue('comp', 0, 'i32') }}}
21892191
};
2190-
});
2192+
};
21912193

21922194
var rwops = SDL.rwops[rwopsID];
21932195
if (rwops === undefined) {

src/library_sockfs.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,15 +727,15 @@ addToLibrary({
727727
* Passing a NULL callback function to a emscripten_set_socket_*_callback call
728728
* will deregister the callback registered for that Event.
729729
*/
730-
$_setNetworkCallback__deps: ['$withStackSave', '$stringToUTF8OnStack'],
730+
$_setNetworkCallback__deps: ['$stackSave', '$stackRestore', '$stringToUTF8OnStack'],
731731
$_setNetworkCallback: (event, userData, callback) => {
732732
function _callback(data) {
733733
try {
734734
if (event === 'error') {
735-
withStackSave(function() {
736-
var msg = stringToUTF8OnStack(data[2]);
737-
{{{ makeDynCall('viiii', 'callback') }}}(data[0], data[1], msg, userData);
738-
});
735+
var sp = stackSave();
736+
var msg = stringToUTF8OnStack(data[2]);
737+
{{{ makeDynCall('viiii', 'callback') }}}(data[0], data[1], msg, userData);
738+
stackRestore(sp);
739739
} else {
740740
{{{ makeDynCall('vii', 'callback') }}}(data, userData);
741741
}

src/library_wasmfs.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ FS.init();
135135
}
136136

137137
// Copy the file into a JS buffer on the heap.
138-
var buf = withStackSave(() => __wasmfs_read_file(stringToUTF8OnStack(path)));
138+
var sp = stackSave();
139+
var buf = __wasmfs_read_file(stringToUTF8OnStack(path));
140+
stackRestore(sp);
139141

140142
// The signed integer length resides in the first 8 bytes of the buffer.
141143
var length = {{{ makeGetValue('buf', '0', 'i53') }}};
@@ -519,8 +521,9 @@ FS.init();
519521
return FS_mknod(path, mode, 0);
520522
},
521523

522-
$FS_writeFile__deps: ['_wasmfs_write_file'],
523-
$FS_writeFile: (path, data) => withStackSave(() => {
524+
$FS_writeFile__deps: ['_wasmfs_write_file', '$stackSave', '$stackRestore'],
525+
$FS_writeFile: (path, data) => {
526+
var sp = stackSave();
524527
var pathBuffer = stringToUTF8OnStack(path);
525528
if (typeof data == 'string') {
526529
var buf = new Uint8Array(lengthBytesUTF8(data) + 1);
@@ -536,8 +539,9 @@ FS.init();
536539
}
537540
var ret = __wasmfs_write_file(pathBuffer, dataBuffer, data.length);
538541
_free(dataBuffer);
542+
stackRestore(sp);
539543
return ret;
540-
}),
544+
},
541545

542546
$FS_mkdir__deps: ['_wasmfs_mkdir'],
543547
$FS_mkdir: (path, mode = 511 /* 0777 */) => FS.handleError(withStackSave(() => {

src/library_wasmfs_node.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ addToLibrary({
5555
_wasmfs_node_readdir__docs: '/** @suppress {checkTypes} */',
5656
_wasmfs_node_readdir__deps: [
5757
'$wasmfsNodeConvertNodeCode',
58-
'$withStackSave',
58+
'$stackSave',
59+
'$stackRestore',
5960
'$stringToUTF8OnStack',
6061
'_wasmfs_node_record_dirent',
6162
],
@@ -69,21 +70,21 @@ addToLibrary({
6970
return wasmfsNodeConvertNodeCode(e);
7071
}
7172
entries.forEach((entry) => {
72-
withStackSave(() => {
73-
let name = stringToUTF8OnStack(entry.name);
74-
let type;
75-
// TODO: Figure out how to use `cDefine` here.
76-
if (entry.isFile()) {
77-
type = 1;
78-
} else if (entry.isDirectory()) {
79-
type = 2;
80-
} else if (entry.isSymbolicLink()) {
81-
type = 3;
82-
} else {
83-
type = 0;
84-
}
85-
__wasmfs_node_record_dirent(vec, name, type);
86-
});
73+
let sp = stackSave();
74+
let name = stringToUTF8OnStack(entry.name);
75+
let type;
76+
// TODO: Figure out how to use `cDefine` here.
77+
if (entry.isFile()) {
78+
type = 1;
79+
} else if (entry.isDirectory()) {
80+
type = 2;
81+
} else if (entry.isSymbolicLink()) {
82+
type = 3;
83+
} else {
84+
type = 0;
85+
}
86+
__wasmfs_node_record_dirent(vec, name, type);
87+
stackRestore(sp);
8788
});
8889
// implicitly return 0
8990
},

src/library_wasmfs_opfs.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ addToLibrary({
151151

152152
_wasmfs_opfs_get_entries__deps: [
153153
'$wasmfsOPFSProxyFinish',
154-
'$withStackSave',
154+
'$stackSave',
155+
'$stackRestore',
155156
'_wasmfs_opfs_record_entry',
156157
],
157158
_wasmfs_opfs_get_entries: async function(ctx, dirID, entriesPtr, errPtr) {
@@ -162,13 +163,13 @@ addToLibrary({
162163
let iter = dirHandle.entries();
163164
for (let entry; entry = await iter.next(), !entry.done;) {
164165
let [name, child] = entry.value;
165-
withStackSave(() => {
166-
let namePtr = stringToUTF8OnStack(name);
167-
let type = child.kind == "file" ?
168-
{{{ cDefine('File::DataFileKind') }}} :
169-
{{{ cDefine('File::DirectoryKind') }}};
166+
let sp = stackSave();
167+
let namePtr = stringToUTF8OnStack(name);
168+
let type = child.kind == "file" ?
169+
{{{ cDefine('File::DataFileKind') }}} :
170+
{{{ cDefine('File::DirectoryKind') }}};
170171
__wasmfs_opfs_record_entry(entriesPtr, namePtr, type)
171-
});
172+
stackRestore(sp);
172173
}
173174
} catch {
174175
let err = -{{{ cDefs.EIO }}};

0 commit comments

Comments
 (0)