Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: pass along errors from object instantiations #25734

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ PromiseWrap* PromiseWrap::New(Environment* env,
Local<Promise> promise,
PromiseWrap* parent_wrap,
bool silent) {
Local<Object> object = env->promise_wrap_template()
->NewInstance(env->context()).ToLocalChecked();
object->SetInternalField(PromiseWrap::kIsChainedPromiseField,
parent_wrap != nullptr ?
v8::True(env->isolate()) :
v8::False(env->isolate()));
Local<Object> obj;
if (!env->promise_wrap_template()->NewInstance(env->context()).ToLocal(&obj))
return nullptr;
obj->SetInternalField(PromiseWrap::kIsChainedPromiseField,
parent_wrap != nullptr ? v8::True(env->isolate())
: v8::False(env->isolate()));
CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr);
promise->SetInternalField(0, object);
return new PromiseWrap(env, object, silent);
promise->SetInternalField(0, obj);
return new PromiseWrap(env, obj, silent);
}

void PromiseWrap::getIsChainedPromise(Local<String> property,
Expand Down Expand Up @@ -242,6 +242,7 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
PromiseWrap* parent_wrap = extractPromiseWrap(parent_promise);
if (parent_wrap == nullptr) {
parent_wrap = PromiseWrap::New(env, parent_promise, nullptr, true);
if (parent_wrap == nullptr) return;
}

AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent_wrap);
Expand All @@ -251,7 +252,8 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
}
}

CHECK_NOT_NULL(wrap);
if (wrap == nullptr) return;

if (type == PromiseHookType::kBefore) {
env->async_hooks()->push_async_ids(
wrap->get_async_id(), wrap->get_trigger_async_id());
Expand Down
7 changes: 4 additions & 3 deletions src/connection_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,

if (status == 0) {
// Instantiate the client javascript object and handle.
Local<Object> client_obj = WrapType::Instantiate(env,
wrap_data,
WrapType::SOCKET);
Local<Object> client_obj;
if (!WrapType::Instantiate(env, wrap_data, WrapType::SOCKET)
.ToLocal(&client_obj))
return;

// Unwrap the client javascript object.
WrapType* wrap;
Expand Down
50 changes: 27 additions & 23 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,10 @@ Environment::~Environment() {
}
}

void Environment::Start(const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
bool start_profiler_idle_notifier) {
void Environment::Start(bool start_profiler_idle_notifier) {
HandleScope handle_scope(isolate());
Context::Scope context_scope(context());

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args)
traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args)
traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
TRACING_CATEGORY_NODE1(environment),
"Environment", this,
"args", std::move(traced_value));
}

CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle()));
uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));

Expand Down Expand Up @@ -346,14 +327,37 @@ void Environment::Start(const std::vector<std::string>& args,
StartProfilerIdleNotifier();
}

Local<Object> process_object = CreateProcessObject(this, args, exec_args);
set_process_object(process_object);

static uv_once_t init_once = UV_ONCE_INIT;
uv_once(&init_once, InitThreadLocalOnce);
uv_key_set(&thread_local_env, this);
}

MaybeLocal<Object> Environment::CreateProcessObject(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this part inside CreateProcessObject? (Or rather why is this method named CreateProcessObject?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung Because it’s the part that handles args and exec_args, that’s pretty much all the reason … also, I called it that because that’s what it mainly does, create + set up the process object?

TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
this,
"args",
std::move(traced_value));
}

Local<Object> process_object =
node::CreateProcessObject(this, args, exec_args)
.FromMaybe(Local<Object>());
set_process_object(process_object);
return process_object;
}

void Environment::RegisterHandleCleanups() {
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
void* arg) {
Expand Down
7 changes: 4 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,10 @@ class Environment {
v8::Local<v8::Context> context);
~Environment();

void Start(const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
bool start_profiler_idle_notifier);
void Start(bool start_profiler_idle_notifier);
v8::MaybeLocal<v8::Object> CreateProcessObject(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args);

typedef void (*HandleCleanupCb)(Environment* env,
uv_handle_t* handle,
Expand Down
6 changes: 4 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,8 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
std::vector<std::string> args(argv, argv + argc);
std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc);
Environment* env = new Environment(isolate_data, context);
env->Start(args, exec_args, per_process::v8_is_profiling);
env->Start(per_process::v8_is_profiling);
env->CreateProcessObject(args, exec_args);
return env;
}

Expand Down Expand Up @@ -1287,7 +1288,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context);
Environment env(isolate_data, context);
env.Start(args, exec_args, per_process::v8_is_profiling);
env.Start(per_process::v8_is_profiling);
env.CreateProcessObject(args, exec_args);

const char* path = args.size() > 1 ? args[1].c_str() : nullptr;
StartInspector(&env, path);
Expand Down
39 changes: 21 additions & 18 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ Local<Name> Uint32ToName(Local<Context> context, uint32_t index) {
ContextifyContext::ContextifyContext(
Environment* env,
Local<Object> sandbox_obj, const ContextOptions& options) : env_(env) {
Local<Context> v8_context = CreateV8Context(env, sandbox_obj, options);
context_.Reset(env->isolate(), v8_context);
MaybeLocal<Context> v8_context = CreateV8Context(env, sandbox_obj, options);

// Allocation failure or maximum call stack size reached
if (context_.IsEmpty())
return;
// Allocation failure, maximum call stack size reached, termination, etc.
if (v8_context.IsEmpty()) return;

context_.Reset(env->isolate(), v8_context.ToLocalChecked());
context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter);
}

Expand All @@ -119,20 +119,19 @@ ContextifyContext::ContextifyContext(
// pass the main JavaScript context object we're embedded in, then the
// NamedPropertyHandler will store a reference to it forever and keep it
// from getting gc'd.
Local<Value> ContextifyContext::CreateDataWrapper(Environment* env) {
EscapableHandleScope scope(env->isolate());
Local<Object> wrapper =
env->script_data_constructor_function()
->NewInstance(env->context()).FromMaybe(Local<Object>());
if (wrapper.IsEmpty())
return scope.Escape(Local<Value>::New(env->isolate(), Local<Value>()));
MaybeLocal<Object> ContextifyContext::CreateDataWrapper(Environment* env) {
Local<Object> wrapper;
if (!env->script_data_constructor_function()
->NewInstance(env->context())
.ToLocal(&wrapper)) {
return MaybeLocal<Object>();
}

wrapper->SetAlignedPointerInInternalField(0, this);
return scope.Escape(wrapper);
return wrapper;
}


Local<Context> ContextifyContext::CreateV8Context(
MaybeLocal<Context> ContextifyContext::CreateV8Context(
Environment* env,
Local<Object> sandbox_obj,
const ContextOptions& options) {
Expand All @@ -145,13 +144,17 @@ Local<Context> ContextifyContext::CreateV8Context(
Local<ObjectTemplate> object_template =
function_template->InstanceTemplate();

Local<Object> data_wrapper;
if (!CreateDataWrapper(env).ToLocal(&data_wrapper))
return MaybeLocal<Context>();

NamedPropertyHandlerConfiguration config(PropertyGetterCallback,
PropertySetterCallback,
PropertyDescriptorCallback,
PropertyDeleterCallback,
PropertyEnumeratorCallback,
PropertyDefinerCallback,
CreateDataWrapper(env));
data_wrapper);

IndexedPropertyHandlerConfiguration indexed_config(
IndexedPropertyGetterCallback,
Expand All @@ -160,7 +163,7 @@ Local<Context> ContextifyContext::CreateV8Context(
IndexedPropertyDeleterCallback,
PropertyEnumeratorCallback,
IndexedPropertyDefinerCallback,
CreateDataWrapper(env));
data_wrapper);

object_template->SetHandler(config);
object_template->SetHandler(indexed_config);
Expand All @@ -169,7 +172,7 @@ Local<Context> ContextifyContext::CreateV8Context(

if (ctx.IsEmpty()) {
env->ThrowError("Could not instantiate context");
return Local<Context>();
return MaybeLocal<Context>();
}

ctx->SetSecurityToken(env->context()->GetSecurityToken());
Expand Down
7 changes: 4 additions & 3 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class ContextifyContext {
v8::Local<v8::Object> sandbox_obj,
const ContextOptions& options);

v8::Local<v8::Value> CreateDataWrapper(Environment* env);
v8::Local<v8::Context> CreateV8Context(Environment* env,
v8::Local<v8::Object> sandbox_obj, const ContextOptions& options);
v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env);
v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env,
v8::Local<v8::Object> sandbox_obj,
const ContextOptions& options);
static void Init(Environment* env, v8::Local<v8::Object> target);

static ContextifyContext* ContextFromContextifiedSandbox(
Expand Down
35 changes: 18 additions & 17 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3327,15 +3327,18 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
return function;
}

Local<Object> KeyObject::Create(Environment* env,
KeyType key_type,
const ManagedEVPPKey& pkey) {
MaybeLocal<Object> KeyObject::Create(Environment* env,
KeyType key_type,
const ManagedEVPPKey& pkey) {
CHECK_NE(key_type, kKeyTypeSecret);
Local<Value> type = Integer::New(env->isolate(), key_type);
Local<Object> obj =
env->crypto_key_object_constructor()->NewInstance(env->context(),
1, &type)
.ToLocalChecked();
Local<Object> obj;
if (!env->crypto_key_object_constructor()
->NewInstance(env->context(), 1, &type)
.ToLocal(&obj)) {
return MaybeLocal<Object>();
}

KeyObject* key = Unwrap<KeyObject>(obj);
CHECK(key);
if (key_type == kKeyTypePublic)
Expand Down Expand Up @@ -5823,24 +5826,22 @@ class GenerateKeyPairJob : public CryptoJob {
if (public_key_encoding_.output_key_object_) {
// Note that this has the downside of containing sensitive data of the
// private key.
*pubkey = KeyObject::Create(env, kKeyTypePublic, pkey_);
if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey))
return false;
} else {
MaybeLocal<Value> maybe_pubkey =
WritePublicKey(env, pkey_.get(), public_key_encoding_);
if (maybe_pubkey.IsEmpty())
if (!WritePublicKey(env, pkey_.get(), public_key_encoding_)
.ToLocal(pubkey))
return false;
*pubkey = maybe_pubkey.ToLocalChecked();
}

// Now do the same for the private key.
if (private_key_encoding_.output_key_object_) {
*privkey = KeyObject::Create(env, kKeyTypePrivate, pkey_);
if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey))
return false;
} else {
MaybeLocal<Value> maybe_privkey =
WritePrivateKey(env, pkey_.get(), private_key_encoding_);
if (maybe_privkey.IsEmpty())
if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_)
.ToLocal(privkey))
return false;
*privkey = maybe_privkey.ToLocalChecked();
}

return true;
Expand Down
6 changes: 3 additions & 3 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ class KeyObject : public BaseObject {
static v8::Local<v8::Function> Initialize(Environment* env,
v8::Local<v8::Object> target);

static v8::Local<v8::Object> Create(Environment* env,
KeyType type,
const ManagedEVPPKey& pkey);
static v8::MaybeLocal<v8::Object> Create(Environment* env,
KeyType type,
const ManagedEVPPKey& pkey);

// TODO(tniessen): track the memory used by OpenSSL types
SET_NO_MEMORY_INFO()
Expand Down
11 changes: 5 additions & 6 deletions src/node_env_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using v8::EscapableHandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Name;
using v8::NamedPropertyHandlerConfiguration;
using v8::NewStringType;
Expand Down Expand Up @@ -209,15 +210,13 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
info.GetReturnValue().Set(envarr);
}

Local<Object> CreateEnvVarProxy(Local<Context> context,
Isolate* isolate,
Local<Value> data) {
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
Isolate* isolate,
Local<Value> data) {
EscapableHandleScope scope(isolate);
Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data));
Local<Object> env_proxy =
env_proxy_template->NewInstance(context).ToLocalChecked();
return scope.Escape(env_proxy);
return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
}
} // namespace node
6 changes: 4 additions & 2 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,8 @@ void Http2Stream::EmitStatistics() {
}
buffer[IDX_STREAM_STATS_SENTBYTES] = entry->sent_bytes();
buffer[IDX_STREAM_STATS_RECEIVEDBYTES] = entry->received_bytes();
entry->Notify(entry->ToObject());
Local<Object> obj;
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj);
}, static_cast<void*>(entry));
}

Expand Down Expand Up @@ -726,7 +727,8 @@ void Http2Session::EmitStatistics() {
buffer[IDX_SESSION_STATS_DATA_RECEIVED] = entry->data_received();
buffer[IDX_SESSION_STATS_MAX_CONCURRENT_STREAMS] =
entry->max_concurrent_streams();
entry->Notify(entry->ToObject());
Local<Object> obj;
if (entry->ToObject().ToLocal(&obj)) entry->Notify(obj);
}, static_cast<void*>(entry));
}

Expand Down
Loading