Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
env.h
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef SRC_ENV_H_
23 #define SRC_ENV_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "ares.h"
28 #if HAVE_INSPECTOR
29 #include "inspector_agent.h"
30 #endif
31 #include "handle_wrap.h"
32 #include "req-wrap.h"
33 #include "util.h"
34 #include "uv.h"
35 #include "v8.h"
36 #include "node.h"
37 
38 #include <list>
39 #include <map>
40 #include <stdint.h>
41 #include <vector>
42 #include <stack>
43 #include <unordered_map>
44 
45 struct nghttp2_rcbuf;
46 
47 namespace node {
48 
49 namespace http2 {
50 struct http2_state;
51 }
52 
53 // Pick an index that's hopefully out of the way when we're embedded inside
54 // another application. Performance-wise or memory-wise it doesn't matter:
55 // Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
56 // worst case we pay a one-time penalty for resizing the array.
57 #ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
58 #define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
59 #endif
60 
61 // The number of items passed to push_values_to_array_function has diminishing
62 // returns around 8. This should be used at all call sites using said function.
63 #ifndef NODE_PUSH_VAL_TO_ARRAY_MAX
64 #define NODE_PUSH_VAL_TO_ARRAY_MAX 8
65 #endif
66 
67 // PER_ISOLATE_* macros: We have a lot of per-isolate properties
68 // and adding and maintaining their getters and setters by hand would be
69 // difficult so let's make the preprocessor generate them for us.
70 //
71 // In each macro, `V` is expected to be the name of a macro or function which
72 // accepts the number of arguments provided in each tuple in the macro body,
73 // typically two. The named function will be invoked against each tuple.
74 //
75 // Make sure that any macro V defined for use with the PER_ISOLATE_* macros is
76 // undefined again after use.
77 
78 // Private symbols are per-isolate primitives but Environment proxies them
79 // for the sake of convenience. Strings should be ASCII-only and have a
80 // "node:" prefix to avoid name clashes with third-party code.
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") \
91 
92 // Strings are per-isolate primitives but Environment proxies them
93 // for the sake of convenience. Strings should be ASCII-only.
94 #define PER_ISOLATE_STRING_PROPERTIES(V) \
95  V(address_string, "address") \
96  V(args_string, "args") \
97  V(async, "async") \
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") \
153  V(fd_string, "fd") \
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") \
166  V(id_string, "id") \
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")
290 
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) \
323 
324 class Environment;
325 
326 struct node_async_ids {
327  double async_id;
328  double trigger_id;
329 };
330 
331 class IsolateData {
332  public:
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;
337 
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)
344 #undef V
345 #undef VS
346 #undef VP
347 
348  std::unordered_map<nghttp2_rcbuf*, v8::Eternal<v8::String>> http2_static_strs;
349 
350  private:
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)
357 #undef V
358 #undef VS
359 #undef VP
360 
361  uv_loop_t* const event_loop_;
362  uint32_t* const zero_fill_field_;
363 
364  DISALLOW_COPY_AND_ASSIGN(IsolateData);
365 };
366 
367 class Environment {
368  public:
369  class AsyncHooks {
370  public:
371  // Reason for both UidFields and Fields are that one is stored as a double*
372  // and the other as a uint32_t*.
373  enum Fields {
374  kInit,
375  kBefore,
376  kAfter,
377  kDestroy,
378  kTotals,
379  kFieldsCount,
380  };
381 
382  enum UidFields {
383  kCurrentAsyncId,
384  kCurrentTriggerId,
385  kAsyncUidCntr,
386  kInitTriggerId,
387  kUidFieldsCount,
388  };
389 
390  AsyncHooks() = delete;
391 
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);
397 
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(); // Used in fatal exceptions.
402 
403  // Used to propagate the trigger_id to the constructor of any newly created
404  // resources using RAII. Instead of needing to pass the trigger_id along
405  // with other constructor arguments.
406  class InitScope {
407  public:
408  InitScope() = delete;
409  explicit InitScope(Environment* env, double init_trigger_id);
410  ~InitScope();
411 
412  private:
413  Environment* env_;
414  double* uid_fields_ref_;
415 
416  DISALLOW_COPY_AND_ASSIGN(InitScope);
417  };
418 
419  // Used to manage the stack of async and trigger ids as calls are made into
420  // JS. Mainly used in MakeCallback().
421  class ExecScope {
422  public:
423  ExecScope() = delete;
424  explicit ExecScope(Environment* env, double async_id, double trigger_id);
425  ~ExecScope();
426  void Dispose();
427 
428  private:
429  Environment* env_;
430  double async_id_;
431  // Manually track if the destructor has run so it isn't accidentally run
432  // twice on RAII cleanup.
433  bool disposed_;
434 
435  DISALLOW_COPY_AND_ASSIGN(ExecScope);
436  };
437 
438  private:
439  friend class Environment; // So we can call the constructor.
440  inline explicit AsyncHooks(v8::Isolate* isolate);
441  // Keep a list of all Persistent strings used for Provider types.
442  v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
443  // Used by provider_string().
444  v8::Isolate* isolate_;
445  // Stores the ids of the current execution context stack.
446  std::stack<struct node_async_ids> ids_stack_;
447  // Attached to a Uint32Array that tracks the number of active hooks for
448  // each type.
449  uint32_t fields_[kFieldsCount];
450  // Attached to a Float64Array that tracks the state of async resources.
451  double uid_fields_[kUidFieldsCount];
452 
453  DISALLOW_COPY_AND_ASSIGN(AsyncHooks);
454  };
455 
456  class AsyncCallbackScope {
457  public:
458  AsyncCallbackScope() = delete;
459  explicit AsyncCallbackScope(Environment* env);
460  ~AsyncCallbackScope();
461  inline bool in_makecallback();
462 
463  private:
464  Environment* env_;
465 
466  DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope);
467  };
468 
469  class DomainFlag {
470  public:
471  inline uint32_t* fields();
472  inline int fields_count() const;
473  inline uint32_t count() const;
474 
475  private:
476  friend class Environment; // So we can call the constructor.
477  inline DomainFlag();
478 
479  enum Fields {
480  kCount,
481  kFieldsCount
482  };
483 
484  uint32_t fields_[kFieldsCount];
485 
486  DISALLOW_COPY_AND_ASSIGN(DomainFlag);
487  };
488 
489  class TickInfo {
490  public:
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);
496 
497  private:
498  friend class Environment; // So we can call the constructor.
499  inline TickInfo();
500 
501  enum Fields {
502  kIndex,
503  kLength,
504  kFieldsCount
505  };
506 
507  uint32_t fields_[kFieldsCount];
508 
509  DISALLOW_COPY_AND_ASSIGN(TickInfo);
510  };
511 
512  typedef void (*HandleCleanupCb)(Environment* env,
513  uv_handle_t* handle,
514  void* arg);
515 
516  class HandleCleanup {
517  private:
518  friend class Environment;
519 
520  HandleCleanup(uv_handle_t* handle, HandleCleanupCb cb, void* arg)
521  : handle_(handle),
522  cb_(cb),
523  arg_(arg) {
524  }
525 
526  uv_handle_t* handle_;
527  HandleCleanupCb cb_;
528  void* arg_;
529  ListNode<HandleCleanup> handle_cleanup_queue_;
530  };
531 
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);
536 
537  template <typename T>
538  static inline Environment* GetCurrent(
539  const v8::PropertyCallbackInfo<T>& info);
540 
541  inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context);
542  inline ~Environment();
543 
544  void Start(int argc,
545  const char* const* argv,
546  int exec_argc,
547  const char* const* exec_argv,
548  bool start_profiler_idle_notifier);
549  void AssignToContext(v8::Local<v8::Context> context);
550  void CleanupHandles();
551 
552  void StartProfilerIdleNotifier();
553  void StopProfilerIdleNotifier();
554 
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;
559 
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();
565 
566  // Register clean-up cb to be called on environment destruction.
567  inline void RegisterHandleCleanup(uv_handle_t* handle,
568  HandleCleanupCb cb,
569  void *arg);
570  inline void FinishHandleCleanup(uv_handle_t* handle);
571 
572  inline AsyncHooks* async_hooks();
573  inline DomainFlag* domain_flag();
574  inline TickInfo* tick_info();
575  inline uint64_t timer_base() const;
576 
577  inline IsolateData* isolate_data() const;
578 
579  inline bool using_domains() const;
580  inline void set_using_domains(bool value);
581 
582  inline bool printed_error() const;
583  inline void set_printed_error(bool value);
584 
585  void PrintSyncTrace() const;
586  inline void set_trace_sync_io(bool value);
587 
588  inline bool abort_on_uncaught_exception() const;
589  inline void set_abort_on_uncaught_exception(bool value);
590 
591  // The necessary API for async_hooks.
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);
597 
598  // List of id's that have been destroyed and need the destroy() cb called.
599  inline std::vector<double>* destroy_ids_list();
600 
601  inline double* heap_statistics_buffer() const;
602  inline void set_heap_statistics_buffer(double* pointer);
603 
604  inline double* heap_space_statistics_buffer() const;
605  inline void set_heap_space_statistics_buffer(double* pointer);
606 
607  inline char* http_parser_buffer() const;
608  inline void set_http_parser_buffer(char* buffer);
609 
610  inline http2::http2_state* http2_state_buffer() const;
611  inline void set_http2_state_buffer(http2::http2_state* buffer);
612 
613  inline double* fs_stats_field_array() const;
614  inline void set_fs_stats_field_array(double* fields);
615 
616  inline performance::performance_state* performance_state();
617  inline std::map<std::string, uint64_t>* performance_marks();
618 
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();
626 
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);
639 
640  inline v8::Local<v8::FunctionTemplate>
641  NewFunctionTemplate(v8::FunctionCallback callback,
642  v8::Local<v8::Signature> signature =
643  v8::Local<v8::Signature>());
644 
645  // Convenience methods for NewFunctionTemplate().
646  inline void SetMethod(v8::Local<v8::Object> that,
647  const char* name,
648  v8::FunctionCallback callback);
649  inline void SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
650  const char* name,
651  v8::FunctionCallback callback);
652  inline void SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
653  const char* name,
654  v8::FunctionCallback callback);
655 
656  void AtExit(void (*cb)(void* arg), void* arg);
657  void RunAtExitCallbacks();
658 
659  // Strings and private symbols are shared across shared contexts
660  // The getters simply proxy to the per-isolate primitive.
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)
667 #undef V
668 #undef VS
669 #undef VP
670 
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)
675 #undef V
676 
677 #if HAVE_INSPECTOR
678  inline inspector::Agent* inspector_agent() {
679  return &inspector_agent_;
680  }
681 #endif
682 
683  typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
684  typedef ListHead<ReqWrap<uv_req_t>, &ReqWrap<uv_req_t>::req_wrap_queue_>
685  ReqWrapQueue;
686 
687  inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
688  inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
689 
690  static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
691 
692  void AddPromiseHook(promise_hook_func fn, void* arg);
693  bool RemovePromiseHook(promise_hook_func fn, void* arg);
694 
695  private:
696  inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
697  const char* errmsg);
698 
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_;
709 
710  AsyncHooks async_hooks_;
711  DomainFlag domain_flag_;
712  TickInfo tick_info_;
713  const uint64_t timer_base_;
714  bool using_domains_;
715  bool printed_error_;
716  bool trace_sync_io_;
717  bool abort_on_uncaught_exception_;
718  size_t makecallback_cntr_;
719  std::vector<double> destroy_ids_list_;
720 
721  performance::performance_state* performance_state_ = nullptr;
722  std::map<std::string, uint64_t> performance_marks_;
723 
724 #if HAVE_INSPECTOR
725  inspector::Agent inspector_agent_;
726 #endif
727 
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_;
733 
734  double* heap_statistics_buffer_ = nullptr;
735  double* heap_space_statistics_buffer_ = nullptr;
736 
737  char* http_parser_buffer_;
738  http2::http2_state* http2_state_buffer_ = nullptr;
739 
740  double* fs_stats_field_array_;
741 
742  struct AtExitCallback {
743  void (*cb_)(void* arg);
744  void* arg_;
745  };
746  std::list<AtExitCallback> at_exit_functions_;
747 
748  struct PromiseHookCallback {
749  promise_hook_func cb_;
750  void* arg_;
751  size_t enable_count_;
752  };
753  std::vector<PromiseHookCallback> promise_hooks_;
754 
755  static void EnvPromiseHook(v8::PromiseHookType type,
756  v8::Local<v8::Promise> promise,
757  v8::Local<v8::Value> parent);
758 
759 #define V(PropertyName, TypeName) \
760  v8::Persistent<TypeName> PropertyName ## _;
761  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
762 #undef V
763 
764  DISALLOW_COPY_AND_ASSIGN(Environment);
765 };
766 
767 } // namespace node
768 
769 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
770 
771 #endif // SRC_ENV_H_
Environment *const env_
void AtExit(void(*cb)(void *arg), void *arg)
Definition: node.cc:4455
void AddPromiseHook(v8::Isolate *isolate, promise_hook_func fn, void *arg)
Definition: node.cc:1306
#define V(PROVIDER)
double async_id
Definition: node.h:541
node::Environment::AsyncHooks AsyncHooks
Definition: node.cc:164
void(* promise_hook_func)(v8::PromiseHookType type, v8::Local< v8::Promise > promise, v8::Local< v8::Value > parent, void *arg)
Definition: node.h:536
node::Environment::AsyncHooks AsyncHooks
Definition: async-wrap.cc:60
StringPtr fields_[32]
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536