[v8-users] Re: Is there any way to create a Handle from a Handle?

Sun, 08 Mar 2009 20:03:20 -0700

So, I did eventually get this to work very much like SpiderMonkey's
evalcx function.  Here's the code if anyone else is interested:

usage:
evalcx( code_to_execute, global_object (default: this), keep_changes
(bool, should changed global be written back to the object?) )
returns the result of the executed code.

static v8::Handle<v8::Value> EvalCX (const v8::Arguments& args)
{
        HandleScope handle_scope;
        Handle<Context> context = Context::New();
        context->SetSecurityToken(
                Context::GetCurrent()->GetSecurityToken()
        );

        Context::Scope context_scope(context);
        Handle<String> code = args[0]->ToString();
        Handle<Object> sandbox = args.Length() >= 1 ? args[1]->ToObject() :
Context::GetCurrent()->Global();

        // share global datas
        copy_obj( sandbox, context->Global());
        context->Enter();

        Handle<Script> script = Script::Compile(code, String::New("evalcx"));
        Handle<Value> result;
        if (script.IsEmpty()) {
                result = ThrowException(String::New("Error parsing script"));
        } else {
                result = script->Run();
                if (result.IsEmpty()) {
                        result = v8::ThrowException(v8::String::New("Error 
running
script"));
                }
        }
        if (args.Length() >= 3 && args[2]->IsTrue()) {
                copy_obj( context->Global(), sandbox );
        }
        context->DetachGlobal();
        context->Exit();
        return result;
}

--i
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to