src: update after v8 api changes
[platform/upstream/nodejs.git] / src / env.h
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 #include "ares.h"
26 #include "tree.h"
27 #include "util.h"
28 #include "uv.h"
29 #include "v8.h"
30
31 #include <stdint.h>
32
33 // Caveat emptor: we're going slightly crazy with macros here but the end
34 // hopefully justifies the means. We have a lot of per-context properties
35 // and adding and maintaining their getters and setters by hand would be
36 // a nightmare so let's make the preprocessor generate them for us.
37 //
38 // Make sure that any macros defined here are undefined again at the bottom
39 // of context-inl.h. The sole exception is NODE_CONTEXT_EMBEDDER_DATA_INDEX,
40 // it may have been defined externally.
41 namespace node {
42
43 // Pick an index that's hopefully out of the way when we're embedded inside
44 // another application. Performance-wise or memory-wise it doesn't matter:
45 // Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
46 // worst case we pay a one-time penalty for resizing the array.
47 #ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
48 #define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
49 #endif
50
51 // Strings are per-isolate primitives but Environment proxies them
52 // for the sake of convenience.
53 #define PER_ISOLATE_STRING_PROPERTIES(V)                                      \
54   V(DELETE_string, "DELETE")                                                  \
55   V(GET_string, "GET")                                                        \
56   V(HEAD_string, "HEAD")                                                      \
57   V(POST_string, "POST")                                                      \
58   V(PUT_string, "PUT")                                                        \
59   V(address_string, "address")                                                \
60   V(atime_string, "atime")                                                    \
61   V(birthtime_string, "birthtime")                                            \
62   V(blksize_string, "blksize")                                                \
63   V(blocks_string, "blocks")                                                  \
64   V(buffer_string, "buffer")                                                  \
65   V(bytes_string, "bytes")                                                    \
66   V(callback_string, "callback")                                              \
67   V(change_string, "change")                                                  \
68   V(close_string, "close")                                                    \
69   V(code_string, "code")                                                      \
70   V(ctime_string, "ctime")                                                    \
71   V(dev_string, "dev")                                                        \
72   V(disposed_string, "_disposed")                                             \
73   V(domain_string, "domain")                                                  \
74   V(enter_string, "enter")                                                    \
75   V(errno_string, "errno")                                                    \
76   V(exit_string, "exit")                                                      \
77   V(exponent_string, "exponent")                                              \
78   V(exports_string, "exports")                                                \
79   V(ext_key_usage_string, "ext_key_usage")                                    \
80   V(family_string, "family")                                                  \
81   V(fatal_exception_string, "_fatalException")                                \
82   V(fingerprint_string, "fingerprint")                                        \
83   V(gid_string, "gid")                                                        \
84   V(handle_string, "handle")                                                  \
85   V(headers_string, "headers")                                                \
86   V(heap_total_string, "heapTotal")                                           \
87   V(heap_used_string, "heapUsed")                                             \
88   V(immediate_callback_string, "_immediateCallback")                          \
89   V(ino_string, "ino")                                                        \
90   V(ipv4_string, "IPv4")                                                      \
91   V(ipv6_string, "IPv6")                                                      \
92   V(issuer_string, "issuer")                                                  \
93   V(method_string, "method")                                                  \
94   V(mode_string, "mode")                                                      \
95   V(modulus_string, "modulus")                                                \
96   V(mtime_string, "mtime")                                                    \
97   V(name_string, "name")                                                      \
98   V(nlink_string, "nlink")                                                    \
99   V(onchange_string, "onchange")                                              \
100   V(onclienthello_string, "onclienthello")                                    \
101   V(oncomplete_string, "oncomplete")                                          \
102   V(onconnection_string, "onconnection")                                      \
103   V(onerror_string, "onerror")                                                \
104   V(onexit_string, "onexit")                                                  \
105   V(onhandshakedone_string, "onhandshakedone")                                \
106   V(onhandshakestart_string, "onhandshakestart")                              \
107   V(onmessage_string, "onmessage")                                            \
108   V(onnewsession_string, "onnewsession")                                      \
109   V(onread_string, "onread")                                                  \
110   V(onsignal_string, "onsignal")                                              \
111   V(onstop_string, "onstop")                                                  \
112   V(path_string, "path")                                                      \
113   V(port_string, "port")                                                      \
114   V(rdev_string, "rdev")                                                      \
115   V(rename_string, "rename")                                                  \
116   V(rss_string, "rss")                                                        \
117   V(servername_string, "servername")                                          \
118   V(session_id_string, "sessionId")                                           \
119   V(should_keep_alive_string, "shouldKeepAlive")                              \
120   V(size_string, "size")                                                      \
121   V(smalloc_p_string, "_smalloc_p")                                           \
122   V(sni_context_string, "sni_context")                                        \
123   V(status_code_string, "statusCode")                                         \
124   V(subject_string, "subject")                                                \
125   V(subjectaltname_string, "subjectaltname")                                  \
126   V(syscall_string, "syscall")                                                \
127   V(tls_ticket_string, "tlsTicket")                                           \
128   V(uid_string, "uid")                                                        \
129   V(upgrade_string, "upgrade")                                                \
130   V(url_string, "url")                                                        \
131   V(valid_from_string, "valid_from")                                          \
132   V(valid_to_string, "valid_to")                                              \
133   V(version_major_string, "versionMajor")                                     \
134   V(version_minor_string, "versionMinor")                                     \
135   V(version_string, "version")                                                \
136   V(write_queue_size_string, "writeQueueSize")                                \
137
138 #define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)                           \
139   V(binding_cache_object, v8::Object)                                         \
140   V(buffer_constructor_function, v8::Function)                                \
141   V(context, v8::Context)                                                     \
142   V(domain_array, v8::Array)                                                  \
143   V(module_load_list_array, v8::Array)                                        \
144   V(pipe_constructor_template, v8::FunctionTemplate)                          \
145   V(process_object, v8::Object)                                               \
146   V(script_context_constructor_template, v8::FunctionTemplate)                \
147   V(script_data_constructor_function, v8::Function)                           \
148   V(secure_context_constructor_template, v8::FunctionTemplate)                \
149   V(stats_constructor_function, v8::Function)                                 \
150   V(tcp_constructor_template, v8::FunctionTemplate)                           \
151   V(tick_callback_function, v8::Function)                                     \
152   V(tls_wrap_constructor_function, v8::Function)                              \
153   V(tty_constructor_template, v8::FunctionTemplate)                           \
154   V(udp_constructor_function, v8::Function)                                   \
155
156 class Environment;
157
158 // TODO(bnoordhuis) Rename struct, the ares_ prefix implies it's part
159 // of the c-ares API while the _t suffix implies it's a typedef.
160 struct ares_task_t {
161   Environment* env;
162   ares_socket_t sock;
163   uv_poll_t poll_watcher;
164   RB_ENTRY(ares_task_t) node;
165 };
166
167 RB_HEAD(ares_task_list, ares_task_t);
168
169 class Environment {
170  public:
171   class DomainFlag {
172    public:
173     inline uint32_t* fields();
174     inline int fields_count() const;
175     inline uint32_t count() const;
176
177    private:
178     friend class Environment;  // So we can call the constructor.
179     inline DomainFlag();
180
181     enum Fields {
182       kCount,
183       kFieldsCount
184     };
185
186     uint32_t fields_[kFieldsCount];
187
188     DISALLOW_COPY_AND_ASSIGN(DomainFlag);
189   };
190
191   class TickInfo {
192    public:
193     inline uint32_t* fields();
194     inline int fields_count() const;
195     inline uint32_t in_tick() const;
196     inline uint32_t index() const;
197     inline uint32_t last_threw() const;
198     inline uint32_t length() const;
199     inline void set_index(uint32_t value);
200     inline void set_last_threw(uint32_t value);
201
202    private:
203     friend class Environment;  // So we can call the constructor.
204     inline TickInfo();
205
206     enum Fields {
207       kInTick,
208       kIndex,
209       kLastThrew,
210       kLength,
211       kFieldsCount
212     };
213
214     uint32_t fields_[kFieldsCount];
215
216     DISALLOW_COPY_AND_ASSIGN(TickInfo);
217   };
218
219   static inline Environment* GetCurrent(v8::Isolate* isolate);
220   static inline Environment* GetCurrent(v8::Local<v8::Context> context);
221   static inline Environment* GetCurrentChecked(v8::Isolate* isolate);
222   static inline Environment* GetCurrentChecked(v8::Local<v8::Context> context);
223
224   // See CreateEnvironment() in src/node.cc.
225   static inline Environment* New(v8::Local<v8::Context> context);
226   inline void Dispose();
227
228   inline v8::Isolate* isolate() const;
229   inline uv_loop_t* event_loop() const;
230   inline bool in_domain() const;
231
232   static inline Environment* from_immediate_check_handle(uv_check_t* handle);
233   inline uv_check_t* immediate_check_handle();
234   inline uv_idle_t* immediate_idle_handle();
235
236   static inline Environment* from_idle_prepare_handle(uv_prepare_t* handle);
237   inline uv_prepare_t* idle_prepare_handle();
238
239   static inline Environment* from_idle_check_handle(uv_check_t* handle);
240   inline uv_check_t* idle_check_handle();
241
242   inline DomainFlag* domain_flag();
243   inline TickInfo* tick_info();
244
245   static inline Environment* from_cares_timer_handle(uv_timer_t* handle);
246   inline uv_timer_t* cares_timer_handle();
247   inline ares_channel cares_channel();
248   inline ares_channel* cares_channel_ptr();
249   inline ares_task_list* cares_task_list();
250
251   inline bool using_smalloc_alloc_cb() const;
252   inline void set_using_smalloc_alloc_cb(bool value);
253
254   inline bool using_domains() const;
255   inline void set_using_domains(bool value);
256
257   // Strings are shared across shared contexts. The getters simply proxy to
258   // the per-isolate primitive.
259 #define V(PropertyName, StringValue)                                          \
260   inline v8::Local<v8::String> PropertyName() const;
261   PER_ISOLATE_STRING_PROPERTIES(V)
262 #undef V
263
264 #define V(PropertyName, TypeName)                                             \
265   inline v8::Local<TypeName> PropertyName() const;                            \
266   inline void set_ ## PropertyName(v8::Local<TypeName> value);
267   ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
268 #undef V
269
270  private:
271   class IsolateData;
272   inline explicit Environment(v8::Local<v8::Context> context);
273   inline ~Environment();
274   inline IsolateData* isolate_data() const;
275
276   enum ContextEmbedderDataIndex {
277     kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX
278   };
279
280   v8::Isolate* const isolate_;
281   IsolateData* const isolate_data_;
282   uv_check_t immediate_check_handle_;
283   uv_idle_t immediate_idle_handle_;
284   uv_prepare_t idle_prepare_handle_;
285   uv_check_t idle_check_handle_;
286   DomainFlag domain_flag_;
287   TickInfo tick_info_;
288   uv_timer_t cares_timer_handle_;
289   ares_channel cares_channel_;
290   ares_task_list cares_task_list_;
291   bool using_smalloc_alloc_cb_;
292   bool using_domains_;
293
294 #define V(PropertyName, TypeName)                                             \
295   v8::Persistent<TypeName> PropertyName ## _;
296   ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
297 #undef V
298
299   // Per-thread, reference-counted singleton.
300   class IsolateData {
301    public:
302     static inline IsolateData* GetOrCreate(v8::Isolate* isolate);
303     inline void Put();
304     inline uv_loop_t* event_loop() const;
305
306 #define V(PropertyName, StringValue)                                          \
307     inline v8::Local<v8::String> PropertyName() const;
308     PER_ISOLATE_STRING_PROPERTIES(V)
309 #undef V
310
311    private:
312     inline explicit IsolateData(v8::Isolate* isolate);
313     inline v8::Isolate* isolate() const;
314
315     uv_loop_t* const event_loop_;
316     v8::Isolate* const isolate_;
317
318 #define V(PropertyName, StringValue)                                          \
319     v8::Eternal<v8::String> PropertyName ## _;
320     PER_ISOLATE_STRING_PROPERTIES(V)
321 #undef V
322
323     unsigned int ref_count_;
324
325     DISALLOW_COPY_AND_ASSIGN(IsolateData);
326   };
327
328   DISALLOW_COPY_AND_ASSIGN(Environment);
329 };
330
331 }  // namespace node
332
333 #endif  // SRC_ENV_H_