16 inline Environment::IsolateData* Environment::IsolateData::Get(
17 v8::Isolate* isolate) {
18 return static_cast<IsolateData*>(isolate->GetData(kIsolateSlot));
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);
28 isolate_data->ref_count_ += 1;
32 inline void Environment::IsolateData::Put() {
33 if (--ref_count_ == 0) {
34 isolate()->SetData(kIsolateSlot, nullptr);
39 // Create string properties as internalized one byte strings.
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.
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,
52 #define V(PropertyName, StringValue) \
53 PropertyName ## _(isolate, \
54 v8::String::NewFromOneByte( \
56 reinterpret_cast<const uint8_t*>(StringValue), \
57 v8::NewStringType::kInternalized, \
58 sizeof(StringValue) - 1).ToLocalChecked()),
59 PER_ISOLATE_STRING_PROPERTIES(V)
63 inline uv_loop_t* Environment::IsolateData::event_loop() const {
67 inline v8::Isolate* Environment::IsolateData::isolate() const {
71 inline Environment::AsyncHooks::AsyncHooks() {
72 for (int i = 0; i < kFieldsCount; i++) fields_[i] = 0;
75 inline uint32_t* Environment::AsyncHooks::fields() {
79 inline int Environment::AsyncHooks::fields_count() const {
83 inline bool Environment::AsyncHooks::callbacks_enabled() {
84 return fields_[kEnableCallbacks] != 0;
87 inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
88 fields_[kEnableCallbacks] = flag;
91 inline Environment::DomainFlag::DomainFlag() {
92 for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
95 inline uint32_t* Environment::DomainFlag::fields() {
99 inline int Environment::DomainFlag::fields_count() const {
103 inline uint32_t Environment::DomainFlag::count() const {
104 return fields_[kCount];
107 inline Environment::TickInfo::TickInfo() : in_tick_(false), last_threw_(false) {
108 for (int i = 0; i < kFieldsCount; ++i)
112 inline uint32_t* Environment::TickInfo::fields() {
116 inline int Environment::TickInfo::fields_count() const {
120 inline bool Environment::TickInfo::in_tick() const {
124 inline uint32_t Environment::TickInfo::index() const {
125 return fields_[kIndex];
128 inline bool Environment::TickInfo::last_threw() const {
132 inline uint32_t Environment::TickInfo::length() const {
133 return fields_[kLength];
136 inline void Environment::TickInfo::set_in_tick(bool value) {
140 inline void Environment::TickInfo::set_index(uint32_t value) {
141 fields_[kIndex] = value;
144 inline void Environment::TickInfo::set_last_threw(bool value) {
148 inline Environment::ArrayBufferAllocatorInfo::ArrayBufferAllocatorInfo() {
149 for (int i = 0; i < kFieldsCount; ++i)
153 inline uint32_t* Environment::ArrayBufferAllocatorInfo::fields() {
157 inline int Environment::ArrayBufferAllocatorInfo::fields_count() const {
161 inline bool Environment::ArrayBufferAllocatorInfo::no_zero_fill() const {
162 return fields_[kNoZeroFill] != 0;
165 inline void Environment::ArrayBufferAllocatorInfo::reset_fill_flag() {
166 fields_[kNoZeroFill] = 0;
169 inline Environment* Environment::New(v8::Local<v8::Context> context,
171 Environment* env = new Environment(context, loop);
172 env->AssignToContext(context);
176 inline void Environment::AssignToContext(v8::Local<v8::Context> context) {
177 context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
180 inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
181 return GetCurrent(isolate->GetCurrentContext());
184 inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
185 return static_cast<Environment*>(
186 context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
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());
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());
205 inline Environment::Environment(v8::Local<v8::Context> context,
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),
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()));
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);
230 RB_INIT(&cares_task_list_);
231 handle_cleanup_waiting_ = 0;
234 inline Environment::~Environment() {
235 v8::HandleScope handle_scope(isolate());
237 context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex,
239 #define V(PropertyName, TypeName) PropertyName ## _.Reset();
240 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
242 isolate_data()->Put();
244 delete[] heap_statistics_buffer_;
245 delete[] http_parser_buffer_;
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_);
255 while (handle_cleanup_waiting_ != 0)
256 uv_run(event_loop(), UV_RUN_ONCE);
259 inline void Environment::Dispose() {
263 inline v8::Isolate* Environment::isolate() const {
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();
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;
278 inline Environment* Environment::from_immediate_check_handle(
279 uv_check_t* handle) {
280 return ContainerOf(&Environment::immediate_check_handle_, handle);
283 inline uv_check_t* Environment::immediate_check_handle() {
284 return &immediate_check_handle_;
287 inline uv_idle_t* Environment::immediate_idle_handle() {
288 return &immediate_idle_handle_;
291 inline Environment* Environment::from_idle_prepare_handle(
292 uv_prepare_t* handle) {
293 return ContainerOf(&Environment::idle_prepare_handle_, handle);
296 inline uv_prepare_t* Environment::idle_prepare_handle() {
297 return &idle_prepare_handle_;
300 inline Environment* Environment::from_idle_check_handle(uv_check_t* handle) {
301 return ContainerOf(&Environment::idle_check_handle_, handle);
304 inline uv_check_t* Environment::idle_check_handle() {
305 return &idle_check_handle_;
308 inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
311 handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
314 inline void Environment::FinishHandleCleanup(uv_handle_t* handle) {
315 handle_cleanup_waiting_--;
318 inline uv_loop_t* Environment::event_loop() const {
319 return isolate_data()->event_loop();
322 inline Environment::AsyncHooks* Environment::async_hooks() {
323 return &async_hooks_;
326 inline Environment::DomainFlag* Environment::domain_flag() {
327 return &domain_flag_;
330 inline Environment::TickInfo* Environment::tick_info() {
334 inline Environment::ArrayBufferAllocatorInfo*
335 Environment::array_buffer_allocator_info() {
336 return &array_buffer_allocator_info_;
339 inline uint64_t Environment::timer_base() const {
343 inline bool Environment::using_domains() const {
344 return using_domains_;
347 inline void Environment::set_using_domains(bool value) {
348 using_domains_ = value;
351 inline bool Environment::printed_error() const {
352 return printed_error_;
355 inline void Environment::set_printed_error(bool value) {
356 printed_error_ = value;
359 inline void Environment::set_trace_sync_io(bool value) {
360 trace_sync_io_ = value;
363 inline int64_t Environment::get_async_wrap_uid() {
364 return ++async_wrap_uid_;
367 inline uint32_t* Environment::heap_statistics_buffer() const {
368 CHECK_NE(heap_statistics_buffer_, nullptr);
369 return heap_statistics_buffer_;
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;
377 inline char* Environment::http_parser_buffer() const {
378 return http_parser_buffer_;
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;
386 inline Environment* Environment::from_cares_timer_handle(uv_timer_t* handle) {
387 return ContainerOf(&Environment::cares_timer_handle_, handle);
390 inline uv_timer_t* Environment::cares_timer_handle() {
391 return &cares_timer_handle_;
394 inline ares_channel Environment::cares_channel() {
395 return cares_channel_;
398 // Only used in the call to ares_init_options().
399 inline ares_channel* Environment::cares_channel_ptr() {
400 return &cares_channel_;
403 inline ares_task_list* Environment::cares_task_list() {
404 return &cares_task_list_;
407 inline Environment::IsolateData* Environment::isolate_data() const {
408 return isolate_data_;
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) \
415 v8::HandleScope scope(isolate); \
416 isolate->ThrowException(fun(OneByteString(isolate, errmsg))); \
420 inline void Environment::ThrowError(v8::Isolate* isolate, const char* errmsg) {
421 THROW_ERROR(v8::Exception::Error);
424 inline void Environment::ThrowTypeError(v8::Isolate* isolate,
425 const char* errmsg) {
426 THROW_ERROR(v8::Exception::TypeError);
429 inline void Environment::ThrowRangeError(v8::Isolate* isolate,
430 const char* errmsg) {
431 THROW_ERROR(v8::Exception::RangeError);
434 inline void Environment::ThrowError(const char* errmsg) {
435 ThrowError(isolate(), errmsg);
438 inline void Environment::ThrowTypeError(const char* errmsg) {
439 ThrowTypeError(isolate(), errmsg);
442 inline void Environment::ThrowRangeError(const char* errmsg) {
443 ThrowRangeError(isolate(), errmsg);
446 inline void Environment::ThrowErrnoException(int errorno,
450 isolate()->ThrowException(
451 ErrnoException(isolate(), errorno, syscall, message, path));
454 inline void Environment::ThrowUVException(int errorno,
459 isolate()->ThrowException(
460 UVException(isolate(), errorno, syscall, message, path, dest));
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);
470 inline void Environment::SetMethod(v8::Local<v8::Object> that,
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.
483 inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
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.
497 inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
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.
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();
516 #define V(PropertyName, StringValue) \
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()); \
522 PER_ISOLATE_STRING_PROPERTIES(V)
525 #define V(PropertyName, StringValue) \
526 inline v8::Local<v8::String> Environment::PropertyName() const { \
527 return isolate_data()->PropertyName(); \
529 PER_ISOLATE_STRING_PROPERTIES(V)
532 #define V(PropertyName, TypeName) \
533 inline v8::Local<TypeName> Environment::PropertyName() const { \
534 return StrongPersistentToLocal(PropertyName ## _); \
536 inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
537 PropertyName ## _.Reset(isolate(), value); \
539 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
542 #undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES
543 #undef PER_ISOLATE_STRING_PROPERTIES
547 #endif // SRC_ENV_INL_H_