@@ -337,6 +337,19 @@ Maybe<void> KVStore::AssignToObject(v8::Isolate* isolate,
337337 return JustVoid ();
338338}
339339
340+ void PrintTraceEnvStack (Environment* env) {
341+ PrintTraceEnvStack (env->options ());
342+ }
343+
344+ void PrintTraceEnvStack (std::shared_ptr<EnvironmentOptions> options) {
345+ if (options->trace_env_native_stack ) {
346+ DumpNativeBacktrace (stderr);
347+ }
348+ if (options->trace_env_js_stack ) {
349+ DumpJavaScriptBacktrace (stderr);
350+ }
351+ }
352+
340353static Intercepted EnvGetter (Local<Name> property,
341354 const PropertyCallbackInfo<Value>& info) {
342355 Environment* env = Environment::GetCurrent (info);
@@ -348,7 +361,27 @@ static Intercepted EnvGetter(Local<Name> property,
348361 CHECK (property->IsString ());
349362 MaybeLocal<String> value_string =
350363 env->env_vars ()->Get (env->isolate (), property.As <String>());
351- if (!value_string.IsEmpty ()) {
364+
365+ bool has_env = !value_string.IsEmpty ();
366+ if (env->options ()->trace_env ) {
367+ Utf8Value key (env->isolate (), property.As <String>());
368+ fprintf (stderr,
369+ " [--trace-env] get environment variable \" %.*s\" : " ,
370+ static_cast <int >(key.length ()),
371+ key.out ());
372+ if (has_env) {
373+ Utf8Value value_utf8 (env->isolate (), value_string.ToLocalChecked ());
374+ fprintf (stderr,
375+ " \" %.*s\"\n " ,
376+ static_cast <int >(value_utf8.length ()),
377+ value_utf8.out ());
378+ } else {
379+ fprintf (stderr, " undefined\n " );
380+ }
381+ PrintTraceEnvStack (env);
382+ }
383+
384+ if (has_env) {
352385 info.GetReturnValue ().Set (value_string.ToLocalChecked ());
353386 return Intercepted::kYes ;
354387 }
@@ -386,6 +419,17 @@ static Intercepted EnvSetter(Local<Name> property,
386419 }
387420
388421 env->env_vars ()->Set (env->isolate (), key, value_string);
422+ if (env->options ()->trace_env ) {
423+ Utf8Value key_utf8 (env->isolate (), key);
424+ Utf8Value value_utf8 (env->isolate (), value_string);
425+ fprintf (stderr,
426+ " [--trace-env] set environment variable \" %.*s\" = \" %.*s\"\n " ,
427+ static_cast <int >(key_utf8.length ()),
428+ key_utf8.out (),
429+ static_cast <int >(value_utf8.length ()),
430+ value_utf8.out ());
431+ PrintTraceEnvStack (env);
432+ }
389433
390434 return Intercepted::kYes ;
391435}
@@ -396,7 +440,18 @@ static Intercepted EnvQuery(Local<Name> property,
396440 CHECK (env->has_run_bootstrapping_code ());
397441 if (property->IsString ()) {
398442 int32_t rc = env->env_vars ()->Query (env->isolate (), property.As <String>());
399- if (rc != -1 ) {
443+ bool has_env = (rc != -1 );
444+
445+ if (env->options ()->trace_env ) {
446+ Utf8Value key_utf8 (env->isolate (), property.As <String>());
447+ fprintf (stderr,
448+ " [--trace-env] query environment variable \" %.*s\" : %s\n " ,
449+ static_cast <int >(key_utf8.length ()),
450+ key_utf8.out (),
451+ has_env ? " is set" : " is not set" );
452+ PrintTraceEnvStack (env);
453+ }
454+ if (has_env) {
400455 // Return attributes for the property.
401456 info.GetReturnValue ().Set (v8::None);
402457 return Intercepted::kYes ;
@@ -411,6 +466,15 @@ static Intercepted EnvDeleter(Local<Name> property,
411466 CHECK (env->has_run_bootstrapping_code ());
412467 if (property->IsString ()) {
413468 env->env_vars ()->Delete (env->isolate (), property.As <String>());
469+
470+ if (env->options ()->trace_env ) {
471+ Utf8Value key_utf8 (env->isolate (), property.As <String>());
472+ fprintf (stderr,
473+ " [--trace-env] delete environment variable \" %.*s\"\n " ,
474+ static_cast <int >(key_utf8.length ()),
475+ key_utf8.out ());
476+ PrintTraceEnvStack (env);
477+ }
414478 }
415479
416480 // process.env never has non-configurable properties, so always
@@ -423,6 +487,12 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
423487 Environment* env = Environment::GetCurrent (info);
424488 CHECK (env->has_run_bootstrapping_code ());
425489
490+ if (env->options ()->trace_env ) {
491+ fprintf (stderr, " [--trace-env] enumerate environment variables\n " );
492+
493+ PrintTraceEnvStack (env);
494+ }
495+
426496 info.GetReturnValue ().Set (
427497 env->env_vars ()->Enumerate (env->isolate ()));
428498}
0 commit comments