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] Minor refactor to use more modern JS features #21331

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 19 additions & 17 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var LibraryEmbind = {
proto[methodName] = function(...args) {
// TODO This check can be removed in -O3 level "unsafe" optimizations.
if (!proto[methodName].overloadTable.hasOwnProperty(args.length)) {
throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${args.length}) - expects one of (${proto[methodName].overloadTable})!`);
throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${args.length}) - expects one of (${proto[methodName].overloadTable})!`);
}
return proto[methodName].overloadTable[args.length].apply(this, args);
};
Expand Down Expand Up @@ -953,7 +953,7 @@ var LibraryEmbind = {
throwUnboundTypeError(`Cannot call ${name} due to unbound types`, argTypes);
}, argCount - 1);

whenDependentTypesAreResolved([], argTypes, function(argTypes) {
whenDependentTypesAreResolved([], argTypes, (argTypes) => {
var invokerArgsArray = [argTypes[0] /* return value */, null /* no class 'this'*/].concat(argTypes.slice(1) /* actual params */);
replacePublicSymbol(name, craftInvokerFunction(name, invokerArgsArray, null /* no class 'this'*/, rawInvoker, fn, isAsync), argCount - 1);
return [];
Expand Down Expand Up @@ -1015,7 +1015,7 @@ var LibraryEmbind = {
var rawConstructor = reg.rawConstructor;
var rawDestructor = reg.rawDestructor;

whenDependentTypesAreResolved([rawTupleType], elementTypes, function(elementTypes) {
whenDependentTypesAreResolved([rawTupleType], elementTypes, (elementTypes) => {
elements.forEach((elt, i) => {
var getterReturnType = elementTypes[i];
var getter = elt.getter;
Expand Down Expand Up @@ -1753,7 +1753,7 @@ var LibraryEmbind = {
whenDependentTypesAreResolved(
[rawType, rawPointerType, rawConstPointerType],
baseClassRawType ? [baseClassRawType] : [],
function(base) {
(base) => {
base = base[0];

var baseClass;
Expand Down Expand Up @@ -1851,7 +1851,7 @@ var LibraryEmbind = {
var args = [rawConstructor];
var destructors = [];

whenDependentTypesAreResolved([], [rawClassType], function(classType) {
whenDependentTypesAreResolved([], [rawClassType], (classType) => {
classType = classType[0];
var humanName = `constructor ${classType.name}`;

Expand Down Expand Up @@ -1938,7 +1938,7 @@ var LibraryEmbind = {
methodName = getFunctionName(methodName);
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);

whenDependentTypesAreResolved([], [rawClassType], function(classType) {
whenDependentTypesAreResolved([], [rawClassType], (classType) => {
classType = classType[0];
var humanName = `${classType.name}.${methodName}`;

Expand Down Expand Up @@ -1969,11 +1969,13 @@ var LibraryEmbind = {
proto[methodName].overloadTable[argCount - 2] = unboundTypesHandler;
}

whenDependentTypesAreResolved([], rawArgTypes, function(argTypes) {
whenDependentTypesAreResolved([], rawArgTypes, (argTypes) => {
var memberFunction = craftInvokerFunction(humanName, argTypes, classType, rawInvoker, context, isAsync);

// Replace the initial unbound-handler-stub function with the appropriate member function, now that all types
// are resolved. If multiple overloads are registered for this function, the function goes into an overload table.
// Replace the initial unbound-handler-stub function with the
// appropriate member function, now that all types are resolved. If
// multiple overloads are registered for this function, the function
// goes into an overload table.
if (undefined === proto[methodName].overloadTable) {
// Set argCount in case an overload is registered later
memberFunction.argCount = argCount - 2;
Expand Down Expand Up @@ -2005,7 +2007,7 @@ var LibraryEmbind = {
fieldName = readLatin1String(fieldName);
getter = embind__requireFunction(getterSignature, getter);

whenDependentTypesAreResolved([], [classType], function(classType) {
whenDependentTypesAreResolved([], [classType], (classType) => {
classType = classType[0];
var humanName = `${classType.name}.${fieldName}`;
var desc = {
Expand All @@ -2026,7 +2028,7 @@ var LibraryEmbind = {
whenDependentTypesAreResolved(
[],
(setter ? [getterReturnType, setterArgumentType] : [getterReturnType]),
function(types) {
(types) => {
var getterReturnType = types[0];
var desc = {
get() {
Expand Down Expand Up @@ -2071,7 +2073,7 @@ var LibraryEmbind = {
methodName = readLatin1String(methodName);
methodName = getFunctionName(methodName);
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
whenDependentTypesAreResolved([], [rawClassType], (classType) => {
classType = classType[0];
var humanName = `${classType.name}.${methodName}`;

Expand All @@ -2095,7 +2097,7 @@ var LibraryEmbind = {
proto[methodName].overloadTable[argCount-1] = unboundTypesHandler;
}

whenDependentTypesAreResolved([], rawArgTypes, function(argTypes) {
whenDependentTypesAreResolved([], rawArgTypes, (argTypes) => {
// Replace the initial unbound-types-handler stub with the proper
// function. If multiple overloads are registered, the function handlers
// go into an overload table.
Expand Down Expand Up @@ -2138,7 +2140,7 @@ var LibraryEmbind = {
fieldName = readLatin1String(fieldName);
getter = embind__requireFunction(getterSignature, getter);

whenDependentTypesAreResolved([], [rawClassType], function(classType) {
whenDependentTypesAreResolved([], [rawClassType], (classType) => {
classType = classType[0];
var humanName = `${classType.name}.${fieldName}`;
var desc = {
Expand All @@ -2160,7 +2162,7 @@ var LibraryEmbind = {

Object.defineProperty(classType.registeredClass.constructor, fieldName, desc);

whenDependentTypesAreResolved([], [rawFieldType], function(fieldType) {
whenDependentTypesAreResolved([], [rawFieldType], (fieldType) => {
fieldType = fieldType[0];
var desc = {
get() {
Expand Down Expand Up @@ -2282,7 +2284,7 @@ var LibraryEmbind = {
rawShare = embind__requireFunction(shareSignature, rawShare);
rawDestructor = embind__requireFunction(destructorSignature, rawDestructor);

whenDependentTypesAreResolved([rawType], [rawPointeeType], function(pointeeType) {
whenDependentTypesAreResolved([rawType], [rawPointeeType], (pointeeType) => {
pointeeType = pointeeType[0];

var registeredPointer = new RegisteredPointer(name,
Expand Down Expand Up @@ -2342,7 +2344,7 @@ var LibraryEmbind = {
_embind_register_constant__deps: ['$readLatin1String', '$whenDependentTypesAreResolved'],
_embind_register_constant: (name, type, value) => {
name = readLatin1String(name);
whenDependentTypesAreResolved([], [type], function(type) {
whenDependentTypesAreResolved([], [type], (type) => {
type = type[0];
Module[name] = type['fromWireType'](value);
return [];
Expand Down
25 changes: 14 additions & 11 deletions src/embind/embind_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ var LibraryEmbindShared = {
$heap32VectorToArray: (count, firstElement) => {
var array = [];
for (var i = 0; i < count; i++) {
// TODO(https://github.com/emscripten-core/emscripten/issues/17310):
// Find a way to hoist the `>> 2` or `>> 3` out of this loop.
array.push({{{ makeGetValue('firstElement', `i * ${POINTER_SIZE}`, '*') }}});
// TODO(https://github.com/emscripten-core/emscripten/issues/17310):
// Find a way to hoist the `>> 2` or `>> 3` out of this loop.
array.push({{{ makeGetValue('firstElement', `i * ${POINTER_SIZE}`, '*') }}});
}
return array;
},
Expand All @@ -165,14 +165,16 @@ var LibraryEmbindShared = {
$requireRegisteredType: (rawType, humanName) => {
var impl = registeredTypes[rawType];
if (undefined === impl) {
throwBindingError(humanName + " has unknown type " + getTypeName(rawType));
throwBindingError(`${humanName} has unknown type ${getTypeName(rawType)}`);
}
return impl;
},

$usesDestructorStack(argTypes) {
for (var i = 1; i < argTypes.length; ++i) { // Skip return value at index 0 - it's not deleted here.
if (argTypes[i] !== null && argTypes[i].destructorFunction === undefined) { // The type does not define a destructor function - must use dynamic stack
// Skip return value at index 0 - it's not deleted here.
for (var i = 1; i < argTypes.length; ++i) {
// The type does not define a destructor function - must use dynamic stack
if (argTypes[i] !== null && argTypes[i].destructorFunction === undefined) {
return true;
}
}
Expand Down Expand Up @@ -252,11 +254,12 @@ var LibraryEmbindShared = {
invokerFnBody +=
(returns || isAsync ? "var rv = ":"") + "invoker(fn"+(argsListWired.length>0?", ":"")+argsListWired+");\n";

var returnVal = returns ? "rv" : "";
#if ASYNCIFY == 1
args1.push("Asyncify");
#endif
#if ASYNCIFY
invokerFnBody += "function onDone(" + (returns ? "rv" : "") + ") {\n";
invokerFnBody += `function onDone(${returnVal}) {\n`;
#endif

if (needsDestructorStack) {
Expand All @@ -265,8 +268,8 @@ var LibraryEmbindShared = {
for (var i = isClassMethodFunc?1:2; i < argTypes.length; ++i) { // Skip return value at index 0 - it's not deleted here. Also skip class type if not a method.
var paramName = (i === 1 ? "thisWired" : ("arg"+(i - 2)+"Wired"));
if (argTypes[i].destructorFunction !== null) {
invokerFnBody += paramName+"_dtor("+paramName+");\n";
args1.push(paramName+"_dtor");
invokerFnBody += `${paramName}_dtor(${paramName});\n`;
args1.push(`${paramName}_dtor`);
}
}
}
Expand All @@ -285,10 +288,10 @@ var LibraryEmbindShared = {

#if ASYNCIFY == 1
invokerFnBody += "}\n";
invokerFnBody += "return Asyncify.currData ? Asyncify.whenDone().then(onDone) : onDone(" + (returns ? "rv" : "") +");\n"
invokerFnBody += `return Asyncify.currData ? Asyncify.whenDone().then(onDone) : onDone(${returnVal});\n`
#elif ASYNCIFY == 2
invokerFnBody += "}\n";
invokerFnBody += "return " + (isAsync ? "rv.then(onDone)" : "onDone(" + (returns ? "rv" : "") + ")") + ";";
invokerFnBody += "return " + (isAsync ? "rv.then(onDone)" : `onDone(${returnVal})`) + ";";
#endif

invokerFnBody += "}\n";
Expand Down
4 changes: 1 addition & 3 deletions src/library_ccall.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ addToLibrary({
return getCFunc(ident);
}
#endif
return function() {
return ccall(ident, returnType, argTypes, arguments, opts);
}
return (...args) => ccall(ident, returnType, argTypes, args, opts);
},
});
Loading