At a loss for how to link together my C++ to the v8 engine to make and use objects in JS

111 views
Skip to first unread message

Gregory Hlavac

unread,
Sep 25, 2013, 11:42:01 PM9/25/13
to v8-u...@googlegroups.com
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..

Handle<Context> ScriptLoader::createGlobalContext(Isolate* isolate)
{
Handle<ObjectTemplate> global = ObjectTemplate::New();

global->Set(String::New("trace"), FunctionTemplate::New(UtilityWrapper::traceWrapper));
global->Set(String::New("randomFloat"), FunctionTemplate::New(UtilityWrapper::randomFloatWrapper));
global->Set(String::New("randomInteger"), FunctionTemplate::New(UtilityWrapper::randomIntegerWrapper));
global->Set(String::New("clamp"), FunctionTemplate::New(UtilityWrapper::clampWrapper));

/* HERE */

return Context::New(isolate, NULL, global);
}


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.

Louis Santillan

unread,
Sep 25, 2013, 11:57:34 PM9/25/13
to v8-u...@googlegroups.com
There were major API changes recently.  Most v8 embedders seem to be still trying to catch up or have given up.  The best examples to work from today are shell.cc in the source code or Node.js source.

-L


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gregory Hlavac

unread,
Sep 26, 2013, 12:47:40 AM9/26/13
to v8-u...@googlegroups.com
Just tried using what little there is in node.js stuff, the part 

  Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
  exports->Set(String::NewSymbol("MyObject"), constructor)

Is no longer valid so I tried to replace it with 

exports->Set(String::NewSymbol("vec3"), Handle<Function>::New(iso, tpl->GetFunction()));
And the program immediately crashes out. =/

Gregory Hlavac

unread,
Sep 26, 2013, 1:28:14 AM9/26/13
to v8-u...@googlegroups.com
So I guess then my project is dead in the water until the community catches up judging by how this seems to be.
Reply all
Reply to author
Forward
0 new messages