|
6 | 6 | #include "node.h" |
7 | 7 | #include "node_builtins.h" |
8 | 8 | #include "node_context_data.h" |
| 9 | +#include "node_contextify.h" |
9 | 10 | #include "node_debug.h" |
10 | 11 | #include "node_errors.h" |
11 | 12 | #include "node_exit_code.h" |
@@ -1057,6 +1058,53 @@ Maybe<bool> InitializeContext(Local<Context> context) { |
1057 | 1058 | return Just(true); |
1058 | 1059 | } |
1059 | 1060 |
|
| 1061 | +ContextifyOptions::ContextifyOptions(Local<String> name, |
| 1062 | + Local<String> origin, |
| 1063 | + bool allow_code_gen_strings, |
| 1064 | + bool allow_code_gen_wasm, |
| 1065 | + MicrotaskMode microtask_mode) |
| 1066 | + : name_(name), |
| 1067 | + origin_(origin), |
| 1068 | + allow_code_gen_strings_(allow_code_gen_strings), |
| 1069 | + allow_code_gen_wasm_(allow_code_gen_wasm), |
| 1070 | + microtask_mode_(microtask_mode) {} |
| 1071 | + |
| 1072 | +MaybeLocal<Context> MakeContextify(Environment* env, |
| 1073 | + Local<Object> contextObject, |
| 1074 | + ContextifyOptions options) { |
| 1075 | + Isolate* isolate = env->isolate(); |
| 1076 | + std::unique_ptr<v8::MicrotaskQueue> microtask_queue; |
| 1077 | + if (options.microtask_mode() == |
| 1078 | + ContextifyOptions::MicrotaskMode::kAfterEvaluate) { |
| 1079 | + microtask_queue = v8::MicrotaskQueue::New(env->isolate(), |
| 1080 | + v8::MicrotasksPolicy::kExplicit); |
| 1081 | + } |
| 1082 | + |
| 1083 | + contextify::ContextOptions ctxOptions{ |
| 1084 | + .name = options.name(), |
| 1085 | + .origin = options.origin(), |
| 1086 | + .allow_code_gen_strings = |
| 1087 | + Boolean::New(isolate, options.allow_code_gen_strings()), |
| 1088 | + .allow_code_gen_wasm = |
| 1089 | + Boolean::New(isolate, options.allow_code_gen_wasm()), |
| 1090 | + .own_microtask_queue = std::move(microtask_queue), |
| 1091 | + .host_defined_options_id = env->vm_dynamic_import_no_callback(), |
| 1092 | + .vanilla = contextObject.IsEmpty(), |
| 1093 | + }; |
| 1094 | + |
| 1095 | + TryCatchScope try_catch(env); |
| 1096 | + contextify::ContextifyContext* context_ptr = |
| 1097 | + contextify::ContextifyContext::New(env, contextObject, &ctxOptions); |
| 1098 | + |
| 1099 | + if (try_catch.HasCaught()) { |
| 1100 | + if (!try_catch.HasTerminated()) try_catch.ReThrow(); |
| 1101 | + // Allocation failure, maximum call stack size reached, termination, etc. |
| 1102 | + return {}; |
| 1103 | + } |
| 1104 | + |
| 1105 | + return context_ptr->context(); |
| 1106 | +} |
| 1107 | + |
1060 | 1108 | uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { |
1061 | 1109 | HandleScope handle_scope(isolate); |
1062 | 1110 | Local<Context> context = isolate->GetCurrentContext(); |
|
0 commit comments