Skip to content

Commit 2282236

Browse files
authored
Simplify embind $new_ helper. NFC (#19109)
This function is only needed when DYNAMIC_EXECUTION is defined. Its also only got two callsites that both pass `Function` as arg0. I guess maybe this function previously had more callers that could explain why it seems to general but I looked back as far as the big refactor in 2014 (350f199) and I couldn't find anything. I think this is can be even further simplified but this is a good first step.
1 parent bc31004 commit 2282236

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/embind/embind.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/*global simpleReadValueFromPointer, 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, new_, RegisteredPointer_getPointee, RegisteredPointer_destructor, RegisteredPointer_deleteObject, char_0, char_9*/
21+
/*global exposePublicSymbol, heap32VectorToArray, newFunc, RegisteredPointer_getPointee, RegisteredPointer_destructor, RegisteredPointer_deleteObject, 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, ClassHandle_isAliasOf, ClassHandle_clone, ClassHandle_isDeleted, ClassHandle_deleteLater*/
@@ -906,20 +906,12 @@ var LibraryEmbind = {
906906
}
907907
},
908908

909-
// Function implementation of operator new, per
910-
// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
911-
// 13.2.2
912-
// ES3
913-
$new___deps: ['$createNamedFunction'],
914-
$new_: function(constructor, argumentList) {
909+
#if DYNAMIC_EXECUTION
910+
$newFunc__deps: ['$createNamedFunction'],
911+
$newFunc: function(constructor, argumentList) {
915912
if (!(constructor instanceof Function)) {
916913
throw new TypeError('new_ called with constructor type ' + typeof(constructor) + " which is not a function");
917914
}
918-
#if DYNAMIC_EXECUTION == 0
919-
if (constructor === Function) {
920-
throw new Error('new_ cannot create a new Function with DYNAMIC_EXECUTION == 0.');
921-
}
922-
#endif
923915
/*
924916
* Previously, the following line was just:
925917
* function dummy() {};
@@ -937,15 +929,19 @@ var LibraryEmbind = {
937929
var r = constructor.apply(obj, argumentList);
938930
return (r instanceof Object) ? r : obj;
939931
},
932+
#endif
940933

941934
// The path to interop from JS code to C++ code:
942935
// (hand-written JS code) -> (autogenerated JS invoker) -> (template-generated C++ invoker) -> (target C++ function)
943936
// craftInvokerFunction generates the JS invoker function for each function exposed to JS through embind.
944937
$craftInvokerFunction__deps: [
945-
'$makeLegalFunctionName', '$new_', '$runDestructors', '$throwBindingError',
946-
#if ASYNCIFY
938+
'$makeLegalFunctionName', '$runDestructors', '$throwBindingError',
939+
#if DYNAMIC_EXECUTION
940+
'$newFunc',
941+
#endif
942+
#if ASYNCIFY
947943
'$Asyncify',
948-
#endif
944+
#endif
949945
],
950946
$craftInvokerFunction: function(humanName, argTypes, classType, cppInvokerFunc, cppTargetFunc, isAsync) {
951947
// humanName: a human-readable string name for the function to be generated.
@@ -1149,8 +1145,7 @@ var LibraryEmbind = {
11491145

11501146
args1.push(invokerFnBody);
11511147

1152-
var invokerFunction = new_(Function, args1).apply(null, args2);
1153-
return invokerFunction;
1148+
return newFunc(Function, args1).apply(null, args2);
11541149
#endif
11551150
},
11561151

src/embind/emval.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/*global Module:true, Runtime*/
77
/*global HEAP32*/
8-
/*global new_*/
8+
/*global newFunc*/
99
/*global createNamedFunction*/
1010
/*global readLatin1String, stringToUTF8*/
1111
/*global requireRegisteredType, throwBindingError, runDestructors*/
@@ -414,7 +414,13 @@ var LibraryEmVal = {
414414

415415
$emval_registeredMethods: [],
416416
_emval_get_method_caller__sig: 'pip',
417-
_emval_get_method_caller__deps: ['$emval_addMethodCaller', '$emval_lookupTypes', '$new_', '$makeLegalFunctionName', '$emval_registeredMethods'],
417+
_emval_get_method_caller__deps: [
418+
'$emval_addMethodCaller', '$emval_lookupTypes',,
419+
'$makeLegalFunctionName', '$emval_registeredMethods',
420+
#if DYNAMIC_EXECUTION
421+
'$newFunc',
422+
#endif
423+
],
418424
_emval_get_method_caller: function(argCount, argTypes) {
419425
var types = emval_lookupTypes(argCount, argTypes);
420426
var retType = types[0];
@@ -479,7 +485,7 @@ var LibraryEmVal = {
479485
"};\n";
480486

481487
params.push(functionBody);
482-
var invokerFunction = new_(Function, params).apply(null, args);
488+
var invokerFunction = newFunc(Function, params).apply(null, args);
483489
#endif
484490
returnId = emval_addMethodCaller(invokerFunction);
485491
emval_registeredMethods[signatureName] = returnId;

0 commit comments

Comments
 (0)