Skip to content

Commit cf4b605

Browse files
committed
Fix addFunction in -sWASM=2 mode
Fixes: #25394
1 parent 3fbc489 commit cf4b605

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/lib/libaddfunction.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,16 @@ addToLibrary({
7777
return code;
7878
})),
7979

80+
#if !WASM2JS || WASM == 2
8081
// Wraps a JS function as a wasm function with a given signature.
81-
#if !WASM2JS
8282
$convertJsFunctionToWasm__deps: [
8383
'$uleb128EncodeWithLen',
8484
#if WASM_JS_TYPES
8585
'$sigToWasmTypes',
8686
#endif
8787
'$generateTypePack'
8888
],
89-
#endif
9089
$convertJsFunctionToWasm: (func, sig) => {
91-
#if WASM2JS
92-
// return func;
93-
#else // WASM2JS
94-
9590
#if ASSERTIONS && !WASM_BIGINT
9691
assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled');
9792
#endif
@@ -135,8 +130,8 @@ addToLibrary({
135130
var instance = new WebAssembly.Instance(module, { 'e': { 'f': func } });
136131
var wrappedFunc = instance.exports['f'];
137132
return wrappedFunc;
138-
#endif // WASM2JS
139133
},
134+
#endif // !WASM2JS && WASM != 2
140135

141136
$freeTableIndexes: [],
142137

@@ -192,18 +187,21 @@ addToLibrary({
192187
* 'sig' parameter is required if the function being added is a JS function.
193188
*/
194189
$addFunction__docs: '/** @param {string=} sig */',
195-
$addFunction__deps: ['$convertJsFunctionToWasm', '$getFunctionAddress',
190+
$addFunction__deps: ['$getFunctionAddress',
196191
'$functionsInTableMap', '$getEmptyTableSlot',
197192
'$setWasmTableEntry',
193+
#if !WASM2JS || WASM == 2
194+
'$convertJsFunctionToWasm',
195+
#endif
198196
#if ASSERTIONS >= 2
199197
'$getWasmTableEntry', '$wasmTable',
200198
#endif
201199
],
202200

203201
$addFunction: (func, sig) => {
204-
#if ASSERTIONS
202+
#if ASSERTIONS
205203
assert(typeof func != 'undefined');
206-
#endif // ASSERTIONS
204+
#endif // ASSERTIONS
207205
// Check if the function is already in the table, to ensure each function
208206
// gets a unique index.
209207
var rtn = getFunctionAddress(func);
@@ -213,17 +211,20 @@ addToLibrary({
213211

214212
// It's not in the table, add it now.
215213

216-
#if ASSERTIONS >= 2
214+
#if ASSERTIONS >= 2
217215
// Make sure functionsInTableMap is actually up to date, that is, that this
218216
// function is not actually in the wasm Table despite not being tracked in
219217
// functionsInTableMap.
220218
for (var i = 0; i < wasmTable.length; i++) {
221219
assert(getWasmTableEntry(i) != func, 'function in Table but not functionsInTableMap');
222220
}
223-
#endif
221+
#endif
224222

225223
var ret = getEmptyTableSlot();
226224

225+
#if WASM2JS && WASM != 2
226+
setWasmTableEntry(ret, func);
227+
#else
227228
// Set the new value.
228229
try {
229230
// Attempting to call this with JS function will cause of table.set() to fail
@@ -232,12 +233,13 @@ addToLibrary({
232233
if (!(err instanceof TypeError)) {
233234
throw err;
234235
}
235-
#if ASSERTIONS
236+
#if ASSERTIONS
236237
assert(typeof sig != 'undefined', 'Missing signature argument to addFunction: ' + func);
237-
#endif
238+
#endif
238239
var wrapped = convertJsFunctionToWasm(func, sig);
239240
setWasmTableEntry(ret, wrapped);
240241
}
242+
#endif
241243

242244
functionsInTableMap.set(func, ret);
243245

test/codesize/test_codesize_hello_dylink.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 26946,
3-
"a.out.js.gz": 11480,
3+
"a.out.js.gz": 11478,
44
"a.out.nodebug.wasm": 18567,
55
"a.out.nodebug.wasm.gz": 9199,
66
"total": 45513,
7-
"total_gz": 20679,
7+
"total_gz": 20677,
88
"sent": [
99
"__heap_base",
1010
"__indirect_function_table",

test/test_other.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14638,25 +14638,24 @@ def test_windows_nodefs_execution_permission(self):
1463814638
self.do_run(src)
1463914639

1464014640
@parameterized({
14641-
'wasm2js': (True,),
14642-
'': (False,),
14641+
'': ([],),
14642+
'wasm2js': (['-sWASM=0'],),
14643+
'wasm2js_fallback': (['-sWASM=2'],),
1464314644
})
14644-
def test_add_js_function(self, wasm2js):
14645+
def test_add_js_function(self, args):
1464514646
self.set_setting('INVOKE_RUN', 0)
1464614647
self.set_setting('WASM_ASYNC_COMPILATION', 0)
1464714648
self.set_setting('ALLOW_TABLE_GROWTH')
1464814649
self.set_setting('EXPORTED_RUNTIME_METHODS', ['callMain'])
14649-
if wasm2js:
14650-
self.set_setting('WASM', 0)
14651-
self.cflags += ['--post-js', test_file('interop/test_add_function_post.js')]
14650+
self.cflags += args + ['--post-js', test_file('interop/test_add_function_post.js')]
1465214651

1465314652
print('basics')
1465414653
self.do_run_in_out_file_test('interop/test_add_function.cpp')
1465514654

1465614655
print('with ALLOW_TABLE_GROWTH=0')
1465714656
self.set_setting('ALLOW_TABLE_GROWTH', 0)
1465814657
expected = 'Unable to grow wasm table'
14659-
if wasm2js:
14658+
if '-sWASM=0' in args:
1466014659
# in wasm2js the error message doesn't come from the VM, but from our
1466114660
# emulation code. when ASSERTIONS are enabled we show a clear message, but
1466214661
# in optimized builds we don't waste code size on that, and the JS engine

0 commit comments

Comments
 (0)