aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <caustin@gmail.com>2012-11-05 13:20:13 -0800
committerJukka Jylänki <jujjyl@gmail.com>2013-04-12 14:21:39 +0300
commit565823a14ad14a5c16f8c40f714963e7cb259e33 (patch)
tree7825d2f9cf2f049e1a7fb34c2ce1641ea3838da1
parent848bdefa4e082936f3b1e26cbd9d0239efb9cf62 (diff)
Returning an empty shared_ptr returns null to JavaScript. Similarly, passing null into a shared_ptr creates an empty shared_ptr.
-rw-r--r--src/embind/embind.js16
-rw-r--r--system/include/emscripten/wire.h18
2 files changed, 31 insertions, 3 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js
index bbbeb96c..98d4b13e 100644
--- a/src/embind/embind.js
+++ b/src/embind/embind.js
@@ -60,7 +60,9 @@ function __embind_register_bool(boolType, name, trueValue, falseValue) {
return o ? trueValue : falseValue;
},
fromWireType: function(wt) {
- return wt === trueValue;
+ // ambiguous emscripten ABI: sometimes return values are
+ // true or false, and sometimes integers (0 or 1)
+ return !!wt;
},
});
}
@@ -397,7 +399,7 @@ function __embind_register_smart_ptr(
};
Handle.prototype['delete'] = function() {
- if (!this.ptr && !this.smartPointer) {
+ if (!this.ptr) {
throw new BindingError(pointeeType.name + ' instance already deleted');
}
@@ -412,10 +414,18 @@ function __embind_register_smart_ptr(
registerType(pointerType, name, {
name: name,
fromWireType: function(ptr) {
+ if (!getPointee(ptr)) {
+ destructor(ptr);
+ return null;
+ }
return new Handle(ptr);
},
toWireType: function(destructors, o) {
- return o.smartPointer;
+ if (null === o) {
+ return 0;
+ } else {
+ return o.smartPointer;
+ }
}
});
}
diff --git a/system/include/emscripten/wire.h b/system/include/emscripten/wire.h
index 89e68c68..452628a0 100644
--- a/system/include/emscripten/wire.h
+++ b/system/include/emscripten/wire.h
@@ -210,6 +210,24 @@ namespace emscripten {
}
};
+ template<typename T>
+ struct BindingType<std::shared_ptr<T>> {
+ typedef std::shared_ptr<T> shared_ptr;
+ typedef std::shared_ptr<T>* WireType;
+
+ static WireType toWireType(shared_ptr p) {
+ return new shared_ptr(p);
+ }
+
+ static shared_ptr fromWireType(WireType wt) {
+ if (wt) {
+ return shared_ptr(*wt);
+ } else {
+ return shared_ptr();
+ }
+ }
+ };
+
template<typename Enum>
struct EnumBindingType {
typedef Enum WireType;