@@ -13,7 +13,7 @@ using v8::Boolean;
1313using v8::Context;
1414using v8::DontDelete;
1515using v8::DontEnum;
16- using v8::EscapableHandleScope ;
16+ using v8::FunctionTemplate ;
1717using v8::HandleScope;
1818using v8::Integer;
1919using v8::Isolate;
@@ -316,6 +316,26 @@ Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
316316 return Just (true );
317317}
318318
319+ // TODO(bnoordhuis) Not super efficient but called infrequently. Not worth
320+ // the trouble yet of specializing for RealEnvStore and MapKVStore.
321+ Maybe<bool > KVStore::AssignToObject (v8::Isolate* isolate,
322+ v8::Local<v8::Context> context,
323+ v8::Local<v8::Object> object) {
324+ HandleScope scope (isolate);
325+ Local<Array> keys = Enumerate (isolate);
326+ uint32_t keys_length = keys->Length ();
327+ for (uint32_t i = 0 ; i < keys_length; i++) {
328+ Local<Value> key;
329+ Local<String> value;
330+ bool ok = keys->Get (context, i).ToLocal (&key);
331+ ok = ok && key->IsString ();
332+ ok = ok && Get (isolate, key.As <String>()).ToLocal (&value);
333+ ok = ok && object->Set (context, key, value).To (&ok);
334+ if (!ok) return Nothing<bool >();
335+ }
336+ return Just (true );
337+ }
338+
319339static void EnvGetter (Local<Name> property,
320340 const PropertyCallbackInfo<Value>& info) {
321341 Environment* env = Environment::GetCurrent (info);
@@ -436,9 +456,13 @@ static void EnvDefiner(Local<Name> property,
436456 }
437457}
438458
439- MaybeLocal<Object> CreateEnvVarProxy (Local<Context> context, Isolate* isolate) {
440- EscapableHandleScope scope (isolate);
441- Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New (isolate);
459+ void CreateEnvProxyTemplate (Isolate* isolate, IsolateData* isolate_data) {
460+ HandleScope scope (isolate);
461+ if (!isolate_data->env_proxy_template ().IsEmpty ()) return ;
462+ Local<FunctionTemplate> env_proxy_ctor_template =
463+ FunctionTemplate::New (isolate);
464+ Local<ObjectTemplate> env_proxy_template =
465+ ObjectTemplate::New (isolate, env_proxy_ctor_template);
442466 env_proxy_template->SetHandler (NamedPropertyHandlerConfiguration (
443467 EnvGetter,
444468 EnvSetter,
@@ -449,7 +473,8 @@ MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
449473 nullptr ,
450474 Local<Value>(),
451475 PropertyHandlerFlags::kHasNoSideEffect ));
452- return scope.EscapeMaybe (env_proxy_template->NewInstance (context));
476+ isolate_data->set_env_proxy_template (env_proxy_template);
477+ isolate_data->set_env_proxy_ctor_template (env_proxy_ctor_template);
453478}
454479
455480void RegisterEnvVarExternalReferences (ExternalReferenceRegistry* registry) {
0 commit comments