nan_implementation_12_inl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2018 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. #if NODE_MAJOR_VERSION >= 10
  83. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  84. v8::Local<v8::Function> function =
  85. v8::Function::New(context, imp::FunctionCallbackWrapper, obj)
  86. .ToLocalChecked();
  87. #else
  88. v8::Local<v8::Function> function =
  89. v8::Function::New(isolate, imp::FunctionCallbackWrapper, obj);
  90. #endif
  91. return scope.Escape(function);
  92. }
  93. //=== Function Template ========================================================
  94. Factory<v8::FunctionTemplate>::return_t
  95. Factory<v8::FunctionTemplate>::New( FunctionCallback callback
  96. , v8::Local<v8::Value> data
  97. , v8::Local<v8::Signature> signature) {
  98. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  99. if (callback) {
  100. v8::EscapableHandleScope scope(isolate);
  101. v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);
  102. tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  103. v8::Local<v8::Object> obj = NewInstance(tpl).ToLocalChecked();
  104. obj->SetInternalField(
  105. imp::kFunctionIndex
  106. , v8::External::New(isolate, reinterpret_cast<void *>(callback)));
  107. v8::Local<v8::Value> val = v8::Local<v8::Value>::New(isolate, data);
  108. if (!val.IsEmpty()) {
  109. obj->SetInternalField(imp::kDataIndex, val);
  110. }
  111. return scope.Escape(v8::FunctionTemplate::New( isolate
  112. , imp::FunctionCallbackWrapper
  113. , obj
  114. , signature));
  115. } else {
  116. return v8::FunctionTemplate::New(isolate, 0, data, signature);
  117. }
  118. }
  119. //=== Number ===================================================================
  120. Factory<v8::Number>::return_t
  121. Factory<v8::Number>::New(double value) {
  122. return v8::Number::New(v8::Isolate::GetCurrent(), value);
  123. }
  124. //=== Number Object ============================================================
  125. Factory<v8::NumberObject>::return_t
  126. Factory<v8::NumberObject>::New(double value) {
  127. return v8::NumberObject::New( v8::Isolate::GetCurrent()
  128. , value).As<v8::NumberObject>();
  129. }
  130. //=== Integer, Int32 and Uint32 ================================================
  131. template <typename T>
  132. typename IntegerFactory<T>::return_t
  133. IntegerFactory<T>::New(int32_t value) {
  134. return To<T>(T::New(v8::Isolate::GetCurrent(), value));
  135. }
  136. template <typename T>
  137. typename IntegerFactory<T>::return_t
  138. IntegerFactory<T>::New(uint32_t value) {
  139. return To<T>(T::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  140. }
  141. Factory<v8::Uint32>::return_t
  142. Factory<v8::Uint32>::New(int32_t value) {
  143. return To<v8::Uint32>(
  144. v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  145. }
  146. Factory<v8::Uint32>::return_t
  147. Factory<v8::Uint32>::New(uint32_t value) {
  148. return To<v8::Uint32>(
  149. v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
  150. }
  151. //=== Object ===================================================================
  152. Factory<v8::Object>::return_t
  153. Factory<v8::Object>::New() {
  154. return v8::Object::New(v8::Isolate::GetCurrent());
  155. }
  156. //=== Object Template ==========================================================
  157. Factory<v8::ObjectTemplate>::return_t
  158. Factory<v8::ObjectTemplate>::New() {
  159. return v8::ObjectTemplate::New(v8::Isolate::GetCurrent());
  160. }
  161. //=== RegExp ===================================================================
  162. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  163. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  164. Factory<v8::RegExp>::return_t
  165. Factory<v8::RegExp>::New(
  166. v8::Local<v8::String> pattern
  167. , v8::RegExp::Flags flags) {
  168. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  169. v8::EscapableHandleScope scope(isolate);
  170. return scope.Escape(
  171. v8::RegExp::New(isolate->GetCurrentContext(), pattern, flags)
  172. .FromMaybe(v8::Local<v8::RegExp>()));
  173. }
  174. #else
  175. Factory<v8::RegExp>::return_t
  176. Factory<v8::RegExp>::New(
  177. v8::Local<v8::String> pattern
  178. , v8::RegExp::Flags flags) {
  179. return v8::RegExp::New(pattern, flags);
  180. }
  181. #endif
  182. //=== Script ===================================================================
  183. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  184. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  185. Factory<v8::Script>::return_t
  186. Factory<v8::Script>::New( v8::Local<v8::String> source) {
  187. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  188. v8::EscapableHandleScope scope(isolate);
  189. v8::ScriptCompiler::Source src(source);
  190. return scope.Escape(
  191. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &src)
  192. .FromMaybe(v8::Local<v8::Script>()));
  193. }
  194. Factory<v8::Script>::return_t
  195. Factory<v8::Script>::New( v8::Local<v8::String> source
  196. , v8::ScriptOrigin const& origin) {
  197. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  198. v8::EscapableHandleScope scope(isolate);
  199. v8::ScriptCompiler::Source src(source, origin);
  200. return scope.Escape(
  201. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &src)
  202. .FromMaybe(v8::Local<v8::Script>()));
  203. }
  204. #else
  205. Factory<v8::Script>::return_t
  206. Factory<v8::Script>::New( v8::Local<v8::String> source) {
  207. v8::ScriptCompiler::Source src(source);
  208. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
  209. }
  210. Factory<v8::Script>::return_t
  211. Factory<v8::Script>::New( v8::Local<v8::String> source
  212. , v8::ScriptOrigin const& origin) {
  213. v8::ScriptCompiler::Source src(source, origin);
  214. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
  215. }
  216. #endif
  217. //=== Signature ================================================================
  218. Factory<v8::Signature>::return_t
  219. Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
  220. return v8::Signature::New(v8::Isolate::GetCurrent(), receiver);
  221. }
  222. //=== String ===================================================================
  223. Factory<v8::String>::return_t
  224. Factory<v8::String>::New() {
  225. return v8::String::Empty(v8::Isolate::GetCurrent());
  226. }
  227. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  228. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  229. Factory<v8::String>::return_t
  230. Factory<v8::String>::New(const char * value, int length) {
  231. return v8::String::NewFromUtf8(
  232. v8::Isolate::GetCurrent(), value, v8::NewStringType::kNormal, length);
  233. }
  234. Factory<v8::String>::return_t
  235. Factory<v8::String>::New(std::string const& value) {
  236. assert(value.size() <= INT_MAX && "string too long");
  237. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(),
  238. value.data(), v8::NewStringType::kNormal, static_cast<int>(value.size()));
  239. }
  240. Factory<v8::String>::return_t
  241. Factory<v8::String>::New(const uint16_t * value, int length) {
  242. return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
  243. v8::NewStringType::kNormal, length);
  244. }
  245. Factory<v8::String>::return_t
  246. Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
  247. return v8::String::NewExternalTwoByte(v8::Isolate::GetCurrent(), value);
  248. }
  249. Factory<v8::String>::return_t
  250. Factory<v8::String>::New(ExternalOneByteStringResource * value) {
  251. return v8::String::NewExternalOneByte(v8::Isolate::GetCurrent(), value);
  252. }
  253. #else
  254. Factory<v8::String>::return_t
  255. Factory<v8::String>::New(const char * value, int length) {
  256. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value,
  257. v8::String::kNormalString, length);
  258. }
  259. Factory<v8::String>::return_t
  260. Factory<v8::String>::New(
  261. std::string const& value) /* NOLINT(build/include_what_you_use) */ {
  262. assert(value.size() <= INT_MAX && "string too long");
  263. return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value.data(),
  264. v8::String::kNormalString,
  265. static_cast<int>(value.size()));
  266. }
  267. Factory<v8::String>::return_t
  268. Factory<v8::String>::New(const uint16_t * value, int length) {
  269. return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
  270. v8::String::kNormalString, length);
  271. }
  272. Factory<v8::String>::return_t
  273. Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
  274. return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
  275. }
  276. Factory<v8::String>::return_t
  277. Factory<v8::String>::New(ExternalOneByteStringResource * value) {
  278. return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
  279. }
  280. #endif
  281. //=== String Object ============================================================
  282. // See https://github.com/nodejs/nan/pull/811#discussion_r224594980.
  283. // Disable the warning as there is no way around it.
  284. // TODO(bnoordhuis) Use isolate-based version in Node.js v12.
  285. Factory<v8::StringObject>::return_t
  286. Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
  287. // V8 > 7.0
  288. #if V8_MAJOR_VERSION > 7 || (V8_MAJOR_VERSION == 7 && V8_MINOR_VERSION > 0)
  289. return v8::StringObject::New(v8::Isolate::GetCurrent(), value)
  290. .As<v8::StringObject>();
  291. #else
  292. #ifdef _MSC_VER
  293. #pragma warning(push)
  294. #pragma warning(disable : 4996)
  295. #endif
  296. #ifdef __GNUC__
  297. #pragma GCC diagnostic push
  298. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  299. #endif
  300. return v8::StringObject::New(value).As<v8::StringObject>();
  301. #ifdef __GNUC__
  302. #pragma GCC diagnostic pop
  303. #endif
  304. #ifdef _MSC_VER
  305. #pragma warning(pop)
  306. #endif
  307. #endif
  308. }
  309. //=== Unbound Script ===========================================================
  310. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  311. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  312. Factory<v8::UnboundScript>::return_t
  313. Factory<v8::UnboundScript>::New(v8::Local<v8::String> source) {
  314. v8::ScriptCompiler::Source src(source);
  315. return v8::ScriptCompiler::CompileUnboundScript(
  316. v8::Isolate::GetCurrent(), &src);
  317. }
  318. Factory<v8::UnboundScript>::return_t
  319. Factory<v8::UnboundScript>::New( v8::Local<v8::String> source
  320. , v8::ScriptOrigin const& origin) {
  321. v8::ScriptCompiler::Source src(source, origin);
  322. return v8::ScriptCompiler::CompileUnboundScript(
  323. v8::Isolate::GetCurrent(), &src);
  324. }
  325. #else
  326. Factory<v8::UnboundScript>::return_t
  327. Factory<v8::UnboundScript>::New(v8::Local<v8::String> source) {
  328. v8::ScriptCompiler::Source src(source);
  329. return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
  330. }
  331. Factory<v8::UnboundScript>::return_t
  332. Factory<v8::UnboundScript>::New( v8::Local<v8::String> source
  333. , v8::ScriptOrigin const& origin) {
  334. v8::ScriptCompiler::Source src(source, origin);
  335. return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
  336. }
  337. #endif
  338. } // end of namespace imp
  339. //=== Presistents and Handles ==================================================
  340. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  341. template <typename T>
  342. inline v8::Local<T> New(v8::Handle<T> h) {
  343. return v8::Local<T>::New(v8::Isolate::GetCurrent(), h);
  344. }
  345. #endif
  346. template <typename T, typename M>
  347. inline v8::Local<T> New(v8::Persistent<T, M> const& p) {
  348. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  349. }
  350. template <typename T, typename M>
  351. inline v8::Local<T> New(Persistent<T, M> const& p) {
  352. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  353. }
  354. template <typename T>
  355. inline v8::Local<T> New(Global<T> const& p) {
  356. return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
  357. }
  358. #endif // NAN_IMPLEMENTATION_12_INL_H_