1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #ifndef SRC_ENV_INL_H_
23 #define SRC_ENV_INL_H_
36 inline Environment::IsolateData* Environment::IsolateData::GetOrCreate(
37 v8::Isolate* isolate) {
38 IsolateData* isolate_data = static_cast<IsolateData*>(isolate->GetData());
39 if (isolate_data == NULL) {
40 isolate_data = new IsolateData(isolate);
41 isolate->SetData(isolate_data);
43 isolate_data->ref_count_ += 1;
47 inline void Environment::IsolateData::Put() {
48 if (--ref_count_ == 0) {
49 isolate()->SetData(NULL);
54 inline Environment::IsolateData::IsolateData(v8::Isolate* isolate)
55 : event_loop_(uv_default_loop()),
57 #define V(PropertyName, StringValue) \
58 PropertyName ## _(isolate, FIXED_ONE_BYTE_STRING(isolate, StringValue)),
59 PER_ISOLATE_STRING_PROPERTIES(V)
64 inline uv_loop_t* Environment::IsolateData::event_loop() const {
68 inline v8::Isolate* Environment::IsolateData::isolate() const {
72 inline Environment::DomainFlag::DomainFlag() {
73 for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
76 inline uint32_t* Environment::DomainFlag::fields() {
80 inline int Environment::DomainFlag::fields_count() const {
84 inline uint32_t Environment::DomainFlag::count() const {
85 return fields_[kCount];
88 inline Environment::TickInfo::TickInfo() {
89 for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
92 inline uint32_t* Environment::TickInfo::fields() {
96 inline int Environment::TickInfo::fields_count() const {
100 inline uint32_t Environment::TickInfo::in_tick() const {
101 return fields_[kInTick];
104 inline uint32_t Environment::TickInfo::index() const {
105 return fields_[kIndex];
108 inline uint32_t Environment::TickInfo::last_threw() const {
109 return fields_[kLastThrew];
112 inline uint32_t Environment::TickInfo::length() const {
113 return fields_[kLength];
116 inline void Environment::TickInfo::set_index(uint32_t value) {
117 fields_[kIndex] = value;
120 inline void Environment::TickInfo::set_last_threw(uint32_t value) {
121 fields_[kLastThrew] = value;
124 inline Environment* Environment::New(v8::Local<v8::Context> context) {
125 Environment* env = new Environment(context);
126 context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, env);
130 inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
131 return GetCurrent(isolate->GetCurrentContext());
134 inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
135 return static_cast<Environment*>(
136 context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
139 inline Environment* Environment::GetCurrentChecked(v8::Isolate* isolate) {
140 if (isolate == NULL) {
143 return GetCurrentChecked(isolate->GetCurrentContext());
147 inline Environment* Environment::GetCurrentChecked(
148 v8::Local<v8::Context> context) {
149 if (context.IsEmpty()) {
152 return GetCurrent(context);
156 inline Environment::Environment(v8::Local<v8::Context> context)
157 : isolate_(context->GetIsolate()),
158 isolate_data_(IsolateData::GetOrCreate(context->GetIsolate())),
159 using_smalloc_alloc_cb_(false),
160 using_domains_(false),
161 context_(context->GetIsolate(), context) {
162 // We'll be creating new objects so make sure we've entered the context.
163 v8::Context::Scope context_scope(context);
164 v8::HandleScope handle_scope(isolate());
165 set_binding_cache_object(v8::Object::New());
166 set_module_load_list_array(v8::Array::New());
167 RB_INIT(&cares_task_list_);
170 inline Environment::~Environment() {
171 context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, NULL);
172 #define V(PropertyName, TypeName) PropertyName ## _.Dispose();
173 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
175 isolate_data()->Put();
178 inline void Environment::Dispose() {
182 inline v8::Isolate* Environment::isolate() const {
186 inline bool Environment::in_domain() const {
187 // The const_cast is okay, it doesn't violate conceptual const-ness.
188 return using_domains() &&
189 const_cast<Environment*>(this)->domain_flag()->count() > 0;
192 inline Environment* Environment::from_immediate_check_handle(
193 uv_check_t* handle) {
194 return CONTAINER_OF(handle, Environment, immediate_check_handle_);
197 inline uv_check_t* Environment::immediate_check_handle() {
198 return &immediate_check_handle_;
201 inline uv_idle_t* Environment::immediate_idle_handle() {
202 return &immediate_idle_handle_;
205 inline Environment* Environment::from_idle_prepare_handle(
206 uv_prepare_t* handle) {
207 return CONTAINER_OF(handle, Environment, idle_prepare_handle_);
210 inline uv_prepare_t* Environment::idle_prepare_handle() {
211 return &idle_prepare_handle_;
214 inline Environment* Environment::from_idle_check_handle(uv_check_t* handle) {
215 return CONTAINER_OF(handle, Environment, idle_check_handle_);
218 inline uv_check_t* Environment::idle_check_handle() {
219 return &idle_check_handle_;
222 inline uv_loop_t* Environment::event_loop() const {
223 return isolate_data()->event_loop();
226 inline Environment::DomainFlag* Environment::domain_flag() {
227 return &domain_flag_;
230 inline Environment::TickInfo* Environment::tick_info() {
234 inline bool Environment::using_smalloc_alloc_cb() const {
235 return using_smalloc_alloc_cb_;
238 inline void Environment::set_using_smalloc_alloc_cb(bool value) {
239 using_smalloc_alloc_cb_ = value;
242 inline bool Environment::using_domains() const {
243 return using_domains_;
246 inline void Environment::set_using_domains(bool value) {
247 using_domains_ = value;
250 inline Environment* Environment::from_cares_timer_handle(uv_timer_t* handle) {
251 return CONTAINER_OF(handle, Environment, cares_timer_handle_);
254 inline uv_timer_t* Environment::cares_timer_handle() {
255 return &cares_timer_handle_;
258 inline ares_channel Environment::cares_channel() {
259 return cares_channel_;
262 // Only used in the call to ares_init_options().
263 inline ares_channel* Environment::cares_channel_ptr() {
264 return &cares_channel_;
267 inline ares_task_list* Environment::cares_task_list() {
268 return &cares_task_list_;
271 inline Environment::IsolateData* Environment::isolate_data() const {
272 return isolate_data_;
275 #define V(PropertyName, StringValue) \
277 v8::Local<v8::String> Environment::IsolateData::PropertyName() const { \
278 /* Strings are immutable so casting away const-ness here is okay. */ \
279 return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate()); \
281 PER_ISOLATE_STRING_PROPERTIES(V)
284 #define V(PropertyName, StringValue) \
285 inline v8::Local<v8::String> Environment::PropertyName() const { \
286 return isolate_data()->PropertyName(); \
288 PER_ISOLATE_STRING_PROPERTIES(V)
291 #define V(PropertyName, TypeName) \
292 inline v8::Local<TypeName> Environment::PropertyName() const { \
293 return StrongPersistentToLocal(PropertyName ## _); \
295 inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) { \
296 PropertyName ## _.Reset(isolate(), value); \
298 ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
301 #undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES
302 #undef PER_ISOLATE_STRING_PROPERTIES
306 #endif // SRC_ENV_INL_H_