|
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,66 @@ 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> context_object, |
| 1074 | + const ContextifyOptions& options) { |
| 1075 | + Isolate* isolate = env->isolate(); |
| 1076 | + EscapableHandleScope scope(isolate); |
| 1077 | + std::unique_ptr<v8::MicrotaskQueue> microtask_queue; |
| 1078 | + if (options.microtask_mode() == |
| 1079 | + ContextifyOptions::MicrotaskMode::kAfterEvaluate) { |
| 1080 | + microtask_queue = v8::MicrotaskQueue::New(env->isolate(), |
| 1081 | + v8::MicrotasksPolicy::kExplicit); |
| 1082 | + } |
| 1083 | + |
| 1084 | + contextify::ContextOptions ctxOptions{ |
| 1085 | + .name = options.name(), |
| 1086 | + .origin = options.origin(), |
| 1087 | + .allow_code_gen_strings = |
| 1088 | + Boolean::New(isolate, options.allow_code_gen_strings()), |
| 1089 | + .allow_code_gen_wasm = |
| 1090 | + Boolean::New(isolate, options.allow_code_gen_wasm()), |
| 1091 | + .own_microtask_queue = std::move(microtask_queue), |
| 1092 | + .host_defined_options_id = env->vm_dynamic_import_no_callback(), |
| 1093 | + .vanilla = context_object.IsEmpty(), |
| 1094 | + }; |
| 1095 | + |
| 1096 | + TryCatchScope try_catch(env); |
| 1097 | + contextify::ContextifyContext* context_ptr = |
| 1098 | + contextify::ContextifyContext::New(env, context_object, &ctxOptions); |
| 1099 | + |
| 1100 | + if (try_catch.HasCaught()) { |
| 1101 | + if (!try_catch.HasTerminated()) try_catch.ReThrow(); |
| 1102 | + // Allocation failure, maximum call stack size reached, termination, etc. |
| 1103 | + return {}; |
| 1104 | + } |
| 1105 | + |
| 1106 | + return scope.Escape(context_ptr->context()); |
| 1107 | +} |
| 1108 | + |
| 1109 | +MaybeLocal<Context> GetContextified(Environment* env, |
| 1110 | + Local<Object> context_object) { |
| 1111 | + Isolate* isolate = env->isolate(); |
| 1112 | + EscapableHandleScope scope(isolate); |
| 1113 | + contextify::ContextifyContext* context_ptr = |
| 1114 | + contextify::ContextifyContext::Get(context_object); |
| 1115 | + if (context_ptr == nullptr) { |
| 1116 | + return {}; |
| 1117 | + } |
| 1118 | + return scope.Escape(context_ptr->context()); |
| 1119 | +} |
| 1120 | + |
1060 | 1121 | uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { |
1061 | 1122 | HandleScope handle_scope(isolate); |
1062 | 1123 | Local<Context> context = isolate->GetCurrentContext(); |
|
0 commit comments