The problem is that when you construct an instance of the traced
object with "new Foo()" the prototype of the result is set to the
prototype of the ObjectTemplate created in TracedObjectCallback, not
the prototype of Foo.  What you need to do is set the prototype
manually, like so:

  ...
  Handle<Object> result = traced_obj->NewInstance();
  Handle<Function> constructor = Handle<Function>::Cast(args.Data());
  result->Set(v8::String::New("__proto__"),
constructor->Get(v8::String::New("prototype")));
  ...

When I do this your test program prints

1
2


-- Christian

On Thu, Sep 11, 2008 at 4:00 AM, Jey Kottalam <[EMAIL PROTECTED]> wrote:
>
> I have a constructor Foo:
>
> function Foo() { this.first = 1; }
> Foo.prototype = { second: 2 };
>
> and I want to intercept all property accesses of instances of Foo. I'm
> trying to do this by creating an ObjectTemplate with a
> NamedPropertyHandler and simulating the effect of "new Foo()" on an
> object created from this ObjectTemplate. This almost works, except I
> can't figure out how to apply Foo.prototype to the newly constructed
> object.
>
>
> Here's an example that demonstrates the problem:
>
> function Foo() { this.first = 1; }
> Foo.prototype = { second: 2 };
> Foo = TracedObject(Foo);
>
> var t = new Foo();
> log(t.first + "\n");
> log(t.second + "\n");
>
>
> The output is:
>
> 1
> undefined
>
>
> TracedObject is implemented as: http://gist.github.com/10136
>
>
> How do I correctly simulate the effect of new Foo() on the object
> created from my ObjectTemplate?
>
> Thanks,
> -Jey Kottalam
> >
>

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to