From 3a9dc23f47cfe6313410eee38a3b5f7f7ff03aca Mon Sep 17 00:00:00 2001 From: Sarunas Valaskevicius Date: Sun, 20 Jan 2013 17:42:43 +0000 Subject: [PATCH] find native implementation from object prototype chain The current object might not always have the native implementation accessible - it might be an "extending" class in js, e.g.: MyWindow = function(parent) { this.prototype = this.__proto__ = new api.QMainWindow(parent); } var w = new MyWindow(null); w.show(); --- src/vu8/Class.hpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/vu8/Class.hpp b/src/vu8/Class.hpp index 7159ad4..974ceb4 100644 --- a/src/vu8/Class.hpp +++ b/src/vu8/Class.hpp @@ -114,18 +114,34 @@ class ClassSingleton template static inline ValueHandle Forward(const v8::Arguments& args) { v8::HandleScope scope; - v8::Local self = args.Holder(); // this will kill without zero-overhead exception handling try { - return scope.Close(ForwardReturn

( - static_cast(self->GetPointerFromInternalField(0)), args)); + return scope.Close( + ForwardReturn

( + retrieveNativeObjectPtr(args.Holder()), + args + ) + ); } catch (std::runtime_error const& e) { return Throw(e.what()); } } + static inline T* retrieveNativeObjectPtr(v8::Local value) + { + while (value->IsObject()) { + v8::Local obj = value->ToObject(); + T * native = static_cast(obj->GetPointerFromInternalField(0)); + if (native) { + return native; + } + value = obj->GetPrototype(); + } + return NULL; + } + static inline void MadeWeak(v8::Persistent object, void *parameter) {