Your code probably always calls NewInstance on a function created from
a function template whenever you access a C++ object from JavaScript.
This will lead to new JavaScript objects being allocated for each
access to a C++ object.  Since you get new JavaScript objects for each
access, properties that you set on the JavaScript objects are not
shared (they are set on different objects).  Additionally, allocating
multiple JavaScript objects corresponding to a C++ object makes it
hard to get memory management right.

In your code, you should make sure to only generate one JavaScript
object per C++ object.  You could handle this by having a mapping from
C++ object pointers to handles to V8 objects, or by putting a handle
to the corresponding V8 object in the C++ objects themselves.  Before
you create a new JavaScript object for a C++ object, you should check
if you already have a JavaScript object corresponding to the C++
object and return that if you do.

-- Mads

On Mon, Jan 26, 2009 at 12:48 AM, vlad <[email protected]> wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to