On Mon, Mar 24, 2014 at 8:03 AM, Michael Klimontov <[email protected]> wrote:
> Hello guys. I need your help for solve this question.
>
> I am want loading cfg.js in context.
> In any action we will loading and working with scr_1.js. In scr_1 changing
> vals from cfg.js. After will need working with scr_2 where have clear vars
> from cfg.js, but they changed in prev work with scr_1.
> How are correct work with context and copy they?
>
> Thanks for your.
>
> ___ cfg.js ___
>
> var a = { a : 22522, b : '123' };
> // Define common functions ....
>
> ___ scr_1.js ___
>
> function test_function(test_arg1,test_arg2)
> {
> log("Src 1");
> log(a.a)
> a.a = 9999;
> log("Src 1 -> new a = " a.a)
> return 0;
> }
>
> ___ scr_2.js ___
>
> function test_function(test_arg1,test_arg2)
> {
> log(a.a)
> return 0;
> }
>
> ___ main.cpp ___
>
> int main(int argc, char* argv[])
> {
> v8::V8::InitializeICU();
>
> Isolate* isolate = Isolate::GetCurrent();
> HandleScope handle_scope(isolate);
>
>
> Handle<ObjectTemplate> global_cfg = ObjectTemplate::New(isolate);
> global_cfg->Set(String::NewFromUtf8(isolate, "log"),
> FunctionTemplate::New(isolate, LogCallback));
> Persistent<Context> persistent_context;
>
> {
> v8::Handle<v8::Context> context_global = Context::New(isolate, NULL,
> global_cfg);
> persistent_context.Reset(isolate, context_global);
>
> v8::Local<v8::Context> context_tmp =
> v8::Local<v8::Context>::New(isolate, persistent_context);
> Context::Scope context_scope_tmp(context_tmp);
> Handle<String> source2 = ReadFile(isolate, "cfg.js");
> v8::Handle<v8::Script> script2 = Script::Compile(source2);
> script2->Run();
> }
>
>
> // Run test 1. Need copy context with out change.
> // After run test_function a = new value
> {
> HandleScope handle_scope(isolate);
> v8::Local<v8::Context> context_tmp =
> v8::Local<v8::Context>::New(isolate, persistent_context);
> context_tmp->Enter();
>
> Context::Scope context_scope_tmp(context_tmp);
> HandleScope handle_scope(isolate);
>
> Handle<String> source = ReadFile(isolate, "scr_1.js");
> v8::Handle<v8::Script> script = Script::Compile(source);
> // ! run function test_function
> script->Run();
> context_tmp->Exit();
> }
>
> // Run test 2. Need copy context without change.
> {
> HandleScope handle_scope(isolate);
> v8::Local<v8::Context> context_tmp =
> v8::Local<v8::Context>::New(isolate, persistent_context);
> context_tmp->Enter();
>
> Context::Scope context_scope_tmp(context_tmp);
> HandleScope handle_scope(isolate);
>
> Handle<String> source = ReadFile(isolate, "scr_1.js");
> v8::Handle<v8::Script> script = Script::Compile(source);
> // This val must equal var a = { a : 22522, b : '123' }, but test1
> changes it.
> script->Run();
>
>
> context_tmp->Exit();
> }
> }
You need to copy them explicitly from the first context's global
object to the second context's after every update, e.g.:
Local<Object> first_global = first_context->Global();
Local<Object> second_global = second_context->Global();
Local<Array> property_names = first_global->GetOwnPropertyNames();
for (uint32_t i = 0, n = property_names->Length(); i < n; i += 1) {
Local<Value> key = property_names->Get(i);
Local<Value> value = first_global->Get(key);
second_global->Set(key, value);
}
Note that both contexts should share the same security token, else
they won't be able to access values created in the other context. You
can change the security token with
context->SetSecurityToken(Integer::New(isolate, 42)).
--
--
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.