@@ -151,40 +151,44 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
151
151
Local<Object> sandbox_obj,
152
152
const ContextOptions& options) {
153
153
EscapableHandleScope scope (env->isolate ());
154
- Local<FunctionTemplate> function_template =
155
- FunctionTemplate::New (env->isolate ());
156
-
157
- function_template->SetClassName (sandbox_obj->GetConstructorName ());
158
-
159
- Local<ObjectTemplate> object_template =
160
- function_template->InstanceTemplate ();
161
154
162
- Local<Object> data_wrapper;
163
- if (!CreateDataWrapper (env).ToLocal (&data_wrapper))
164
- return MaybeLocal<Context>();
165
-
166
- NamedPropertyHandlerConfiguration config (
167
- PropertyGetterCallback,
168
- PropertySetterCallback,
169
- PropertyDescriptorCallback,
170
- PropertyDeleterCallback,
171
- PropertyEnumeratorCallback,
172
- PropertyDefinerCallback,
173
- data_wrapper,
174
- PropertyHandlerFlags::kHasNoSideEffect );
175
-
176
- IndexedPropertyHandlerConfiguration indexed_config (
177
- IndexedPropertyGetterCallback,
178
- IndexedPropertySetterCallback,
179
- IndexedPropertyDescriptorCallback,
180
- IndexedPropertyDeleterCallback,
181
- PropertyEnumeratorCallback,
182
- IndexedPropertyDefinerCallback,
183
- data_wrapper,
184
- PropertyHandlerFlags::kHasNoSideEffect );
185
-
186
- object_template->SetHandler (config);
187
- object_template->SetHandler (indexed_config);
155
+ Local<ObjectTemplate> object_template;
156
+
157
+ if (!sandbox_obj.IsEmpty ()) {
158
+ Local<FunctionTemplate> function_template =
159
+ FunctionTemplate::New (env->isolate ());
160
+
161
+ function_template->SetClassName (sandbox_obj->GetConstructorName ());
162
+
163
+ object_template = function_template->InstanceTemplate ();
164
+
165
+ Local<Object> data_wrapper;
166
+ if (!CreateDataWrapper (env).ToLocal (&data_wrapper))
167
+ return MaybeLocal<Context>();
168
+
169
+ NamedPropertyHandlerConfiguration config (
170
+ PropertyGetterCallback,
171
+ PropertySetterCallback,
172
+ PropertyDescriptorCallback,
173
+ PropertyDeleterCallback,
174
+ PropertyEnumeratorCallback,
175
+ PropertyDefinerCallback,
176
+ data_wrapper,
177
+ PropertyHandlerFlags::kHasNoSideEffect );
178
+
179
+ IndexedPropertyHandlerConfiguration indexed_config (
180
+ IndexedPropertyGetterCallback,
181
+ IndexedPropertySetterCallback,
182
+ IndexedPropertyDescriptorCallback,
183
+ IndexedPropertyDeleterCallback,
184
+ PropertyEnumeratorCallback,
185
+ IndexedPropertyDefinerCallback,
186
+ data_wrapper,
187
+ PropertyHandlerFlags::kHasNoSideEffect );
188
+
189
+ object_template->SetHandler (config);
190
+ object_template->SetHandler (indexed_config);
191
+ }
188
192
189
193
Local<Context> ctx = NewContext (env->isolate (), object_template);
190
194
@@ -194,16 +198,18 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
194
198
195
199
ctx->SetSecurityToken (env->context ()->GetSecurityToken ());
196
200
197
- // We need to tie the lifetime of the sandbox object with the lifetime of
198
- // newly created context. We do this by making them hold references to each
199
- // other. The context can directly hold a reference to the sandbox as an
200
- // embedder data field. However, we cannot hold a reference to a v8::Context
201
- // directly in an Object, we instead hold onto the new context's global
202
- // object instead (which then has a reference to the context).
203
- ctx->SetEmbedderData (ContextEmbedderIndex::kSandboxObject , sandbox_obj);
204
- sandbox_obj->SetPrivate (env->context (),
205
- env->contextify_global_private_symbol (),
206
- ctx->Global ());
201
+ if (!sandbox_obj.IsEmpty ()) {
202
+ // We need to tie the lifetime of the sandbox object with the lifetime of
203
+ // newly created context. We do this by making them hold references to each
204
+ // other. The context can directly hold a reference to the sandbox as an
205
+ // embedder data field. However, we cannot hold a reference to a v8::Context
206
+ // directly in an Object, we instead hold onto the new context's global
207
+ // object instead (which then has a reference to the context).
208
+ ctx->SetEmbedderData (ContextEmbedderIndex::kSandboxObject , sandbox_obj);
209
+ sandbox_obj->SetPrivate (env->context (),
210
+ env->contextify_global_private_symbol (),
211
+ ctx->Global ());
212
+ }
207
213
208
214
Utf8Value name_val (env->isolate (), options.name );
209
215
ctx->AllowCodeGenerationFromStrings (options.allow_code_gen_strings ->IsTrue ());
@@ -236,19 +242,23 @@ void ContextifyContext::Init(Environment* env, Local<Object> target) {
236
242
}
237
243
238
244
239
- // makeContext(sandbox, name, origin, strings, wasm);
245
+ // makeContext(sandbox, name, origin, strings, wasm, instance );
240
246
void ContextifyContext::MakeContext (const FunctionCallbackInfo<Value>& args) {
241
247
Environment* env = Environment::GetCurrent (args);
242
248
243
- CHECK_EQ (args.Length (), 5 );
244
- CHECK (args[0 ]->IsObject ());
245
- Local<Object> sandbox = args[0 ].As <Object>();
249
+ CHECK_EQ (args.Length (), 6 );
246
250
247
- // Don't allow contextifying a sandbox multiple times.
248
- CHECK (
249
- !sandbox->HasPrivate (
250
- env->context (),
251
- env->contextify_context_private_symbol ()).FromJust ());
251
+ Local<Object> sandbox;
252
+ if (args[5 ]->IsUndefined ()) {
253
+ CHECK (args[0 ]->IsObject ());
254
+ sandbox = args[0 ].As <Object>();
255
+
256
+ // Don't allow contextifying a sandbox multiple times.
257
+ CHECK (
258
+ !sandbox->HasPrivate (
259
+ env->context (),
260
+ env->contextify_context_private_symbol ()).FromJust ());
261
+ }
252
262
253
263
ContextOptions options;
254
264
@@ -278,7 +288,15 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
278
288
if (context_ptr->context ().IsEmpty ())
279
289
return ;
280
290
281
- sandbox->SetPrivate (
291
+ Local<Object> instance;
292
+ if (args[5 ]->IsUndefined ()) {
293
+ instance = sandbox;
294
+ } else {
295
+ CHECK (args[5 ]->IsObject ());
296
+ args.GetReturnValue ().Set (context_ptr->global_proxy ());
297
+ instance = args[5 ].As <Object>();
298
+ }
299
+ instance->SetPrivate (
282
300
env->context (),
283
301
env->contextify_context_private_symbol (),
284
302
External::New (env->isolate (), context_ptr.release ()));
0 commit comments