var myPoint = new Point(3,4);
print(myPoint.x); //Assume print is defined and works splendidly
//The Point C++ class
class Point {
public:
Point(int x, int y) : x_(x), y_(y) { }
int x_, y_;
};
//The method where I start doing V8 things. Set up an Isolate, Context, etc. Only the relevant parts are shown here.
bool MyTest::DoStuff(...)
{
...
Local<FunctionTemplate> constructor = FunctionTemplate::New(ConstructPoint);
context->Global()->Set(String::New("Point"), constructor->GetFunction());
pointTemplate = FunctionTemplate::New();
Local<ObjectTemplate> instance_templ = pointTemplate->InstanceTemplate();
instance_templ->SetInternalFieldCount(1);
instance_templ->SetAccessor(String::New("x"), GetPointX);
...
}
//The Point constructor function
void MyTest::ConstructPoint(const FunctionCallbackInfo<Value>& args)
{
//start a handle scope
HandleScope handle_scope;
//get an x and y
int x = args[0]->Int32Value();
int y = args[1]->Int32Value();
//generate a new point
Point *point = new Point(x, y);
//return the wrapped point
WrapPoint(point);
}
//The wrapper function
Handle<Object> MyTest::WrapPoint(Point *pointToWrap)
{
//enter a handle scope
HandleScope handle_scope;
//create a new point instance
Local<Object> point_instance = pointTemplate->GetFunction()->NewInstance();
//set that internal field
point_instance->SetInternalField(0, External::New(pointToWrap));
//to prevent the point_instance from being destroyed when its
//scope handle_scope is, use the Close() function
return handle_scope.Close(point_instance);
}
//The getter function for "somePoint.x" in Javascript. The guts of this could be wrong. Sadly, I am never getting here at all!
void MyTest::GetPointX(Local<String> name, const PropertyCallbackInfo<Value>& info)
{
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<Point*>(ptr)->x_;
info.GetReturnValue().Set(Number::New(value));
}
Thank you Ben. I assume you mean something like what's being done here? I think the problem is that I'm not using node, and that would be needed to do the following, correct?point->Wrap(args.This()); //Where Wrap is available because the C++ Point class extends ObjectWrap (from node.h)I would like to do this in "pure V8" without using node. Is that possible? Am I making things too difficult?
or may not work for you, YMMV. Node.js has the luxury of beingallowed to assume that the executed JS code isn't hostile, unlike e.g.
a browser.
args.This()->SetInternalField(0, External::New(point));
# Fatal error in v8::Object::SetInternalField()
# Internal field out of bounds
constructor->InstanceTemplate()->SetInternalFieldCount(1);
//The Point C++ class
class Point {
public:
Point(int x, int y) : x_(x), y_(y) { }
int x_, y_;
};
//The method where I start doing V8 things. Set up an Isolate, Context, etc. Only the relevant parts are shown here.
bool MyTest::DoStuff(...)
{
...
Local<FunctionTemplate> constructor = FunctionTemplate::New(ConstructPoint);
constructor->InstanceTemplate()->SetInternalFieldCount(1); //Set the InternalFieldCount on the CONSTRUCTOR'S InstanceTemplate
constructor->InstanceTemplate()->SetAccessor(String::New("x"), GetPointX); //Same goes for the Accessor
context->Global()->Set(String::New("Point"), constructor->GetFunction());
...
}
//The Point constructor function
void MyTest::ConstructPoint(const FunctionCallbackInfo<Value>& args)
{
//start a handle scope
HandleScope handle_scope;
//get an x and y
int x = args[0]->Int32Value();
int y = args[1]->Int32Value();
//generate a new point
Point *point = new Point(x, y);
//Set the C++ point to the JS point
args.This()->SetInternalField(0, External::New(point));
}
//The getter function for "somePoint.x" in Javascript. Sadly, I am never getting here!
Hello. I've tried following along with this and the embedder's guide, but my point binding only works to an extent. The constructor works, but for some reason when I use an invalid constructor the object in JavaScript is not undefined (I set the return value to be so).
Okay, then is there a way to return an undefined object on an improper constructor? And I still have yet to solve the problem with values not being what they're supposed to be :/
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/xuR2iL1NoWI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.