Skip to content

Commit ae9349c

Browse files
authored
Extract two standalone functions from runJSify. NFC (#23851)
In that past we avoided doing this kind of thing since it would make these symbol visible outside of the module. However, since we converted ES6 modules that is no longer the case. Without `export` or `addToCompileTimeContext` these symbols will stay local to this file.
1 parent 9eb8f5f commit ae9349c

File tree

1 file changed

+102
-102
lines changed

1 file changed

+102
-102
lines changed

src/jsifier.mjs

+102-102
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,108 @@ function preJS() {
184184
return result;
185185
}
186186

187+
function addImplicitDeps(snippet, deps) {
188+
// There are some common dependencies that we inject automatically by
189+
// conservatively scanning the input functions for their usage.
190+
// Specifically, these are dependencies that are very common and would be
191+
// burdensome to add manually to all functions.
192+
// The first four are deps that are automatically/conditionally added
193+
// by the {{{ makeDynCall }}}, and {{{ runtimeKeepalivePush/Pop }}} macros.
194+
const autoDeps = [
195+
'getDynCaller',
196+
'getWasmTableEntry',
197+
'runtimeKeepalivePush',
198+
'runtimeKeepalivePop',
199+
'UTF8ToString',
200+
];
201+
for (const dep of autoDeps) {
202+
if (snippet.includes(dep + '(')) {
203+
deps.push('$' + dep);
204+
}
205+
}
206+
}
207+
208+
function handleI64Signatures(symbol, snippet, sig, i53abi) {
209+
// Handle i64 parameters and return values.
210+
//
211+
// When WASM_BIGINT is enabled these arrive as BigInt values which we
212+
// convert to int53 JS numbers. If necessary, we also convert the return
213+
// value back into a BigInt.
214+
//
215+
// When WASM_BIGINT is not enabled we receive i64 values as a pair of i32
216+
// numbers which is converted to single int53 number. In necessary, we also
217+
// split the return value into a pair of i32 numbers.
218+
return modifyJSFunction(snippet, (args, body, async_, oneliner) => {
219+
let argLines = args.split('\n');
220+
argLines = argLines.map((line) => line.split('//')[0]);
221+
const argNames = argLines
222+
.join(' ')
223+
.split(',')
224+
.map((name) => name.trim());
225+
const newArgs = [];
226+
let argConversions = '';
227+
if (sig.length > argNames.length + 1) {
228+
error(`handleI64Signatures: signature too long for ${symbol}`);
229+
return snippet;
230+
}
231+
for (let i = 0; i < argNames.length; i++) {
232+
const name = argNames[i];
233+
// If sig is shorter than argNames list then argType will be undefined
234+
// here, which will result in the default case below.
235+
const argType = sig[i + 1];
236+
if (WASM_BIGINT && ((MEMORY64 && argType == 'p') || (i53abi && argType == 'j'))) {
237+
argConversions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
238+
} else {
239+
if (argType == 'j' && i53abi) {
240+
argConversions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
241+
newArgs.push(defineI64Param(name));
242+
} else if (argType == 'p' && CAN_ADDRESS_2GB) {
243+
argConversions += ` ${name} >>>= 0;\n`;
244+
newArgs.push(name);
245+
} else {
246+
newArgs.push(name);
247+
}
248+
}
249+
}
250+
251+
if (!WASM_BIGINT) {
252+
args = newArgs.join(',');
253+
}
254+
255+
if ((sig[0] == 'j' && i53abi) || (sig[0] == 'p' && MEMORY64)) {
256+
const await_ = async_ ? 'await ' : '';
257+
// For functions that where we need to mutate the return value, we
258+
// also need to wrap the body in an inner function.
259+
if (oneliner) {
260+
if (argConversions) {
261+
return `${async_}(${args}) => {
262+
${argConversions}
263+
return ${makeReturn64(await_ + body)};
264+
}`;
265+
}
266+
return `${async_}(${args}) => ${makeReturn64(await_ + body)};`;
267+
}
268+
return `\
269+
${async_}function(${args}) {
270+
${argConversions}
271+
var ret = (() => { ${body} })();
272+
return ${makeReturn64(await_ + 'ret')};
273+
}`;
274+
}
275+
276+
// Otherwise no inner function is needed and we covert the arguments
277+
// before executing the function body.
278+
if (oneliner) {
279+
body = `return ${body}`;
280+
}
281+
return `\
282+
${async_}function(${args}) {
283+
${argConversions}
284+
${body};
285+
}`;
286+
});
287+
}
288+
187289
export async function runJSify(outputFile, symbolsOnly) {
188290
const libraryItems = [];
189291
const symbolDeps = {};
@@ -217,87 +319,6 @@ export async function runJSify(outputFile, symbolsOnly) {
217319
}
218320
}
219321

220-
function handleI64Signatures(symbol, snippet, sig, i53abi) {
221-
// Handle i64 parameters and return values.
222-
//
223-
// When WASM_BIGINT is enabled these arrive as BigInt values which we
224-
// convert to int53 JS numbers. If necessary, we also convert the return
225-
// value back into a BigInt.
226-
//
227-
// When WASM_BIGINT is not enabled we receive i64 values as a pair of i32
228-
// numbers which is converted to single int53 number. In necessary, we also
229-
// split the return value into a pair of i32 numbers.
230-
return modifyJSFunction(snippet, (args, body, async_, oneliner) => {
231-
let argLines = args.split('\n');
232-
argLines = argLines.map((line) => line.split('//')[0]);
233-
const argNames = argLines
234-
.join(' ')
235-
.split(',')
236-
.map((name) => name.trim());
237-
const newArgs = [];
238-
let argConversions = '';
239-
if (sig.length > argNames.length + 1) {
240-
error(`handleI64Signatures: signature too long for ${symbol}`);
241-
return snippet;
242-
}
243-
for (let i = 0; i < argNames.length; i++) {
244-
const name = argNames[i];
245-
// If sig is shorter than argNames list then argType will be undefined
246-
// here, which will result in the default case below.
247-
const argType = sig[i + 1];
248-
if (WASM_BIGINT && ((MEMORY64 && argType == 'p') || (i53abi && argType == 'j'))) {
249-
argConversions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
250-
} else {
251-
if (argType == 'j' && i53abi) {
252-
argConversions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
253-
newArgs.push(defineI64Param(name));
254-
} else if (argType == 'p' && CAN_ADDRESS_2GB) {
255-
argConversions += ` ${name} >>>= 0;\n`;
256-
newArgs.push(name);
257-
} else {
258-
newArgs.push(name);
259-
}
260-
}
261-
}
262-
263-
if (!WASM_BIGINT) {
264-
args = newArgs.join(',');
265-
}
266-
267-
if ((sig[0] == 'j' && i53abi) || (sig[0] == 'p' && MEMORY64)) {
268-
const await_ = async_ ? 'await ' : '';
269-
// For functions that where we need to mutate the return value, we
270-
// also need to wrap the body in an inner function.
271-
if (oneliner) {
272-
if (argConversions) {
273-
return `${async_}(${args}) => {
274-
${argConversions}
275-
return ${makeReturn64(await_ + body)};
276-
}`;
277-
}
278-
return `${async_}(${args}) => ${makeReturn64(await_ + body)};`;
279-
}
280-
return `\
281-
${async_}function(${args}) {
282-
${argConversions}
283-
var ret = (() => { ${body} })();
284-
return ${makeReturn64(await_ + 'ret')};
285-
}`;
286-
}
287-
288-
// Otherwise no inner function is needed and we covert the arguments
289-
// before executing the function body.
290-
if (oneliner) {
291-
body = `return ${body}`;
292-
}
293-
return `\
294-
${async_}function(${args}) {
295-
${argConversions}
296-
${body};
297-
}`;
298-
});
299-
}
300-
301322
function processLibraryFunction(snippet, symbol, mangled, deps, isStub) {
302323
// It is possible that when printing the function as a string on Windows,
303324
// the js interpreter we are in returns the string with Windows line endings
@@ -397,27 +418,6 @@ function(${args}) {
397418
return snippet;
398419
}
399420

400-
function addImplicitDeps(snippet, deps) {
401-
// There are some common dependencies that we inject automatically by
402-
// conservatively scanning the input functions for their usage.
403-
// Specifically, these are dependencies that are very common and would be
404-
// burdensome to add manually to all functions.
405-
// The first four are deps that are automatically/conditionally added
406-
// by the {{{ makeDynCall }}}, and {{{ runtimeKeepalivePush/Pop }}} macros.
407-
const autoDeps = [
408-
'getDynCaller',
409-
'getWasmTableEntry',
410-
'runtimeKeepalivePush',
411-
'runtimeKeepalivePop',
412-
'UTF8ToString',
413-
];
414-
for (const dep of autoDeps) {
415-
if (snippet.includes(dep + '(')) {
416-
deps.push('$' + dep);
417-
}
418-
}
419-
}
420-
421421
function symbolHandler(symbol) {
422422
// In LLVM, exceptions generate a set of functions of form
423423
// __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. where

0 commit comments

Comments
 (0)