Skip to content

Commit 8069174

Browse files
committed
src: Handle NULL env scenario
Convert hard assertion into a throw with a useful error message in src/module_wrap.cc
1 parent 757e203 commit 8069174

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,13 @@ provided.
889889
Encoding provided to `TextDecoder()` API was not one of the
890890
[WHATWG Supported Encodings][].
891891

892+
<a id="ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE"></a>
893+
### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE`
894+
895+
The JS execution context is not associated with a Node.js environment.
896+
This may occur when Node.js is used as an embedded library and some hooks
897+
for the JS engine are not set up properly.
898+
892899
<a id="ERR_FALSY_VALUE_REJECTION"></a>
893900
### `ERR_FALSY_VALUE_REJECTION`
894901

src/module_wrap.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,12 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
451451
Local<String> specifier,
452452
Local<Module> referrer) {
453453
Environment* env = Environment::GetCurrent(context);
454-
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here.
454+
if (env == nullptr) {
455+
Isolate* isolate = context->GetIsolate();
456+
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(isolate);
457+
return MaybeLocal<Module>();
458+
}
459+
455460
Isolate* isolate = env->isolate();
456461

457462
ModuleWrap* dependent = GetFromModule(env, referrer);
@@ -1443,7 +1448,11 @@ static MaybeLocal<Promise> ImportModuleDynamically(
14431448
Local<String> specifier) {
14441449
Isolate* iso = context->GetIsolate();
14451450
Environment* env = Environment::GetCurrent(context);
1446-
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here.
1451+
if (env == nullptr) {
1452+
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(iso);
1453+
return MaybeLocal<Promise>();
1454+
}
1455+
14471456
EscapableHandleScope handle_scope(iso);
14481457

14491458
Local<Function> import_callback =

src/node_errors.h

+25-22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void OnFatalError(const char* location, const char* message);
3939
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \
4040
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \
4141
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
42+
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
4243
V(ERR_INVALID_ARG_VALUE, TypeError) \
4344
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
4445
V(ERR_INVALID_ARG_TYPE, TypeError) \
@@ -86,28 +87,30 @@ void OnFatalError(const char* location, const char* message);
8687

8788
// Errors with predefined static messages
8889

89-
#define PREDEFINED_ERROR_MESSAGES(V) \
90-
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
91-
"Buffer is not available for the current Context") \
92-
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
93-
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
94-
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
95-
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
96-
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
97-
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
98-
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
99-
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \
100-
"MessagePort was found in message but not listed in transferList") \
101-
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
102-
"The V8 platform used by this instance of Node does not support " \
103-
"creating Workers") \
104-
V(ERR_NON_CONTEXT_AWARE_DISABLED, \
105-
"Loading non context-aware native modules has been disabled") \
106-
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
107-
"Script execution was interrupted by `SIGINT`") \
108-
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \
109-
"Cannot serialize externalized SharedArrayBuffer") \
110-
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \
90+
#define PREDEFINED_ERROR_MESSAGES(V) \
91+
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
92+
"Buffer is not available for the current Context") \
93+
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
94+
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
95+
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
96+
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
97+
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
98+
"Context not associated with Node.js environment") \
99+
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
100+
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
101+
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
102+
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \
103+
"MessagePort was found in message but not listed in transferList") \
104+
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
105+
"The V8 platform used by this instance of Node does not support " \
106+
"creating Workers") \
107+
V(ERR_NON_CONTEXT_AWARE_DISABLED, \
108+
"Loading non context-aware native modules has been disabled") \
109+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
110+
"Script execution was interrupted by `SIGINT`") \
111+
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \
112+
"Cannot serialize externalized SharedArrayBuffer") \
113+
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint")
111114

112115
#define V(code, message) \
113116
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \

0 commit comments

Comments
 (0)