@@ -131,81 +131,7 @@ BaseObjectPtr<ContextifyContext> ContextifyContext::New(
131131 // Allocation failure, maximum call stack size reached, termination, etc.
132132 return BaseObjectPtr<ContextifyContext>();
133133 }
134-
135- // This only initializes part of the context. The primordials are
136- // only initilaized when needed because even deserializing them slows
137- // things down significantly and they are only needed in rare occasions
138- // in the vm contexts.
139- if (InitializeContextRuntime (v8_context).IsNothing ()) {
140- return BaseObjectPtr<ContextifyContext>();
141- }
142-
143- Local<Context> main_context = env->context ();
144- Local<Object> new_context_global = v8_context->Global ();
145- v8_context->SetSecurityToken (main_context->GetSecurityToken ());
146-
147- // We need to tie the lifetime of the sandbox object with the lifetime of
148- // newly created context. We do this by making them hold references to each
149- // other. The context can directly hold a reference to the sandbox as an
150- // embedder data field. The sandbox uses a private symbol to hold a reference
151- // to the ContextifyContext wrapper which in turn internally references
152- // the context from its constructor.
153- v8_context->SetEmbedderData (ContextEmbedderIndex::kSandboxObject ,
154- sandbox_obj);
155-
156- // Delegate the code generation validation to
157- // node::ModifyCodeGenerationFromStrings.
158- v8_context->AllowCodeGenerationFromStrings (false );
159- v8_context->SetEmbedderData (
160- ContextEmbedderIndex::kAllowCodeGenerationFromStrings ,
161- options.allow_code_gen_strings );
162- v8_context->SetEmbedderData (ContextEmbedderIndex::kAllowWasmCodeGeneration ,
163- options.allow_code_gen_wasm );
164-
165- Utf8Value name_val (env->isolate (), options.name );
166- ContextInfo info (*name_val);
167- if (!options.origin .IsEmpty ()) {
168- Utf8Value origin_val (env->isolate (), options.origin );
169- info.origin = *origin_val;
170- }
171-
172- BaseObjectPtr<ContextifyContext> result;
173- Local<Object> wrapper;
174- {
175- Context::Scope context_scope (v8_context);
176- Local<String> ctor_name = sandbox_obj->GetConstructorName ();
177- if (!ctor_name->Equals (v8_context, env->object_string ()).FromMaybe (false ) &&
178- new_context_global
179- ->DefineOwnProperty (
180- v8_context,
181- v8::Symbol::GetToStringTag (env->isolate ()),
182- ctor_name,
183- static_cast <v8::PropertyAttribute>(v8::DontEnum))
184- .IsNothing ()) {
185- return BaseObjectPtr<ContextifyContext>();
186- }
187- env->AssignToContext (v8_context, nullptr , info);
188-
189- if (!env->contextify_wrapper_template ()
190- ->NewInstance (v8_context)
191- .ToLocal (&wrapper)) {
192- return BaseObjectPtr<ContextifyContext>();
193- }
194-
195- result =
196- MakeBaseObject<ContextifyContext>(env, wrapper, v8_context, options);
197- // The only strong reference to the wrapper will come from the sandbox.
198- result->MakeWeak ();
199- }
200-
201- if (sandbox_obj
202- ->SetPrivate (
203- v8_context, env->contextify_context_private_symbol (), wrapper)
204- .IsNothing ()) {
205- return BaseObjectPtr<ContextifyContext>();
206- }
207-
208- return result;
134+ return New (v8_context, env, sandbox_obj, options);
209135}
210136
211137void ContextifyContext::MemoryInfo (MemoryTracker* tracker) const {
@@ -316,6 +242,89 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
316242 return scope.Escape (ctx);
317243}
318244
245+
246+ BaseObjectPtr<ContextifyContext> ContextifyContext::New (
247+ Local<Context> v8_context,
248+ Environment* env,
249+ Local<Object> sandbox_obj,
250+ const ContextOptions& options) {
251+ HandleScope scope (env->isolate ());
252+ // This only initializes part of the context. The primordials are
253+ // only initilaized when needed because even deserializing them slows
254+ // things down significantly and they are only needed in rare occasions
255+ // in the vm contexts.
256+ if (InitializeContextRuntime (v8_context).IsNothing ()) {
257+ return BaseObjectPtr<ContextifyContext>();
258+ }
259+
260+ Local<Context> main_context = env->context ();
261+ Local<Object> new_context_global = v8_context->Global ();
262+ v8_context->SetSecurityToken (main_context->GetSecurityToken ());
263+
264+ // We need to tie the lifetime of the sandbox object with the lifetime of
265+ // newly created context. We do this by making them hold references to each
266+ // other. The context can directly hold a reference to the sandbox as an
267+ // embedder data field. The sandbox uses a private symbol to hold a reference
268+ // to the ContextifyContext wrapper which in turn internally references
269+ // the context from its constructor.
270+ v8_context->SetEmbedderData (ContextEmbedderIndex::kSandboxObject ,
271+ sandbox_obj);
272+
273+ // Delegate the code generation validation to
274+ // node::ModifyCodeGenerationFromStrings.
275+ v8_context->AllowCodeGenerationFromStrings (false );
276+ v8_context->SetEmbedderData (
277+ ContextEmbedderIndex::kAllowCodeGenerationFromStrings ,
278+ options.allow_code_gen_strings );
279+ v8_context->SetEmbedderData (ContextEmbedderIndex::kAllowWasmCodeGeneration ,
280+ options.allow_code_gen_wasm );
281+
282+ Utf8Value name_val (env->isolate (), options.name );
283+ ContextInfo info (*name_val);
284+ if (!options.origin .IsEmpty ()) {
285+ Utf8Value origin_val (env->isolate (), options.origin );
286+ info.origin = *origin_val;
287+ }
288+
289+ BaseObjectPtr<ContextifyContext> result;
290+ Local<Object> wrapper;
291+ {
292+ Context::Scope context_scope (v8_context);
293+ Local<String> ctor_name = sandbox_obj->GetConstructorName ();
294+ if (!ctor_name->Equals (v8_context, env->object_string ()).FromMaybe (false ) &&
295+ new_context_global
296+ ->DefineOwnProperty (
297+ v8_context,
298+ v8::Symbol::GetToStringTag (env->isolate ()),
299+ ctor_name,
300+ static_cast <v8::PropertyAttribute>(v8::DontEnum))
301+ .IsNothing ()) {
302+ return BaseObjectPtr<ContextifyContext>();
303+ }
304+ env->AssignToContext (v8_context, nullptr , info);
305+
306+ if (!env->contextify_wrapper_template ()
307+ ->NewInstance (v8_context)
308+ .ToLocal (&wrapper)) {
309+ return BaseObjectPtr<ContextifyContext>();
310+ }
311+
312+ result =
313+ MakeBaseObject<ContextifyContext>(env, wrapper, v8_context, options);
314+ // The only strong reference to the wrapper will come from the sandbox.
315+ result->MakeWeak ();
316+ }
317+
318+ if (sandbox_obj
319+ ->SetPrivate (
320+ v8_context, env->contextify_context_private_symbol (), wrapper)
321+ .IsNothing ()) {
322+ return BaseObjectPtr<ContextifyContext>();
323+ }
324+
325+ return result;
326+ }
327+
319328void ContextifyContext::Init (Environment* env, Local<Object> target) {
320329 Local<Context> context = env->context ();
321330 SetMethod (context, target, " makeContext" , MakeContext);
@@ -387,12 +396,6 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
387396 try_catch.ReThrow ();
388397 return ;
389398 }
390-
391- if (context_ptr.get () == nullptr ) {
392- return ;
393- }
394- Local<Context> new_context = context_ptr->context ();
395- if (new_context.IsEmpty ()) return ;
396399}
397400
398401
0 commit comments