On Sat, Nov 26, 2016 at 2:19 AM, George Marques <[email protected]> wrote: > Hey, it's me again. > > I'm trying to use a global JavaScript file with internal definitions from a > game engine. So far so good, but I need to get one of those constructors > defined in the JavaScript to create a new object in C++ land. > > What I tried is something like this: > > v8::Local<v8::Context> ctx = p_isolate->GetCurrentContext(); > v8::Local<v8::Function> constructor = > v8::Local<v8::Function>::Cast(ctx->Global()->Get( > v8::String::NewFromUtf8(p_isolate, "Vector2"))); > > v8::Local<v8::Value> cargs[] = { v8::Number::New(p_isolate, vec.x), > v8::Number::New(p_isolate, vec.y) }; > v8::Local<v8::Object> instance = > v8::Local<v8::Object>::Cast(constructor->CallAsConstructor(2, cargs)); > > However, this give an error as it can't find the Vector2 constructor. I can > use the constructor normally in JavaScript land. > > If I make the Vector2 constructor as a C++ function, this code works > perfectly. However, I believe it has a better performance to reimplement > those core types in JS than to keep passing around an object and calling C++ > functions with a bunch of handles (I might be wrong though). This would also > mitigate the memory management overhead, I think, which I haven't yet got > into.
The constructor should be visible from C++. Is your JS code wrapped in an IIFE? -- -- v8-users mailing list [email protected] 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
