I came up with a manager to bind callback functions from C++ to JavaScript 
and to run scripts. To run a single script and use bound functions from 
inside it works. But now I tried to run another script and access bound 
functions or global variables of the first script, but it doesn't work.

    Uncaught ReferenceError: Name is not defined

This is the manager class I use to bind and run scripts.

    class HelperScript
    {
    public:
        HelperScript(Module* Module)
        {
            v8::Isolate* isolate = v8::Isolate::GetCurrent();
            v8::HandleScope scope(isolate);
            v8::Persistent<v8::Context> context = v8::Context::New();
            context->Enter();
            v8::Local<v8::External> handle = 
v8::External::New(reinterpret_cast<void*>(Module));
            module = v8::Persistent<v8::External>::New(isolate, handle);
        }
        ~HelperScript()
        {
            v8::Isolate* isolate = v8::Isolate::GetCurrent();
            v8::HandleScope scope(isolate);
            v8::Local<v8::Context> context = v8::Context::GetCurrent();
            context->Exit();
        }

        void Bind(std::string Name, 
std::function<v8::Handle<v8::Value>(v8::Arguments const &)> Function)
        {
            v8::Isolate* isolate = v8::Isolate::GetCurrent();
            v8::HandleScope scope(isolate);
            v8::Local<v8::Context> context = v8::Context::GetCurrent();
            v8::InvocationCallback* function = 
Function.target<v8::InvocationCallback>();
            v8::Local<v8::Object> global = context->Global();
            global->Set(v8::String::New(Name.c_str()), 
v8::FunctionTemplate::New(*function, module)->GetFunction(), v8::ReadOnly);
        }
        void Load(std::string Path)
        {
            if(scripts.find(Path) != scripts.end())
            {
                HelperDebug::Warning("system", "The script " + Path + " is 
already loaded.");
                return;
            }

            std::string source = HelperFile::Read(Path);

            v8::Isolate* isolate = v8::Isolate::GetCurrent();
            v8::HandleScope scope(isolate);
            v8::Local<v8::Context> context = v8::Context::GetCurrent();
            v8::Handle<v8::Script> script = 
v8::Script::Compile(v8::String::New(source.c_str()));
            v8::Persistent<v8::Script> handle = 
v8::Persistent<v8::Script>::New(isolate, script);
            scripts.insert(std::make_pair(Path, handle));
        }
        v8::Persistent<v8::Value> Run(std::string Path)
        {
            if(scripts.find(Path) == scripts.end()) Load(Path);

            v8::Isolate* isolate = v8::Isolate::GetCurrent();
            v8::HandleScope scope(isolate);
            v8::Local<v8::Context> context = v8::Context::GetCurrent();
            v8::Local<v8::Value> result = scripts[Path]->Run();
            v8::Persistent<v8::Value> handle = 
v8::Persistent<v8::Value>::New(isolate, result);
            return handle;
        }

        static Module *Unwrap(v8::Local<v8::Value> Data)
        {
            if(Data.IsEmpty())
            {
                HelperDebug::Fail("", "cannot get module from script 
argument because it is empty");
                return nullptr;
            }
            else if(!Data->IsExternal())
            {
                HelperDebug::Fail("", "cannot get module from script 
argument because it's a wrong type");
                return nullptr;
            }

            v8::External *handle = v8::External::Cast(*Data);
            return static_cast<Module*>(handle->Value());
        }
    private:
        std::unordered_map<std::string, v8::Persistent<v8::Script>> scripts;
        v8::Persistent<v8::External> module;
    };

How can I make bound functions accessible to all scripts I want to run? And 
how to use global variables defined in one script in another one executed 
later?

-- 
-- 
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.


Reply via email to