I'm at a loss for how to get this done, it seems the 2000000 different examples I've come across are all woefully out of date and the documentation I've come across is pretty much entirely useless for how tog et this done.
Where i'd like to put my global object templates to use them in code..
And then for the time being I've decided to start out trying to wrap something simple, but again I've not really gotten anywhere because of the terrible documentation overall.
#include "stdafx.h"
#include "Vec3Wrapper.h"
namespace Vec3Wrapper
{
Handle<Function> Initialize(Handle<Object> target)
{
HandleScope scope(Isolate::GetCurrent());
Handle<FunctionTemplate> vec3Tpl = FunctionTemplate::New(vec3Constructor);
vec3Tpl->SetClassName(String::New("vec3"));
Handle<ObjectTemplate> instance = vec3Tpl->InstanceTemplate();
instance->SetInternalFieldCount(1);
instance->SetAccessor(String::NewSymbol("x"), GetVec3X, SetVec3X);
instance->SetAccessor(String::NewSymbol("y"), GetVec3Y, SetVec3Y);
instance->SetAccessor(String::NewSymbol("z"), GetVec3Z, SetVec3Z);
target->Set(String::New("vec3"), vec3Tpl->GetFunction());
vec3_template_.Reset(Isolate::GetCurrent(), instance);
return scope.Close(instance);
}
Handle<ObjectTemplate> MakeTemplate(Isolate* iso)
{
HandleScope scope(iso);
Handle<FunctionTemplate> ftmp = FunctionTemplate::New(vec3Constructor);
Handle<ObjectTemplate> result = ftmp->InstanceTemplate();
result->SetInternalFieldCount(1);
ftmp->SetClassName(String::New("vec3"));
result->Set(String::New("vec3"), FunctionTemplate::New(vec3Constructor));
result->SetAccessor(String::NewSymbol("x"), GetVec3X, SetVec3X);
result->SetAccessor(String::NewSymbol("y"), GetVec3Y, SetVec3Y);
result->SetAccessor(String::NewSymbol("z"), GetVec3Z, SetVec3Z);
return scope.Close(result);
}
Handle<Object> Wrap(vec3* vector)
{
HandleScope scope(Isolate::GetCurrent());
if(vec3_template_.IsEmpty())
{
Handle<ObjectTemplate> rawTemplate = MakeTemplate(Isolate::GetCurrent());
vec3_template_.Reset(Isolate::GetCurrent(), rawTemplate);
}
Handle<ObjectTemplate> templ = Local<ObjectTemplate>::New(Isolate::GetCurrent(), vec3_template_);
Handle<Object> result = templ->NewInstance();
Handle<External> vec3_ptr = External::New(vector);
result->SetInternalField(0, vec3_ptr);
return scope.Close(result);
}
vec3* Unwrap(Handle<Object> obj)
{
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
void* ptr = field->Value();
return static_cast<vec3*>(ptr);
}
void vec3Constructor(const FunctionCallbackInfo<Value>& args)
{
if(!args.IsConstructCall())
ThrowException(String::New("Cannot call vec3 constructor as a function."));
HandleScope scope(Isolate::GetCurrent());
float x = args[0]->NumberValue();
float y = args[1]->NumberValue();
float z = args[2]->NumberValue();
vec3* vec = new vec3(x, y, z);
args.GetReturnValue().Set(Wrap(vec));
}
void GetVec3X(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
vec3* v = Unwrap(info.Holder());
info.GetReturnValue().Set(v->x);
}
void SetVec3X(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
{
Unwrap(info.Holder())->x = value->NumberValue();
}
void GetVec3Y(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
vec3* v = Unwrap(info.Holder());
info.GetReturnValue().Set(v->y);
}
void SetVec3Y(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
{
Unwrap(info.Holder())->y = value->NumberValue();
}
void GetVec3Z(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
vec3* v = Unwrap(info.Holder());
info.GetReturnValue().Set(v->z);
}
void SetVec3Z(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
{
Unwrap(info.Holder())->z = value->NumberValue();
}
};
Any help pushing me in the right direction would really be greatly welcomed in this case, because this is ridiculous.