diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-04-14 14:06:52 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-04-14 14:06:52 -0700 |
commit | 512b7bd6689699a16a5afb6d38c786d17ec45c91 (patch) | |
tree | b1fb3c871b626d16b92ef3818fc05d3caf23430c | |
parent | 411e6287c76d0cf7ff869fa6fc97237ad8be1b1d (diff) | |
parent | 1838f123d3fcd45453b6ca373dce94cc40621d77 (diff) |
Merge pull request #2286 from chadaustin/embind-code-size-reduction
Embind code size reduction by using lightweight RTTI record for non-polymorphic types
-rw-r--r-- | system/include/emscripten/bind.h | 22 | ||||
-rw-r--r-- | system/include/emscripten/wire.h | 64 | ||||
-rw-r--r-- | system/lib/embind/bind.cpp | 38 | ||||
-rw-r--r-- | tests/embind/embind.test.js | 6 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 54 |
5 files changed, 120 insertions, 64 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index 872f279b..0699715c 100644 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -813,7 +813,7 @@ namespace emscripten { // NOTE: this returns the class type, not the pointer type template<typename T> inline TYPEID getActualType(T* ptr) { - return reinterpret_cast<TYPEID>(&typeid(*ptr)); + return getLightTypeID(*ptr); }; } @@ -851,15 +851,15 @@ namespace emscripten { template<typename T> struct SmartPtrIfNeeded { template<typename U> - SmartPtrIfNeeded(U& cls) { - cls.template smart_ptr<T>(); + SmartPtrIfNeeded(U& cls, const char* smartPtrName) { + cls.template smart_ptr<T>(smartPtrName); } }; template<typename T> struct SmartPtrIfNeeded<T*> { template<typename U> - SmartPtrIfNeeded(U&) { + SmartPtrIfNeeded(U&, const char*) { } }; }; @@ -890,7 +890,7 @@ namespace emscripten { } template<typename PointerType> - const class_& smart_ptr() const { + const class_& smart_ptr(const char* name) const { using namespace internal; typedef smart_ptr_trait<PointerType> PointerTrait; @@ -901,7 +901,7 @@ namespace emscripten { _embind_register_smart_ptr( TypeID<PointerType>::get(), TypeID<PointeeType>::get(), - typeid(PointerType).name(), + name, PointerTrait::get_sharing_policy(), reinterpret_cast<GenericFunction>(&PointerTrait::get), reinterpret_cast<GenericFunction>(&PointerTrait::construct_null), @@ -933,10 +933,10 @@ namespace emscripten { } template<typename SmartPtr, typename... Args, typename... Policies> - const class_& smart_ptr_constructor(SmartPtr (*factory)(Args...), Policies...) const { + const class_& smart_ptr_constructor(const char* smartPtrName, SmartPtr (*factory)(Args...), Policies...) const { using namespace internal; - smart_ptr<SmartPtr>(); + smart_ptr<SmartPtr>(smartPtrName); typename WithPolicies<Policies...>::template ArgTypeList<SmartPtr, Args...> args; _embind_register_class_constructor( @@ -949,12 +949,12 @@ namespace emscripten { } template<typename WrapperType, typename PointerType = WrapperType*> - const class_& allow_subclass() const { + const class_& allow_subclass(const char* wrapperClassName, const char* pointerName = "<UnknownPointerName>") const { using namespace internal; - auto cls = class_<WrapperType, base<ClassType>>(typeid(WrapperType).name()) + auto cls = class_<WrapperType, base<ClassType>>(wrapperClassName) ; - SmartPtrIfNeeded<PointerType> _(cls); + SmartPtrIfNeeded<PointerType> _(cls, pointerName); return class_function( "implement", diff --git a/system/include/emscripten/wire.h b/system/include/emscripten/wire.h index 05b3ac33..c20e2f55 100644 --- a/system/include/emscripten/wire.h +++ b/system/include/emscripten/wire.h @@ -15,22 +15,66 @@ #define EMSCRIPTEN_ALWAYS_INLINE __attribute__((always_inline)) namespace emscripten { + #ifndef EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES + #define EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES 1 + #endif + + + #if EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES + constexpr bool has_unbound_type_names = true; + #else + constexpr bool has_unbound_type_names = false; + #endif + namespace internal { typedef void (*GenericFunction)(); - typedef const struct _TYPEID* TYPEID; + typedef const struct _TYPEID {}* TYPEID; + + + // We don't need the full std::type_info implementation. We + // just need a unique identifier per type and polymorphic type + // identification. + + template<typename T> + struct CanonicalizedID { + static TYPEID get() { + static _TYPEID c; + return &c; + } + }; + + template<typename T> + struct Canonicalized { + typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type; + }; + + template<typename T> + struct LightTypeID { + static TYPEID get() { + typedef typename Canonicalized<T>::type C; + if (has_unbound_type_names || std::is_polymorphic<C>::value) { + return reinterpret_cast<TYPEID>(&typeid(C)); + } else { + return CanonicalizedID<C>::get(); + } + } + }; + + template<typename T> + const TYPEID getLightTypeID(const T& value) { + typedef typename Canonicalized<T>::type C; + if (has_unbound_type_names || std::is_polymorphic<C>::value) { + return reinterpret_cast<TYPEID>(&typeid(value)); + } else { + return LightTypeID<T>::get(); + } + } - // This implementation is technically not legal, as it's not - // required that two calls to typeid produce the same exact - // std::type_info instance. That said, it's likely to work - // given Emscripten compiles everything into one binary. - // Should it not work in the future: replace TypeID with an - // int, and store all TypeInfo we see in a map, allocating new - // TypeIDs as we add new items to the map. template<typename T> struct TypeID { static TYPEID get() { - return reinterpret_cast<TYPEID>(&typeid(T)); + return LightTypeID<T>::get(); } }; @@ -53,7 +97,7 @@ namespace emscripten { template<typename T> struct TypeID<AllowedRawPointer<T>> { static TYPEID get() { - return reinterpret_cast<TYPEID>(&typeid(T*)); + return LightTypeID<T*>::get(); } }; diff --git a/system/lib/embind/bind.cpp b/system/lib/embind/bind.cpp index f43d1ea1..37918050 100644 --- a/system/lib/embind/bind.cpp +++ b/system/lib/embind/bind.cpp @@ -14,26 +14,32 @@ using namespace emscripten; extern "C" {
const char* __attribute__((used)) __getTypeName(const std::type_info* ti) {
+ if (has_unbound_type_names) {
#ifdef USE_CXA_DEMANGLE
- int stat;
- char* demangled = abi::__cxa_demangle(ti->name(), NULL, NULL, &stat);
- if (stat == 0 && demangled) {
- return demangled;
- }
+ int stat;
+ char* demangled = abi::__cxa_demangle(ti->name(), NULL, NULL, &stat);
+ if (stat == 0 && demangled) {
+ return demangled;
+ }
- switch (stat) {
- case -1:
- return strdup("<allocation failure>");
- case -2:
- return strdup("<invalid C++ symbol>");
- case -3:
- return strdup("<invalid argument>");
- default:
- return strdup("<unknown error>");
- }
+ switch (stat) {
+ case -1:
+ return strdup("<allocation failure>");
+ case -2:
+ return strdup("<invalid C++ symbol>");
+ case -3:
+ return strdup("<invalid argument>");
+ default:
+ return strdup("<unknown error>");
+ }
#else
- return strdup(ti->name());
+ return strdup(ti->name());
#endif
+ } else {
+ char str[80];
+ sprintf(str, "%p", ti);
+ return strdup(str);
+ }
}
}
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index e2160c33..6bba4de0 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -1408,7 +1408,7 @@ module({ var e = assert.throws(cm.BindingError, function() { cm.passThroughCustomSmartPtr(o); }); - assert.equal('Cannot convert argument of type NSt3__110shared_ptrI20HeldByCustomSmartPtrEE to parameter type 14CustomSmartPtrI20HeldByCustomSmartPtrE', e.message); + assert.equal('Cannot convert argument of type shared_ptr<HeldByCustomSmartPtr> to parameter type CustomSmartPtr<HeldByCustomSmartPtr>', e.message); o.delete(); }); @@ -1646,6 +1646,10 @@ module({ if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well! BaseFixture.extend("unbound types", function() { + if (!cm.hasUnboundTypeNames) { + return; + } + function assertMessage(fn, message) { var e = assert.throws(cm.UnboundTypeError, fn); assert.equal(message, e.message); diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index d299660a..1b835751 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -1150,8 +1150,8 @@ void callDifferentArguments(AbstractClass& ac, int i, double d, unsigned char f, EMSCRIPTEN_BINDINGS(interface_tests) { class_<AbstractClass>("AbstractClass") - .smart_ptr<std::shared_ptr<AbstractClass>>() - .allow_subclass<AbstractClassWrapper>() + .smart_ptr<std::shared_ptr<AbstractClass>>("shared_ptr<AbstractClass>") + .allow_subclass<AbstractClassWrapper>("AbstractClassWrapper") .function("abstractMethod", &AbstractClass::abstractMethod) .function("optionalMethod", &AbstractClass::optionalMethod) ; @@ -1483,7 +1483,7 @@ EMSCRIPTEN_BINDINGS(tests) { function("emval_test_take_and_return_TupleInStruct", &emval_test_take_and_return_TupleInStruct); class_<ValHolder>("ValHolder") - .smart_ptr<std::shared_ptr<ValHolder>>() + .smart_ptr<std::shared_ptr<ValHolder>>("std::shared_ptr<ValHolder>") .constructor<val>() .function("getVal", &ValHolder::getVal) .function("getValNonConst", &ValHolder::getValNonConst) @@ -1522,7 +1522,7 @@ EMSCRIPTEN_BINDINGS(tests) { function("emval_test_take_and_call_functor", &emval_test_take_and_call_functor); class_<StringHolder>("StringHolder") - .smart_ptr<std::shared_ptr<StringHolder>>() + .smart_ptr<std::shared_ptr<StringHolder>>("shared_ptr<StringHolder>") .constructor<std::string>() .function("set", &StringHolder::set) .function("get", &StringHolder::get) @@ -1566,7 +1566,7 @@ EMSCRIPTEN_BINDINGS(tests) { // register Derived before Base as a test that it's possible to // register base classes afterwards class_<Derived, base<Base>>("Derived") - .smart_ptr<std::shared_ptr<Derived>>() + .smart_ptr<std::shared_ptr<Derived>>("shared_ptr<Derived>") .constructor<>() .function("getClassName", &Derived::getClassName) .function("getMember", &Derived::getMember) @@ -1575,7 +1575,7 @@ EMSCRIPTEN_BINDINGS(tests) { ; class_<Base>("Base") - .smart_ptr<std::shared_ptr<Base>>() + .smart_ptr<std::shared_ptr<Base>>("shared_ptr<Base") .constructor<>() .function("getClassName", &Base::getClassName) .function("getClassNameFromBase", &Base::getClassNameFromBase) @@ -1589,7 +1589,7 @@ EMSCRIPTEN_BINDINGS(tests) { ; class_<SecondBase>("SecondBase") - .smart_ptr<std::shared_ptr<SecondBase>>() + .smart_ptr<std::shared_ptr<SecondBase>>("shared_ptr<SecondBase>") .constructor<>() .function("getClassName", &SecondBase::getClassName) .function("getClassNameFromSecondBase", &SecondBase::getClassNameFromSecondBase) @@ -1612,13 +1612,13 @@ EMSCRIPTEN_BINDINGS(tests) { ; class_<SiblingDerived>("SiblingDerived") - .smart_ptr<std::shared_ptr<SiblingDerived>>() + .smart_ptr<std::shared_ptr<SiblingDerived>>("shared_ptr<SiblingDerived>") .constructor<>() .function("getClassName", &SiblingDerived::getClassName) ; class_<MultiplyDerived, base<Base>>("MultiplyDerived") - .smart_ptr<std::shared_ptr<MultiplyDerived>>() + .smart_ptr<std::shared_ptr<MultiplyDerived>>("shared_ptr<MultiplyDerived>") .constructor<>() .function("getClassName", &MultiplyDerived::getClassName) .class_function("getInstanceCount", &MultiplyDerived::getInstanceCount) @@ -1630,26 +1630,26 @@ EMSCRIPTEN_BINDINGS(tests) { ; class_<DerivedThrice, base<Derived> >("DerivedThrice") - .smart_ptr<std::shared_ptr<DerivedThrice>>() + .smart_ptr<std::shared_ptr<DerivedThrice>>("shared_ptr<DerivedThrice>") .constructor<>() .function("getClassName", &DerivedThrice::getClassName) ; class_<PolyBase>("PolyBase") - .smart_ptr<std::shared_ptr<PolyBase>>() + .smart_ptr<std::shared_ptr<PolyBase>>("shared_ptr<PolyBase>") .constructor<>() .function("virtualGetClassName", &PolyBase::virtualGetClassName) .function("getClassName", &PolyBase::getClassName) ; class_<PolySecondBase>("PolySecondBase") - .smart_ptr<std::shared_ptr<PolySecondBase>>() + .smart_ptr<std::shared_ptr<PolySecondBase>>("shared_ptr<PolySecondBase>") .constructor<>() .function("getClassName", &PolySecondBase::getClassName) ; class_<PolyDerived, base<PolyBase>>("PolyDerived") - .smart_ptr<std::shared_ptr<PolyDerived>>() + .smart_ptr<std::shared_ptr<PolyDerived>>("shared_ptr<PolyDerived>") .constructor<>() .function("virtualGetClassName", &PolyDerived::virtualGetClassName) .function("getClassName", &PolyDerived::getClassName) @@ -1671,43 +1671,43 @@ EMSCRIPTEN_BINDINGS(tests) { // } class_<PolySiblingDerived, base<PolyBase>>("PolySiblingDerived") - .smart_ptr<std::shared_ptr<PolySiblingDerived>>() + .smart_ptr<std::shared_ptr<PolySiblingDerived>>("shared_ptr<PolySiblingDerived>") .constructor<>() .function("getClassName", &PolySiblingDerived::getClassName) ; class_<PolyMultiplyDerived, base<PolyBase>>("PolyMultiplyDerived") - .smart_ptr<std::shared_ptr<PolyMultiplyDerived>>() + .smart_ptr<std::shared_ptr<PolyMultiplyDerived>>("shared_ptr<PolyMultiplyDerived>") .constructor<>() .function("getClassName", &PolyMultiplyDerived::getClassName) ; class_<PolyDerivedThrice, base<PolyDerived>>("PolyDerivedThrice") - .smart_ptr<std::shared_ptr<PolyDerivedThrice>>() + .smart_ptr<std::shared_ptr<PolyDerivedThrice>>("shared_ptr<PolyDerivedThrice>") .constructor<>() .function("getClassName", &PolyDerivedThrice::getClassName) ; class_<PolyDiamondBase>("PolyDiamondBase") - .smart_ptr<std::shared_ptr<PolyDiamondBase>>() + .smart_ptr<std::shared_ptr<PolyDiamondBase>>("shared_ptr<PolyDiamondBase>") .constructor<>() .function("getClassName", &PolyDiamondBase::getClassName) ; class_<PolyDiamondDerived>("PolyDiamondDerived") - .smart_ptr<std::shared_ptr<PolyDiamondDerived>>() + .smart_ptr<std::shared_ptr<PolyDiamondDerived>>("shared_ptr<PolyDiamondDerived>") .constructor<>() .function("getClassName", &PolyDiamondDerived::getClassName) ; class_<PolyDiamondSiblingDerived>("PolyDiamondSiblingDerived") - .smart_ptr<std::shared_ptr<PolyDiamondSiblingDerived>>() + .smart_ptr<std::shared_ptr<PolyDiamondSiblingDerived>>("shared_ptr<PolyDiamondSiblingDerived>") .constructor<>() .function("getClassName", &PolyDiamondSiblingDerived::getClassName) ; class_<PolyDiamondMultiplyDerived>("PolyDiamondMultiplyDerived") - .smart_ptr<std::shared_ptr<PolyDiamondMultiplyDerived>>() + .smart_ptr<std::shared_ptr<PolyDiamondMultiplyDerived>>("shared_ptr<PolyDiamondMultiplyDerived>") .constructor<>() .function("getClassName", &PolyDiamondMultiplyDerived::getClassName) ; @@ -1825,8 +1825,8 @@ EMSCRIPTEN_BINDINGS(tests) { auto HeldBySmartPtr_class = class_<HeldBySmartPtr>("HeldBySmartPtr"); HeldBySmartPtr_class - .smart_ptr<CustomSmartPtr<HeldBySmartPtr>>() - .smart_ptr_constructor(&std::make_shared<HeldBySmartPtr, int, std::string>) + .smart_ptr<CustomSmartPtr<HeldBySmartPtr>>("CustomSmartPtr<HeldBySmartPtr>") + .smart_ptr_constructor("shared_ptr<HeldbySmartPtr>", &std::make_shared<HeldBySmartPtr, int, std::string>) .class_function("newCustomPtr", HeldBySmartPtr::newCustomPtr) .function("returnThis", &takesHeldBySmartPtrSharedPtr) .property("i", &HeldBySmartPtr::i) @@ -1836,8 +1836,8 @@ EMSCRIPTEN_BINDINGS(tests) { function("takesHeldBySmartPtrSharedPtr", &takesHeldBySmartPtrSharedPtr); class_<HeldByCustomSmartPtr>("HeldByCustomSmartPtr") - .smart_ptr<std::shared_ptr<HeldByCustomSmartPtr>>() - .smart_ptr_constructor(&HeldByCustomSmartPtr::create) + .smart_ptr<std::shared_ptr<HeldByCustomSmartPtr>>("shared_ptr<HeldByCustomSmartPtr>") + .smart_ptr_constructor("CustomSmartPtr<HeldByCustomSmartPtr>", &HeldByCustomSmartPtr::create) .class_function("createSharedPtr", &HeldByCustomSmartPtr::createSharedPtr) .property("i", &HeldByCustomSmartPtr::i) .property("s", &HeldByCustomSmartPtr::s) @@ -2022,7 +2022,7 @@ EMSCRIPTEN_BINDINGS(overloads) { ; class_<MultipleSmartCtors>("MultipleSmartCtors") - .smart_ptr<std::shared_ptr<MultipleSmartCtors>>() + .smart_ptr<std::shared_ptr<MultipleSmartCtors>>("shared_ptr<MultipleSmartCtors>") .constructor(&std::make_shared<MultipleSmartCtors, int>) .constructor(&std::make_shared<MultipleSmartCtors, int, int>) .function("WhichCtorCalled", &MultipleSmartCtors::WhichCtorCalled) @@ -2147,6 +2147,8 @@ struct BoundClass { }; EMSCRIPTEN_BINDINGS(incomplete) { + constant("hasUnboundTypeNames", emscripten::has_unbound_type_names); + function("getUnboundClass", &passThrough<UnboundClass>); class_<HasUnboundBase, base<UnboundClass>>("HasUnboundBase") @@ -2247,7 +2249,7 @@ std::shared_ptr<Base> return_Base_from_DerivedWithOffset(std::shared_ptr<Derived EMSCRIPTEN_BINDINGS(with_adjustment) { class_<DerivedWithOffset, base<Base>>("DerivedWithOffset") - .smart_ptr_constructor(&std::make_shared<DerivedWithOffset>) + .smart_ptr_constructor("shared_ptr<DerivedWithOffset>", &std::make_shared<DerivedWithOffset>) ; function("return_Base_from_DerivedWithOffset", &return_Base_from_DerivedWithOffset); |