@@ -13,7 +13,7 @@ using v8::Boolean;
13
13
using v8::Context;
14
14
using v8::DontDelete;
15
15
using v8::DontEnum;
16
- using v8::EscapableHandleScope ;
16
+ using v8::FunctionTemplate ;
17
17
using v8::HandleScope;
18
18
using v8::Integer;
19
19
using v8::Isolate;
@@ -316,6 +316,26 @@ Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
316
316
return Just (true );
317
317
}
318
318
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
+
319
339
static void EnvGetter (Local<Name> property,
320
340
const PropertyCallbackInfo<Value>& info) {
321
341
Environment* env = Environment::GetCurrent (info);
@@ -436,9 +456,13 @@ static void EnvDefiner(Local<Name> property,
436
456
}
437
457
}
438
458
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);
442
466
env_proxy_template->SetHandler (NamedPropertyHandlerConfiguration (
443
467
EnvGetter,
444
468
EnvSetter,
@@ -449,7 +473,8 @@ MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
449
473
nullptr ,
450
474
Local<Value>(),
451
475
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);
453
478
}
454
479
455
480
void RegisterEnvVarExternalReferences (ExternalReferenceRegistry* registry) {
0 commit comments