Skip to content

Commit

Permalink
Allow arrow functions in JS libraries. NFC (#19539)
Browse files Browse the repository at this point in the history
This saves a few bytes of code size.  I only changes the most commonly
used library functions but we could potentially do the rest too.
  • Loading branch information
sbc100 authored Jun 8, 2023
1 parent 38ca4f8 commit 48b0645
Show file tree
Hide file tree
Showing 63 changed files with 357 additions and 432 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ See docs/process.md for more on how version tagging works.
`modifyJSFunction` and its callback function no longer takes the name of the
function being modified. The name is not relevant for JS library functions
and can be safely ignored.
- JS library functions can now be implemented using ES6 arrow notation, which
can save to a few bytes on JS code size. (#19539)

3.1.41 - 06/06/23
-----------------
Expand Down
15 changes: 8 additions & 7 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,21 @@ function(${args}) {
if (isFunction) {
// Emit the body of a JS library function.
if ((USE_ASAN || USE_LSAN || UBSAN_RUNTIME) && LibraryManager.library[symbol + '__noleakcheck']) {
contentText = modifyJSFunction(snippet, (args, body) => `
function(${args}) {
return withBuiltinMalloc(function() {
${body}
});
}\n`);
contentText = modifyJSFunction(snippet, (args, body) => `(${args}) => withBuiltinMalloc(() => {${body}})`);
deps.push('$withBuiltinMalloc');
} else {
contentText = snippet; // Regular JS function that will be executed in the context of the calling thread.
}
// Give the function the correct (mangled) name. Overwrite it if it's
// already named. This must happen after the last call to
// modifyJSFunction which could have changed or removed the name.
contentText = contentText.replace(/function(?:\s+([^(]+))?\s*\(/, `function ${mangled}(`);
if (contentText.match(/^\s*([^}]*)\s*=>/s)) {
// Handle arrow functions
contentText = `var ${mangled} = ` + contentText + ';';
} else {
// Handle regular (non-arrow) functions
contentText = contentText.replace(/function(?:\s+([^(]+))?\s*\(/, `function ${mangled}(`);
}
} else if (typeof snippet == 'string' && snippet.startsWith(';')) {
// In JS libraries
// foo: ';[code here verbatim]'
Expand Down
Loading

0 comments on commit 48b0645

Please sign in to comment.