Skip to content

Commit

Permalink
[embind] Simplify createNamedFunction. NFC (#20479)
Browse files Browse the repository at this point in the history
Further simplification of createNamedFunction on top of #18748: we can avoid extra JS wrapper by setting `name` of the function directly.

This is only supported via "configuring" it by `Object.defineProperty` rather than plain assignment, but otherwise has exactly same effect - it sets debug name of the function in-place.

Also, ever since #18748 switched away from `eval`, there is no requirement for the function name to be "legal" - we can set it to the plain demangled string, making debug names even more readable.
RReverser authored Oct 18, 2023
1 parent 6870616 commit 94f726a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
@@ -167,18 +167,21 @@ 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];
},
$createNamedFunction: (name, body) => Object.defineProperty(body, 'name', {
value: name
}),
// 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) {

0 comments on commit 94f726a

Please sign in to comment.