Skip to content

Commit

Permalink
embedding: allow creating context within inspectable node::Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragiyski committed Jan 20, 2020
1 parent 3199996 commit 00e7108
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/api/environment.cc
Expand Up @@ -399,6 +399,33 @@ Local<Context> NewContext(Isolate* isolate,
return context;
}

Local<Context> NewContext(Environment* env,
Local<ObjectTemplate> 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<Context>();
}

v8::Local<v8::String> 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> context) {
Expand Down
7 changes: 7 additions & 0 deletions src/node.h
Expand Up @@ -362,6 +362,13 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(
v8::Local<v8::ObjectTemplate> object_template =
v8::Local<v8::ObjectTemplate>());

// Create a new context for an existing environment.
// This add several fields to make inspector work properly.
NODE_EXTERN v8::Local<v8::Context> NewContext(
Environment* env,
v8::Local<v8::ObjectTemplate> 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<v8::Context> context);
Expand Down

0 comments on commit 00e7108

Please sign in to comment.