Skip to content

Commit

Permalink
Use standard named for native manipulation functions. NFC (emscripten…
Browse files Browse the repository at this point in the history
…-core#21555)

We continue to use the JS names when calling from JS code but use
prefixed, native symbol names for the native implementation.

I'm hoping we can eventually completely remove the `WASM_SYSTEM_EXPORTS`
special casing.
  • Loading branch information
sbc100 authored Mar 20, 2024
1 parent 89ff6c9 commit faee8d3
Show file tree
Hide file tree
Showing 77 changed files with 181 additions and 142 deletions.
14 changes: 12 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ See docs/process.md for more on how version tagging works.
-----------------------
- In `MODULARIZE` mode we no longer export the module ready promise as `ready`.
This was previously exposed on the Module for historical reasons even though
in `MODULARIZE` mode the only way to get access to the module is to wait on the
promise returned from the factory function. (#21564)
in `MODULARIZE` mode the only way to get access to the module is to wait on
the promise returned from the factory function. (#21564)
- JS library code is now executed in its own context/scope, which limits how
much of the compiler internals are accessible. If there are build time JS
symbols that you are depending on, but that were not added to this scope,
please file a bug and we can add more to this scope. (#21542)
- The JS functions for manipulating the native/shadow stack
(`stackSave`/`stackRestore`/`stackAlloc`) are now just regular JS library
function and as such are only included if you explicitly depend on them. If
you use these functions in your JS code you will need to depend on them via
either:
- The `EM_JS_DEPS` macro for `EM_ASM`/`EM_JS` code.
- The `__deps` attribute for JS library functions
- The `-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE` flag for `--pre-js`/`--post-js`
code
(#21555)

3.1.56 - 03/14/24
-----------------
Expand Down
1 change: 1 addition & 0 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
printErr,
read,
warn,
warnOnce,
warningOccured,
} from './utility.mjs';
import {LibraryManager, librarySymbols} from './modules.mjs';
Expand Down
16 changes: 15 additions & 1 deletion src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
// new function with an '_', it will not be found.

addToLibrary({
// JS aliases for native stack manipulation functions
$stackSave__deps: ['emscripten_stack_get_current'],
$stackSave: () => _emscripten_stack_get_current(),
$stackRestore__deps: ['_emscripten_stack_restore'],
$stackRestore: (val) => __emscripten_stack_restore(val),
$stackAlloc__deps: ['_emscripten_stack_alloc'],
$stackAlloc: (sz) => __emscripten_stack_alloc(sz),

// Aliases that allow legacy names (without leading $) for these
// stack functions to continue to work in `__deps` entries.
stackAlloc: '$stackAlloc',
stackSave: '$stackSave',
stackRestore: '$stackSave',

$ptrToString: (ptr) => {
#if ASSERTIONS
assert(typeof ptr === 'number');
Expand Down Expand Up @@ -596,7 +610,7 @@ addToLibrary({
#endif

$withStackSave__internal: true,
$withStackSave__deps: ['stackSave', 'stackRestore'],
$withStackSave__deps: ['$stackSave', '$stackRestore'],
$withStackSave: (f) => {
var stack = stackSave();
var ret = f();
Expand Down
4 changes: 2 additions & 2 deletions src/library_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ addToLibrary({
});
},

$Fibers__deps: ['$Asyncify', 'emscripten_stack_set_limits', 'stackRestore'],
$Fibers__deps: ['$Asyncify', 'emscripten_stack_set_limits', '$stackRestore'],
$Fibers: {
nextFiber: 0,
trampolineRunning: false,
Expand Down Expand Up @@ -601,7 +601,7 @@ addToLibrary({
},
},

emscripten_fiber_swap__deps: ["$Asyncify", "$Fibers", 'stackSave'],
emscripten_fiber_swap__deps: ["$Asyncify", "$Fibers", '$stackSave'],
emscripten_fiber_swap__async: true,
emscripten_fiber_swap: (oldFiber, newFiber) => {
if (ABORT) return;
Expand Down
2 changes: 1 addition & 1 deletion src/library_ccall.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addToLibrary({
},

// C calling interface.
$ccall__deps: ['$getCFunc', '$writeArrayToMemory', '$stringToUTF8OnStack', 'stackSave', 'stackRestore', 'stackAlloc'],
$ccall__deps: ['$getCFunc', '$writeArrayToMemory', '$stringToUTF8OnStack', '$stackSave', '$stackRestore', '$stackAlloc'],
$ccall__docs: `
/**
* @param {string|null=} returnType
Expand Down
2 changes: 1 addition & 1 deletion src/library_dylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var LibraryDylink = {
// generation time.
#if !DISABLE_EXCEPTION_CATCHING || SUPPORT_LONGJMP == 'emscripten'
$createInvokeFunction__internal: true,
$createInvokeFunction__deps: ['$dynCall', 'setThrew', 'stackSave', 'stackRestore'],
$createInvokeFunction__deps: ['$dynCall', 'setThrew', '$stackSave', '$stackRestore'],
$createInvokeFunction: (sig) => {
return function() {
var sp = stackSave();
Expand Down
2 changes: 1 addition & 1 deletion src/library_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ var LibraryExceptions = {

#endif
#if WASM_EXCEPTIONS || !DISABLE_EXCEPTION_CATCHING
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$withStackSave', 'stackAlloc'],
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$withStackSave', '$stackAlloc'],
$getExceptionMessageCommon: (ptr) => withStackSave(() => {
var type_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
var message_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
Expand Down
2 changes: 1 addition & 1 deletion src/library_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ legacyFuncs = {
* @param {(Uint8Array|Array<number>)} slab: An array of data.
* @param {number=} allocator : How to allocate memory, see ALLOC_*
*/
$allocate__deps: ['$ALLOC_NORMAL', '$ALLOC_STACK', 'malloc', 'stackAlloc'],
$allocate__deps: ['$ALLOC_NORMAL', '$ALLOC_STACK', 'malloc', '$stackAlloc'],
$allocate: (slab, allocator) => {
var ret;
#if ASSERTIONS
Expand Down
6 changes: 3 additions & 3 deletions src/library_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ addToLibrary({
$makePromiseCallback__deps: ['$getPromise',
'$POINTER_SIZE',
'emscripten_promise_destroy',
'stackAlloc',
'stackRestore',
'stackSave'],
'$stackAlloc',
'$stackRestore',
'$stackSave'],
$makePromiseCallback: (callback, userData) => {
return (value) => {
#if RUNTIME_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ var LibraryPThread = {
$proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
#endif
$proxyToMainThread__deps: ['$withStackSave', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
$proxyToMainThread__deps: ['$withStackSave', '$stackAlloc', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
$proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
$proxyToMainThread: (funcIndex, emAsmAddr, sync, ...callArgs) => {
// EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
Expand Down Expand Up @@ -1065,7 +1065,7 @@ var LibraryPThread = {
},
$establishStackSpace__internal: true,
$establishStackSpace__deps: ['stackRestore'],
$establishStackSpace__deps: ['$stackRestore'],
$establishStackSpace: () => {
var pthread_ptr = _pthread_self();
var stackHigh = {{{ makeGetValue('pthread_ptr', C_STRUCTS.pthread.stack, '*') }}};
Expand Down
2 changes: 1 addition & 1 deletion src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2157,7 +2157,7 @@ var LibrarySDL = {
// We support JPG, PNG, TIF because browsers do
IMG_Init: (flags) => flags,

IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', '$withStackSave', '$stringToUTF8OnStack', 'stackAlloc'],
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', '$withStackSave', '$stringToUTF8OnStack', '$stackAlloc'],
IMG_Load_RW__proxy: 'sync',
IMG_Load_RW: (rwopsID, freeSrc) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/library_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ addToLibrary({
},

// Allocate stack space for a JS string, and write it there.
$stringToUTF8OnStack__deps: ['$lengthBytesUTF8', '$stringToUTF8'],
$stringToUTF8OnStack__deps: ['$lengthBytesUTF8', '$stringToUTF8', '$stackAlloc'],
$stringToUTF8OnStack: (str) => {
var size = lengthBytesUTF8(str) + 1;
var ret = stackAlloc(size);
Expand Down
5 changes: 3 additions & 2 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3942,6 +3942,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if MEMORY64
// Convert an array of i64 offsets to an array of i32 offsets returning a
// pointer to the new (stack allocated) array.
$convertOffsets__deps: ['$stackAlloc'],
$convertOffsets__internal: true,
$convertOffsets: (offsets, count) => {
var offsets32 = stackAlloc(count * 4);
Expand All @@ -3960,7 +3961,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glMultiDrawElements: 'glMultiDrawElementsWEBGL',
glMultiDrawElementsANGLE: 'glMultiDrawElementsWEBGL',
#if MEMORY64
glMultiDrawElementsWEBGL__deps: ['$convertOffsets', 'stackSave', 'stackRestore'],
glMultiDrawElementsWEBGL__deps: ['$convertOffsets', '$stackSave', '$stackRestore'],
#endif
glMultiDrawElementsWEBGL: (mode, counts, type, offsets, drawcount) => {
#if MEMORY64
Expand All @@ -3983,7 +3984,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glMultiDrawElementsInstancedWEBGL__sig: 'vipippi',
glMultiDrawElementsInstancedANGLE: 'glMultiDrawElementsInstancedWEBGL',
#if MEMORY64
glMultiDrawElementsInstancedWEBGL__deps: ['$convertOffsets', 'stackSave', 'stackRestore'],
glMultiDrawElementsInstancedWEBGL__deps: ['$convertOffsets', '$stackSave', '$stackRestore'],
#endif
glMultiDrawElementsInstancedWEBGL: (mode, counts, type, offsets, instanceCounts, drawcount) => {
#if MEMORY64
Expand Down
2 changes: 1 addition & 1 deletion src/settings_internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var SUPPORTS_PROMISE_ANY = false;
// Wasm backend symbols that are considered system symbols and don't
// have the normal C symbol name mangled applied (== prefix with an underscore)
// (Also implicily on this list is any function that starts with string "dynCall_")
var WASM_SYSTEM_EXPORTS = ['stackAlloc', 'stackSave', 'stackRestore', 'getTempRet0', 'setTempRet0'];
var WASM_SYSTEM_EXPORTS = ['getTempRet0', 'setTempRet0'];

// Internal: value of -flto argument (either full or thin)
var LTO = 0;
Expand Down
19 changes: 6 additions & 13 deletions system/lib/compiler-rt/stack_ops.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.globl stackSave
.globl stackRestore
.globl stackAlloc
.globl _emscripten_stack_restore
.globl _emscripten_stack_alloc
.globl emscripten_stack_get_current

#ifdef __wasm64__
Expand All @@ -13,19 +12,14 @@

.globaltype __stack_pointer, PTR

stackSave:
.functype stackSave() -> (PTR)
global.get __stack_pointer
end_function

stackRestore:
.functype stackRestore(PTR) -> ()
_emscripten_stack_restore:
.functype _emscripten_stack_restore(PTR) -> ()
local.get 0
global.set __stack_pointer
end_function

stackAlloc:
.functype stackAlloc(PTR) -> (PTR)
_emscripten_stack_alloc:
.functype _emscripten_stack_alloc(PTR) -> (PTR)
.local PTR, PTR
global.get __stack_pointer
# Get arg 0 -> number of bytes to allocate
Expand All @@ -44,4 +38,3 @@ emscripten_stack_get_current:
.functype emscripten_stack_get_current () -> (PTR)
global.get __stack_pointer
end_function

2 changes: 2 additions & 0 deletions test/core/stackAlloc.cpp → test/core/test_stackAlloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <emscripten.h>

EM_JS_DEPS(deps, "$stackSave,$stackAlloc");

int main() {
EM_ASM({
var size = 128;
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions test/other/metadce/test_metadce_cxx_ctors1.exports
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
__cxa_is_pointer_type
__indirect_function_table
__wasm_call_ctors
_emscripten_stack_alloc
_emscripten_stack_restore
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiji
dynCall_viijii
emscripten_stack_get_current
main
memory
stackAlloc
stackRestore
stackSave
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125815
125860
6 changes: 3 additions & 3 deletions test/other/metadce/test_metadce_cxx_ctors2.exports
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
__cxa_is_pointer_type
__indirect_function_table
_emscripten_stack_alloc
_emscripten_stack_restore
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiji
dynCall_viijii
emscripten_stack_get_current
main
memory
stackAlloc
stackRestore
stackSave
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125274
125319
6 changes: 3 additions & 3 deletions test/other/metadce/test_metadce_cxx_except.exports
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ __cxa_increment_exception_refcount
__cxa_is_pointer_type
__indirect_function_table
__wasm_call_ctors
_emscripten_stack_alloc
_emscripten_stack_restore
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiiii
dynCall_jiji
dynCall_viijii
emscripten_stack_get_current
main
memory
setTempRet0
setThrew
stackAlloc
stackRestore
stackSave
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10931
10942
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28115
28148
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
168051
168096
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.exports
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
__indirect_function_table
__trap
__wasm_call_ctors
_emscripten_stack_alloc
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiji
dynCall_viijii
main
memory
stackAlloc
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139189
139202
6 changes: 3 additions & 3 deletions test/other/metadce/test_metadce_cxx_mangle.exports
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ __cxa_increment_exception_refcount
__cxa_is_pointer_type
__indirect_function_table
__wasm_call_ctors
_emscripten_stack_alloc
_emscripten_stack_restore
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiiii
dynCall_jiji
dynCall_viijii
emscripten_stack_get_current
free
main
memory
setTempRet0
setThrew
stackAlloc
stackRestore
stackSave
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10939
10949
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28116
28149
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
223535
223580
6 changes: 3 additions & 3 deletions test/other/metadce/test_metadce_cxx_noexcept.exports
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
__cxa_is_pointer_type
__indirect_function_table
__wasm_call_ctors
_emscripten_stack_alloc
_emscripten_stack_restore
dynCall_iiiiiijj
dynCall_iiiiij
dynCall_iiiiijj
dynCall_jiji
dynCall_viijii
emscripten_stack_get_current
main
memory
stackAlloc
stackRestore
stackSave
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128612
128657
Loading

0 comments on commit faee8d3

Please sign in to comment.