To expose a C++ object to JS, this is about what you want to do:
template = ObjectTemplate::New();
template->SetInternalFieldCount(1);
template->SetAccessor("property", Getter);
// you can actually do this many times, template acts like a class
instance = template->NewInstance();
instance->SetInternalField(0, External::Wrap(myC++Object));
context->Global()->Set("name", instance);
...
Getter(Local<String> property, const Arguments& args) {
HandleScope locals;
X* myC++Object =
static_cast<X*>(External::Unwrap(args->This()->GetInternalField(0)));
...
return locals.Close(result);
}
Now in JS: name.property should call your Getter.
There's some more sophisticated variations on this, e.g. using
FunctionTemplate and exposing its function instance to JS, too, so you can
attach JS methods to the prototype, but you should start with this.
On Thu, Apr 8, 2010 at 11:52 PM, vlad <[email protected]> wrote:
> Hi,
> Could somebody help me to figure out how to make c++ object available
> within the frame's JS context?
>
> Frame* frame = ...;
>
> // Get context for the frame
> v8::Local<v8::Context> context = V8Proxy::mainWorldContext(frame);
>
> // Create v8 object v8::ObjectTemplate::New()
> v8::Handle<v8::ObjectTemplate> obj = v8::ObjectTemplate::New();
>
> // Linking passed object with the v8 object
> obj->Set(v8::String::New("MyObject"), v8::ExternalWrap(MyObject));
>
> // How to actually add the object to the context????
> ....
>
> Thank you
> Vlad
>
> --
> 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