diff options
author | Bill Welden <bwelden@imvu.com> | 2013-01-03 08:35:48 -0800 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-04-12 14:22:53 +0300 |
commit | 5ea90f9345ebfb271dfd960f77fb0c669d30fca6 (patch) | |
tree | de9ef87eb19a1f752625176bb059fc068355a8f1 | |
parent | 32ccee42ba92e8efc9b467af5e90bf424e0bb016 (diff) |
Support for automatic upcasting of pointer parameters passed to C++.
-rwxr-xr-x | src/embind/embind.js | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js index c9a6a4f6..b9a16fbb 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -206,7 +206,11 @@ function makeInvoker(name, argCount, argTypes, invoker, fn) { var args = new Array(argCount); args[0] = fn; for (var i = 1; i < argCount; ++i) { - args[i] = argTypes[i].toWireType(destructors, arguments[i-1]); + if (argTypes[i].toWireTypeAutoUpcast) { + args[i] = argTypes[i].toWireTypeAutoUpcast(destructors, arguments[i-1]); + } else { + args[i] = argTypes[i].toWireType(destructors, arguments[i-1]); + } } var rv = invoker.apply(null, args); if (argTypes[0].fromWireTypeAutoDowncast) { @@ -411,6 +415,7 @@ function RegisteredPointer(Handle, isPolymorphic, isSmartPointer, rawGetPointee, this.rawDestructor = rawDestructor; } +// todo: this will go away RegisteredPointer.prototype.toWireType = function(destructors, o) { if (null === o) { return 0; @@ -423,6 +428,21 @@ RegisteredPointer.prototype.toWireType = function(destructors, o) { } }; +// todo: distinguish ptr and rawPtr +RegisteredPointer.prototype.toWireTypeAutoUpcast = function(destructors, o) { + if (this.isSmartPointer) { + return this.toWireType(destructors, o); // for now + } else { + if (o.pointeeType.isPolymorphic) { + var dynamicType = o.pointeeType.getDynamicRawPointerType(o.ptr); + return ___staticPointerCast(o.ptr, dynamicType, this.pointeeType.rawType); + } else { + return ___staticPointerCast(o.ptr, o.pointeeType.rawType, this.pointeeType.rawType); + } + // todo: this cast can fail + } + }; + RegisteredPointer.prototype.getPointee = function(ptr) { if (this.rawGetPointee) { ptr = this.rawGetPointee(ptr); @@ -444,6 +464,7 @@ RegisteredPointer.prototype.fromWireType = function(ptr) { return new this.Handle(ptr); }; +// todo: could this return the actual type if not polymorphic? RegisteredPointer.prototype.getDynamicRawPointerType = function(ptr) { var type = null; if (this.isPolymorphic) { @@ -496,6 +517,7 @@ RegisteredPointer.prototype.fromWireTypeAutoDowncast = function(ptr) { // ptr is return handle; }; +// todo: don't need isPolymorphic parameter any more function __embind_register_smart_ptr( rawType, rawPointeeType, @@ -513,6 +535,7 @@ function __embind_register_smart_ptr( this.count = {value: 1}; this.smartPointer = ptr; // std::shared_ptr<T>* this.ptr = rawGetPointee(ptr); // T* + this.pointeeType = pointeeType; }); // TODO: test for SmartPtr.prototype.constructor property? @@ -673,7 +696,6 @@ RegisteredRawConstPointer.prototype.toWireType = function(destructors, o) { }; // TODO: null pointers are always zero (not a Handle) in Javascript -/*global ___staticPointerCast: false*/ function __embind_register_class( rawType, rawPointerType, @@ -696,6 +718,7 @@ function __embind_register_class( h.count = {value: 1, ptr: ptr }; h.ptr = ptr; + h.pointeeType = type; // set below for(var prop in Handle.prototype) { var dp = Object.getOwnPropertyDescriptor(Handle.prototype, prop); @@ -738,6 +761,7 @@ function __embind_register_class( this.ptr = undefined; }; + // todo: clean this up! var registeredClass = new RegisteredPointer(Handle, isPolymorphic, false); var type = registerType(rawType, name, registeredClass); registeredClass.pointeeType = type; |