diff --git a/src/embind/embind.js b/src/embind/embind.js index c8f5f39c8c69f..850b99577f22e 100644 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -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'], @@ -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'], diff --git a/src/embind/embind_ts.js b/src/embind/embind_ts.js index 3cc20c33163df..cb5da1ce0dc3e 100644 --- a/src/embind/embind_ts.js +++ b/src/embind/embind_ts.js @@ -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) => { diff --git a/src/library_sigs.js b/src/library_sigs.js index 8a543bec40021..b4495488a0369 100644 --- a/src/library_sigs.js +++ b/src/library_sigs.js @@ -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', diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index b8efc2f1b215d..535619a549b17 100644 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -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, diff --git a/system/lib/embind/bind.cpp b/system/lib/embind/bind.cpp index 3ea5f34812b04..e7f17c64631b7 100644 --- a/system/lib/embind/bind.cpp +++ b/system/lib/embind/bind.cpp @@ -154,7 +154,7 @@ EMSCRIPTEN_BINDINGS(builtin) { _embind_register_std_wstring(TypeID::get(), sizeof(wchar_t), "std::wstring"); _embind_register_std_wstring(TypeID::get(), sizeof(char16_t), "std::u16string"); _embind_register_std_wstring(TypeID::get(), sizeof(char32_t), "std::u32string"); - _embind_register_emval(TypeID::get(), "emscripten::val"); + _embind_register_emval(TypeID::get()); // Some of these types are aliases for each other. Luckily, // embind.js's _embind_register_memory_view ignores duplicate