revise installing a license file
[platform/upstream/nodejs.git] / src / env-inl.h
1 #ifndef SRC_ENV_INL_H_
2 #define SRC_ENV_INL_H_
3
4 #include "env.h"
5 #include "node.h"
6 #include "util.h"
7 #include "util-inl.h"
8 #include "uv.h"
9 #include "v8.h"
10
11 #include <stddef.h>
12 #include <stdint.h>
13
14 namespace node {
15
16 inline Environment::IsolateData* Environment::IsolateData::Get(
17     v8::Isolate* isolate) {
18   return static_cast<IsolateData*>(isolate->GetData(kIsolateSlot));
19 }
20
21 inline Environment::IsolateData* Environment::IsolateData::GetOrCreate(
22     v8::Isolate* isolate, uv_loop_t* loop) {
23   IsolateData* isolate_data = Get(isolate);
24   if (isolate_data == nullptr) {
25     isolate_data = new IsolateData(isolate, loop);
26     isolate->SetData(kIsolateSlot, isolate_data);
27   }
28   isolate_data->ref_count_ += 1;
29   return isolate_data;
30 }
31
32 inline void Environment::IsolateData::Put() {
33   if (--ref_count_ == 0) {
34     isolate()->SetData(kIsolateSlot, nullptr);
35     delete this;
36   }
37 }
38
39 // Create string properties as internalized one byte strings.
40 //
41 // Internalized because it makes property lookups a little faster and because
42 // the string is created in the old space straight away.  It's going to end up
43 // in the old space sooner or later anyway but now it doesn't go through
44 // v8::Eternal's new space handling first.
45 //
46 // One byte because our strings are ASCII and we can safely skip V8's UTF-8
47 // decoding step.  It's a one-time cost, but why pay it when you don't have to?
48 inline Environment::IsolateData::IsolateData(v8::Isolate* isolate,
49                                              uv_loop_t* loop)
50     : event_loop_(loop),
51       isolate_(isolate),
52 #define V(PropertyName, StringValue)                                          \
53     PropertyName ## _(isolate,                                                \
54                       v8::String::NewFromOneByte(                             \
55                           isolate,                                            \
56                           reinterpret_cast<const uint8_t*>(StringValue),      \
57                           v8::NewStringType::kInternalized,                   \
58                           sizeof(StringValue) - 1).ToLocalChecked()),
59     PER_ISOLATE_STRING_PROPERTIES(V)
60 #undef V
61     ref_count_(0) {}
62
63 inline uv_loop_t* Environment::IsolateData::event_loop() const {
64   return event_loop_;
65 }
66
67 inline v8::Isolate* Environment::IsolateData::isolate() const {
68   return isolate_;
69 }
70
71 inline Environment::AsyncHooks::AsyncHooks() {
72   for (int i = 0; i < kFieldsCount; i++) fields_[i] = 0;
73 }
74
75 inline uint32_t* Environment::AsyncHooks::fields() {
76   return fields_;
77 }
78
79 inline int Environment::AsyncHooks::fields_count() const {
80   return kFieldsCount;
81 }
82
83 inline bool Environment::AsyncHooks::callbacks_enabled() {
84   return fields_[kEnableCallbacks] != 0;
85 }
86
87 inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
88   fields_[kEnableCallbacks] = flag;
89 }
90
91 inline Environment::DomainFlag::DomainFlag() {
92   for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
93 }
94
95 inline uint32_t* Environment::DomainFlag::fields() {
96   return fields_;
97 }
98
99 inline int Environment::DomainFlag::fields_count() const {
100   return kFieldsCount;
101 }
102
103 inline uint32_t Environment::DomainFlag::count() const {
104   return fields_[kCount];
105 }
106
107 inline Environment::TickInfo::TickInfo() : in_tick_(false), last_threw_(false) {
108   for (int i = 0; i < kFieldsCount; ++i)
109     fields_[i] = 0;
110 }
111
112 inline uint32_t* Environment::TickInfo::fields() {
113   return fields_;
114 }
115
116 inline int Environment::TickInfo::fields_count() const {
117   return kFieldsCount;
118 }
119
120 inline bool Environment::TickInfo::in_tick() const {
121   return in_tick_;
122 }
123
124 inline uint32_t Environment::TickInfo::index() const {
125   return fields_[kIndex];
126 }
127
128 inline bool Environment::TickInfo::last_threw() const {
129   return last_threw_;
130 }
131
132 inline uint32_t Environment::TickInfo::length() const {
133   return fields_[kLength];
134 }
135
136 inline void Environment::TickInfo::set_in_tick(bool value) {
137   in_tick_ = value;
138 }
139
140 inline void Environment::TickInfo::set_index(uint32_t value) {
141   fields_[kIndex] = value;
142 }
143
144 inline void Environment::TickInfo::set_last_threw(bool value) {
145   last_threw_ = value;
146 }
147
148 inline Environment::ArrayBufferAllocatorInfo::ArrayBufferAllocatorInfo() {
149   for (int i = 0; i < kFieldsCount; ++i)
150     fields_[i] = 0;
151 }
152
153 inline uint32_t* Environment::ArrayBufferAllocatorInfo::fields() {
154   return fields_;
155 }
156
157 inline int Environment::ArrayBufferAllocatorInfo::fields_count() const {
158   return kFieldsCount;
159 }
160
161 inline bool Environment::ArrayBufferAllocatorInfo::no_zero_fill() const {
162   return fields_[kNoZeroFill] != 0;
163 }
164
165 inline void Environment::ArrayBufferAllocatorInfo::reset_fill_flag() {
166   fields_[kNoZeroFill] = 0;
167 }
168
169 inline Environment* Environment::New(v8::Local<v8::Context> context,
170                                      uv_loop_t* loop) {
171   Environment* env = new Environment(context, loop);
172   env->AssignToContext(context);
173   return env;
174 }
175
176 inline void Environment::AssignToContext(v8::Local<v8::Context> context) {
177   context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
178 }
179
180 inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
181   return GetCurrent(isolate->GetCurrentContext());
182 }
183
184 inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
185   return static_cast<Environment*>(
186       context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
187 }
188
189 inline Environment* Environment::GetCurrent(
190     const v8::FunctionCallbackInfo<v8::Value>& info) {
191   ASSERT(info.Data()->IsExternal());
192   return static_cast<Environment*>(info.Data().As<v8::External>()->Value());
193 }
194
195 template <typename T>
196 inline Environment* Environment::GetCurrent(
197     const v8::PropertyCallbackInfo<T>& info) {
198   ASSERT(info.Data()->IsExternal());
199   // XXX(bnoordhuis) Work around a g++ 4.9.2 template type inferrer bug
200   // when the expression is written as info.Data().As<v8::External>().
201   v8::Local<v8::Value> data = info.Data();
202   return static_cast<Environment*>(data.As<v8::External>()->Value());
203 }
204
205 inline Environment::Environment(v8::Local<v8::Context> context,
206                                 uv_loop_t* loop)
207     : isolate_(context->GetIsolate()),
208       isolate_data_(IsolateData::GetOrCreate(context->GetIsolate(), loop)),
209       timer_base_(uv_now(loop)),
210       using_domains_(false),
211       printed_error_(false),
212       trace_sync_io_(false),
213       async_wrap_uid_(0),
214       debugger_agent_(this),
215       http_parser_buffer_(nullptr),
216       context_(context->GetIsolate(), context) {
217   // We'll be creating new objects so make sure we've entered the context.
218   v8::HandleScope handle_scope(isolate());
219   v8::Context::Scope context_scope(context);
220   set_as_external(v8::External::New(isolate(), this));
221   set_binding_cache_object(v8::Object::New(isolate()));
222   set_module_load_list_array(v8::Array::New(isolate()));
223
224   v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate());
225   fn->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "InternalFieldObject"));
226   v8::Local<v8::ObjectTemplate> obj = fn->InstanceTemplate();
227   obj->SetInternalFieldCount(1);
228   set_generic_internal_field_template(obj);
229
230   RB_INIT(&cares_task_list_);
231   handle_cleanup_waiting_ = 0;
232 }
233
234 inline Environment::~Environment() {
235   v8::HandleScope handle_scope(isolate());
236
237   context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex,
238                                              nullptr);
239 #define V(PropertyName, TypeName) PropertyName ## _.Reset();
240   ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
241 #undef V
242   isolate_data()->Put();
243
244   delete[] heap_statistics_buffer_;
245   delete[] http_parser_buffer_;
246 }
247
248 inline void Environment::CleanupHandles() {
249   while (HandleCleanup* hc = handle_cleanup_queue_.PopFront()) {
250     handle_cleanup_waiting_++;
251     hc->cb_(this, hc->handle_, hc->arg_);
252     delete hc;
253   }
254
255   while (handle_cleanup_waiting_ != 0)
256     uv_run(event_loop(), UV_RUN_ONCE);
257 }
258
259 inline void Environment::Dispose() {
260   delete this;
261 }
262
263 inline v8::Isolate* Environment::isolate() const {
264   return isolate_;
265 }
266
267 inline bool Environment::async_wrap_callbacks_enabled() const {
268   // The const_cast is okay, it doesn't violate conceptual const-ness.
269   return const_cast<Environment*>(this)->async_hooks()->callbacks_enabled();
270 }
271
272 inline bool Environment::in_domain() const {
273   // The const_cast is okay, it doesn't violate conceptual const-ness.
274   return using_domains() &&
275          const_cast<Environment*>(this)->domain_flag()->count() > 0;
276 }
277
278 inline Environment* Environment::from_immediate_check_handle(
279     uv_check_t* handle) {
280   return ContainerOf(&Environment::immediate_check_handle_, handle);
281 }
282
283 inline uv_check_t* Environment::immediate_check_handle() {
284   return &immediate_check_handle_;
285 }
286
287 inline uv_idle_t* Environment::immediate_idle_handle() {
288   return &immediate_idle_handle_;
289 }
290
291 inline Environment* Environment::from_idle_prepare_handle(
292     uv_prepare_t* handle) {
293   return ContainerOf(&Environment::idle_prepare_handle_, handle);
294 }
295
296 inline uv_prepare_t* Environment::idle_prepare_handle() {
297   return &idle_prepare_handle_;
298 }
299
300 inline Environment* Environment::from_idle_check_handle(uv_check_t* handle) {
301   return ContainerOf(&Environment::idle_check_handle_, handle);
302 }
303
304 inline uv_check_t* Environment::idle_check_handle() {
305   return &idle_check_handle_;
306 }
307
308 inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
309                                                HandleCleanupCb cb,
310                                                void *arg) {
311   handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
312 }
313
314 inline void Environment::FinishHandleCleanup(uv_handle_t* handle) {
315   handle_cleanup_waiting_--;
316 }
317
318 inline uv_loop_t* Environment::event_loop() const {
319   return isolate_data()->event_loop();
320 }
321
322 inline Environment::AsyncHooks* Environment::async_hooks() {
323   return &async_hooks_;
324 }
325
326 inline Environment::DomainFlag* Environment::domain_flag() {
327   return &domain_flag_;
328 }
329
330 inline Environment::TickInfo* Environment::tick_info() {
331   return &tick_info_;
332 }
333
334 inline Environment::ArrayBufferAllocatorInfo*
335     Environment::array_buffer_allocator_info() {
336   return &array_buffer_allocator_info_;
337 }
338
339 inline uint64_t Environment::timer_base() const {
340   return timer_base_;
341 }
342
343 inline bool Environment::using_domains() const {
344   return using_domains_;
345 }
346
347 inline void Environment::set_using_domains(bool value) {
348   using_domains_ = value;
349 }
350
351 inline bool Environment::printed_error() const {
352   return printed_error_;
353 }
354
355 inline void Environment::set_printed_error(bool value) {
356   printed_error_ = value;
357 }
358
359 inline void Environment::set_trace_sync_io(bool value) {
360   trace_sync_io_ = value;
361 }
362
363 inline int64_t Environment::get_async_wrap_uid() {
364   return ++async_wrap_uid_;
365 }
366
367 inline uint32_t* Environment::heap_statistics_buffer() const {
368   CHECK_NE(heap_statistics_buffer_, nullptr);
369   return heap_statistics_buffer_;
370 }
371
372 inline void Environment::set_heap_statistics_buffer(uint32_t* pointer) {
373   CHECK_EQ(heap_statistics_buffer_, nullptr);  // Should be set only once.
374   heap_statistics_buffer_ = pointer;
375 }
376
377 inline char* Environment::http_parser_buffer() const {
378   return http_parser_buffer_;
379 }
380
381 inline void Environment::set_http_parser_buffer(char* buffer) {
382   CHECK_EQ(http_parser_buffer_, nullptr);  // Should be set only once.
383   http_parser_buffer_ = buffer;
384 }
385
386 inline Environment* Environment::from_cares_timer_handle(uv_timer_t* handle) {
387   return ContainerOf(&Environment::cares_timer_handle_, handle);
388 }
389
390 inline uv_timer_t* Environment::cares_timer_handle() {
391   return &cares_timer_handle_;
392 }
393
394 inline ares_channel Environment::cares_channel() {
395   return cares_channel_;
396 }
397
398 // Only used in the call to ares_init_options().
399 inline ares_channel* Environment::cares_channel_ptr() {
400   return &cares_channel_;
401 }
402
403 inline ares_task_list* Environment::cares_task_list() {
404   return &cares_task_list_;
405 }
406
407 inline Environment::IsolateData* Environment::isolate_data() const {
408   return isolate_data_;
409 }
410
411 // this would have been a template function were it not for the fact that g++
412 // sometimes fails to resolve it...
413 #define THROW_ERROR(fun)                                                      \
414   do {                                                                        \
415     v8::HandleScope scope(isolate);                                           \
416     isolate->ThrowException(fun(OneByteString(isolate, errmsg)));             \
417   }                                                                           \
418   while (0)
419
420 inline void Environment::ThrowError(v8::Isolate* isolate, const char* errmsg) {
421   THROW_ERROR(v8::Exception::Error);
422 }
423
424 inline void Environment::ThrowTypeError(v8::Isolate* isolate,
425                                         const char* errmsg) {
426   THROW_ERROR(v8::Exception::TypeError);
427 }
428
429 inline void Environment::ThrowRangeError(v8::Isolate* isolate,
430                                          const char* errmsg) {
431   THROW_ERROR(v8::Exception::RangeError);
432 }
433
434 inline void Environment::ThrowError(const char* errmsg) {
435   ThrowError(isolate(), errmsg);
436 }
437
438 inline void Environment::ThrowTypeError(const char* errmsg) {
439   ThrowTypeError(isolate(), errmsg);
440 }
441
442 inline void Environment::ThrowRangeError(const char* errmsg) {
443   ThrowRangeError(isolate(), errmsg);
444 }
445
446 inline void Environment::ThrowErrnoException(int errorno,
447                                              const char* syscall,
448                                              const char* message,
449                                              const char* path) {
450   isolate()->ThrowException(
451       ErrnoException(isolate(), errorno, syscall, message, path));
452 }
453
454 inline void Environment::ThrowUVException(int errorno,
455                                           const char* syscall,
456                                           const char* message,
457                                           const char* path,
458                                           const char* dest) {
459   isolate()->ThrowException(
460       UVException(isolate(), errorno, syscall, message, path, dest));
461 }
462
463 inline v8::Local<v8::FunctionTemplate>
464     Environment::NewFunctionTemplate(v8::FunctionCallback callback,
465                                      v8::Local<v8::Signature> signature) {
466   v8::Local<v8::External> external = as_external();
467   return v8::FunctionTemplate::New(isolate(), callback, external, signature);
468 }
469
470 inline void Environment::SetMethod(v8::Local<v8::Object> that,
471                                    const char* name,
472                                    v8::FunctionCallback callback) {
473   v8::Local<v8::Function> function =
474       NewFunctionTemplate(callback)->GetFunction();
475   // kInternalized strings are created in the old space.
476   const v8::NewStringType type = v8::NewStringType::kInternalized;
477   v8::Local<v8::String> name_string =
478       v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
479   that->Set(name_string, function);
480   function->SetName(name_string);  // NODE_SET_METHOD() compatibility.
481 }
482
483 inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
484                                         const char* name,
485                                         v8::FunctionCallback callback) {
486   v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that);
487   v8::Local<v8::Function> function =
488       NewFunctionTemplate(callback, signature)->GetFunction();
489   // kInternalized strings are created in the old space.
490   const v8::NewStringType type = v8::NewStringType::kInternalized;
491   v8::Local<v8::String> name_string =
492       v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
493   that->PrototypeTemplate()->Set(name_string, function);
494   function->SetName(name_string);  // NODE_SET_PROTOTYPE_METHOD() compatibility.
495 }
496
497 inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
498                                            const char* name,
499                                            v8::FunctionCallback callback) {
500   v8::Local<v8::Function> function =
501       NewFunctionTemplate(callback)->GetFunction();
502   // kInternalized strings are created in the old space.
503   const v8::NewStringType type = v8::NewStringType::kInternalized;
504   v8::Local<v8::String> name_string =
505       v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
506   that->Set(name_string, function);
507   function->SetName(name_string);  // NODE_SET_METHOD() compatibility.
508 }
509
510 inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
511   v8::MaybeLocal<v8::Object> m_obj =
512       generic_internal_field_template()->NewInstance(context());
513   return m_obj.ToLocalChecked();
514 }
515
516 #define V(PropertyName, StringValue)                                          \
517   inline                                                                      \
518   v8::Local<v8::String> Environment::IsolateData::PropertyName() const {      \
519     /* Strings are immutable so casting away const-ness here is okay. */      \
520     return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate());  \
521   }
522   PER_ISOLATE_STRING_PROPERTIES(V)
523 #undef V
524
525 #define V(PropertyName, StringValue)                                          \
526   inline v8::Local<v8::String> Environment::PropertyName() const {            \
527     return isolate_data()->PropertyName();                                    \
528   }
529   PER_ISOLATE_STRING_PROPERTIES(V)
530 #undef V
531
532 #define V(PropertyName, TypeName)                                             \
533   inline v8::Local<TypeName> Environment::PropertyName() const {              \
534     return StrongPersistentToLocal(PropertyName ## _);                        \
535   }                                                                           \
536   inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) {  \
537     PropertyName ## _.Reset(isolate(), value);                                \
538   }
539   ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
540 #undef V
541
542 #undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES
543 #undef PER_ISOLATE_STRING_PROPERTIES
544
545 }  // namespace node
546
547 #endif  // SRC_ENV_INL_H_