Short Question:
In an accessor function, how can one return a reference to a member
variable. That is, a 'reference' to a member, not a new object with
the same values of the member object. The following doesn't work.
[CODE]
v8::Handle<v8::Value> get_bar(v8::Local<v8::String> property, const
v8::AccessorInfo& info)
{
return info.Holder()->Get(property); // property holds the value
'bar'.
}
[/CODE]
------------------------------------------------------------
Detailed Explanation:
Let's say I create a Javascript Function to be used as a class in C++.
Let's call that class 'Foo', which contains another object as one of
it's members. Let's call inner member variable 'bar'.
Creating Foo is like so:
[CODE]
Handle<FunctionTemplate> function_tpl = FunctionTemplate::New
(constructor_function);
Handle<ObjectTemplate> instance_tpl = functionTpl->InstanceTemplate
();
// The 'bar' member
instance_tpl->SetAccessor(v8::String::New("bar"), get_bar, set_bar);
[/CODE]
I'm able to return a bar member fine. The issue is that it's a new
instance of bar, and not one attached to the Foo object. For instance,
this prints the proper value:
[CODE]
var f = new Foo;
print (f.bar); // [object bar]
print (f.bar.age); // prints the value of 'f.bar.age' fine
[/CODE]
The problem comes when doing something like this:
[CODE]
f.bar.age = 6;
[/CODE]
The reason being that 'f.bar' returns a new instance of bar. But that
new instance is not the same instance connected to 'Foo'. My question
is then:
How can I return a reference to a member variable of some object. I
thought this would work:
[CODE]
v8::Handle<v8::Value> get_bar(v8::Local<v8::String> property, const
v8::AccessorInfo& info)
{
return info.Holder()->Get(property); // property holds the value
'bar'.
}
[/CODE]
Not only does this not work but it also crashes my system. That was
unexpected. I thought that it would, at worst, return 'undefined'.
Changing 'Holder()' to 'This()' also has the same effect.
--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---