nan_implementation_12_inl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #ifndef NAN_IMPLEMENTATION_12_INL_H_
  9. #define NAN_IMPLEMENTATION_12_INL_H_
  10. //==============================================================================
  11. // node v0.11 implementation
  12. //==============================================================================
  13. namespace imp {
  14. //=== Array ====================================================================
  15. Factory<v8::Array>::return_t
  16. Factory<v8::Array>::New() {
  17. return v8::Array::New(v8::Isolate::GetCurrent());
  18. }
  19. Factory<v8::Array>::return_t
  20. Factory<v8::Array>::New(int length) {
  21. return v8::Array::New(v8::Isolate::GetCurrent(), length);
  22. }
  23. //=== Boolean ==================================================================
  24. Factory<v8::Boolean>::return_t
  25. Factory<v8::Boolean>::New(bool value) {
  26. return v8::Boolean::New(v8::Isolate::GetCurrent(), value);
  27. }
  28. //=== Boolean Object ===========================================================
  29. Factory<v8::BooleanObject>::return_t
  30. Factory<v8::BooleanObject>::New(bool value) {
  31. #if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  32. return v8::BooleanObject::New(
  33. v8::Isolate::GetCurrent(), value).As<v8::BooleanObject>();
  34. #else
  35. return v8::BooleanObject::New(value).As<v8::BooleanObject>();
  36. #endif
  37. }
  38. //=== Context ==================================================================
  39. Factory<v8::Context>::return_t
  40. Factory<v8::Context>::New( v8::ExtensionConfiguration* extensions
  41. , v8::Local<v8::ObjectTemplate> tmpl
  42. , v8::Local<v8::Value> obj) {
  43. return v8::Context::New(v8::Isolate::GetCurrent(), extensions, tmpl, obj);
  44. }
  45. //=== Date =====================================================================
  46. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  47. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  48. Factory<v8::Date>::return_t
  49. Factory<v8::Date>::New(double value) {
  50. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  51. v8::EscapableHandleScope scope(isolate);
  52. return scope.Escape(v8::Date::New(isolate->GetCurrentContext(), value)
  53. .FromMaybe(v8::Local<v8::Value>()).As<v8::Date>());
  54. }
  55. #else
  56. Factory<v8::Date>::return_t
  57. Factory<v8::Date>::New(double value) {
  58. return v8::Date::New(v8::Isolate::GetCurrent(), value).As<v8::Date>();
  59. }
  60. #endif
  61. //=== External =================================================================
  62. Factory<v8::External>::return_t
  63. Factory<v8::External>::New(void * value) {
  64. return v8::External::New(v8::Isolate::GetCurrent(), value);
  65. }
  66. //=== Function =================================================================
  67. Factory<v8::Function>::return_t
  68. Factory<v8::Function>::New( FunctionCallback callback
  69. , v8::Local<v8::Value> data) {
  70. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  71. v8::EscapableHandleScope scope(isolate);
  72. v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);
  73. tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  74. v8::Local<v8::Object> obj = NewInstance(tpl).ToLocalChecked();
  75. obj->SetInternalField(
  76. imp::kFunctionIndex
  77. , v8::External::New(isolate, reinterpret_cast<void *>(callback)));
  78. v8::Local<v8::Value> val = v8::Local<v8::Value>::New(isolate, data);
  79. if (!val.IsEmpty()) {
  80. obj->SetInternalField(imp::kDataIndex, val);
  81. }
  82. return scope.Escape(v8::Function::New( isolate
  83. , imp::FunctionCallbackWrapper
  84. , obj));
  85. }
  86. //=== Function Template ========================================================
  87. Factory<v8::FunctionTemplate>::return_t
  88. Factory<v8::FunctionTemplate>::New( FunctionCallback callback
  89. , v8::Local<v8::Value> data
  90. , v8::Local<v8::Signature> signature) {
  91. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  92. if (callback) {
  93. v8::EscapableHandleScope scope(isolate);
  94. v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);
  95. tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  96. v8::Local<v8::Object> obj = NewInstance(tpl).ToLocalChecked();
  97. obj->SetInternalField(
  98. imp::kFunctionIndex
  99. , v8::External::New(isolate, reinterpret_cast<void *>(callback)));
  100. v8::Local<v8::Value> val = v8::Local<v8::Value>::New(isolate, data);
  101. if (!val.IsEmpty()) {
  102. obj->SetInternalField(imp::kDataIndex, val);
  103. }
  104. return scope.Escape(v8::FunctionTemplate::New( isolate
  105. , imp::FunctionCallbackWrapper
  106. , obj
  107. , signature));
  108. } else {
  109. return v8::FunctionTemplate::New(isolate, 0, data, signature);
  110. }
  111. }
  112. //=== Number ===================================================================
  113. Factory<v8::Number>::return_t
  114. Factory<v8::Number>::New(double value) {
  115. return v8::Number::New(v8::Isolate::GetCurrent(), value);
  116. }
  117. //=== Number Object ============================================================
  118. Factory<v8::NumberObject>::return_t
  119. Factory<v8::NumberObject>::New(double value) {
  120. return v8::NumberObject::New( v8::Isolate::GetCurrent()
  121. , value).As<v8::NumberObject>();
  122. }
  123. //=== Integer, Int32 and Uint32 ================================================
  124. template <typename T>
  125. typename IntegerFactory<T>::return_t
  126. IntegerFactory<T>::New(int32_t value) {
  127. return To<T>(T::New(v8::Isolate::GetCurrent(), value));
  128. }
  129. template <typename T>
  130. typename IntegerFactory<T>::return_t
  131. IntegerFactory<T>::New(uint32_t value) {
  132. return To<T>(T::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  133. }
  134. Factory<v8::Uint32>::return_t
  135. Factory<v8::Uint32>::New(int32_t value) {
  136. return To<v8::Uint32>(
  137. v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  138. }
  139. Factory<v8::Uint32>::return_t
  140. Factory<v8::Uint32>::New(uint32_t value) {
  141. return To<v8::Uint32>(
  142. v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  143. }
  144. //=== Object ===================================================================
  145. Factory<v8::Object>::return_t
  146. Factory<v8::Object>::New() {
  147. return v8::Object::New(v8::Isolate::GetCurrent());
  148. }
  149. //=== Object Template ==========================================================
  150. Factory<v8::ObjectTemplate>::return_t
  151. Factory<v8::ObjectTemplate>::New() {
  152. return v8::ObjectTemplate::New(v8::Isolate::GetCurrent());
  153. }
  154. //=== RegExp ===================================================================
  155. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  156. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  157. Factory<v8::RegExp>::return_t
  158. Factory<v8::RegExp>::New(
  159. v8::Local<v8::String> pattern
  160. , v8::RegExp::Flags flags) {
  161. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  162. v8::EscapableHandleScope scope(isolate);
  163. return scope.Escape(
  164. v8::RegExp::New(isolate->GetCurrentContext(), pattern, flags)
  165. .FromMaybe(v8::Local<v8::RegExp>()));
  166. }
  167. #else
  168. Factory<v8::RegExp>::return_t
  169. Factory<v8::RegExp>::New(
  170. v8::Local<v8::String> pattern
  171. , v8::RegExp::Flags flags) {
  172. return v8::RegExp::New(pattern, flags);
  173. }
  174. #endif
  175. //=== Script ===================================================================
  176. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  177. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  178. Factory<v8::Script>::return_t
  179. Factory<v8::Script>::New( v8::Local<v8::String> source) {
  180. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  181. v8::EscapableHandleScope scope(isolate);
  182. v8::ScriptCompiler::Source src(source);
  183. return scope.Escape(
  184. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &src)
  185. .FromMaybe(v8::Local<v8::Script>()));
  186. }
  187. Factory<v8::Script>::return_t
  188. Factory<v8::Script>::New( v8::Local<v8::String> source
  189. , v8::ScriptOrigin const& origin) {
  190. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  191. v8::EscapableHandleScope scope(isolate);
  192. v8::ScriptCompiler::Source src(source, origin);
  193. return scope.Escape(
  194. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &src)
  195. .FromMaybe(v8::Local<v8::Script>()));
  196. }
  197. #else
  198. Factory<v8::Script>::return_t
  199. Factory<v8::Script>::New( v8::Local<v8::String> source) {
  200. v8::ScriptCompiler::Source src(source);
  201. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
  202. }
  203. Factory<v8::Script>::return_t
  204. Factory<v8::Script>::New( v8::Local<v8::String> source
  205. , v8::ScriptOrigin const& origin) {
  206. v8::ScriptCompiler::Source src(source, origin);
  207. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
  208. }
  209. #endif
  210. //=== Signature ================================================================
  211. Factory<v8::Signature>::return_t
  212. Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
  213. return v8::Signature::New(v8::Isolate::GetCurrent(), receiver);
  214. }
  215. //=== String ===================================================================
  216. Factory<v8::String>::return_t
  217. Factory<v8::String>::New() {
  218. return v8::String::Empty(v8::Isolate::GetCurrent());
  219. }
  220. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  221. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  222. Factory<v8::String>::return_t
  223. Factory<v8::String>::New(const char * value, int length) {
  224. return v8::String::NewFromUtf8(
  225. v8::Isolate::GetCurrent(), value, v8::NewStringType::kNormal, length);
  226. }
  227. Factory<v8::String>::return_t
  228. Factory<v8::String>::New(std::string const& value) {
  229. assert(value.size() <= INT_MAX && "string too long");
  230. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(),
  231. value.data(), v8::NewStringType::kNormal, static_cast<int>(value.size()));
  232. }
  233. Factory<v8::String>::return_t
  234. Factory<v8::String>::New(const uint16_t * value, int length) {
  235. return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
  236. v8::NewStringType::kNormal, length);
  237. }
  238. Factory<v8::String>::return_t
  239. Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
  240. return v8::String::NewExternalTwoByte(v8::Isolate::GetCurrent(), value);
  241. }
  242. Factory<v8::String>::return_t
  243. Factory<v8::String>::New(ExternalOneByteStringResource * value) {
  244. return v8::String::NewExternalOneByte(v8::Isolate::GetCurrent(), value);
  245. }
  246. #else
  247. Factory<v8::String>::return_t
  248. Factory<v8::String>::New(const char * value, int length) {
  249. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value,
  250. v8::String::kNormalString, length);
  251. }
  252. Factory<v8::String>::return_t
  253. Factory<v8::String>::New(
  254. std::string const& value) /* NOLINT(build/include_what_you_use) */ {
  255. assert(value.size() <= INT_MAX && "string too long");
  256. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value.data(),
  257. v8::String::kNormalString,
  258. static_cast<int>(value.size()));
  259. }
  260. Factory<v8::String>::return_t
  261. Factory<v8::String>::New(const uint16_t * value, int length) {
  262. return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
  263. v8::String::kNormalString, length);
  264. }
  265. Factory<v8::String>::return_t
  266. Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
  267. return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
  268. }
  269. Factory<v8::String>::return_t
  270. Factory<v8::String>::New(ExternalOneByteStringResource * value) {
  271. return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
  272. }
  273. #endif
  274. //=== String Object ============================================================
  275. Factory<v8::StringObject>::return_t
  276. Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
  277. return v8::StringObject::New(value).As<v8::StringObject>();
  278. }
  279. //=== Unbound Script ===========================================================
  280. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  281. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  282. Factory<v8::UnboundScript>::return_t
  283. Factory<v8::UnboundScript>::New(v8::Local<v8::String> source) {
  284. v8::ScriptCompiler::Source src(source);
  285. return v8::ScriptCompiler::CompileUnboundScript(
  286. v8::Isolate::GetCurrent(), &src);
  287. }
  288. Factory<v8::UnboundScript>::return_t
  289. Factory<v8::UnboundScript>::New( v8::Local<v8::String> source
  290. , v8::ScriptOrigin const& origin) {
  291. v8::ScriptCompiler::Source src(source, origin);
  292. return v8::ScriptCompiler::CompileUnboundScript(
  293. v8::Isolate::GetCurrent(), &src);
  294. }
  295. #else
  296. Factory<v8::UnboundScript>::return_t
  297. Factory<v8::UnboundScript>::New(v8::Local<v8::String> source) {
  298. v8::ScriptCompiler::Source src(source);
  299. return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
  300. }
  301. Factory<v8::UnboundScript>::return_t
  302. Factory<v8::UnboundScript>::New( v8::Local<v8::String> source
  303. , v8::ScriptOrigin const& origin) {
  304. v8::ScriptCompiler::Source src(source, origin);
  305. return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
  306. }
  307. #endif
  308. } // end of namespace imp
  309. //=== Presistents and Handles ==================================================
  310. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  311. template <typename T>
  312. inline v8::Local<T> New(v8::Handle<T> h) {
  313. return v8::Local<T>::New(v8::Isolate::GetCurrent(), h);
  314. }
  315. #endif
  316. template <typename T, typename M>
  317. inline v8::Local<T> New(v8::Persistent<T, M> const& p) {
  318. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  319. }
  320. template <typename T, typename M>
  321. inline v8::Local<T> New(Persistent<T, M> const& p) {
  322. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  323. }
  324. template <typename T>
  325. inline v8::Local<T> New(Global<T> const& p) {
  326. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  327. }
  328. #endif // NAN_IMPLEMENTATION_12_INL_H_