@@ -10,6 +10,7 @@ void Context::Init() {
1010 defineSingletonMethod (" GetCalling" , &GetCalling).
1111 defineSingletonMethod (" InContext" , &InContext).
1212 defineMethod (" Dispose" , &Dispose).
13+ defineMethod (" Clone" , &Clone).
1314 defineMethod (" Global" , &Global).
1415 defineMethod (" DetachGlobal" , &Global).
1516 defineMethod (" ReattachGlobal" , &ReattachGlobal).
@@ -33,6 +34,53 @@ VALUE Context::Dispose(VALUE self) {
3334 Void (Context (self).dispose ())
3435}
3536
37+ VALUE Context::Clone (VALUE self, VALUE new_rr_context) {
38+ // acquire the lock
39+ v8::Locker locker;
40+ v8::HandleScope handle_scope;
41+
42+ // unwrap V8 contexts
43+ v8::Context* original_context = *((v8::Handle<v8::Context>) Context (self));
44+ v8::Context* new_context = *((v8::Handle<v8::Context>) Context (new_rr_context));
45+
46+ // get the original global context
47+ v8::Local<v8::Value> key, value;
48+
49+ original_context->Enter ();
50+
51+ v8::Local<v8::Object> original_global (original_context->Global ());
52+ v8::Local<v8::Array> property_names = original_global->GetPropertyNames ();
53+ uint32_t i, length = property_names->Length ();
54+ std::vector< v8::Local<v8::Value> > values;
55+
56+ for (i = 0 ; i < length; i++) {
57+ key = property_names->Get (i);
58+ value = original_global->Get (key);
59+ values.push_back (value);
60+ }
61+
62+ original_context->Exit ();
63+
64+ // and copy everything to the new context
65+ new_context->Enter ();
66+
67+ v8::Local<v8::Object> new_global (new_context->Global ());
68+ std::vector< v8::Local<v8::Value> >::iterator it;
69+
70+ for (i = 0 , it = values.begin (); i < length; i++, it++) {
71+ key = property_names->Get (i);
72+ value = *it;
73+ new_global->Set (key, value);
74+ }
75+
76+ new_context->Exit ();
77+
78+ // unlock
79+ v8::Unlocker unlocker;
80+
81+ return Qnil;
82+ }
83+
3684VALUE Context::Global (VALUE self) {
3785 return Object (Context (self)->Global ());
3886}
0 commit comments