GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/env.h Lines: 10 10 100.0 %
Date: 2017-10-21 Branches: 0 0 0.0 %

Line Branch Exec Source
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 "aliased_buffer.h"
28
#include "ares.h"
29
#if HAVE_INSPECTOR
30
#include "inspector_agent.h"
31
#endif
32
#include "handle_wrap.h"
33
#include "req-wrap.h"
34
#include "util.h"
35
#include "uv.h"
36
#include "v8.h"
37
#include "node.h"
38
#include "node_http2_state.h"
39
40
#include <list>
41
#include <map>
42
#include <stdint.h>
43
#include <vector>
44
#include <stack>
45
#include <unordered_map>
46
47
struct nghttp2_rcbuf;
48
49
namespace node {
50
51
namespace performance {
52
struct performance_state;
53
}
54
55
namespace loader {
56
class ModuleWrap;
57
}
58
59
// Pick an index that's hopefully out of the way when we're embedded inside
60
// another application. Performance-wise or memory-wise it doesn't matter:
61
// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
62
// worst case we pay a one-time penalty for resizing the array.
63
#ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
64
#define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
65
#endif
66
67
// The number of items passed to push_values_to_array_function has diminishing
68
// returns around 8. This should be used at all call sites using said function.
69
#ifndef NODE_PUSH_VAL_TO_ARRAY_MAX
70
#define NODE_PUSH_VAL_TO_ARRAY_MAX 8
71
#endif
72
73
// PER_ISOLATE_* macros: We have a lot of per-isolate properties
74
// and adding and maintaining their getters and setters by hand would be
75
// difficult so let's make the preprocessor generate them for us.
76
//
77
// In each macro, `V` is expected to be the name of a macro or function which
78
// accepts the number of arguments provided in each tuple in the macro body,
79
// typically two. The named function will be invoked against each tuple.
80
//
81
// Make sure that any macro V defined for use with the PER_ISOLATE_* macros is
82
// undefined again after use.
83
84
// Private symbols are per-isolate primitives but Environment proxies them
85
// for the sake of convenience.  Strings should be ASCII-only and have a
86
// "node:" prefix to avoid name clashes with third-party code.
87
#define PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)                              \
88
  V(alpn_buffer_private_symbol, "node:alpnBuffer")                            \
89
  V(arrow_message_private_symbol, "node:arrowMessage")                        \
90
  V(contextify_context_private_symbol, "node:contextify:context")             \
91
  V(contextify_global_private_symbol, "node:contextify:global")               \
92
  V(decorated_private_symbol, "node:decorated")                               \
93
  V(npn_buffer_private_symbol, "node:npnBuffer")                              \
94
  V(processed_private_symbol, "node:processed")                               \
95
  V(selected_npn_buffer_private_symbol, "node:selectedNpnBuffer")             \
96
  V(domain_private_symbol, "node:domain")                                     \
97
98
// Strings are per-isolate primitives but Environment proxies them
99
// for the sake of convenience.  Strings should be ASCII-only.
100
#define PER_ISOLATE_STRING_PROPERTIES(V)                                      \
101
  V(address_string, "address")                                                \
102
  V(args_string, "args")                                                      \
103
  V(async, "async")                                                           \
104
  V(buffer_string, "buffer")                                                  \
105
  V(bytes_string, "bytes")                                                    \
106
  V(bytes_parsed_string, "bytesParsed")                                       \
107
  V(bytes_read_string, "bytesRead")                                           \
108
  V(cached_data_string, "cachedData")                                         \
109
  V(cached_data_produced_string, "cachedDataProduced")                        \
110
  V(cached_data_rejected_string, "cachedDataRejected")                        \
111
  V(callback_string, "callback")                                              \
112
  V(change_string, "change")                                                  \
113
  V(channel_string, "channel")                                                \
114
  V(constants_string, "constants")                                            \
115
  V(oncertcb_string, "oncertcb")                                              \
116
  V(onclose_string, "_onclose")                                               \
117
  V(code_string, "code")                                                      \
118
  V(configurable_string, "configurable")                                      \
119
  V(cwd_string, "cwd")                                                        \
120
  V(dest_string, "dest")                                                      \
121
  V(destroy_string, "destroy")                                                \
122
  V(detached_string, "detached")                                              \
123
  V(dns_a_string, "A")                                                        \
124
  V(dns_aaaa_string, "AAAA")                                                  \
125
  V(dns_cname_string, "CNAME")                                                \
126
  V(dns_mx_string, "MX")                                                      \
127
  V(dns_naptr_string, "NAPTR")                                                \
128
  V(dns_ns_string, "NS")                                                      \
129
  V(dns_ptr_string, "PTR")                                                    \
130
  V(dns_soa_string, "SOA")                                                    \
131
  V(dns_srv_string, "SRV")                                                    \
132
  V(dns_txt_string, "TXT")                                                    \
133
  V(domain_string, "domain")                                                  \
134
  V(emit_string, "emit")                                                      \
135
  V(emitting_top_level_domain_error_string, "_emittingTopLevelDomainError")   \
136
  V(exchange_string, "exchange")                                              \
137
  V(enumerable_string, "enumerable")                                          \
138
  V(idle_string, "idle")                                                      \
139
  V(irq_string, "irq")                                                        \
140
  V(enablepush_string, "enablePush")                                          \
141
  V(encoding_string, "encoding")                                              \
142
  V(enter_string, "enter")                                                    \
143
  V(entries_string, "entries")                                                \
144
  V(env_pairs_string, "envPairs")                                             \
145
  V(errno_string, "errno")                                                    \
146
  V(error_string, "error")                                                    \
147
  V(events_string, "_events")                                                 \
148
  V(exiting_string, "_exiting")                                               \
149
  V(exit_code_string, "exitCode")                                             \
150
  V(exit_string, "exit")                                                      \
151
  V(expire_string, "expire")                                                  \
152
  V(exponent_string, "exponent")                                              \
153
  V(exports_string, "exports")                                                \
154
  V(ext_key_usage_string, "ext_key_usage")                                    \
155
  V(external_stream_string, "_externalStream")                                \
156
  V(family_string, "family")                                                  \
157
  V(fatal_exception_string, "_fatalException")                                \
158
  V(fd_string, "fd")                                                          \
159
  V(file_string, "file")                                                      \
160
  V(fingerprint_string, "fingerprint")                                        \
161
  V(flags_string, "flags")                                                    \
162
  V(get_string, "get")                                                        \
163
  V(get_data_clone_error_string, "_getDataCloneError")                        \
164
  V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId")             \
165
  V(gid_string, "gid")                                                        \
166
  V(handle_string, "handle")                                                  \
167
  V(heap_total_string, "heapTotal")                                           \
168
  V(heap_used_string, "heapUsed")                                             \
169
  V(homedir_string, "homedir")                                                \
170
  V(hostmaster_string, "hostmaster")                                          \
171
  V(id_string, "id")                                                          \
172
  V(ignore_string, "ignore")                                                  \
173
  V(immediate_callback_string, "_immediateCallback")                          \
174
  V(infoaccess_string, "infoAccess")                                          \
175
  V(inherit_string, "inherit")                                                \
176
  V(input_string, "input")                                                    \
177
  V(internal_string, "internal")                                              \
178
  V(ipv4_string, "IPv4")                                                      \
179
  V(ipv6_string, "IPv6")                                                      \
180
  V(isalive_string, "isAlive")                                                \
181
  V(isclosing_string, "isClosing")                                            \
182
  V(issuer_string, "issuer")                                                  \
183
  V(issuercert_string, "issuerCertificate")                                   \
184
  V(kill_signal_string, "killSignal")                                         \
185
  V(length_string, "length")                                                  \
186
  V(mac_string, "mac")                                                        \
187
  V(max_buffer_string, "maxBuffer")                                           \
188
  V(message_string, "message")                                                \
189
  V(minttl_string, "minttl")                                                  \
190
  V(model_string, "model")                                                    \
191
  V(modulus_string, "modulus")                                                \
192
  V(name_string, "name")                                                      \
193
  V(netmask_string, "netmask")                                                \
194
  V(nice_string, "nice")                                                      \
195
  V(nsname_string, "nsname")                                                  \
196
  V(nexttick_string, "nextTick")                                              \
197
  V(ocsp_request_string, "OCSPRequest")                                       \
198
  V(onchange_string, "onchange")                                              \
199
  V(onclienthello_string, "onclienthello")                                    \
200
  V(oncomplete_string, "oncomplete")                                          \
201
  V(onconnection_string, "onconnection")                                      \
202
  V(ondone_string, "ondone")                                                  \
203
  V(onerror_string, "onerror")                                                \
204
  V(onexit_string, "onexit")                                                  \
205
  V(onframeerror_string, "onframeerror")                                      \
206
  V(ongetpadding_string, "ongetpadding")                                      \
207
  V(onhandshakedone_string, "onhandshakedone")                                \
208
  V(onhandshakestart_string, "onhandshakestart")                              \
209
  V(onheaders_string, "onheaders")                                            \
210
  V(onmessage_string, "onmessage")                                            \
211
  V(onnewsession_string, "onnewsession")                                      \
212
  V(onnewsessiondone_string, "onnewsessiondone")                              \
213
  V(onocspresponse_string, "onocspresponse")                                  \
214
  V(ongoawaydata_string, "ongoawaydata")                                      \
215
  V(onpriority_string, "onpriority")                                          \
216
  V(onread_string, "onread")                                                  \
217
  V(onreadstart_string, "onreadstart")                                        \
218
  V(onreadstop_string, "onreadstop")                                          \
219
  V(onselect_string, "onselect")                                              \
220
  V(onsettings_string, "onsettings")                                          \
221
  V(onshutdown_string, "onshutdown")                                          \
222
  V(onsignal_string, "onsignal")                                              \
223
  V(onstop_string, "onstop")                                                  \
224
  V(onstreamclose_string, "onstreamclose")                                    \
225
  V(ontrailers_string, "ontrailers")                                          \
226
  V(onwrite_string, "onwrite")                                                \
227
  V(openssl_error_stack, "opensslErrorStack")                                 \
228
  V(output_string, "output")                                                  \
229
  V(order_string, "order")                                                    \
230
  V(owner_string, "owner")                                                    \
231
  V(parse_error_string, "Parse Error")                                        \
232
  V(path_string, "path")                                                      \
233
  V(pbkdf2_error_string, "PBKDF2 Error")                                      \
234
  V(pid_string, "pid")                                                        \
235
  V(pipe_string, "pipe")                                                      \
236
  V(port_string, "port")                                                      \
237
  V(preference_string, "preference")                                          \
238
  V(priority_string, "priority")                                              \
239
  V(produce_cached_data_string, "produceCachedData")                          \
240
  V(raw_string, "raw")                                                        \
241
  V(read_host_object_string, "_readHostObject")                               \
242
  V(readable_string, "readable")                                              \
243
  V(received_shutdown_string, "receivedShutdown")                             \
244
  V(refresh_string, "refresh")                                                \
245
  V(regexp_string, "regexp")                                                  \
246
  V(rename_string, "rename")                                                  \
247
  V(replacement_string, "replacement")                                        \
248
  V(retry_string, "retry")                                                    \
249
  V(serial_string, "serial")                                                  \
250
  V(scopeid_string, "scopeid")                                                \
251
  V(sent_shutdown_string, "sentShutdown")                                     \
252
  V(serial_number_string, "serialNumber")                                     \
253
  V(service_string, "service")                                                \
254
  V(servername_string, "servername")                                          \
255
  V(session_id_string, "sessionId")                                           \
256
  V(set_string, "set")                                                        \
257
  V(shell_string, "shell")                                                    \
258
  V(signal_string, "signal")                                                  \
259
  V(size_string, "size")                                                      \
260
  V(sni_context_err_string, "Invalid SNI context")                            \
261
  V(sni_context_string, "sni_context")                                        \
262
  V(speed_string, "speed")                                                    \
263
  V(stack_string, "stack")                                                    \
264
  V(status_string, "status")                                                  \
265
  V(stdio_string, "stdio")                                                    \
266
  V(stream_string, "stream")                                                  \
267
  V(subject_string, "subject")                                                \
268
  V(subjectaltname_string, "subjectaltname")                                  \
269
  V(sys_string, "sys")                                                        \
270
  V(syscall_string, "syscall")                                                \
271
  V(tick_callback_string, "_tickCallback")                                    \
272
  V(tick_domain_cb_string, "_tickDomainCallback")                             \
273
  V(ticketkeycallback_string, "onticketkeycallback")                          \
274
  V(timeout_string, "timeout")                                                \
275
  V(times_string, "times")                                                    \
276
  V(tls_ticket_string, "tlsTicket")                                           \
277
  V(ttl_string, "ttl")                                                        \
278
  V(type_string, "type")                                                      \
279
  V(uid_string, "uid")                                                        \
280
  V(unknown_string, "<unknown>")                                              \
281
  V(user_string, "user")                                                      \
282
  V(username_string, "username")                                              \
283
  V(valid_from_string, "valid_from")                                          \
284
  V(valid_to_string, "valid_to")                                              \
285
  V(value_string, "value")                                                    \
286
  V(verify_error_string, "verifyError")                                       \
287
  V(version_string, "version")                                                \
288
  V(weight_string, "weight")                                                  \
289
  V(windows_hide_string, "windowsHide")                                       \
290
  V(windows_verbatim_arguments_string, "windowsVerbatimArguments")            \
291
  V(wrap_string, "wrap")                                                      \
292
  V(writable_string, "writable")                                              \
293
  V(write_host_object_string, "_writeHostObject")                             \
294
  V(write_queue_size_string, "writeQueueSize")                                \
295
  V(x_forwarded_string, "x-forwarded-for")                                    \
296
  V(zero_return_string, "ZERO_RETURN")
297
298
#define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)                           \
299
  V(as_external, v8::External)                                                \
300
  V(async_hooks_destroy_function, v8::Function)                               \
301
  V(async_hooks_init_function, v8::Function)                                  \
302
  V(async_hooks_before_function, v8::Function)                                \
303
  V(async_hooks_after_function, v8::Function)                                 \
304
  V(async_hooks_promise_resolve_function, v8::Function)                       \
305
  V(binding_cache_object, v8::Object)                                         \
306
  V(internal_binding_cache_object, v8::Object)                                \
307
  V(buffer_prototype_object, v8::Object)                                      \
308
  V(context, v8::Context)                                                     \
309
  V(domain_array, v8::Array)                                                  \
310
  V(domains_stack_array, v8::Array)                                           \
311
  V(inspector_console_api_object, v8::Object)                                 \
312
  V(module_load_list_array, v8::Array)                                        \
313
  V(pbkdf2_constructor_template, v8::ObjectTemplate)                          \
314
  V(pipe_constructor_template, v8::FunctionTemplate)                          \
315
  V(performance_entry_callback, v8::Function)                                 \
316
  V(performance_entry_template, v8::Function)                                 \
317
  V(process_object, v8::Object)                                               \
318
  V(promise_reject_function, v8::Function)                                    \
319
  V(promise_wrap_template, v8::ObjectTemplate)                                \
320
  V(push_values_to_array_function, v8::Function)                              \
321
  V(randombytes_constructor_template, v8::ObjectTemplate)                     \
322
  V(script_context_constructor_template, v8::FunctionTemplate)                \
323
  V(script_data_constructor_function, v8::Function)                           \
324
  V(secure_context_constructor_template, v8::FunctionTemplate)                \
325
  V(tcp_constructor_template, v8::FunctionTemplate)                           \
326
  V(tick_callback_function, v8::Function)                                     \
327
  V(tls_wrap_constructor_function, v8::Function)                              \
328
  V(tty_constructor_template, v8::FunctionTemplate)                           \
329
  V(udp_constructor_function, v8::Function)                                   \
330
  V(vm_parsing_context_symbol, v8::Symbol)                                    \
331
  V(url_constructor_function, v8::Function)                                   \
332
  V(write_wrap_constructor_function, v8::Function)                            \
333
334
class Environment;
335
336
struct node_async_ids {
337
  double async_id;
338
  double trigger_async_id;
339
};
340
341
2846
class IsolateData {
342
 public:
343
  inline IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop,
344
                     uint32_t* zero_fill_field = nullptr);
345
  inline uv_loop_t* event_loop() const;
346
  inline uint32_t* zero_fill_field() const;
347
348
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
349
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
350
#define V(TypeName, PropertyName)                                             \
351
  inline v8::Local<TypeName> PropertyName(v8::Isolate* isolate) const;
352
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
353
  PER_ISOLATE_STRING_PROPERTIES(VS)
354
#undef V
355
#undef VS
356
#undef VP
357
358
  std::unordered_map<nghttp2_rcbuf*, v8::Eternal<v8::String>> http2_static_strs;
359
360
 private:
361
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
362
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
363
#define V(TypeName, PropertyName)                                             \
364
  v8::Eternal<TypeName> PropertyName ## _;
365
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
366
  PER_ISOLATE_STRING_PROPERTIES(VS)
367
#undef V
368
#undef VS
369
#undef VP
370
371
  uv_loop_t* const event_loop_;
372
  uint32_t* const zero_fill_field_;
373
374
  DISALLOW_COPY_AND_ASSIGN(IsolateData);
375
};
376
377
class Environment {
378
 public:
379
2846
  class AsyncHooks {
380
   public:
381
    // Reason for both UidFields and Fields are that one is stored as a double*
382
    // and the other as a uint32_t*.
383
    enum Fields {
384
      kInit,
385
      kBefore,
386
      kAfter,
387
      kDestroy,
388
      kPromiseResolve,
389
      kTotals,
390
      kCheck,
391
      kFieldsCount,
392
    };
393
394
    enum UidFields {
395
      kExecutionAsyncId,
396
      kTriggerAsyncId,
397
      kAsyncIdCounter,
398
      kInitTriggerAsyncId,
399
      kUidFieldsCount,
400
    };
401
402
    AsyncHooks() = delete;
403
404
    inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
405
    inline int fields_count() const;
406
407
    inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
408
    inline int async_id_fields_count() const;
409
410
    inline v8::Local<v8::String> provider_string(int idx);
411
412
    inline void force_checks();
413
414
    inline void push_async_ids(double async_id, double trigger_async_id);
415
    inline bool pop_async_id(double async_id);
416
    inline size_t stack_size();
417
    inline void clear_async_id_stack();  // Used in fatal exceptions.
418
419
    // Used to propagate the trigger_async_id to the constructor of any newly
420
    // created resources using RAII. Instead of needing to pass the
421
    // trigger_async_id along with other constructor arguments.
422
    class InitScope {
423
     public:
424
      InitScope() = delete;
425
      explicit InitScope(Environment* env, double init_trigger_async_id);
426
      ~InitScope();
427
428
     private:
429
      Environment* env_;
430
      AliasedBuffer<double, v8::Float64Array> async_id_fields_ref_;
431
432
      DISALLOW_COPY_AND_ASSIGN(InitScope);
433
    };
434
435
   private:
436
    friend class Environment;  // So we can call the constructor.
437
    inline explicit AsyncHooks(v8::Isolate* isolate);
438
    // Keep a list of all Persistent strings used for Provider types.
439
    v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
440
    // Used by provider_string().
441
    v8::Isolate* isolate_;
442
    // Stores the ids of the current execution context stack.
443
    std::stack<struct node_async_ids> async_ids_stack_;
444
    // Attached to a Uint32Array that tracks the number of active hooks for
445
    // each type.
446
    AliasedBuffer<uint32_t, v8::Uint32Array> fields_;
447
    // Attached to a Float64Array that tracks the state of async resources.
448
    AliasedBuffer<double, v8::Float64Array> async_id_fields_;
449
450
    DISALLOW_COPY_AND_ASSIGN(AsyncHooks);
451
  };
452
453
  class AsyncCallbackScope {
454
   public:
455
    AsyncCallbackScope() = delete;
456
    explicit AsyncCallbackScope(Environment* env);
457
    ~AsyncCallbackScope();
458
    inline bool in_makecallback() const;
459
460
   private:
461
    Environment* env_;
462
463
    DISALLOW_COPY_AND_ASSIGN(AsyncCallbackScope);
464
  };
465
466
  class DomainFlag {
467
   public:
468
    inline uint32_t* fields();
469
    inline int fields_count() const;
470
    inline uint32_t count() const;
471
472
   private:
473
    friend class Environment;  // So we can call the constructor.
474
    inline DomainFlag();
475
476
    enum Fields {
477
      kCount,
478
      kFieldsCount
479
    };
480
481
    uint32_t fields_[kFieldsCount];
482
483
    DISALLOW_COPY_AND_ASSIGN(DomainFlag);
484
  };
485
486
  class TickInfo {
487
   public:
488
    inline uint32_t* fields();
489
    inline int fields_count() const;
490
    inline uint32_t index() const;
491
    inline uint32_t length() const;
492
    inline void set_index(uint32_t value);
493
494
   private:
495
    friend class Environment;  // So we can call the constructor.
496
    inline TickInfo();
497
498
    enum Fields {
499
      kIndex,
500
      kLength,
501
      kFieldsCount
502
    };
503
504
    uint32_t fields_[kFieldsCount];
505
506
    DISALLOW_COPY_AND_ASSIGN(TickInfo);
507
  };
508
509
  typedef void (*HandleCleanupCb)(Environment* env,
510
                                  uv_handle_t* handle,
511
                                  void* arg);
512
513
20
  class HandleCleanup {
514
   private:
515
    friend class Environment;
516
517
16030
    HandleCleanup(uv_handle_t* handle, HandleCleanupCb cb, void* arg)
518
        : handle_(handle),
519
          cb_(cb),
520
16030
          arg_(arg) {
521
16030
    }
522
523
    uv_handle_t* handle_;
524
    HandleCleanupCb cb_;
525
    void* arg_;
526
    ListNode<HandleCleanup> handle_cleanup_queue_;
527
  };
528
529
  static inline Environment* GetCurrent(v8::Isolate* isolate);
530
  static inline Environment* GetCurrent(v8::Local<v8::Context> context);
531
  static inline Environment* GetCurrent(
532
      const v8::FunctionCallbackInfo<v8::Value>& info);
533
534
  template <typename T>
535
  static inline Environment* GetCurrent(
536
      const v8::PropertyCallbackInfo<T>& info);
537
538
  inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context);
539
  inline ~Environment();
540
541
  void Start(int argc,
542
             const char* const* argv,
543
             int exec_argc,
544
             const char* const* exec_argv,
545
             bool start_profiler_idle_notifier);
546
  void AssignToContext(v8::Local<v8::Context> context);
547
  void CleanupHandles();
548
549
  void StartProfilerIdleNotifier();
550
  void StopProfilerIdleNotifier();
551
552
  inline v8::Isolate* isolate() const;
553
  inline uv_loop_t* event_loop() const;
554
  inline bool in_domain() const;
555
  inline uint32_t watched_providers() const;
556
557
  static inline Environment* from_immediate_check_handle(uv_check_t* handle);
558
  static inline Environment* from_destroy_async_ids_timer_handle(
559
    uv_timer_t* handle);
560
  inline uv_check_t* immediate_check_handle();
561
  inline uv_idle_t* immediate_idle_handle();
562
  inline uv_timer_t* destroy_async_ids_timer_handle();
563
564
  // Register clean-up cb to be called on environment destruction.
565
  inline void RegisterHandleCleanup(uv_handle_t* handle,
566
                                    HandleCleanupCb cb,
567
                                    void *arg);
568
  inline void FinishHandleCleanup(uv_handle_t* handle);
569
570
  inline AsyncHooks* async_hooks();
571
  inline DomainFlag* domain_flag();
572
  inline TickInfo* tick_info();
573
  inline uint64_t timer_base() const;
574
575
  inline IsolateData* isolate_data() const;
576
577
  inline bool using_domains() const;
578
  inline void set_using_domains(bool value);
579
580
  inline bool printed_error() const;
581
  inline void set_printed_error(bool value);
582
583
  void PrintSyncTrace() const;
584
  inline void set_trace_sync_io(bool value);
585
586
  inline bool abort_on_uncaught_exception() const;
587
  inline void set_abort_on_uncaught_exception(bool value);
588
589
  // The necessary API for async_hooks.
590
  inline double new_async_id();
591
  inline double execution_async_id();
592
  inline double trigger_async_id();
593
  inline double get_init_trigger_async_id();
594
  inline void set_init_trigger_async_id(const double id);
595
596
  // List of id's that have been destroyed and need the destroy() cb called.
597
  inline std::vector<double>* destroy_async_id_list();
598
599
  std::unordered_multimap<int, loader::ModuleWrap*> module_map;
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() const;
611
  inline void set_http2_state(http2::http2_state * state);
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
  inline void ThrowError(const char* errmsg);
620
  inline void ThrowTypeError(const char* errmsg);
621
  inline void ThrowRangeError(const char* errmsg);
622
  inline void ThrowErrnoException(int errorno,
623
                                  const char* syscall = nullptr,
624
                                  const char* message = nullptr,
625
                                  const char* path = nullptr);
626
  inline void ThrowUVException(int errorno,
627
                               const char* syscall = nullptr,
628
                               const char* message = nullptr,
629
                               const char* path = nullptr,
630
                               const char* dest = nullptr);
631
632
  inline v8::Local<v8::FunctionTemplate>
633
      NewFunctionTemplate(v8::FunctionCallback callback,
634
                          v8::Local<v8::Signature> signature =
635
                              v8::Local<v8::Signature>());
636
637
  // Convenience methods for NewFunctionTemplate().
638
  inline void SetMethod(v8::Local<v8::Object> that,
639
                        const char* name,
640
                        v8::FunctionCallback callback);
641
  inline void SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
642
                             const char* name,
643
                             v8::FunctionCallback callback);
644
  inline void SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
645
                                const char* name,
646
                                v8::FunctionCallback callback);
647
648
  void AtExit(void (*cb)(void* arg), void* arg);
649
  void RunAtExitCallbacks();
650
651
  // Strings and private symbols are shared across shared contexts
652
  // The getters simply proxy to the per-isolate primitive.
653
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
654
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
655
#define V(TypeName, PropertyName)                                             \
656
  inline v8::Local<TypeName> PropertyName() const;
657
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
658
  PER_ISOLATE_STRING_PROPERTIES(VS)
659
#undef V
660
#undef VS
661
#undef VP
662
663
#define V(PropertyName, TypeName)                                             \
664
  inline v8::Local<TypeName> PropertyName() const;                            \
665
  inline void set_ ## PropertyName(v8::Local<TypeName> value);
666
  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
667
#undef V
668
669
#if HAVE_INSPECTOR
670
41677
  inline inspector::Agent* inspector_agent() {
671
41677
    return &inspector_agent_;
672
  }
673
#endif
674
675
  typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
676
  typedef ListHead<ReqWrap<uv_req_t>, &ReqWrap<uv_req_t>::req_wrap_queue_>
677
          ReqWrapQueue;
678
679
13775
  inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
680
217174
  inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
681
682
  static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
683
684
  void AddPromiseHook(promise_hook_func fn, void* arg);
685
  bool RemovePromiseHook(promise_hook_func fn, void* arg);
686
  bool EmitNapiWarning();
687
688
 private:
689
  inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
690
                         const char* errmsg);
691
692
  v8::Isolate* const isolate_;
693
  IsolateData* const isolate_data_;
694
  uv_check_t immediate_check_handle_;
695
  uv_idle_t immediate_idle_handle_;
696
  uv_timer_t destroy_async_ids_timer_handle_;
697
  uv_prepare_t idle_prepare_handle_;
698
  uv_check_t idle_check_handle_;
699
700
  AsyncHooks async_hooks_;
701
  DomainFlag domain_flag_;
702
  TickInfo tick_info_;
703
  const uint64_t timer_base_;
704
  bool using_domains_;
705
  bool printed_error_;
706
  bool trace_sync_io_;
707
  bool abort_on_uncaught_exception_;
708
  bool emit_napi_warning_;
709
  size_t makecallback_cntr_;
710
  std::vector<double> destroy_async_id_list_;
711
712
  performance::performance_state* performance_state_ = nullptr;
713
  std::map<std::string, uint64_t> performance_marks_;
714
715
#if HAVE_INSPECTOR
716
  inspector::Agent inspector_agent_;
717
#endif
718
719
  HandleWrapQueue handle_wrap_queue_;
720
  ReqWrapQueue req_wrap_queue_;
721
  ListHead<HandleCleanup,
722
           &HandleCleanup::handle_cleanup_queue_> handle_cleanup_queue_;
723
  int handle_cleanup_waiting_;
724
725
  double* heap_statistics_buffer_ = nullptr;
726
  double* heap_space_statistics_buffer_ = nullptr;
727
728
  char* http_parser_buffer_;
729
  http2::http2_state* http2_state_ = nullptr;
730
731
  double* fs_stats_field_array_;
732
733
  struct AtExitCallback {
734
    void (*cb_)(void* arg);
735
    void* arg_;
736
  };
737
  std::list<AtExitCallback> at_exit_functions_;
738
739
  struct PromiseHookCallback {
740
    promise_hook_func cb_;
741
    void* arg_;
742
    size_t enable_count_;
743
  };
744
  std::vector<PromiseHookCallback> promise_hooks_;
745
746
  static void EnvPromiseHook(v8::PromiseHookType type,
747
                             v8::Local<v8::Promise> promise,
748
                             v8::Local<v8::Value> parent);
749
750
#define V(PropertyName, TypeName)                                             \
751
  v8::Persistent<TypeName> PropertyName ## _;
752
  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
753
#undef V
754
755
  DISALLOW_COPY_AND_ASSIGN(Environment);
756
};
757
758
}  // namespace node
759
760
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
761
762
#endif  // SRC_ENV_H_