From 00e71083060981b6577de6fae9909fefe3eea4f5 Mon Sep 17 00:00:00 2001 From: Plamen Dragiyski Date: Mon, 20 Jan 2020 19:56:52 +0100 Subject: [PATCH] embedding: allow creating context within inspectable node::Environment --- src/api/environment.cc | 27 +++++++++++++++++++++++++++ src/node.h | 7 +++++++ 2 files changed, 34 insertions(+) diff --git a/src/api/environment.cc b/src/api/environment.cc index 4999349d282de1..908230b4ed4ffa 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -399,6 +399,33 @@ Local NewContext(Isolate* isolate, return context; } +Local NewContext(Environment* env, + Local object_template, + bool initialize) { + v8::EscapableHandleScope scope(env->isolate()); + auto context = Context::New(env->isolate(), nullptr, object_template); + if (context.IsEmpty()) return context; + + if (initialize && !InitializeContext(context)) { + return Local(); + } + + v8::Local name; + + { + // A consstructor name should be invoked in the newly created context + // to prevent access check failures. + v8::Context::Scope scope(context); + name = context->Global()->GetConstructorName(); + } + + Utf8Value name_val(env->isolate(), name); + ContextInfo info(*name_val); + env->AssignToContext(context, info); + + return scope.Escape(context); +} + // This runs at runtime, regardless of whether the context // is created from a snapshot. void InitializeContextRuntime(Local context) { diff --git a/src/node.h b/src/node.h index 00b0c0ea7e4541..978880d4a8fa07 100644 --- a/src/node.h +++ b/src/node.h @@ -362,6 +362,13 @@ NODE_EXTERN v8::Local NewContext( v8::Local object_template = v8::Local()); +// Create a new context for an existing environment. +// This add several fields to make inspector work properly. +NODE_EXTERN v8::Local NewContext( + Environment* env, + v8::Local object_template, + bool initialize = false); + // Runs Node.js-specific tweaks on an already constructed context // Return value indicates success of operation NODE_EXTERN bool InitializeContext(v8::Local context);