To efficiently use V8 in my game engine I want to develop a manager class.
This is the code so far.
#include "v8.h"
>
> class Manager
> {
> public:
> Manager()
> {
> v8::Isolate* isolate = v8::Isolate::GetCurrent();
> v8::HandleScope handle_scope(isolate);
> globals = v8::Persistent<v8::ObjectTemplate>::New(isolate,
> v8::ObjectTemplate::New());
> }
> void Bind(string Name, function<v8::Handle<v8::Value>(v8::Arguments
> const &)> Function)
> {
> v8::InvocationCallback* function =
> Function.target<v8::InvocationCallback>();
> globals->Set(v8::String::New(Name.c_str()),
> v8::FunctionTemplate::New(*function));
> }
> void Init()
> {
> context = v8::Context::New(NULL, globals);
> context->Enter();
> }
> void Run(string Source)
> {
> v8::Isolate* isolate = context->GetIsolate();
> v8::HandleScope handle_scope(isolate);
> v8::Handle<v8::Script> script =
> v8::Script::Compile(v8::String::New(Source.c_str()));
> v8::Handle<v8::Value> result = script->Run();
> }
> ~Manager()
> {
> context->Exit();
> v8::Isolate* isolate = context->GetIsolate();
> context.Dispose(isolate);
> v8::V8::Dispose();
> }
> private:
> v8::Persistent<v8::Context> context;
> v8::Persistent<v8::ObjectTemplate> globals;
> };
>
To provide a complete example, I show you an example class that uses the
manager to run a script.
class Example
> {
> public:
> Example() : Script(new Manager())
> {
> Script->Bind("print", Print);
> Script->Init();
> Script->Run("print('Hello World!');");
> }
> static v8::Handle<v8::Value> Print(const v8::Arguments& args)
> {
> bool first = true;
> for (int i = 0; i < args.Length(); i++)
> {
> v8::HandleScope handle_scope(args.GetIsolate());
> if (first) first = false;
> else printf(" ");
> v8::String::Utf8Value str(args[i]);
> const char* cstr = *str ? *str : "<string conversion failed>";
> printf("%s", cstr);
> }
> printf("\n");
> fflush(stdout);
> return v8::Undefined();
> }
> private:
> Manager* Script;
> };
>
The code compiled without errors but executing it results in an access
violation
in the V8 header file factory.cc at line 373.
Unhandled exception at 0x004003B6 in Application.exe: 0xC0000005: Access
> violation writing location 0x00000000.
>
I cannot see the mistake I made. Could you give me an advice, please?
--
--
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/groups/opt_out.