Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[embind] Simplify createNamedFunction. NFC #20479

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,23 @@ var LibraryEmbind = {
return errorClass;
},


$createNamedFunction__deps: ['$makeLegalFunctionName'],
$createNamedFunction: function(name, body) {
name = makeLegalFunctionName(name);
// Use an abject with a computed property name to create a new function with
// a name specified at runtime, but without using `new Function` or `eval`.
return {
[name]: function() {
return body.apply(this, arguments);
}
}[name];
return Object.defineProperty(body, 'name', {
value: name
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do this whole thing with an arrow function. skipping the return and the outer curly braces?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the old version of this function return a new object while the new version mutates the function in place, is that right/ok?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell from usages, it should be fine - we only use it with newly created function which is why we want to have a unique name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do this whole thing with an arrow function. skipping the return and the outer curly braces?

Hm arrow functions for libraries seem somewhat rare in the codebase generally. If it's something we want more of, maybe worth running a separate codemod step over all libs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this one to arrow function for now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should be fairly common these days. I've been converting them over time, where possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just meant we should probably do a single codemod run (see my ast-grep comment in the other PR).

},
// All browsers that support WebAssembly also support configurable function name,
// but we might be building for very old browsers via WASM2JS.
#if MIN_CHROME_VERSION < 43 || MIN_EDGE_VERSION < 14 || MIN_SAFARI_VERSION < 100101 || MIN_FIREFOX_VERSION < 38
// In that case, check if configurable function name is supported at init time
// and, if not, replace with a fallback that returns function as-is as those browsers
// don't support other methods either.
$createNamedFunction__postset: `
if (!Object.getOwnPropertyDescriptor(Function.prototype, 'name').configurable) {
createNamedFunction = (name, body) => body;
}
`,
#endif

$embindRepr: (v) => {
if (v === null) {
Expand Down