You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nan_implementation_pre_12_inl.h 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_PRE_12_INL_H_
  9. #define NAN_IMPLEMENTATION_PRE_12_INL_H_
  10. //==============================================================================
  11. // node v0.10 implementation
  12. //==============================================================================
  13. namespace imp {
  14. //=== Array ====================================================================
  15. Factory<v8::Array>::return_t
  16. Factory<v8::Array>::New() {
  17. return v8::Array::New();
  18. }
  19. Factory<v8::Array>::return_t
  20. Factory<v8::Array>::New(int length) {
  21. return v8::Array::New(length);
  22. }
  23. //=== Boolean ==================================================================
  24. Factory<v8::Boolean>::return_t
  25. Factory<v8::Boolean>::New(bool value) {
  26. return v8::Boolean::New(value)->ToBoolean();
  27. }
  28. //=== Boolean Object ===========================================================
  29. Factory<v8::BooleanObject>::return_t
  30. Factory<v8::BooleanObject>::New(bool value) {
  31. return v8::BooleanObject::New(value).As<v8::BooleanObject>();
  32. }
  33. //=== Context ==================================================================
  34. Factory<v8::Context>::return_t
  35. Factory<v8::Context>::New( v8::ExtensionConfiguration* extensions
  36. , v8::Local<v8::ObjectTemplate> tmpl
  37. , v8::Local<v8::Value> obj) {
  38. v8::Persistent<v8::Context> ctx = v8::Context::New(extensions, tmpl, obj);
  39. v8::Local<v8::Context> lctx = v8::Local<v8::Context>::New(ctx);
  40. ctx.Dispose();
  41. return lctx;
  42. }
  43. //=== Date =====================================================================
  44. Factory<v8::Date>::return_t
  45. Factory<v8::Date>::New(double value) {
  46. return v8::Date::New(value).As<v8::Date>();
  47. }
  48. //=== External =================================================================
  49. Factory<v8::External>::return_t
  50. Factory<v8::External>::New(void * value) {
  51. return v8::External::New(value);
  52. }
  53. //=== Function =================================================================
  54. Factory<v8::Function>::return_t
  55. Factory<v8::Function>::New( FunctionCallback callback
  56. , v8::Local<v8::Value> data) {
  57. v8::HandleScope scope;
  58. return scope.Close(Factory<v8::FunctionTemplate>::New(
  59. callback, data, v8::Local<v8::Signature>())
  60. ->GetFunction());
  61. }
  62. //=== FunctionTemplate =========================================================
  63. Factory<v8::FunctionTemplate>::return_t
  64. Factory<v8::FunctionTemplate>::New( FunctionCallback callback
  65. , v8::Local<v8::Value> data
  66. , v8::Local<v8::Signature> signature) {
  67. if (callback) {
  68. v8::HandleScope scope;
  69. v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
  70. tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  71. v8::Local<v8::Object> obj = tpl->NewInstance();
  72. obj->SetInternalField(
  73. imp::kFunctionIndex
  74. , v8::External::New(reinterpret_cast<void *>(callback)));
  75. v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);
  76. if (!val.IsEmpty()) {
  77. obj->SetInternalField(imp::kDataIndex, val);
  78. }
  79. // Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
  80. // a way. Have at it though...
  81. return scope.Close(
  82. v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
  83. , obj
  84. , signature));
  85. } else {
  86. return v8::FunctionTemplate::New(0, data, signature);
  87. }
  88. }
  89. //=== Number ===================================================================
  90. Factory<v8::Number>::return_t
  91. Factory<v8::Number>::New(double value) {
  92. return v8::Number::New(value);
  93. }
  94. //=== Number Object ============================================================
  95. Factory<v8::NumberObject>::return_t
  96. Factory<v8::NumberObject>::New(double value) {
  97. return v8::NumberObject::New(value).As<v8::NumberObject>();
  98. }
  99. //=== Integer, Int32 and Uint32 ================================================
  100. template <typename T>
  101. typename IntegerFactory<T>::return_t
  102. IntegerFactory<T>::New(int32_t value) {
  103. return To<T>(T::New(value));
  104. }
  105. template <typename T>
  106. typename IntegerFactory<T>::return_t
  107. IntegerFactory<T>::New(uint32_t value) {
  108. return To<T>(T::NewFromUnsigned(value));
  109. }
  110. Factory<v8::Uint32>::return_t
  111. Factory<v8::Uint32>::New(int32_t value) {
  112. return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
  113. }
  114. Factory<v8::Uint32>::return_t
  115. Factory<v8::Uint32>::New(uint32_t value) {
  116. return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
  117. }
  118. //=== Object ===================================================================
  119. Factory<v8::Object>::return_t
  120. Factory<v8::Object>::New() {
  121. return v8::Object::New();
  122. }
  123. //=== Object Template ==========================================================
  124. Factory<v8::ObjectTemplate>::return_t
  125. Factory<v8::ObjectTemplate>::New() {
  126. return v8::ObjectTemplate::New();
  127. }
  128. //=== RegExp ===================================================================
  129. Factory<v8::RegExp>::return_t
  130. Factory<v8::RegExp>::New(
  131. v8::Local<v8::String> pattern
  132. , v8::RegExp::Flags flags) {
  133. return v8::RegExp::New(pattern, flags);
  134. }
  135. //=== Script ===================================================================
  136. Factory<v8::Script>::return_t
  137. Factory<v8::Script>::New( v8::Local<v8::String> source) {
  138. return v8::Script::New(source);
  139. }
  140. Factory<v8::Script>::return_t
  141. Factory<v8::Script>::New( v8::Local<v8::String> source
  142. , v8::ScriptOrigin const& origin) {
  143. return v8::Script::New(source, const_cast<v8::ScriptOrigin*>(&origin));
  144. }
  145. //=== Signature ================================================================
  146. Factory<v8::Signature>::return_t
  147. Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
  148. return v8::Signature::New(receiver);
  149. }
  150. //=== String ===================================================================
  151. Factory<v8::String>::return_t
  152. Factory<v8::String>::New() {
  153. return v8::String::Empty();
  154. }
  155. Factory<v8::String>::return_t
  156. Factory<v8::String>::New(const char * value, int length) {
  157. return v8::String::New(value, length);
  158. }
  159. Factory<v8::String>::return_t
  160. Factory<v8::String>::New(
  161. std::string const& value) /* NOLINT(build/include_what_you_use) */ {
  162. assert(value.size() <= INT_MAX && "string too long");
  163. return v8::String::New(value.data(), static_cast<int>(value.size()));
  164. }
  165. Factory<v8::String>::return_t
  166. Factory<v8::String>::New(const uint16_t * value, int length) {
  167. return v8::String::New(value, length);
  168. }
  169. Factory<v8::String>::return_t
  170. Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
  171. return v8::String::NewExternal(value);
  172. }
  173. Factory<v8::String>::return_t
  174. Factory<v8::String>::New(v8::String::ExternalAsciiStringResource * value) {
  175. return v8::String::NewExternal(value);
  176. }
  177. //=== String Object ============================================================
  178. Factory<v8::StringObject>::return_t
  179. Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
  180. return v8::StringObject::New(value).As<v8::StringObject>();
  181. }
  182. } // end of namespace imp
  183. //=== Presistents and Handles ==================================================
  184. template <typename T>
  185. inline v8::Local<T> New(v8::Handle<T> h) {
  186. return v8::Local<T>::New(h);
  187. }
  188. template <typename T>
  189. inline v8::Local<T> New(v8::Persistent<T> const& p) {
  190. return v8::Local<T>::New(p);
  191. }
  192. template <typename T, typename M>
  193. inline v8::Local<T> New(Persistent<T, M> const& p) {
  194. return v8::Local<T>::New(p.persistent);
  195. }
  196. template <typename T>
  197. inline v8::Local<T> New(Global<T> const& p) {
  198. return v8::Local<T>::New(p.persistent);
  199. }
  200. #endif // NAN_IMPLEMENTATION_PRE_12_INL_H_