@@ -104,12 +104,12 @@ Local<Name> Uint32ToName(Local<Context> context, uint32_t index) {
104
104
ContextifyContext::ContextifyContext (
105
105
Environment* env,
106
106
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);
109
108
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 ());
113
113
context_.SetWeak (this , WeakCallback, WeakCallbackType::kParameter );
114
114
}
115
115
@@ -119,20 +119,19 @@ ContextifyContext::ContextifyContext(
119
119
// pass the main JavaScript context object we're embedded in, then the
120
120
// NamedPropertyHandler will store a reference to it forever and keep it
121
121
// 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
+ }
129
129
130
130
wrapper->SetAlignedPointerInInternalField (0 , this );
131
- return scope. Escape ( wrapper) ;
131
+ return wrapper;
132
132
}
133
133
134
-
135
- Local<Context> ContextifyContext::CreateV8Context (
134
+ MaybeLocal<Context> ContextifyContext::CreateV8Context (
136
135
Environment* env,
137
136
Local<Object> sandbox_obj,
138
137
const ContextOptions& options) {
@@ -145,13 +144,17 @@ Local<Context> ContextifyContext::CreateV8Context(
145
144
Local<ObjectTemplate> object_template =
146
145
function_template->InstanceTemplate ();
147
146
147
+ Local<Object> data_wrapper;
148
+ if (!CreateDataWrapper (env).ToLocal (&data_wrapper))
149
+ return MaybeLocal<Context>();
150
+
148
151
NamedPropertyHandlerConfiguration config (PropertyGetterCallback,
149
152
PropertySetterCallback,
150
153
PropertyDescriptorCallback,
151
154
PropertyDeleterCallback,
152
155
PropertyEnumeratorCallback,
153
156
PropertyDefinerCallback,
154
- CreateDataWrapper (env) );
157
+ data_wrapper );
155
158
156
159
IndexedPropertyHandlerConfiguration indexed_config (
157
160
IndexedPropertyGetterCallback,
@@ -160,7 +163,7 @@ Local<Context> ContextifyContext::CreateV8Context(
160
163
IndexedPropertyDeleterCallback,
161
164
PropertyEnumeratorCallback,
162
165
IndexedPropertyDefinerCallback,
163
- CreateDataWrapper (env) );
166
+ data_wrapper );
164
167
165
168
object_template->SetHandler (config);
166
169
object_template->SetHandler (indexed_config);
@@ -169,7 +172,7 @@ Local<Context> ContextifyContext::CreateV8Context(
169
172
170
173
if (ctx.IsEmpty ()) {
171
174
env->ThrowError (" Could not instantiate context" );
172
- return Local <Context>();
175
+ return MaybeLocal <Context>();
173
176
}
174
177
175
178
ctx->SetSecurityToken (env->context ()->GetSecurityToken ());
0 commit comments