On Sat, Apr 16, 2011 at 5:04 AM, crtmpserver <[email protected]> wrote:
> Hello,
>
> I have the following situation. I have a C++ class (Point) and I
> wrapped it inside a JS object. In JS, I do something like:
>
> Point.prototype.testProperty="Hi there!"
> var p=new Point(1,2);
> for(var i in p)
> {
> trace("property: "+i);
> }
>
> Unfortunately, p doesn't have any property called testProperty.
> Similar thing works just fine with 100% JS managed objects:
>
> function Circle(r){
> this.radius=r;
> this.print=function(){
> trace("Radius: "+r);
> }
> }
>
> Circle.prototype.border="thick";
>
> var c=new Circle(100);
> for(var i in c)
> {
> trace("property: "+i);
> }
>
> And the output is:
> property: radius
> property: print
> property: border
>
>
> Is it possible to alter the prototype of a wrapped C++ object from JS
> or not?
>
>
> Here is the C++ relevant part:
>
> Persistent<FunctionTemplate> _pointFunctionTemplate;
>
> _pointFunctionTemplate =
> Persistent<FunctionTemplate>::New(FunctionTemplate::New(pointConstructor));
> _pointFunctionTemplate->SetClassName(String::New("Point"));
> _pointFunctionTemplate->PrototypeTemplate()->SetInternalFieldCount(1);
>
> global->Set(String::New("Point"), _pointFunctionTemplate);
>
I'm surprised the new call works at all. __pointFunctionTemplate is a
function *template*. You want to bind the *function* to that name:
> global->Set(String::New("Point"), _pointFunctionTemplate->GetFunction());
Not sure if that'll change things but it's the right thing.
Matthias
>
>
> and the pointConstructor function is this:
>
> Handle<Value> pointConstructor(const Arguments& args) {
> Handle<Object> result = _pointFunctionTemplate->PrototypeTemplate()-
> >NewInstance();
> Point *pPoint = new Point(args[0]->ToNumber()->Uint32Value(),
> args[1]-
> >ToNumber()->Uint32Value());
> result->SetInternalField(0, External::New(pPoint));
> return result;
> }
>
> Thank you very much
>
> --
> 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