Initial commit
[yaffs-website] / node_modules / nan / nan_implementation_pre_12_inl.h
1 /*********************************************************************
2  * NAN - Native Abstractions for Node.js
3  *
4  * Copyright (c) 2017 NAN contributors
5  *
6  * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7  ********************************************************************/
8
9 #ifndef NAN_IMPLEMENTATION_PRE_12_INL_H_
10 #define NAN_IMPLEMENTATION_PRE_12_INL_H_
11
12 //==============================================================================
13 // node v0.10 implementation
14 //==============================================================================
15
16 namespace imp {
17
18 //=== Array ====================================================================
19
20 Factory<v8::Array>::return_t
21 Factory<v8::Array>::New() {
22   return v8::Array::New();
23 }
24
25 Factory<v8::Array>::return_t
26 Factory<v8::Array>::New(int length) {
27   return v8::Array::New(length);
28 }
29
30 //=== Boolean ==================================================================
31
32 Factory<v8::Boolean>::return_t
33 Factory<v8::Boolean>::New(bool value) {
34   return v8::Boolean::New(value)->ToBoolean();
35 }
36
37 //=== Boolean Object ===========================================================
38
39 Factory<v8::BooleanObject>::return_t
40 Factory<v8::BooleanObject>::New(bool value) {
41   return v8::BooleanObject::New(value).As<v8::BooleanObject>();
42 }
43
44 //=== Context ==================================================================
45
46 Factory<v8::Context>::return_t
47 Factory<v8::Context>::New( v8::ExtensionConfiguration* extensions
48                          , v8::Local<v8::ObjectTemplate> tmpl
49                          , v8::Local<v8::Value> obj) {
50   v8::Persistent<v8::Context> ctx = v8::Context::New(extensions, tmpl, obj);
51   v8::Local<v8::Context> lctx = v8::Local<v8::Context>::New(ctx);
52   ctx.Dispose();
53   return lctx;
54 }
55
56 //=== Date =====================================================================
57
58 Factory<v8::Date>::return_t
59 Factory<v8::Date>::New(double value) {
60   return v8::Date::New(value).As<v8::Date>();
61 }
62
63 //=== External =================================================================
64
65 Factory<v8::External>::return_t
66 Factory<v8::External>::New(void * value) {
67   return v8::External::New(value);
68 }
69
70 //=== Function =================================================================
71
72 Factory<v8::Function>::return_t
73 Factory<v8::Function>::New( FunctionCallback callback
74                           , v8::Local<v8::Value> data) {
75   v8::HandleScope scope;
76
77   return scope.Close(Factory<v8::FunctionTemplate>::New(
78                          callback, data, v8::Local<v8::Signature>())
79                          ->GetFunction());
80 }
81
82
83 //=== FunctionTemplate =========================================================
84
85 Factory<v8::FunctionTemplate>::return_t
86 Factory<v8::FunctionTemplate>::New( FunctionCallback callback
87                                   , v8::Local<v8::Value> data
88                                   , v8::Local<v8::Signature> signature) {
89   if (callback) {
90     v8::HandleScope scope;
91
92     v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
93     tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
94     v8::Local<v8::Object> obj = tpl->NewInstance();
95
96     obj->SetInternalField(
97         imp::kFunctionIndex
98       , v8::External::New(reinterpret_cast<void *>(callback)));
99
100     v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);
101
102     if (!val.IsEmpty()) {
103       obj->SetInternalField(imp::kDataIndex, val);
104     }
105
106     // Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
107     // a way. Have at it though...
108     return scope.Close(
109         v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
110                                  , obj
111                                  , signature));
112   } else {
113     return v8::FunctionTemplate::New(0, data, signature);
114   }
115 }
116
117 //=== Number ===================================================================
118
119 Factory<v8::Number>::return_t
120 Factory<v8::Number>::New(double value) {
121   return v8::Number::New(value);
122 }
123
124 //=== Number Object ============================================================
125
126 Factory<v8::NumberObject>::return_t
127 Factory<v8::NumberObject>::New(double value) {
128   return v8::NumberObject::New(value).As<v8::NumberObject>();
129 }
130
131 //=== Integer, Int32 and Uint32 ================================================
132
133 template <typename T>
134 typename IntegerFactory<T>::return_t
135 IntegerFactory<T>::New(int32_t value) {
136   return To<T>(T::New(value));
137 }
138
139 template <typename T>
140 typename IntegerFactory<T>::return_t
141 IntegerFactory<T>::New(uint32_t value) {
142   return To<T>(T::NewFromUnsigned(value));
143 }
144
145 Factory<v8::Uint32>::return_t
146 Factory<v8::Uint32>::New(int32_t value) {
147   return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
148 }
149
150 Factory<v8::Uint32>::return_t
151 Factory<v8::Uint32>::New(uint32_t value) {
152   return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
153 }
154
155
156 //=== Object ===================================================================
157
158 Factory<v8::Object>::return_t
159 Factory<v8::Object>::New() {
160   return v8::Object::New();
161 }
162
163 //=== Object Template ==========================================================
164
165 Factory<v8::ObjectTemplate>::return_t
166 Factory<v8::ObjectTemplate>::New() {
167   return v8::ObjectTemplate::New();
168 }
169
170 //=== RegExp ===================================================================
171
172 Factory<v8::RegExp>::return_t
173 Factory<v8::RegExp>::New(
174     v8::Local<v8::String> pattern
175   , v8::RegExp::Flags flags) {
176   return v8::RegExp::New(pattern, flags);
177 }
178
179 //=== Script ===================================================================
180
181 Factory<v8::Script>::return_t
182 Factory<v8::Script>::New( v8::Local<v8::String> source) {
183   return v8::Script::New(source);
184 }
185 Factory<v8::Script>::return_t
186 Factory<v8::Script>::New( v8::Local<v8::String> source
187                         , v8::ScriptOrigin const& origin) {
188   return v8::Script::New(source, const_cast<v8::ScriptOrigin*>(&origin));
189 }
190
191 //=== Signature ================================================================
192
193 Factory<v8::Signature>::return_t
194 Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
195   return v8::Signature::New(receiver);
196 }
197
198 //=== String ===================================================================
199
200 Factory<v8::String>::return_t
201 Factory<v8::String>::New() {
202   return v8::String::Empty();
203 }
204
205 Factory<v8::String>::return_t
206 Factory<v8::String>::New(const char * value, int length) {
207   return v8::String::New(value, length);
208 }
209
210 Factory<v8::String>::return_t
211 Factory<v8::String>::New(
212     std::string const& value) /* NOLINT(build/include_what_you_use) */ {
213   assert(value.size() <= INT_MAX && "string too long");
214   return v8::String::New(value.data(), static_cast<int>(value.size()));
215 }
216
217 Factory<v8::String>::return_t
218 Factory<v8::String>::New(const uint16_t * value, int length) {
219   return v8::String::New(value, length);
220 }
221
222 Factory<v8::String>::return_t
223 Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
224   return v8::String::NewExternal(value);
225 }
226
227 Factory<v8::String>::return_t
228 Factory<v8::String>::New(v8::String::ExternalAsciiStringResource * value) {
229   return v8::String::NewExternal(value);
230 }
231
232 //=== String Object ============================================================
233
234 Factory<v8::StringObject>::return_t
235 Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
236   return v8::StringObject::New(value).As<v8::StringObject>();
237 }
238
239 }  // end of namespace imp
240
241 //=== Presistents and Handles ==================================================
242
243 template <typename T>
244 inline v8::Local<T> New(v8::Handle<T> h) {
245   return v8::Local<T>::New(h);
246 }
247
248 template <typename T>
249 inline v8::Local<T> New(v8::Persistent<T> const& p) {
250   return v8::Local<T>::New(p);
251 }
252
253 template <typename T, typename M>
254 inline v8::Local<T> New(Persistent<T, M> const& p) {
255   return v8::Local<T>::New(p.persistent);
256 }
257
258 template <typename T>
259 inline v8::Local<T> New(Global<T> const& p) {
260   return v8::Local<T>::New(p.persistent);
261 }
262
263 #endif  // NAN_IMPLEMENTATION_PRE_12_INL_H_