From bfcc6330b29040d837d4b7de123c106f3f661b15 Mon Sep 17 00:00:00 2001 From: Chad Austin Date: Wed, 20 Mar 2013 17:51:15 -0700 Subject: [PATCH] Simplify getTypeName --- src/embind/embind.js | 22 ++++++++++++-------- system/lib/embind/bind.cpp | 42 ++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/embind/embind.js b/src/embind/embind.js index d3ab0bf55..99d7005b2 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -3,10 +3,10 @@ /*global FUNCTION_TABLE, HEAP32, HEAPU8*/ /*global Pointer_stringify*/ /*global __emval_register, _emval_handle_array, __emval_decref*/ -/*global ___typeName:false*/ var InternalError = Module.InternalError = extendError(Error, 'InternalError'); var BindingError = Module.BindingError = extendError(Error, 'BindingError'); +var UnboundTypeError = Module.UnboundTypeError = extendError(BindingError, 'UnboundTypeError'); function throwInternalError(value) { throw new InternalError(value); @@ -124,11 +124,8 @@ function whenDependentTypesAreResolved(dependentTypes, onComplete) { } } -function typeName(rawType) { - var bt = ___typeName(rawType); - var rv = Pointer_stringify(bt); - _free(bt); - return rv; +function getTypeName(type) { + return Module._embind_getTypeName(type); } function heap32VectorToArray(count, firstElement) { @@ -142,7 +139,7 @@ function heap32VectorToArray(count, firstElement) { function requireRegisteredType(rawType, humanName) { var impl = registeredTypes[rawType]; if (undefined === impl) { - throwBindingError(humanName + " has unknown type " + typeName(rawType)); + throwBindingError(humanName + " has unknown type " + getTypeName(rawType)); } return impl; } @@ -279,8 +276,17 @@ function __embind_register_function(name, argCount, rawArgTypesAddr, rawInvoker, var argTypes = heap32VectorToArray(argCount, rawArgTypesAddr); name = Pointer_stringify(name); rawInvoker = FUNCTION_TABLE[rawInvoker]; + + var invoker = function() { + throw new UnboundTypeError('Cannot call ' + name + ' due to unbound types: UnboundFoo'); + } + + exposePublicSymbol(name, function() { + return invoker.apply(this, arguments); + }); + whenDependentTypesAreResolved(argTypes, function(argTypes) { - exposePublicSymbol(name, makeInvoker(name, argCount, argTypes, rawInvoker, fn)); + invoker = makeInvoker(name, argCount, argTypes, rawInvoker, fn); }); } diff --git a/system/lib/embind/bind.cpp b/system/lib/embind/bind.cpp index 6994ef0cb..04a30dfcc 100755 --- a/system/lib/embind/bind.cpp +++ b/system/lib/embind/bind.cpp @@ -1,5 +1,4 @@ #include -#include <../lib/libcxxabi/src/private_typeinfo.h> #include <../lib/libcxxabi/include/cxxabi.h> #include #include @@ -9,29 +8,26 @@ using namespace emscripten; +static std::string _embind_getTypeName(intptr_t ti_raw) { + auto ti = reinterpret_cast(ti_raw); + int stat; + char* demangled = abi::__cxa_demangle(ti->name(), NULL, NULL, &stat); + switch (stat) { + case -1: + return ""; + case -2: + return ""; + case -3: + return ""; + } + + std::string rv(demangled); + free(demangled); + return rv; +} + namespace emscripten { namespace internal { - extern "C" { - const char* EMSCRIPTEN_KEEPALIVE __typeName(const std::type_info* ti) { - size_t nameLen = std::min(strlen(ti->name()), 1024U); - char* name = (char *)malloc(nameLen+1); - int stat; - - __cxxabiv1::__cxa_demangle(ti->name(), name, &nameLen, &stat); - - if (stat != 0) { - strncpy(name, ti->name(), nameLen); - name[nameLen] = '\0'; - } - return name; - } - - size_t EMSCRIPTEN_KEEPALIVE __peek32(size_t p) { - return *reinterpret_cast(p); - } - - } - JSInterface* create_js_interface(EM_VAL e) { return new JSInterface(e); } @@ -60,4 +56,6 @@ EMSCRIPTEN_BINDINGS(native_and_builtin_types) { _embind_register_cstring(TypeID::get(), "std::string"); _embind_register_emval(TypeID::get(), "emscripten::val"); + + function("_embind_getTypeName", &_embind_getTypeName); }