25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 43 #include <unordered_map> 57 #ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX 58 #define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32 63 #ifndef NODE_PUSH_VAL_TO_ARRAY_MAX 64 #define NODE_PUSH_VAL_TO_ARRAY_MAX 8 81 #define PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) \ 82 V(alpn_buffer_private_symbol, "node:alpnBuffer") \ 83 V(arrow_message_private_symbol, "node:arrowMessage") \ 84 V(contextify_context_private_symbol, "node:contextify:context") \ 85 V(contextify_global_private_symbol, "node:contextify:global") \ 86 V(inspector_delegate_private_symbol, "node:inspector:delegate") \ 87 V(decorated_private_symbol, "node:decorated") \ 88 V(npn_buffer_private_symbol, "node:npnBuffer") \ 89 V(processed_private_symbol, "node:processed") \ 90 V(selected_npn_buffer_private_symbol, "node:selectedNpnBuffer") \ 94 #define PER_ISOLATE_STRING_PROPERTIES(V) \ 95 V(address_string, "address") \ 96 V(args_string, "args") \ 98 V(buffer_string, "buffer") \ 99 V(bytes_string, "bytes") \ 100 V(bytes_parsed_string, "bytesParsed") \ 101 V(bytes_read_string, "bytesRead") \ 102 V(cached_data_string, "cachedData") \ 103 V(cached_data_produced_string, "cachedDataProduced") \ 104 V(cached_data_rejected_string, "cachedDataRejected") \ 105 V(callback_string, "callback") \ 106 V(change_string, "change") \ 107 V(channel_string, "channel") \ 108 V(constants_string, "constants") \ 109 V(oncertcb_string, "oncertcb") \ 110 V(onclose_string, "_onclose") \ 111 V(code_string, "code") \ 112 V(configurable_string, "configurable") \ 113 V(cwd_string, "cwd") \ 114 V(dest_string, "dest") \ 115 V(destroy_string, "destroy") \ 116 V(detached_string, "detached") \ 117 V(disposed_string, "_disposed") \ 118 V(dns_a_string, "A") \ 119 V(dns_aaaa_string, "AAAA") \ 120 V(dns_cname_string, "CNAME") \ 121 V(dns_mx_string, "MX") \ 122 V(dns_naptr_string, "NAPTR") \ 123 V(dns_ns_string, "NS") \ 124 V(dns_ptr_string, "PTR") \ 125 V(dns_soa_string, "SOA") \ 126 V(dns_srv_string, "SRV") \ 127 V(dns_txt_string, "TXT") \ 128 V(domain_string, "domain") \ 129 V(emit_string, "emit") \ 130 V(emitting_top_level_domain_error_string, "_emittingTopLevelDomainError") \ 131 V(exchange_string, "exchange") \ 132 V(enumerable_string, "enumerable") \ 133 V(idle_string, "idle") \ 134 V(irq_string, "irq") \ 135 V(enablepush_string, "enablePush") \ 136 V(encoding_string, "encoding") \ 137 V(enter_string, "enter") \ 138 V(entries_string, "entries") \ 139 V(env_pairs_string, "envPairs") \ 140 V(errno_string, "errno") \ 141 V(error_string, "error") \ 142 V(events_string, "_events") \ 143 V(exiting_string, "_exiting") \ 144 V(exit_code_string, "exitCode") \ 145 V(exit_string, "exit") \ 146 V(expire_string, "expire") \ 147 V(exponent_string, "exponent") \ 148 V(exports_string, "exports") \ 149 V(ext_key_usage_string, "ext_key_usage") \ 150 V(external_stream_string, "_externalStream") \ 151 V(family_string, "family") \ 152 V(fatal_exception_string, "_fatalException") \ 154 V(file_string, "file") \ 155 V(fingerprint_string, "fingerprint") \ 156 V(flags_string, "flags") \ 157 V(get_string, "get") \ 158 V(get_data_clone_error_string, "_getDataCloneError") \ 159 V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId") \ 160 V(gid_string, "gid") \ 161 V(handle_string, "handle") \ 162 V(heap_total_string, "heapTotal") \ 163 V(heap_used_string, "heapUsed") \ 164 V(homedir_string, "homedir") \ 165 V(hostmaster_string, "hostmaster") \ 167 V(ignore_string, "ignore") \ 168 V(immediate_callback_string, "_immediateCallback") \ 169 V(infoaccess_string, "infoAccess") \ 170 V(inherit_string, "inherit") \ 171 V(input_string, "input") \ 172 V(internal_string, "internal") \ 173 V(ipv4_string, "IPv4") \ 174 V(ipv6_string, "IPv6") \ 175 V(isalive_string, "isAlive") \ 176 V(isclosing_string, "isClosing") \ 177 V(issuer_string, "issuer") \ 178 V(issuercert_string, "issuerCertificate") \ 179 V(kill_signal_string, "killSignal") \ 180 V(length_string, "length") \ 181 V(mac_string, "mac") \ 182 V(max_buffer_string, "maxBuffer") \ 183 V(message_string, "message") \ 184 V(minttl_string, "minttl") \ 185 V(model_string, "model") \ 186 V(modulus_string, "modulus") \ 187 V(name_string, "name") \ 188 V(netmask_string, "netmask") \ 189 V(nice_string, "nice") \ 190 V(nsname_string, "nsname") \ 191 V(nexttick_string, "nextTick") \ 192 V(ocsp_request_string, "OCSPRequest") \ 193 V(onchange_string, "onchange") \ 194 V(onclienthello_string, "onclienthello") \ 195 V(oncomplete_string, "oncomplete") \ 196 V(onconnection_string, "onconnection") \ 197 V(ondone_string, "ondone") \ 198 V(onerror_string, "onerror") \ 199 V(onexit_string, "onexit") \ 200 V(onframeerror_string, "onframeerror") \ 201 V(ongetpadding_string, "ongetpadding") \ 202 V(onhandshakedone_string, "onhandshakedone") \ 203 V(onhandshakestart_string, "onhandshakestart") \ 204 V(onheaders_string, "onheaders") \ 205 V(onmessage_string, "onmessage") \ 206 V(onnewsession_string, "onnewsession") \ 207 V(onnewsessiondone_string, "onnewsessiondone") \ 208 V(onocspresponse_string, "onocspresponse") \ 209 V(ongoawaydata_string, "ongoawaydata") \ 210 V(onpriority_string, "onpriority") \ 211 V(onread_string, "onread") \ 212 V(onreadstart_string, "onreadstart") \ 213 V(onreadstop_string, "onreadstop") \ 214 V(onselect_string, "onselect") \ 215 V(onsettings_string, "onsettings") \ 216 V(onshutdown_string, "onshutdown") \ 217 V(onsignal_string, "onsignal") \ 218 V(onstop_string, "onstop") \ 219 V(onstreamclose_string, "onstreamclose") \ 220 V(ontrailers_string, "ontrailers") \ 221 V(onwrite_string, "onwrite") \ 222 V(output_string, "output") \ 223 V(order_string, "order") \ 224 V(owner_string, "owner") \ 225 V(parse_error_string, "Parse Error") \ 226 V(path_string, "path") \ 227 V(pbkdf2_error_string, "PBKDF2 Error") \ 228 V(pid_string, "pid") \ 229 V(pipe_string, "pipe") \ 230 V(port_string, "port") \ 231 V(preference_string, "preference") \ 232 V(priority_string, "priority") \ 233 V(produce_cached_data_string, "produceCachedData") \ 234 V(raw_string, "raw") \ 235 V(read_host_object_string, "_readHostObject") \ 236 V(readable_string, "readable") \ 237 V(received_shutdown_string, "receivedShutdown") \ 238 V(refresh_string, "refresh") \ 239 V(regexp_string, "regexp") \ 240 V(rename_string, "rename") \ 241 V(replacement_string, "replacement") \ 242 V(retry_string, "retry") \ 243 V(serial_string, "serial") \ 244 V(scopeid_string, "scopeid") \ 245 V(sent_shutdown_string, "sentShutdown") \ 246 V(serial_number_string, "serialNumber") \ 247 V(service_string, "service") \ 248 V(servername_string, "servername") \ 249 V(session_id_string, "sessionId") \ 250 V(set_string, "set") \ 251 V(shell_string, "shell") \ 252 V(signal_string, "signal") \ 253 V(size_string, "size") \ 254 V(sni_context_err_string, "Invalid SNI context") \ 255 V(sni_context_string, "sni_context") \ 256 V(speed_string, "speed") \ 257 V(stack_string, "stack") \ 258 V(status_string, "status") \ 259 V(stdio_string, "stdio") \ 260 V(stream_string, "stream") \ 261 V(subject_string, "subject") \ 262 V(subjectaltname_string, "subjectaltname") \ 263 V(sys_string, "sys") \ 264 V(syscall_string, "syscall") \ 265 V(tick_callback_string, "_tickCallback") \ 266 V(tick_domain_cb_string, "_tickDomainCallback") \ 267 V(ticketkeycallback_string, "onticketkeycallback") \ 268 V(timeout_string, "timeout") \ 269 V(times_string, "times") \ 270 V(tls_ticket_string, "tlsTicket") \ 271 V(ttl_string, "ttl") \ 272 V(type_string, "type") \ 273 V(uid_string, "uid") \ 274 V(unknown_string, "<unknown>") \ 275 V(user_string, "user") \ 276 V(username_string, "username") \ 277 V(valid_from_string, "valid_from") \ 278 V(valid_to_string, "valid_to") \ 279 V(value_string, "value") \ 280 V(verify_error_string, "verifyError") \ 281 V(version_string, "version") \ 282 V(weight_string, "weight") \ 283 V(windows_verbatim_arguments_string, "windowsVerbatimArguments") \ 284 V(wrap_string, "wrap") \ 285 V(writable_string, "writable") \ 286 V(write_host_object_string, "_writeHostObject") \ 287 V(write_queue_size_string, "writeQueueSize") \ 288 V(x_forwarded_string, "x-forwarded-for") \ 289 V(zero_return_string, "ZERO_RETURN") 291 #define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \ 292 V(as_external, v8::External) \ 293 V(async_hooks_destroy_function, v8::Function) \ 294 V(async_hooks_init_function, v8::Function) \ 295 V(async_hooks_before_function, v8::Function) \ 296 V(async_hooks_after_function, v8::Function) \ 297 V(binding_cache_object, v8::Object) \ 298 V(buffer_prototype_object, v8::Object) \ 299 V(context, v8::Context) \ 300 V(domain_array, v8::Array) \ 301 V(domains_stack_array, v8::Array) \ 302 V(inspector_console_api_object, v8::Object) \ 303 V(module_load_list_array, v8::Array) \ 304 V(pbkdf2_constructor_template, v8::ObjectTemplate) \ 305 V(pipe_constructor_template, v8::FunctionTemplate) \ 306 V(performance_entry_callback, v8::Function) \ 307 V(performance_entry_template, v8::Function) \ 308 V(process_object, v8::Object) \ 309 V(promise_reject_function, v8::Function) \ 310 V(promise_wrap_template, v8::ObjectTemplate) \ 311 V(push_values_to_array_function, v8::Function) \ 312 V(randombytes_constructor_template, v8::ObjectTemplate) \ 313 V(script_context_constructor_template, v8::FunctionTemplate) \ 314 V(script_data_constructor_function, v8::Function) \ 315 V(secure_context_constructor_template, v8::FunctionTemplate) \ 316 V(tcp_constructor_template, v8::FunctionTemplate) \ 317 V(tick_callback_function, v8::Function) \ 318 V(tls_wrap_constructor_function, v8::Function) \ 319 V(tty_constructor_template, v8::FunctionTemplate) \ 320 V(udp_constructor_function, v8::Function) \ 321 V(url_constructor_function, v8::Function) \ 322 V(write_wrap_constructor_function, v8::Function) \ 326 struct node_async_ids {
333 inline IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop,
334 uint32_t* zero_fill_field =
nullptr);
335 inline uv_loop_t* event_loop()
const;
336 inline uint32_t* zero_fill_field()
const;
338 #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) 339 #define VS(PropertyName, StringValue) V(v8::String, PropertyName) 340 #define V(TypeName, PropertyName) \ 341 inline v8::Local<TypeName> PropertyName(v8::Isolate* isolate) const; 342 PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
343 PER_ISOLATE_STRING_PROPERTIES(VS)
348 std::unordered_map<nghttp2_rcbuf*, v8::Eternal<v8::String>> http2_static_strs;
351 #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) 352 #define VS(PropertyName, StringValue) V(v8::String, PropertyName) 353 #define V(TypeName, PropertyName) \ 354 v8::Eternal<TypeName> PropertyName ## _; 355 PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
356 PER_ISOLATE_STRING_PROPERTIES(VS)
361 uv_loop_t*
const event_loop_;
362 uint32_t*
const zero_fill_field_;
364 DISALLOW_COPY_AND_ASSIGN(IsolateData);
392 inline uint32_t* fields();
393 inline int fields_count()
const;
394 inline double* uid_fields();
395 inline int uid_fields_count()
const;
396 inline v8::Local<v8::String> provider_string(
int idx);
398 inline void push_ids(
double async_id,
double trigger_id);
399 inline bool pop_ids(
double async_id);
400 inline size_t stack_size();
401 inline void clear_id_stack();
408 InitScope() =
delete;
409 explicit InitScope(Environment* env,
double init_trigger_id);
414 double* uid_fields_ref_;
416 DISALLOW_COPY_AND_ASSIGN(InitScope);
423 ExecScope() =
delete;
424 explicit ExecScope(Environment* env,
double async_id,
double trigger_id);
435 DISALLOW_COPY_AND_ASSIGN(ExecScope);
439 friend class Environment;
440 inline explicit AsyncHooks(v8::Isolate* isolate);
442 v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
444 v8::Isolate* isolate_;
446 std::stack<struct node_async_ids> ids_stack_;
449 uint32_t
fields_[kFieldsCount];
451 double uid_fields_[kUidFieldsCount];
456 class AsyncCallbackScope {
458 AsyncCallbackScope() =
delete;
459 explicit AsyncCallbackScope(Environment* env);
460 ~AsyncCallbackScope();
461 inline bool in_makecallback();
466 DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope);
471 inline uint32_t* fields();
472 inline int fields_count()
const;
473 inline uint32_t count()
const;
476 friend class Environment;
484 uint32_t
fields_[kFieldsCount];
486 DISALLOW_COPY_AND_ASSIGN(DomainFlag);
491 inline uint32_t* fields();
492 inline int fields_count()
const;
493 inline uint32_t index()
const;
494 inline uint32_t length()
const;
495 inline void set_index(uint32_t value);
498 friend class Environment;
507 uint32_t
fields_[kFieldsCount];
509 DISALLOW_COPY_AND_ASSIGN(TickInfo);
512 typedef void (*HandleCleanupCb)(Environment* env,
516 class HandleCleanup {
518 friend class Environment;
520 HandleCleanup(uv_handle_t* handle, HandleCleanupCb cb,
void* arg)
526 uv_handle_t* handle_;
529 ListNode<HandleCleanup> handle_cleanup_queue_;
532 static inline Environment* GetCurrent(v8::Isolate* isolate);
533 static inline Environment* GetCurrent(v8::Local<v8::Context> context);
534 static inline Environment* GetCurrent(
535 const v8::FunctionCallbackInfo<v8::Value>& info);
537 template <
typename T>
538 static inline Environment* GetCurrent(
539 const v8::PropertyCallbackInfo<T>& info);
541 inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context);
542 inline ~Environment();
545 const char*
const* argv,
547 const char*
const* exec_argv,
548 bool start_profiler_idle_notifier);
549 void AssignToContext(v8::Local<v8::Context> context);
550 void CleanupHandles();
552 void StartProfilerIdleNotifier();
553 void StopProfilerIdleNotifier();
555 inline v8::Isolate* isolate()
const;
556 inline uv_loop_t* event_loop()
const;
557 inline bool in_domain()
const;
558 inline uint32_t watched_providers()
const;
560 static inline Environment* from_immediate_check_handle(uv_check_t* handle);
561 static inline Environment* from_destroy_ids_timer_handle(uv_timer_t* handle);
562 inline uv_check_t* immediate_check_handle();
563 inline uv_idle_t* immediate_idle_handle();
564 inline uv_timer_t* destroy_ids_timer_handle();
567 inline void RegisterHandleCleanup(uv_handle_t* handle,
570 inline void FinishHandleCleanup(uv_handle_t* handle);
573 inline DomainFlag* domain_flag();
574 inline TickInfo* tick_info();
575 inline uint64_t timer_base()
const;
577 inline IsolateData* isolate_data()
const;
579 inline bool using_domains()
const;
580 inline void set_using_domains(
bool value);
582 inline bool printed_error()
const;
583 inline void set_printed_error(
bool value);
585 void PrintSyncTrace()
const;
586 inline void set_trace_sync_io(
bool value);
588 inline bool abort_on_uncaught_exception()
const;
589 inline void set_abort_on_uncaught_exception(
bool value);
592 inline double new_async_id();
593 inline double current_async_id();
594 inline double trigger_id();
595 inline double get_init_trigger_id();
596 inline void set_init_trigger_id(
const double id);
599 inline std::vector<double>* destroy_ids_list();
601 inline double* heap_statistics_buffer()
const;
602 inline void set_heap_statistics_buffer(
double* pointer);
604 inline double* heap_space_statistics_buffer()
const;
605 inline void set_heap_space_statistics_buffer(
double* pointer);
607 inline char* http_parser_buffer()
const;
608 inline void set_http_parser_buffer(
char* buffer);
610 inline http2::http2_state* http2_state_buffer()
const;
611 inline void set_http2_state_buffer(http2::http2_state* buffer);
613 inline double* fs_stats_field_array()
const;
614 inline void set_fs_stats_field_array(
double* fields);
616 inline performance::performance_state* performance_state();
617 inline std::map<std::string, uint64_t>* performance_marks();
619 static inline Environment* from_performance_check_handle(uv_check_t* handle);
620 static inline Environment* from_performance_idle_handle(uv_idle_t* handle);
621 static inline Environment* from_performance_prepare_handle(
622 uv_prepare_t* handle);
623 inline uv_check_t* performance_check_handle();
624 inline uv_idle_t* performance_idle_handle();
625 inline uv_prepare_t* performance_prepare_handle();
627 inline void ThrowError(
const char* errmsg);
628 inline void ThrowTypeError(
const char* errmsg);
629 inline void ThrowRangeError(
const char* errmsg);
630 inline void ThrowErrnoException(
int errorno,
631 const char* syscall =
nullptr,
632 const char* message =
nullptr,
633 const char* path =
nullptr);
634 inline void ThrowUVException(
int errorno,
635 const char* syscall =
nullptr,
636 const char* message =
nullptr,
637 const char* path =
nullptr,
638 const char* dest =
nullptr);
640 inline v8::Local<v8::FunctionTemplate>
641 NewFunctionTemplate(v8::FunctionCallback callback,
642 v8::Local<v8::Signature> signature =
643 v8::Local<v8::Signature>());
646 inline void SetMethod(v8::Local<v8::Object> that,
648 v8::FunctionCallback callback);
649 inline void SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
651 v8::FunctionCallback callback);
652 inline void SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
654 v8::FunctionCallback callback);
656 void AtExit(
void (*cb)(
void* arg),
void* arg);
657 void RunAtExitCallbacks();
661 #define VP(PropertyName, StringValue) V(v8::Private, PropertyName) 662 #define VS(PropertyName, StringValue) V(v8::String, PropertyName) 663 #define V(TypeName, PropertyName) \ 664 inline v8::Local<TypeName> PropertyName() const; 665 PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
666 PER_ISOLATE_STRING_PROPERTIES(VS)
671 #define V(PropertyName, TypeName) \ 672 inline v8::Local<TypeName> PropertyName() const; \ 673 inline void set_ ## PropertyName(v8::Local<TypeName> value); 674 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(
V)
678 inline inspector::Agent* inspector_agent() {
679 return &inspector_agent_;
683 typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
684 typedef ListHead<ReqWrap<uv_req_t>, &ReqWrap<uv_req_t>::req_wrap_queue_>
687 inline HandleWrapQueue* handle_wrap_queue() {
return &handle_wrap_queue_; }
688 inline ReqWrapQueue* req_wrap_queue() {
return &req_wrap_queue_; }
690 static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
696 inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
699 v8::Isolate*
const isolate_;
700 IsolateData*
const isolate_data_;
701 uv_check_t immediate_check_handle_;
702 uv_idle_t immediate_idle_handle_;
703 uv_timer_t destroy_ids_timer_handle_;
704 uv_prepare_t idle_prepare_handle_;
705 uv_check_t idle_check_handle_;
706 uv_prepare_t performance_prepare_handle_;
707 uv_check_t performance_check_handle_;
708 uv_idle_t performance_idle_handle_;
711 DomainFlag domain_flag_;
713 const uint64_t timer_base_;
717 bool abort_on_uncaught_exception_;
718 size_t makecallback_cntr_;
719 std::vector<double> destroy_ids_list_;
721 performance::performance_state* performance_state_ =
nullptr;
722 std::map<std::string, uint64_t> performance_marks_;
725 inspector::Agent inspector_agent_;
728 HandleWrapQueue handle_wrap_queue_;
729 ReqWrapQueue req_wrap_queue_;
730 ListHead<HandleCleanup,
731 &HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
732 int handle_cleanup_waiting_;
734 double* heap_statistics_buffer_ =
nullptr;
735 double* heap_space_statistics_buffer_ =
nullptr;
737 char* http_parser_buffer_;
738 http2::http2_state* http2_state_buffer_ =
nullptr;
740 double* fs_stats_field_array_;
742 struct AtExitCallback {
743 void (*cb_)(
void* arg);
746 std::list<AtExitCallback> at_exit_functions_;
748 struct PromiseHookCallback {
751 size_t enable_count_;
753 std::vector<PromiseHookCallback> promise_hooks_;
755 static void EnvPromiseHook(v8::PromiseHookType type,
756 v8::Local<v8::Promise> promise,
757 v8::Local<v8::Value> parent);
759 #define V(PropertyName, TypeName) \ 760 v8::Persistent<TypeName> PropertyName ## _; 761 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(
V)
764 DISALLOW_COPY_AND_ASSIGN(Environment);
769 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
void AtExit(void(*cb)(void *arg), void *arg)
void AddPromiseHook(v8::Isolate *isolate, promise_hook_func fn, void *arg)
node::Environment::AsyncHooks AsyncHooks
void(* promise_hook_func)(v8::PromiseHookType type, v8::Local< v8::Promise > promise, v8::Local< v8::Value > parent, void *arg)
node::Environment::AsyncHooks AsyncHooks
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)