void Initialize(Handle<ObjectTemplate> exports, Isolate* iso)
{
HandleScope scope(iso);
Local<FunctionTemplate> funcTmpl = FunctionTemplate::New(New);
funcTmpl->SetClassName(String::NewSymbol("vec2"));
Local<ObjectTemplate> protoTmpl = funcTmpl->PrototypeTemplate();
protoTmpl->SetInternalFieldCount(2);
protoTmpl->Set(String::New("add"), FunctionTemplate::New(OperatorAdd));
protoTmpl->Set(String::New("sub"), FunctionTemplate::New(OperatorSubtract));
protoTmpl->Set(String::New("mul"), FunctionTemplate::New(OperatorMultiply));
protoTmpl->Set(String::New("div"), FunctionTemplate::New(OperatorDivide));
protoTmpl->Set(String::New("length"), FunctionTemplate::New(Length));
protoTmpl->Set(String::New("normalize"), FunctionTemplate::New(Normalize));
protoTmpl->Set(String::New("distance"), FunctionTemplate::New(Distance));
protoTmpl->Set(String::New("equals"), FunctionTemplate::New(Equals));
protoTmpl->Set(String::New("toString"), FunctionTemplate::New(ToString));
protoTmpl->SetAccessor(String::NewSymbol("x"), GetVecX, SetVecX);
protoTmpl->SetAccessor(String::NewSymbol("y"), GetVecY, SetVecY);
exports->Set(String::NewSymbol("vec2"), funcTmpl);
if(fncTmpl.IsEmpty())
fncTmpl.Reset(iso, funcTmpl);
}
void New(const FunctionCallbackInfo<Value>& args)
{
if(!args.IsConstructCall())
ThrowException(String::New("Cannot call vec2 constructor as a function."));
HandleScope scope(Isolate::GetCurrent());
float x = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
float y = args[1]->IsUndefined() ? 0 : args[1]->NumberValue();
vec2* vec = new vec2(x, y);
args.GetReturnValue().Set(Wrap(vec));
}