Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2014 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e22f9f9448ad6be4adff to your computer and use it in GitHub Desktop.
Save anonymous/e22f9f9448ad6be4adff to your computer and use it in GitHub Desktop.
v8 expose type
struct Point {
// Point(int x = 0, int y = 0) : x(x), y(y) {};
int x, y;
auto static
v8_get_x(v8::Local<v8::String> name,
const v8::PropertyCallbackInfo<v8::Value>& info) -> void {
auto self = info.Holder();
auto wrapped = v8::Handle<v8::External>::Cast(self->GetInternalField(0));
auto ptr = wrapped->Value();
auto value = static_cast<Point*>(ptr)->x;
info.GetReturnValue().Set(v8::Integer::New(info.GetIsolate(), value));
};
auto static
v8_get_y(v8::Local<v8::String> name,
const v8::PropertyCallbackInfo<v8::Value>& info) -> void {
auto self = info.Holder();
auto wrapped = v8::Handle<v8::External>::Cast(self->GetInternalField(0));
auto ptr = wrapped->Value();
auto value = static_cast<Point*>(ptr)->y;
info.GetReturnValue().Set(v8::Integer::New(info.GetIsolate(), value));
};
auto static
v8_set_x(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) -> void {
auto self = info.Holder();
auto wrapped = v8::Handle<v8::External>::Cast(self->GetInternalField(0));
auto ptr = wrapped->Value();
auto point = static_cast<Point*>(ptr);
point->x = value->Int32Value();
};
auto static
v8_set_y(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) -> void {
auto self = info.Holder();
auto wrapped = v8::Handle<v8::External>::Cast(self->GetInternalField(0));
auto ptr = wrapped->Value();
auto point = static_cast<Point*>(ptr);
point->y = value->Int32Value();
};
auto static
v8_register_type(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate>& global) -> void {
auto v8_ctor = v8::FunctionTemplate::New(isolate, Point::v8_ctor_fn);
auto ctor_tpl = v8_ctor->InstanceTemplate();
ctor_tpl->SetInternalFieldCount(1);
ctor_tpl->SetAccessor(v8::String::NewFromUtf8(isolate, "x"),
Point::v8_get_x, Point::v8_set_x);
ctor_tpl->SetAccessor(v8::String::NewFromUtf8(isolate, "y"),
Point::v8_get_y, Point::v8_set_y);
global->Set(v8::String::NewFromUtf8(isolate, "Point"), v8_ctor);
}
auto static
v8_ctor_fn(const v8::FunctionCallbackInfo<v8::Value>& info) -> void {
v8::HandleScope scope(info.GetIsolate());
auto point = new Point{0,0};
//if (2 == info.Length()) {
// point->x = info[0]->Int32Value();
// point->y = info[1]->Int32Value();
//}
info.This()->SetInternalField(0, v8::External::New(info.GetIsolate(), point));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment