On Mon, Jun 7, 2010 at 4:45 PM, Martin Cohen <[email protected]> wrote: > Hello guys, > > I'm trying to "wrap" a C++ object to JavaScript with this notorious > technique: > > //------------------------------------------------------------------------ > /// Wraps C++ instance to JS object. > v8::Persistent<v8::Object> wrap( T * object ) > //------------------------------------------------------------------------ > { > // Make sure we have only one JS object per C++ instance. > Instances::iterator it = m_instances.find( object ); > if( it != m_instances.end() ) > return it->second; > // Create an instance of JS object > v8::Persistent<v8::Object> > js_object( v8::Persistent<v8::Object>::New(m_t_object->GetFunction()- >>NewInstance()) ); > // Make the handle weak > js_object.MakeWeak( object, &TClass<W,T>::destructorCall ); > // Register the JS object with the C++ instance > m_instances.insert( std::make_pair( object, js_object ) ); > // Set the internal field value to the C++ object > js_object->SetInternalField( 0, v8::External::New( object ) ); > return js_object; > } > > However a returned object has no class name set, so the "instanceof" > operator does not work. From what I've learned, the "instanceof" > operator works using the object.prototype.constructor property, though > I'm not sure how to correctly set it up in this wrap function.
instanceof should work out of the box. instanceof(object, function) is defined in terms of searching for "function.prototype" along the object's prototype chain, not in terms of class name. Have you exported "m_t_object->GetFunction()" to javascript, used it in the instanceof expression and it is broken for you? In any case, you can set the class name on the function: FunctionTemplate::SetClassName(). In V8, this only influences toString, though. Matthias > > Any ideas? > > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
