Skip to content

Commit 0cf8e69

Browse files
authored
[embind] Remove newFunc helper. NFC (#23937)
The newFunc approach is kind old, from 2013, using new Function can improve the performance drawback from prototype re-assigning.
1 parent 66d2137 commit 0cf8e69

File tree

4 files changed

+9
-40
lines changed

4 files changed

+9
-40
lines changed

src/lib/libembind.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/*global floatReadValueFromPointer, integerReadValueFromPointer, enumReadValueFromPointer, replacePublicSymbol, craftInvokerFunction, tupleRegistrations*/
1919
/*global finalizationRegistry, attachFinalizer, detachFinalizer, releaseClassHandle, runDestructor*/
2020
/*global ClassHandle, makeClassHandle, structRegistrations, whenDependentTypesAreResolved, BindingError, deletionQueue, delayFunction:true, upcastPointer*/
21-
/*global exposePublicSymbol, heap32VectorToArray, newFunc, char_0, char_9*/
21+
/*global exposePublicSymbol, heap32VectorToArray, char_0, char_9*/
2222
/*global getInheritedInstanceCount, getLiveInheritedInstances, setDelayFunction, InternalError, runDestructors*/
2323
/*global requireRegisteredType, unregisterInheritedInstance, registerInheritedInstance, PureVirtualError, throwUnboundTypeError*/
2424
/*global assert, validateThis, downcastPointer, registeredPointers, RegisteredClass, getInheritedInstance */
@@ -705,42 +705,14 @@ var LibraryEmbind = {
705705
}
706706
},
707707

708-
#if DYNAMIC_EXECUTION
709-
$newFunc__deps: ['$createNamedFunction'],
710-
$newFunc: function(constructor, argumentList) {
711-
if (!(constructor instanceof Function)) {
712-
throw new TypeError(`new_ called with constructor type ${typeof(constructor)} which is not a function`);
713-
}
714-
/*
715-
* Previously, the following line was just:
716-
* function dummy() {};
717-
* Unfortunately, Chrome was preserving 'dummy' as the object's name, even
718-
* though at creation, the 'dummy' has the correct constructor name. Thus,
719-
* objects created with IMVU.new would show up in the debugger as 'dummy',
720-
* which isn't very helpful. Using IMVU.createNamedFunction addresses the
721-
* issue. Doubly-unfortunately, there's no way to write a test for this
722-
* behavior. -NRD 2013.02.22
723-
*/
724-
var dummy = createNamedFunction(constructor.name || 'unknownFunctionName', function(){});
725-
dummy.prototype = constructor.prototype;
726-
var obj = new dummy;
727-
728-
var r = constructor.apply(obj, argumentList);
729-
return (r instanceof Object) ? r : obj;
730-
},
731-
#endif
732-
733708
// The path to interop from JS code to C++ code:
734709
// (hand-written JS code) -> (autogenerated JS invoker) -> (template-generated C++ invoker) -> (target C++ function)
735710
// craftInvokerFunction generates the JS invoker function for each function exposed to JS through embind.
736711
$craftInvokerFunction__deps: [
737712
'$createNamedFunction', '$runDestructors', '$throwBindingError', '$usesDestructorStack',
738-
#if DYNAMIC_EXECUTION
739-
'$newFunc',
740713
#if !EMBIND_AOT
741714
'$createJsInvoker',
742715
#endif
743-
#endif
744716
#if EMBIND_AOT
745717
'$InvokerFunctions',
746718
'$createJsInvokerSignature',
@@ -878,9 +850,11 @@ var LibraryEmbind = {
878850
var signature = createJsInvokerSignature(argTypes, isClassMethodFunc, returns, isAsync);
879851
var invokerFn = InvokerFunctions[signature](...closureArgs);
880852
#else
853+
881854
let [args, invokerFnBody] = createJsInvoker(argTypes, isClassMethodFunc, returns, isAsync);
882855
args.push(invokerFnBody);
883-
var invokerFn = newFunc(Function, args)(...closureArgs);
856+
var invokerFn = new Function(...args)(...closureArgs);
857+
884858
#endif
885859
#endif
886860
return createNamedFunction(humanName, invokerFn);

src/lib/libembind_gen.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,6 @@ var LibraryEmbind = {
857857

858858
// Stub functions used by eval, but not needed for TS generation:
859859
$makeLegalFunctionName: () => { throw new Error('stub function should not be called'); },
860-
$newFunc: () => { throw new Error('stub function should not be called'); },
861860
$runDestructors: () => { throw new Error('stub function should not be called'); },
862861
$createNamedFunction: () => { throw new Error('stub function should not be called'); },
863862
};

src/lib/libemval.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
/*global Module:true, Runtime*/
77
/*global HEAP32*/
8-
/*global newFunc*/
98
/*global createNamedFunction*/
109
/*global readLatin1String, stringToUTF8*/
1110
/*global requireRegisteredType, throwBindingError, runDestructors*/
@@ -332,9 +331,6 @@ var LibraryEmVal = {
332331
'$emval_addMethodCaller', '$emval_lookupTypes',
333332
'$createNamedFunction',
334333
'$reflectConstruct', '$emval_returnValue',
335-
#if DYNAMIC_EXECUTION
336-
'$newFunc',
337-
#endif
338334
],
339335
_emval_get_method_caller: (argCount, argTypes, kind) => {
340336
var types = emval_lookupTypes(argCount, argTypes);
@@ -384,7 +380,7 @@ var LibraryEmVal = {
384380
"};\n";
385381

386382
params.push(functionBody);
387-
var invokerFunction = newFunc(Function, params)(...args);
383+
var invokerFunction = new Function(...params)(...args);
388384
#endif
389385
var functionName = `methodCaller<(${types.map(t => t.name).join(', ')}) => ${retType.name}>`;
390386
return emval_addMethodCaller(createNamedFunction(functionName, invokerFunction));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 552,
33
"a.html.gz": 380,
4-
"a.js": 9750,
5-
"a.js.gz": 4262,
4+
"a.js": 9482,
5+
"a.js.gz": 4167,
66
"a.wasm": 7348,
77
"a.wasm.gz": 3378,
8-
"total": 17650,
9-
"total_gz": 8020
8+
"total": 17382,
9+
"total_gz": 7925
1010
}

0 commit comments

Comments
 (0)