Skip to content

Commit

Permalink
embind: Use single type object for emval.
Browse files Browse the repository at this point in the history
If register_type is used, emval will be registered multiple times for
different type id's, but only a single type object is needed on the JS side
for all of them.
  • Loading branch information
brendandahl committed Nov 30, 2023
1 parent 7e7beb8 commit f20db67
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
44 changes: 22 additions & 22 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,25 @@ var LibraryEmbind = {
$PureVirtualError__deps: ['$extendError'],
$PureVirtualError: undefined,
$GenericWireTypeSize: {{{ 2 * POINTER_SIZE }}},

// If register_type is used, emval will be registered multiple times for
// different type id's, but only a single type object is needed on the JS side
// for all of them. Store the type for reuse.
$EmValType__deps: ['_emval_decref', '$Emval', '$simpleReadValueFromPointer', '$GenericWireTypeSize'],
$EmValType: `{
name: 'emscripten::val',
'fromWireType': (handle) => {
var rv = Emval.toValue(handle);
__emval_decref(handle);
return rv;
},
'toWireType': (destructors, value) => Emval.toHandle(value),
'argPackAdvance': GenericWireTypeSize,
'readValueFromPointer': simpleReadValueFromPointer,
destructorFunction: null, // This type does not need a destructor
// TODO: do we need a deleteObject here? write a test where
// emval is passed into JS via an interface
}`,
$init_embind__deps: [
'$getInheritedInstanceCount', '$getLiveInheritedInstances',
'$flushPendingDeletes', '$setDelayFunction'],
Expand Down Expand Up @@ -660,30 +678,12 @@ var LibraryEmbind = {
},

_embind_register_emval__deps: [
'_emval_decref', '$Emval',
'$readLatin1String', '$registerType', '$simpleReadValueFromPointer'],
_embind_register_emval: (rawType, name) => {
name = readLatin1String(name);
registerType(rawType, {
name,
'fromWireType': (handle) => {
var rv = Emval.toValue(handle);
__emval_decref(handle);
return rv;
},
'toWireType': (destructors, value) => Emval.toHandle(value),
'argPackAdvance': GenericWireTypeSize,
'readValueFromPointer': simpleReadValueFromPointer,
destructorFunction: null, // This type does not need a destructor

// TODO: do we need a deleteObject here? write a test where
// emval is passed into JS via an interface
});
},
'$registerType', '$EmValType'],
_embind_register_emval: (rawType) => registerType(rawType, EmValType),

_embind_register_user_type__deps: ['_embind_register_emval'],
_embind_register_user_type: (rawType, name) => {
__embind_register_emval(rawType, name);
__embind_register_emval(rawType);
},

_embind_register_memory_view__deps: ['$readLatin1String', '$registerType'],
Expand Down
5 changes: 3 additions & 2 deletions src/embind/embind_ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ var LibraryEmbind = {
_embind_register_std_wstring: (rawType, charSize, name) => {
registerPrimitiveType(rawType, name);
},
_embind_register_emval: (rawType, name) => {
registerPrimitiveType(rawType, name);
_embind_register_emval__deps: ['$registerType', '$PrimitiveType'],
_embind_register_emval: (rawType) => {
registerType(rawType, new PrimitiveType(rawType, 'emscripten::val'));
},
_embind_register_user_type__deps: ['$registerType', '$readLatin1String', '$UserType'],
_embind_register_user_type: (rawType, name) => {
Expand Down
2 changes: 1 addition & 1 deletion src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ sigs = {
_embind_register_class_function__sig: 'vppippppii',
_embind_register_class_property__sig: 'vpppppppppp',
_embind_register_constant__sig: 'vppd',
_embind_register_emval__sig: 'vpp',
_embind_register_emval__sig: 'vp',
_embind_register_enum__sig: 'vpppi',
_embind_register_enum_value__sig: 'vppp',
_embind_register_float__sig: 'vppp',
Expand Down
3 changes: 1 addition & 2 deletions system/include/emscripten/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ void _embind_register_std_wstring(
const char* name);

void _embind_register_emval(
TYPEID emvalType,
const char* name);
TYPEID emvalType);

void _embind_register_memory_view(
TYPEID memoryViewType,
Expand Down
2 changes: 1 addition & 1 deletion system/lib/embind/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ EMSCRIPTEN_BINDINGS(builtin) {
_embind_register_std_wstring(TypeID<std::wstring>::get(), sizeof(wchar_t), "std::wstring");
_embind_register_std_wstring(TypeID<std::u16string>::get(), sizeof(char16_t), "std::u16string");
_embind_register_std_wstring(TypeID<std::u32string>::get(), sizeof(char32_t), "std::u32string");
_embind_register_emval(TypeID<val>::get(), "emscripten::val");
_embind_register_emval(TypeID<val>::get());

// Some of these types are aliases for each other. Luckily,
// embind.js's _embind_register_memory_view ignores duplicate
Expand Down

0 comments on commit f20db67

Please sign in to comment.