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