Skip to content

Commit deebf10

Browse files
addaleaxtargos
authored andcommitted
src: pass along errors from vm data wrapper creation
PR-URL: #25734 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 8ee4810 commit deebf10

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/node_contextify.cc

+21-18
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ Local<Name> Uint32ToName(Local<Context> context, uint32_t index) {
104104
ContextifyContext::ContextifyContext(
105105
Environment* env,
106106
Local<Object> sandbox_obj, const ContextOptions& options) : env_(env) {
107-
Local<Context> v8_context = CreateV8Context(env, sandbox_obj, options);
108-
context_.Reset(env->isolate(), v8_context);
107+
MaybeLocal<Context> v8_context = CreateV8Context(env, sandbox_obj, options);
109108

110-
// Allocation failure or maximum call stack size reached
111-
if (context_.IsEmpty())
112-
return;
109+
// Allocation failure, maximum call stack size reached, termination, etc.
110+
if (v8_context.IsEmpty()) return;
111+
112+
context_.Reset(env->isolate(), v8_context.ToLocalChecked());
113113
context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter);
114114
}
115115

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

130130
wrapper->SetAlignedPointerInInternalField(0, this);
131-
return scope.Escape(wrapper);
131+
return wrapper;
132132
}
133133

134-
135-
Local<Context> ContextifyContext::CreateV8Context(
134+
MaybeLocal<Context> ContextifyContext::CreateV8Context(
136135
Environment* env,
137136
Local<Object> sandbox_obj,
138137
const ContextOptions& options) {
@@ -145,13 +144,17 @@ Local<Context> ContextifyContext::CreateV8Context(
145144
Local<ObjectTemplate> object_template =
146145
function_template->InstanceTemplate();
147146

147+
Local<Object> data_wrapper;
148+
if (!CreateDataWrapper(env).ToLocal(&data_wrapper))
149+
return MaybeLocal<Context>();
150+
148151
NamedPropertyHandlerConfiguration config(PropertyGetterCallback,
149152
PropertySetterCallback,
150153
PropertyDescriptorCallback,
151154
PropertyDeleterCallback,
152155
PropertyEnumeratorCallback,
153156
PropertyDefinerCallback,
154-
CreateDataWrapper(env));
157+
data_wrapper);
155158

156159
IndexedPropertyHandlerConfiguration indexed_config(
157160
IndexedPropertyGetterCallback,
@@ -160,7 +163,7 @@ Local<Context> ContextifyContext::CreateV8Context(
160163
IndexedPropertyDeleterCallback,
161164
PropertyEnumeratorCallback,
162165
IndexedPropertyDefinerCallback,
163-
CreateDataWrapper(env));
166+
data_wrapper);
164167

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

170173
if (ctx.IsEmpty()) {
171174
env->ThrowError("Could not instantiate context");
172-
return Local<Context>();
175+
return MaybeLocal<Context>();
173176
}
174177

175178
ctx->SetSecurityToken(env->context()->GetSecurityToken());

src/node_contextify.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ class ContextifyContext {
2323
v8::Local<v8::Object> sandbox_obj,
2424
const ContextOptions& options);
2525

26-
v8::Local<v8::Value> CreateDataWrapper(Environment* env);
27-
v8::Local<v8::Context> CreateV8Context(Environment* env,
28-
v8::Local<v8::Object> sandbox_obj, const ContextOptions& options);
26+
v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env);
27+
v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env,
28+
v8::Local<v8::Object> sandbox_obj,
29+
const ContextOptions& options);
2930
static void Init(Environment* env, v8::Local<v8::Object> target);
3031

3132
static ContextifyContext* ContextFromContextifiedSandbox(

0 commit comments

Comments
 (0)