Skip to content

Commit 07af96a

Browse files
authored
jsifier: Simplify runJSify. NFC (#19047)
Rather than processing JS objects, just process symbols and geneate JS strings. I think there was perhaps more complex needed in the past but no longer.
1 parent 9d46f34 commit 07af96a

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/jsifier.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,34 +211,29 @@ function ${name}(${args}) {
211211
}
212212
}
213213

214-
function itemHandler(item) {
214+
function symbolHandler(symbol) {
215215
// In LLVM, exceptions generate a set of functions of form
216216
// __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. where
217217
// the number specifies the number of arguments. In Emscripten, route all
218218
// these to a single function '__cxa_find_matching_catch' that variadically
219219
// processes all of these functions using JS 'arguments' object.
220-
if (item.mangled.startsWith('___cxa_find_matching_catch_')) {
220+
if (symbol.startsWith('__cxa_find_matching_catch_')) {
221221
if (DISABLE_EXCEPTION_THROWING) {
222222
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.');
223223
return;
224224
}
225-
const num = +item.mangled.split('_').slice(-1)[0];
225+
const num = +symbol.split('_').slice(-1)[0];
226226
addCxaCatch(num);
227227
// Continue, with the code below emitting the proper JavaScript based on
228228
// what we just added to the library.
229229
}
230230

231-
const TOP_LEVEL = 'top-level compiled C/C++ code';
232-
233-
function addFromLibrary(item, dependent) {
231+
function addFromLibrary(symbol, dependent) {
234232
// dependencies can be JS functions, which we just run
235-
if (typeof item == 'function') {
236-
return item();
233+
if (typeof symbol == 'function') {
234+
return symbol();
237235
}
238236

239-
const symbol = item.symbol;
240-
const mangled = item.mangled;
241-
242237
if (symbol in addedLibraryItems) {
243238
return;
244239
}
@@ -271,6 +266,8 @@ function ${name}(${args}) {
271266
// will resolve the correct symbol at runtime, or assert if its missing.
272267
let isStub = false;
273268

269+
const mangled = mangleCSymbolName(symbol);
270+
274271
if (!LibraryManager.library.hasOwnProperty(symbol)) {
275272
const isWeakImport = WEAK_IMPORTS.has(symbol);
276273
if (!isDefined(symbol) && !isWeakImport) {
@@ -369,9 +366,7 @@ function ${name}(${args}) {
369366
}
370367
if (postset && !addedLibraryItems[postsetId]) {
371368
addedLibraryItems[postsetId] = true;
372-
postSets.push({
373-
JS: postset + ';',
374-
});
369+
postSets.push(postset + ';');
375370
}
376371
}
377372

@@ -381,9 +376,6 @@ function ${name}(${args}) {
381376
const deps_list = deps.join("','");
382377
const identDependents = symbol + `__deps: ['${deps_list}']`;
383378
function addDependency(dep) {
384-
if (typeof dep != 'function') {
385-
dep = {symbol: dep, mangled: mangleCSymbolName(dep)};
386-
}
387379
return addFromLibrary(dep, `${identDependents}, referenced by ${dependent}`);
388380
}
389381
let contentText;
@@ -447,8 +439,8 @@ function ${name}(${args}) {
447439
return depsText + commentText + contentText;
448440
}
449441

450-
libraryItems.push(item);
451-
item.JS = addFromLibrary(item, TOP_LEVEL);
442+
const JS = addFromLibrary(symbol, 'top-level compiled C/C++ code');
443+
libraryItems.push(JS);
452444
}
453445

454446
function includeFile(fileName) {
@@ -492,7 +484,7 @@ function ${name}(${args}) {
492484
includeFile(preFile);
493485

494486
for (const item of libraryItems.concat(postSets)) {
495-
print(indentify(item.JS || '', 2));
487+
print(indentify(item || '', 2));
496488
}
497489

498490
if (PTHREADS) {
@@ -544,10 +536,7 @@ function ${name}(${args}) {
544536
}
545537

546538
for (const sym of symbolsNeeded) {
547-
itemHandler({
548-
symbol: sym,
549-
mangled: mangleCSymbolName(sym),
550-
});
539+
symbolHandler(sym);
551540
}
552541

553542
if (symbolsOnly) {

0 commit comments

Comments
 (0)