Skip to content

Commit dfb3585

Browse files
authored
Make wasmTable into a library symbol (#20158)
1 parent 202127c commit dfb3585

20 files changed

+52
-39
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.46 (in development)
2222
-----------------------
23+
- The `wasmTable` global is now a JS library function that will only be included
24+
as needed. Code that references `wasmTable` will no need to declare a
25+
dependency on it. It can also be explictly included using
26+
`-sEXPORTED_RUNTIME_METHODS=wasmTable`.
2327
- libunwind updated to LLVM 16.0.6. (#20088)
2428
- The `--minify=0` commnad line flag will now preserve comments as well as
2529
whitespace. This means the resulting output can then be run though closure

emcc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,9 @@ def phase_linker_setup(options, state, newargs):
21212121
if not settings.BOOTSTRAPPING_STRUCT_INFO and settings.SAFE_HEAP:
21222122
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getValue_safe', '$setValue_safe']
21232123

2124+
if settings.ABORT_ON_WASM_EXCEPTIONS or settings.SPLIT_MODULE:
2125+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$wasmTable']
2126+
21242127
if settings.MAIN_MODULE:
21252128
assert not settings.SIDE_MODULE
21262129
if settings.MAIN_MODULE == 1:

src/jsifier.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ function(${args}) {
509509
// 'var foo;[code here verbatim];'
510510
contentText = 'var ' + mangled + snippet;
511511
if (snippet[snippet.length - 1] != ';' && snippet[snippet.length - 1] != '}') contentText += ';';
512+
} else if (typeof snippet == 'undefined') {
513+
contentText = `var ${mangled};`;
512514
} else {
513515
// In JS libraries
514516
// foo: '=[value]'

src/library.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,7 +3199,7 @@ addToLibrary({
31993199
$wasmTableMirror: [],
32003200

32013201
$setWasmTableEntry__internal: true,
3202-
$setWasmTableEntry__deps: ['$wasmTableMirror'],
3202+
$setWasmTableEntry__deps: ['$wasmTableMirror', '$wasmTable'],
32033203
$setWasmTableEntry: (idx, func) => {
32043204
wasmTable.set(idx, func);
32053205
// With ABORT_ON_WASM_EXCEPTIONS wasmTable.get is overriden to return wrapped
@@ -3209,7 +3209,7 @@ addToLibrary({
32093209
},
32103210

32113211
$getWasmTableEntry__internal: true,
3212-
$getWasmTableEntry__deps: ['$wasmTableMirror'],
3212+
$getWasmTableEntry__deps: ['$wasmTableMirror', '$wasmTable'],
32133213
$getWasmTableEntry: (funcPtr) => {
32143214
#if MEMORY64
32153215
// Function pointers are 64-bit, but wasmTable.get() requires a Number.
@@ -3234,8 +3234,10 @@ addToLibrary({
32343234

32353235
#else
32363236

3237+
$setWasmTableEntry__deps: ['$wasmTable'],
32373238
$setWasmTableEntry: (idx, func) => wasmTable.set(idx, func),
32383239

3240+
$getWasmTableEntry__deps: ['$wasmTable'],
32393241
$getWasmTableEntry: (funcPtr) => {
32403242
#if MEMORY64
32413243
// Function pointers are 64-bit, but wasmTable.get() requires a Number.
@@ -3612,6 +3614,20 @@ addToLibrary({
36123614
$getNativeTypeSize__deps: ['$POINTER_SIZE'],
36133615
$getNativeTypeSize: {{{ getNativeTypeSize }}},
36143616

3617+
#if RELOCATABLE
3618+
// In RELOCATABLE mode we create the table in JS.
3619+
$wasmTable: `=new WebAssembly.Table({
3620+
'initial': {{{ INITIAL_TABLE }}},
3621+
#if !ALLOW_TABLE_GROWTH
3622+
'maximum': {{{ INITIAL_TABLE }}},
3623+
#endif
3624+
'element': 'anyfunc'
3625+
});
3626+
`,
3627+
#else
3628+
$wasmTable: undefined,
3629+
#endif
3630+
36153631
// We used to define these globals unconditionally in support code.
36163632
// Instead, we now define them here so folks can pull it in explicitly, on
36173633
// demand.

src/library_addfunction.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ addToLibrary({
141141
// Weak map of functions in the table to their indexes, created on first use.
142142
$functionsInTableMap: undefined,
143143

144-
$getEmptyTableSlot__deps: ['$freeTableIndexes'],
144+
$getEmptyTableSlot__deps: ['$freeTableIndexes', '$wasmTable'],
145145
$getEmptyTableSlot: () => {
146146
// Reuse a free index if there is one, otherwise grow.
147147
if (freeTableIndexes.length) {
@@ -172,7 +172,7 @@ addToLibrary({
172172
}
173173
},
174174

175-
$getFunctionAddress__deps: ['$updateTableMap', '$functionsInTableMap'],
175+
$getFunctionAddress__deps: ['$updateTableMap', '$functionsInTableMap', '$wasmTable'],
176176
$getFunctionAddress: (func) => {
177177
// First, create the map if this is the first use.
178178
if (!functionsInTableMap) {
@@ -189,7 +189,8 @@ addToLibrary({
189189
$addFunction__docs: '/** @param {string=} sig */',
190190
$addFunction__deps: ['$convertJsFunctionToWasm', '$getFunctionAddress',
191191
'$functionsInTableMap', '$getEmptyTableSlot',
192-
'$getWasmTableEntry', '$setWasmTableEntry'],
192+
'$getWasmTableEntry', '$setWasmTableEntry',
193+
'$wasmTable'],
193194
$addFunction: (func, sig) => {
194195
#if ASSERTIONS
195196
assert(typeof func != 'undefined');

src/library_dylink.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ var LibraryDylink = {
581581
},
582582

583583
#if DYLINK_DEBUG
584+
$dumpTable__deps: ['$wasmTable'],
584585
$dumpTable: () => {
585586
for (var i = 0; i < wasmTable.length; i++)
586587
dbg(`table: ${i} : ${wasmTable.get(i)}`);
@@ -601,6 +602,7 @@ var LibraryDylink = {
601602
'$getDylinkMetadata', '$alignMemory', '$zeroMemory',
602603
'$currentModuleWeakSymbols',
603604
'$updateTableMap',
605+
'$wasmTable',
604606
],
605607
$loadWebAssemblyModule: (binary, flags, libName, localScope, handle) => {
606608
#if DYLINK_DEBUG

src/library_makeDynCall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
addToLibrary({
8-
$createDyncallWrapper__deps: ['$generateFuncType', '$uleb128Encode', 'setTempRet0'],
8+
$createDyncallWrapper__deps: ['$generateFuncType', '$uleb128Encode', 'setTempRet0', '$wasmTable'],
99
$createDyncallWrapper: (sig) => {
1010
var sections = [];
1111
var prelude = [

src/modules.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ function exportRuntime() {
381381
'abort',
382382
'keepRuntimeAlive',
383383
'wasmMemory',
384-
'wasmTable',
385384
'wasmExports',
386385
];
387386

src/postamble_minimal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,12 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
187187
#else
188188
<<< WASM_MODULE_EXPORTS >>>
189189
#endif
190+
#if '$wasmTable' in addedLibraryItems
190191
wasmTable = wasmExports['__indirect_function_table'];
191192
#if ASSERTIONS
192193
assert(wasmTable);
193194
#endif
195+
#endif
194196

195197
#if AUDIO_WORKLET
196198
// If we are in the audio worklet environment, we can only access the Module object

src/preamble.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ assert(!Module['wasmMemory'], 'Use of `wasmMemory` detected. Use -sIMPORTED_MEM
175175
assert(!Module['INITIAL_MEMORY'], 'Detected runtime INITIAL_MEMORY setting. Use -sIMPORTED_MEMORY to define wasmMemory dynamically');
176176
#endif // !IMPORTED_MEMORY && ASSERTIONS
177177

178-
#include "runtime_init_table.js"
179178
#include "runtime_stack_check.js"
180179
#include "runtime_assertions.js"
181180

@@ -1010,20 +1009,20 @@ function createWasm() {
10101009
runMemoryInitializer();
10111010
#endif
10121011

1013-
#if !RELOCATABLE
1012+
#if '$wasmTable' in addedLibraryItems && !RELOCATABLE
10141013
wasmTable = wasmExports['__indirect_function_table'];
10151014
{{{ receivedSymbol('wasmTable') }}}
10161015
#if ASSERTIONS && !PURE_WASI
10171016
assert(wasmTable, "table not found in wasm exports");
10181017
#endif
1019-
#endif
10201018

10211019
#if AUDIO_WORKLET
10221020
// If we are in the audio worklet environment, we can only access the Module object
10231021
// and not the global scope of the main JS script. Therefore we need to export
10241022
// all functions that the audio worklet scope needs onto the Module object.
10251023
Module['wasmTable'] = wasmTable;
10261024
#endif
1025+
#endif
10271026

10281027
#if hasExportedSymbol('__wasm_call_ctors')
10291028
addOnInit(wasmExports['__wasm_call_ctors']);

0 commit comments

Comments
 (0)