aboutsummaryrefslogtreecommitdiff
path: root/system
diff options
context:
space:
mode:
authorBill Welden <bwelden@imvu.com>2013-01-08 07:45:50 -0800
committerJukka Jylänki <jujjyl@gmail.com>2013-04-12 14:22:56 +0300
commitcd535236f6bb28f7a3856fff148a49c261563db5 (patch)
tree48e3be86837fb28db884ba70295add3bc0ad1d5a /system
parentcd3be4e930df1358d9b07746f774a6ad00f3f39c (diff)
Auto upcast of pointer parameters to C++ routines.
Diffstat (limited to 'system')
-rwxr-xr-xsystem/include/emscripten/bind.h27
-rwxr-xr-xsystem/lib/embind/bind.cpp7
2 files changed, 28 insertions, 6 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h
index 592928f5..bbce9a93 100755
--- a/system/include/emscripten/bind.h
+++ b/system/include/emscripten/bind.h
@@ -98,6 +98,7 @@ namespace emscripten {
TYPEID pointeeType,
bool isPolymorphic,
const char* pointerName,
+ GenericFunction constructor,
GenericFunction destructor,
GenericFunction getPointee);
@@ -282,9 +283,18 @@ namespace emscripten {
return *static_cast<ToType*>(&from);
};
+ template<typename FromRawType, typename ToRawType, bool isPolymorphic>
+ struct performShared {
+ static std::shared_ptr<ToRawType> cast(std::shared_ptr<FromRawType> from) {
+ return std::dynamic_pointer_cast<ToRawType>(from);
+ };
+ };
+
template<typename FromRawType, typename ToRawType>
- std::shared_ptr<ToRawType> performSharedStaticCast(std::shared_ptr<FromRawType> from) {
- return std::shared_ptr<ToRawType>(from, static_cast<ToRawType*>(from.get()));
+ struct performShared<FromRawType, ToRawType, false> {
+ static std::shared_ptr<ToRawType> cast(std::shared_ptr<FromRawType> from) {
+ return std::shared_ptr<ToRawType>(from, static_cast<ToRawType*>(from.get()));
+ };
};
template<typename ReturnType, typename... Args, typename... Policies>
@@ -312,6 +322,14 @@ namespace emscripten {
);
}
+ template<typename PointerType>
+ void nullDeallocator(PointerType* p) {}
+
+ template<typename PointerType>
+ typename std::shared_ptr<PointerType> raw_smart_pointer_constructor(PointerType *ptr, void (PointerType*)) {
+ return std::shared_ptr<PointerType>(ptr, nullDeallocator<PointerType>);
+ }
+
template<typename ClassType>
void raw_destructor(ClassType* ptr) {
delete ptr;
@@ -604,6 +622,7 @@ namespace emscripten {
TypeID<PointeeType>::get(),
std::is_polymorphic<PointeeType>::value,
name,
+ reinterpret_cast<GenericFunction>(&raw_smart_pointer_constructor<PointeeType*>),
reinterpret_cast<GenericFunction>(&raw_destructor<PointerType>),
reinterpret_cast<GenericFunction>(&get_pointee<PointerType>));
@@ -620,8 +639,8 @@ namespace emscripten {
TypeID<ReturnPointeeType>::get(),
std::is_polymorphic<PointeeType>::value,
methodName,
- reinterpret_cast<GenericFunction>(&performSharedStaticCast<PointeeType,ReturnPointeeType>));
- return *this;
+ reinterpret_cast<GenericFunction>(&performShared<PointeeType, ReturnPointeeType, std::is_polymorphic<PointeeType>::value>::cast));
+ return *this;
}
};
diff --git a/system/lib/embind/bind.cpp b/system/lib/embind/bind.cpp
index 46924505..a4b67fa5 100755
--- a/system/lib/embind/bind.cpp
+++ b/system/lib/embind/bind.cpp
@@ -190,8 +190,6 @@ namespace emscripten {
// __dynamicPointerCast performs a C++ dynamic_cast<>() operation, but allowing run-time specification of
// the from and to pointer types.
int EMSCRIPTEN_KEEPALIVE __dynamicPointerCast(int p, int to) {
- // The final parameter is a place-holder for a hint, a feature which is not currently implemented
- // in the emscripten runtime. The compiler passes a dummy value of -1, and so do we.
int ret = (int)__staticPointerCast((void *)p, __getDynamicPointerType(p), to);
if (ret < 0) {
return 0;
@@ -214,11 +212,16 @@ namespace emscripten {
return name;
}
+ int EMSCRIPTEN_KEEPALIVE __peek32(int p) {
+ return *(int *)p;
+ }
+
EMSCRIPTEN_BINDINGS(([]() {
// We bind __getDerivationPath in order to take advantage of the std::vector to Javascript array
// conversion for the return value. This has the unfortunate side-effect of exposing it to third party
// developers, but perhaps the double underscore will scare them away from calling it.
function("__getDerivationPath", &__getDerivationPath);
+ function("__peek32", &__peek32);
}));
}