Skip to content

Commit 1c40702

Browse files
authored
Avoid always including getWasmTableEntry/setWasmTableEntry library functions (#17373)
1 parent cbcda4b commit 1c40702

8 files changed

+40
-15
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ See docs/process.md for more on how version tagging works.
3636
added to `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. (#17370)
3737
- The `run` runtime function is no longer exported by default. It can be added
3838
to `EXPORTED_RUNTIME_METHODS` if needed.
39+
- The getWasmTableEntry/setWasmTableEntry library function are no longer
40+
included by default. Add them to `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE` or
41+
`EXPORTED_RUNTIME_METHODS` if you want to use them outside of JS library code.
3942

4043
3.1.15 - 07/01/2022
4144
-------------------

emcc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,8 +1851,6 @@ def phase_linker_setup(options, state, newargs, user_settings):
18511851
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$dynCall']
18521852

18531853
if not settings.BOOTSTRAPPING_STRUCT_INFO:
1854-
# Include the internal library function since they are used by runtime functions.
1855-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getWasmTableEntry', '$setWasmTableEntry']
18561854
if settings.SAFE_HEAP:
18571855
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getValue_safe', '$setValue_safe']
18581856
if not settings.MINIMAL_RUNTIME:

emscripten.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
347347
if settings.INITIAL_TABLE == -1:
348348
settings.INITIAL_TABLE = dylink_sec.table_size + 1
349349

350+
invoke_funcs = metadata['invokeFuncs']
351+
if invoke_funcs:
352+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getWasmTableEntry']
353+
350354
glue, forwarded_data = compile_settings()
351355
if DEBUG:
352356
logger.debug(' emscript: glue took %s seconds' % (time.time() - t))
@@ -397,7 +401,6 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
397401
out.write(normalize_line_endings(pre))
398402
pre = None
399403

400-
invoke_funcs = metadata['invokeFuncs']
401404
sending = create_sending(invoke_funcs, metadata)
402405
receiving = create_receiving(exports)
403406

src/jsifier.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ ${argConvertions}
143143
});
144144
}
145145

146-
function processLibraryFunction(snippet, ident, finalName) {
147-
// It is possible that when printing the function as a string on Windows, the js interpreter we are in returns the string with Windows
148-
// line endings \r\n. This is undesirable, since line endings are managed in the form \n in the output for binary file writes, so
149-
// make sure the endings are uniform.
146+
function processLibraryFunction(snippet, ident, finalName, deps) {
147+
// It is possible that when printing the function as a string on Windows,
148+
// the js interpreter we are in returns the string with Windows line endings
149+
// \r\n. This is undesirable, since line endings are managed in the form \n
150+
// in the output for binary file writes, so make sure the endings are
151+
// uniform.
150152
snippet = snippet.toString().replace(/\r\n/gm, '\n');
151153

152154
// name the function; overwrite if it's already named
@@ -163,20 +165,37 @@ function ${name}(${args}) {
163165
return ret;
164166
}`);
165167
}
168+
166169
if (MEMORY64) {
167170
const sig = LibraryManager.library[ident + '__sig'];
168171
if (sig && sig.includes('p')) {
169172
snippet = convertPointerParams(snippet, sig);
170173
}
171174
}
175+
172176
return snippet;
173177
}
174178

179+
function addImplicitDeps(snippet, deps) {
180+
// There are some very common dependencies that we inject automatically
181+
// by conservatively scanning the input functions for their usage.
182+
// Specifically, these are dependencies that are automatically generated by
183+
// the {{{ makeDynCall }}} macro which is very common.
184+
const autoDeps = ['getDynCaller', 'getWasmTableEntry'];
185+
for (const dep of autoDeps) {
186+
if (snippet.includes(dep + '(')) {
187+
deps.push('$' + dep);
188+
}
189+
}
190+
}
191+
175192
// functionStub
176193
function functionStubHandler(item) {
177-
// In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc.
178-
// where the number specifies the number of arguments. In Emscripten, route all these to a single function '__cxa_find_matching_catch'
179-
// that variadically processes all of these functions using JS 'arguments' object.
194+
// In LLVM, exceptions generate a set of functions of form
195+
// __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. where
196+
// the number specifies the number of arguments. In Emscripten, route all
197+
// these to a single function '__cxa_find_matching_catch' that variadically
198+
// processes all of these functions using JS 'arguments' object.
180199
if (item.identMangled.startsWith('___cxa_find_matching_catch_')) {
181200
if (DISABLE_EXCEPTION_THROWING) {
182201
error('DISABLE_EXCEPTION_THROWING was set (likely due to -fno-exceptions), which means no C++ exception throwing support code is linked in, but exception catching code appears. Either do not set DISABLE_EXCEPTION_THROWING (if you do want exception throwing) or compile all source files with -fno-except (so that no exceptions support code is required); also make sure DISABLE_EXCEPTION_CATCHING is set to the right value - if you want exceptions, it should be off, and vice versa.');
@@ -301,9 +320,11 @@ function ${name}(${args}) {
301320
}
302321
} else if (typeof snippet == 'object') {
303322
snippet = stringifyWithFunctions(snippet);
323+
addImplicitDeps(snippet, deps);
304324
} else if (typeof snippet == 'function') {
305325
isFunction = true;
306-
snippet = processLibraryFunction(snippet, ident, finalName);
326+
snippet = processLibraryFunction(snippet, ident, finalName, deps);
327+
addImplicitDeps(snippet, deps);
307328
libraryFunctions.push(finalName);
308329
}
309330

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31596
1+
31595
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31696
1+
31695
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
93189
1+
92392
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
54734
1+
54045

0 commit comments

Comments
 (0)