From fa84e91813492365371318fc317dad1498a295ab Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 8 Dec 2018 22:31:57 +0800 Subject: [PATCH] src: create env->inspector_console_api_object earlier Previously we create env->inspector_console_api_object() when `process.binding('inspector')` is called, which may be too late if the inspector console is used before the first call to `process.binding('inspector')` - that is possible when using `--inspect-brk-node`. Setting a breakpoint and using the inspector console before that would crash the process. This patch moves the initialization of the console API object to the point when Environment is initialized so that `installAdditionalCommandLineAPI()` can be essentially a noop if we use the inspector console before the inspector binding is initialized instead of crashing on an empty object. PR-URL: https://github.com/nodejs/node/pull/24906 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Franziska Hinkelmann --- src/env.cc | 7 +++++++ src/inspector_agent.cc | 1 + src/inspector_js_api.cc | 6 ------ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/env.cc b/src/env.cc index 57c0be960f7784..8f061f9f83f288 100644 --- a/src/env.cc +++ b/src/env.cc @@ -241,6 +241,13 @@ void Environment::Start(const std::vector& args, static uv_once_t init_once = UV_ONCE_INIT; uv_once(&init_once, InitThreadLocalOnce); uv_key_set(&thread_local_env, this); + +#if HAVE_INSPECTOR + // This needs to be set before we start the inspector + Local obj = Object::New(isolate()); + CHECK(obj->SetPrototype(context(), Null(isolate())).FromJust()); + set_inspector_console_api_object(obj); +#endif // HAVE_INSPECTOR } void Environment::RegisterHandleCleanups() { diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index ae266ba86a4a97..47bcf8cbfb1b89 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -505,6 +505,7 @@ class NodeInspectorClient : public V8InspectorClient { void installAdditionalCommandLineAPI(Local context, Local target) override { Local console_api = env_->inspector_console_api_object(); + CHECK(!console_api.IsEmpty()); Local properties = console_api->GetOwnPropertyNames(context).ToLocalChecked(); diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 52f14639a430d3..2e69665e09cd87 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -277,12 +277,6 @@ void Url(const FunctionCallbackInfo& args) { void Initialize(Local target, Local unused, Local context, void* priv) { Environment* env = Environment::GetCurrent(context); - { - auto obj = Object::New(env->isolate()); - auto null = Null(env->isolate()); - CHECK(obj->SetPrototype(context, null).FromJust()); - env->set_inspector_console_api_object(obj); - } Agent* agent = env->inspector_agent(); env->SetMethod(target, "consoleCall", InspectorConsoleCall);