napi.h 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  1. #ifndef SRC_NAPI_H_
  2. #define SRC_NAPI_H_
  3. #include <node_api.h>
  4. #include <functional>
  5. #include <initializer_list>
  6. #include <memory>
  7. #include <mutex>
  8. #include <string>
  9. #include <vector>
  10. // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known good version)
  11. #if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210
  12. #define NAPI_HAS_CONSTEXPR 1
  13. #endif
  14. // VS2013 does not support char16_t literal strings, so we'll work around it using wchar_t strings
  15. // and casting them. This is safe as long as the character sizes are the same.
  16. #if defined(_MSC_VER) && _MSC_VER <= 1800
  17. static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16_t and wchar_t");
  18. #define NAPI_WIDE_TEXT(x) reinterpret_cast<char16_t*>(L ## x)
  19. #else
  20. #define NAPI_WIDE_TEXT(x) u ## x
  21. #endif
  22. // If C++ exceptions are not explicitly enabled or disabled, enable them
  23. // if exceptions were enabled in the compiler settings.
  24. #if !defined(NAPI_CPP_EXCEPTIONS) && !defined(NAPI_DISABLE_CPP_EXCEPTIONS)
  25. #if defined(_CPPUNWIND) || defined (__EXCEPTIONS)
  26. #define NAPI_CPP_EXCEPTIONS
  27. #else
  28. #error Exception support not detected. \
  29. Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS.
  30. #endif
  31. #endif
  32. #ifdef _NOEXCEPT
  33. #define NAPI_NOEXCEPT _NOEXCEPT
  34. #else
  35. #define NAPI_NOEXCEPT noexcept
  36. #endif
  37. #ifdef NAPI_CPP_EXCEPTIONS
  38. // When C++ exceptions are enabled, Errors are thrown directly. There is no need
  39. // to return anything after the throw statements. The variadic parameter is an
  40. // optional return value that is ignored.
  41. // We need _VOID versions of the macros to avoid warnings resulting from
  42. // leaving the NAPI_THROW_* `...` argument empty.
  43. #define NAPI_THROW(e, ...) throw e
  44. #define NAPI_THROW_VOID(e) throw e
  45. #define NAPI_THROW_IF_FAILED(env, status, ...) \
  46. if ((status) != napi_ok) throw Napi::Error::New(env);
  47. #define NAPI_THROW_IF_FAILED_VOID(env, status) \
  48. if ((status) != napi_ok) throw Napi::Error::New(env);
  49. #else // NAPI_CPP_EXCEPTIONS
  50. // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
  51. // which are pending until the callback returns to JS. The variadic parameter
  52. // is an optional return value; usually it is an empty result.
  53. // We need _VOID versions of the macros to avoid warnings resulting from
  54. // leaving the NAPI_THROW_* `...` argument empty.
  55. #define NAPI_THROW(e, ...) \
  56. do { \
  57. (e).ThrowAsJavaScriptException(); \
  58. return __VA_ARGS__; \
  59. } while (0)
  60. #define NAPI_THROW_VOID(e) \
  61. do { \
  62. (e).ThrowAsJavaScriptException(); \
  63. return; \
  64. } while (0)
  65. #define NAPI_THROW_IF_FAILED(env, status, ...) \
  66. if ((status) != napi_ok) { \
  67. Napi::Error::New(env).ThrowAsJavaScriptException(); \
  68. return __VA_ARGS__; \
  69. }
  70. #define NAPI_THROW_IF_FAILED_VOID(env, status) \
  71. if ((status) != napi_ok) { \
  72. Napi::Error::New(env).ThrowAsJavaScriptException(); \
  73. return; \
  74. }
  75. #endif // NAPI_CPP_EXCEPTIONS
  76. # define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
  77. # define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
  78. #define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \
  79. NAPI_DISALLOW_ASSIGN(CLASS) \
  80. NAPI_DISALLOW_COPY(CLASS)
  81. #define NAPI_FATAL_IF_FAILED(status, location, message) \
  82. do { \
  83. if ((status) != napi_ok) { \
  84. Napi::Error::Fatal((location), (message)); \
  85. } \
  86. } while (0)
  87. ////////////////////////////////////////////////////////////////////////////////
  88. /// N-API C++ Wrapper Classes
  89. ///
  90. /// These classes wrap the "N-API" ABI-stable C APIs for Node.js, providing a
  91. /// C++ object model and C++ exception-handling semantics with low overhead.
  92. /// The wrappers are all header-only so that they do not affect the ABI.
  93. ////////////////////////////////////////////////////////////////////////////////
  94. namespace Napi {
  95. // Forward declarations
  96. class Env;
  97. class Value;
  98. class Boolean;
  99. class Number;
  100. #if NAPI_VERSION > 5
  101. class BigInt;
  102. #endif // NAPI_VERSION > 5
  103. #if (NAPI_VERSION > 4)
  104. class Date;
  105. #endif
  106. class String;
  107. class Object;
  108. class Array;
  109. class ArrayBuffer;
  110. class Function;
  111. class Error;
  112. class PropertyDescriptor;
  113. class CallbackInfo;
  114. class TypedArray;
  115. template <typename T> class TypedArrayOf;
  116. typedef TypedArrayOf<int8_t> Int8Array; ///< Typed-array of signed 8-bit integers
  117. typedef TypedArrayOf<uint8_t> Uint8Array; ///< Typed-array of unsigned 8-bit integers
  118. typedef TypedArrayOf<int16_t> Int16Array; ///< Typed-array of signed 16-bit integers
  119. typedef TypedArrayOf<uint16_t> Uint16Array; ///< Typed-array of unsigned 16-bit integers
  120. typedef TypedArrayOf<int32_t> Int32Array; ///< Typed-array of signed 32-bit integers
  121. typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
  122. typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
  123. typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
  124. #if NAPI_VERSION > 5
  125. typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
  126. typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
  127. #endif // NAPI_VERSION > 5
  128. /// Defines the signature of a N-API C++ module's registration callback (init) function.
  129. typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
  130. class MemoryManagement;
  131. /// Environment for N-API values and operations.
  132. ///
  133. /// All N-API values and operations must be associated with an environment. An environment
  134. /// instance is always provided to callback functions; that environment must then be used for any
  135. /// creation of N-API values or other N-API operations within the callback. (Many methods infer
  136. /// the environment from the `this` instance that the method is called on.)
  137. ///
  138. /// In the future, multiple environments per process may be supported, although current
  139. /// implementations only support one environment per process.
  140. ///
  141. /// In the V8 JavaScript engine, a N-API environment approximately corresponds to an Isolate.
  142. class Env {
  143. #if NAPI_VERSION > 5
  144. private:
  145. template <typename T> static void DefaultFini(Env, T* data);
  146. template <typename DataType, typename HintType>
  147. static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
  148. #endif // NAPI_VERSION > 5
  149. public:
  150. Env(napi_env env);
  151. operator napi_env() const;
  152. Object Global() const;
  153. Value Undefined() const;
  154. Value Null() const;
  155. bool IsExceptionPending() const;
  156. Error GetAndClearPendingException();
  157. Value RunScript(const char* utf8script);
  158. Value RunScript(const std::string& utf8script);
  159. Value RunScript(String script);
  160. #if NAPI_VERSION > 5
  161. template <typename T> T* GetInstanceData();
  162. template <typename T> using Finalizer = void (*)(Env, T*);
  163. template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
  164. void SetInstanceData(T* data);
  165. template <typename DataType, typename HintType>
  166. using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
  167. template <typename DataType,
  168. typename HintType,
  169. FinalizerWithHint<DataType, HintType> fini =
  170. Env::DefaultFiniWithHint<DataType, HintType>>
  171. void SetInstanceData(DataType* data, HintType* hint);
  172. #endif // NAPI_VERSION > 5
  173. private:
  174. napi_env _env;
  175. };
  176. /// A JavaScript value of unknown type.
  177. ///
  178. /// For type-specific operations, convert to one of the Value subclasses using a `To*` or `As()`
  179. /// method. The `To*` methods do type coercion; the `As()` method does not.
  180. ///
  181. /// Napi::Value value = ...
  182. /// if (!value.IsString()) throw Napi::TypeError::New(env, "Invalid arg...");
  183. /// Napi::String str = value.As<Napi::String>(); // Cast to a string value
  184. ///
  185. /// Napi::Value anotherValue = ...
  186. /// bool isTruthy = anotherValue.ToBoolean(); // Coerce to a boolean value
  187. class Value {
  188. public:
  189. Value(); ///< Creates a new _empty_ Value instance.
  190. Value(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  191. /// Creates a JS value from a C++ primitive.
  192. ///
  193. /// `value` may be any of:
  194. /// - bool
  195. /// - Any integer type
  196. /// - Any floating point type
  197. /// - const char* (encoded using UTF-8, null-terminated)
  198. /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
  199. /// - std::string (encoded using UTF-8)
  200. /// - std::u16string
  201. /// - napi::Value
  202. /// - napi_value
  203. template <typename T>
  204. static Value From(napi_env env, const T& value);
  205. /// Converts to a N-API value primitive.
  206. ///
  207. /// If the instance is _empty_, this returns `nullptr`.
  208. operator napi_value() const;
  209. /// Tests if this value strictly equals another value.
  210. bool operator ==(const Value& other) const;
  211. /// Tests if this value does not strictly equal another value.
  212. bool operator !=(const Value& other) const;
  213. /// Tests if this value strictly equals another value.
  214. bool StrictEquals(const Value& other) const;
  215. /// Gets the environment the value is associated with.
  216. Napi::Env Env() const;
  217. /// Checks if the value is empty (uninitialized).
  218. ///
  219. /// An empty value is invalid, and most attempts to perform an operation on an empty value
  220. /// will result in an exception. Note an empty value is distinct from JavaScript `null` or
  221. /// `undefined`, which are valid values.
  222. ///
  223. /// When C++ exceptions are disabled at compile time, a method with a `Value` return type may
  224. /// return an empty value to indicate a pending exception. So when not using C++ exceptions,
  225. /// callers should check whether the value is empty before attempting to use it.
  226. bool IsEmpty() const;
  227. napi_valuetype Type() const; ///< Gets the type of the value.
  228. bool IsUndefined() const; ///< Tests if a value is an undefined JavaScript value.
  229. bool IsNull() const; ///< Tests if a value is a null JavaScript value.
  230. bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
  231. bool IsNumber() const; ///< Tests if a value is a JavaScript number.
  232. #if NAPI_VERSION > 5
  233. bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
  234. #endif // NAPI_VERSION > 5
  235. #if (NAPI_VERSION > 4)
  236. bool IsDate() const; ///< Tests if a value is a JavaScript date.
  237. #endif
  238. bool IsString() const; ///< Tests if a value is a JavaScript string.
  239. bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
  240. bool IsArray() const; ///< Tests if a value is a JavaScript array.
  241. bool IsArrayBuffer() const; ///< Tests if a value is a JavaScript array buffer.
  242. bool IsTypedArray() const; ///< Tests if a value is a JavaScript typed array.
  243. bool IsObject() const; ///< Tests if a value is a JavaScript object.
  244. bool IsFunction() const; ///< Tests if a value is a JavaScript function.
  245. bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
  246. bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
  247. bool IsBuffer() const; ///< Tests if a value is a Node buffer.
  248. bool IsExternal() const; ///< Tests if a value is a pointer to external data.
  249. /// Casts to another type of `Napi::Value`, when the actual type is known or assumed.
  250. ///
  251. /// This conversion does NOT coerce the type. Calling any methods inappropriate for the actual
  252. /// value type will throw `Napi::Error`.
  253. template <typename T> T As() const;
  254. Boolean ToBoolean() const; ///< Coerces a value to a JavaScript boolean.
  255. Number ToNumber() const; ///< Coerces a value to a JavaScript number.
  256. String ToString() const; ///< Coerces a value to a JavaScript string.
  257. Object ToObject() const; ///< Coerces a value to a JavaScript object.
  258. protected:
  259. /// !cond INTERNAL
  260. napi_env _env;
  261. napi_value _value;
  262. /// !endcond
  263. };
  264. /// A JavaScript boolean value.
  265. class Boolean : public Value {
  266. public:
  267. static Boolean New(
  268. napi_env env, ///< N-API environment
  269. bool value ///< Boolean value
  270. );
  271. Boolean(); ///< Creates a new _empty_ Boolean instance.
  272. Boolean(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  273. operator bool() const; ///< Converts a Boolean value to a boolean primitive.
  274. bool Value() const; ///< Converts a Boolean value to a boolean primitive.
  275. };
  276. /// A JavaScript number value.
  277. class Number : public Value {
  278. public:
  279. static Number New(
  280. napi_env env, ///< N-API environment
  281. double value ///< Number value
  282. );
  283. Number(); ///< Creates a new _empty_ Number instance.
  284. Number(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  285. operator int32_t() const; ///< Converts a Number value to a 32-bit signed integer value.
  286. operator uint32_t() const; ///< Converts a Number value to a 32-bit unsigned integer value.
  287. operator int64_t() const; ///< Converts a Number value to a 64-bit signed integer value.
  288. operator float() const; ///< Converts a Number value to a 32-bit floating-point value.
  289. operator double() const; ///< Converts a Number value to a 64-bit floating-point value.
  290. int32_t Int32Value() const; ///< Converts a Number value to a 32-bit signed integer value.
  291. uint32_t Uint32Value() const; ///< Converts a Number value to a 32-bit unsigned integer value.
  292. int64_t Int64Value() const; ///< Converts a Number value to a 64-bit signed integer value.
  293. float FloatValue() const; ///< Converts a Number value to a 32-bit floating-point value.
  294. double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
  295. };
  296. #if NAPI_VERSION > 5
  297. /// A JavaScript bigint value.
  298. class BigInt : public Value {
  299. public:
  300. static BigInt New(
  301. napi_env env, ///< N-API environment
  302. int64_t value ///< Number value
  303. );
  304. static BigInt New(
  305. napi_env env, ///< N-API environment
  306. uint64_t value ///< Number value
  307. );
  308. /// Creates a new BigInt object using a specified sign bit and a
  309. /// specified list of digits/words.
  310. /// The resulting number is calculated as:
  311. /// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
  312. static BigInt New(
  313. napi_env env, ///< N-API environment
  314. int sign_bit, ///< Sign bit. 1 if negative.
  315. size_t word_count, ///< Number of words in array
  316. const uint64_t* words ///< Array of words
  317. );
  318. BigInt(); ///< Creates a new _empty_ BigInt instance.
  319. BigInt(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  320. int64_t Int64Value(bool* lossless) const; ///< Converts a BigInt value to a 64-bit signed integer value.
  321. uint64_t Uint64Value(bool* lossless) const; ///< Converts a BigInt value to a 64-bit unsigned integer value.
  322. size_t WordCount() const; ///< The number of 64-bit words needed to store the result of ToWords().
  323. /// Writes the contents of this BigInt to a specified memory location.
  324. /// `sign_bit` must be provided and will be set to 1 if this BigInt is negative.
  325. /// `*word_count` has to be initialized to the length of the `words` array.
  326. /// Upon return, it will be set to the actual number of words that would
  327. /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
  328. void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
  329. };
  330. #endif // NAPI_VERSION > 5
  331. #if (NAPI_VERSION > 4)
  332. /// A JavaScript date value.
  333. class Date : public Value {
  334. public:
  335. /// Creates a new Date value from a double primitive.
  336. static Date New(
  337. napi_env env, ///< N-API environment
  338. double value ///< Number value
  339. );
  340. Date(); ///< Creates a new _empty_ Date instance.
  341. Date(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  342. operator double() const; ///< Converts a Date value to double primitive
  343. double ValueOf() const; ///< Converts a Date value to a double primitive.
  344. };
  345. #endif
  346. /// A JavaScript string or symbol value (that can be used as a property name).
  347. class Name : public Value {
  348. public:
  349. Name(); ///< Creates a new _empty_ Name instance.
  350. Name(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  351. };
  352. /// A JavaScript string value.
  353. class String : public Name {
  354. public:
  355. /// Creates a new String value from a UTF-8 encoded C++ string.
  356. static String New(
  357. napi_env env, ///< N-API environment
  358. const std::string& value ///< UTF-8 encoded C++ string
  359. );
  360. /// Creates a new String value from a UTF-16 encoded C++ string.
  361. static String New(
  362. napi_env env, ///< N-API environment
  363. const std::u16string& value ///< UTF-16 encoded C++ string
  364. );
  365. /// Creates a new String value from a UTF-8 encoded C string.
  366. static String New(
  367. napi_env env, ///< N-API environment
  368. const char* value ///< UTF-8 encoded null-terminated C string
  369. );
  370. /// Creates a new String value from a UTF-16 encoded C string.
  371. static String New(
  372. napi_env env, ///< N-API environment
  373. const char16_t* value ///< UTF-16 encoded null-terminated C string
  374. );
  375. /// Creates a new String value from a UTF-8 encoded C string with specified length.
  376. static String New(
  377. napi_env env, ///< N-API environment
  378. const char* value, ///< UTF-8 encoded C string (not necessarily null-terminated)
  379. size_t length ///< length of the string in bytes
  380. );
  381. /// Creates a new String value from a UTF-16 encoded C string with specified length.
  382. static String New(
  383. napi_env env, ///< N-API environment
  384. const char16_t* value, ///< UTF-16 encoded C string (not necessarily null-terminated)
  385. size_t length ///< Length of the string in 2-byte code units
  386. );
  387. /// Creates a new String based on the original object's type.
  388. ///
  389. /// `value` may be any of:
  390. /// - const char* (encoded using UTF-8, null-terminated)
  391. /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
  392. /// - std::string (encoded using UTF-8)
  393. /// - std::u16string
  394. template <typename T>
  395. static String From(napi_env env, const T& value);
  396. String(); ///< Creates a new _empty_ String instance.
  397. String(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  398. operator std::string() const; ///< Converts a String value to a UTF-8 encoded C++ string.
  399. operator std::u16string() const; ///< Converts a String value to a UTF-16 encoded C++ string.
  400. std::string Utf8Value() const; ///< Converts a String value to a UTF-8 encoded C++ string.
  401. std::u16string Utf16Value() const; ///< Converts a String value to a UTF-16 encoded C++ string.
  402. };
  403. /// A JavaScript symbol value.
  404. class Symbol : public Name {
  405. public:
  406. /// Creates a new Symbol value with an optional description.
  407. static Symbol New(
  408. napi_env env, ///< N-API environment
  409. const char* description = nullptr ///< Optional UTF-8 encoded null-terminated C string
  410. /// describing the symbol
  411. );
  412. /// Creates a new Symbol value with a description.
  413. static Symbol New(
  414. napi_env env, ///< N-API environment
  415. const std::string& description ///< UTF-8 encoded C++ string describing the symbol
  416. );
  417. /// Creates a new Symbol value with a description.
  418. static Symbol New(
  419. napi_env env, ///< N-API environment
  420. String description ///< String value describing the symbol
  421. );
  422. /// Creates a new Symbol value with a description.
  423. static Symbol New(
  424. napi_env env, ///< N-API environment
  425. napi_value description ///< String value describing the symbol
  426. );
  427. /// Get a public Symbol (e.g. Symbol.iterator).
  428. static Symbol WellKnown(napi_env, const std::string& name);
  429. Symbol(); ///< Creates a new _empty_ Symbol instance.
  430. Symbol(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  431. };
  432. /// A JavaScript object value.
  433. class Object : public Value {
  434. public:
  435. /// Enables property and element assignments using indexing syntax.
  436. ///
  437. /// Example:
  438. ///
  439. /// Napi::Value propertyValue = object1['A'];
  440. /// object2['A'] = propertyValue;
  441. /// Napi::Value elementValue = array[0];
  442. /// array[1] = elementValue;
  443. template <typename Key>
  444. class PropertyLValue {
  445. public:
  446. /// Converts an L-value to a value.
  447. operator Value() const;
  448. /// Assigns a value to the property. The type of value can be
  449. /// anything supported by `Object::Set`.
  450. template <typename ValueType>
  451. PropertyLValue& operator =(ValueType value);
  452. private:
  453. PropertyLValue() = delete;
  454. PropertyLValue(Object object, Key key);
  455. napi_env _env;
  456. napi_value _object;
  457. Key _key;
  458. friend class Napi::Object;
  459. };
  460. /// Creates a new Object value.
  461. static Object New(
  462. napi_env env ///< N-API environment
  463. );
  464. Object(); ///< Creates a new _empty_ Object instance.
  465. Object(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  466. /// Gets or sets a named property.
  467. PropertyLValue<std::string> operator [](
  468. const char* utf8name ///< UTF-8 encoded null-terminated property name
  469. );
  470. /// Gets or sets a named property.
  471. PropertyLValue<std::string> operator [](
  472. const std::string& utf8name ///< UTF-8 encoded property name
  473. );
  474. /// Gets or sets an indexed property or array element.
  475. PropertyLValue<uint32_t> operator [](
  476. uint32_t index /// Property / element index
  477. );
  478. /// Gets a named property.
  479. Value operator [](
  480. const char* utf8name ///< UTF-8 encoded null-terminated property name
  481. ) const;
  482. /// Gets a named property.
  483. Value operator [](
  484. const std::string& utf8name ///< UTF-8 encoded property name
  485. ) const;
  486. /// Gets an indexed property or array element.
  487. Value operator [](
  488. uint32_t index ///< Property / element index
  489. ) const;
  490. /// Checks whether a property is present.
  491. bool Has(
  492. napi_value key ///< Property key primitive
  493. ) const;
  494. /// Checks whether a property is present.
  495. bool Has(
  496. Value key ///< Property key
  497. ) const;
  498. /// Checks whether a named property is present.
  499. bool Has(
  500. const char* utf8name ///< UTF-8 encoded null-terminated property name
  501. ) const;
  502. /// Checks whether a named property is present.
  503. bool Has(
  504. const std::string& utf8name ///< UTF-8 encoded property name
  505. ) const;
  506. /// Checks whether a own property is present.
  507. bool HasOwnProperty(
  508. napi_value key ///< Property key primitive
  509. ) const;
  510. /// Checks whether a own property is present.
  511. bool HasOwnProperty(
  512. Value key ///< Property key
  513. ) const;
  514. /// Checks whether a own property is present.
  515. bool HasOwnProperty(
  516. const char* utf8name ///< UTF-8 encoded null-terminated property name
  517. ) const;
  518. /// Checks whether a own property is present.
  519. bool HasOwnProperty(
  520. const std::string& utf8name ///< UTF-8 encoded property name
  521. ) const;
  522. /// Gets a property.
  523. Value Get(
  524. napi_value key ///< Property key primitive
  525. ) const;
  526. /// Gets a property.
  527. Value Get(
  528. Value key ///< Property key
  529. ) const;
  530. /// Gets a named property.
  531. Value Get(
  532. const char* utf8name ///< UTF-8 encoded null-terminated property name
  533. ) const;
  534. /// Gets a named property.
  535. Value Get(
  536. const std::string& utf8name ///< UTF-8 encoded property name
  537. ) const;
  538. /// Sets a property.
  539. template <typename ValueType>
  540. void Set(
  541. napi_value key, ///< Property key primitive
  542. const ValueType& value ///< Property value primitive
  543. );
  544. /// Sets a property.
  545. template <typename ValueType>
  546. void Set(
  547. Value key, ///< Property key
  548. const ValueType& value ///< Property value
  549. );
  550. /// Sets a named property.
  551. template <typename ValueType>
  552. void Set(
  553. const char* utf8name, ///< UTF-8 encoded null-terminated property name
  554. const ValueType& value
  555. );
  556. /// Sets a named property.
  557. template <typename ValueType>
  558. void Set(
  559. const std::string& utf8name, ///< UTF-8 encoded property name
  560. const ValueType& value ///< Property value primitive
  561. );
  562. /// Delete property.
  563. bool Delete(
  564. napi_value key ///< Property key primitive
  565. );
  566. /// Delete property.
  567. bool Delete(
  568. Value key ///< Property key
  569. );
  570. /// Delete property.
  571. bool Delete(
  572. const char* utf8name ///< UTF-8 encoded null-terminated property name
  573. );
  574. /// Delete property.
  575. bool Delete(
  576. const std::string& utf8name ///< UTF-8 encoded property name
  577. );
  578. /// Checks whether an indexed property is present.
  579. bool Has(
  580. uint32_t index ///< Property / element index
  581. ) const;
  582. /// Gets an indexed property or array element.
  583. Value Get(
  584. uint32_t index ///< Property / element index
  585. ) const;
  586. /// Sets an indexed property or array element.
  587. template <typename ValueType>
  588. void Set(
  589. uint32_t index, ///< Property / element index
  590. const ValueType& value ///< Property value primitive
  591. );
  592. /// Deletes an indexed property or array element.
  593. bool Delete(
  594. uint32_t index ///< Property / element index
  595. );
  596. Array GetPropertyNames() const; ///< Get all property names
  597. /// Defines a property on the object.
  598. void DefineProperty(
  599. const PropertyDescriptor& property ///< Descriptor for the property to be defined
  600. );
  601. /// Defines properties on the object.
  602. void DefineProperties(
  603. const std::initializer_list<PropertyDescriptor>& properties
  604. ///< List of descriptors for the properties to be defined
  605. );
  606. /// Defines properties on the object.
  607. void DefineProperties(
  608. const std::vector<PropertyDescriptor>& properties
  609. ///< Vector of descriptors for the properties to be defined
  610. );
  611. /// Checks if an object is an instance created by a constructor function.
  612. ///
  613. /// This is equivalent to the JavaScript `instanceof` operator.
  614. bool InstanceOf(
  615. const Function& constructor ///< Constructor function
  616. ) const;
  617. template <typename Finalizer, typename T>
  618. inline void AddFinalizer(Finalizer finalizeCallback, T* data);
  619. template <typename Finalizer, typename T, typename Hint>
  620. inline void AddFinalizer(Finalizer finalizeCallback,
  621. T* data,
  622. Hint* finalizeHint);
  623. };
  624. template <typename T>
  625. class External : public Value {
  626. public:
  627. static External New(napi_env env, T* data);
  628. // Finalizer must implement `void operator()(Env env, T* data)`.
  629. template <typename Finalizer>
  630. static External New(napi_env env,
  631. T* data,
  632. Finalizer finalizeCallback);
  633. // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
  634. template <typename Finalizer, typename Hint>
  635. static External New(napi_env env,
  636. T* data,
  637. Finalizer finalizeCallback,
  638. Hint* finalizeHint);
  639. External();
  640. External(napi_env env, napi_value value);
  641. T* Data() const;
  642. };
  643. class Array : public Object {
  644. public:
  645. static Array New(napi_env env);
  646. static Array New(napi_env env, size_t length);
  647. Array();
  648. Array(napi_env env, napi_value value);
  649. uint32_t Length() const;
  650. };
  651. /// A JavaScript array buffer value.
  652. class ArrayBuffer : public Object {
  653. public:
  654. /// Creates a new ArrayBuffer instance over a new automatically-allocated buffer.
  655. static ArrayBuffer New(
  656. napi_env env, ///< N-API environment
  657. size_t byteLength ///< Length of the buffer to be allocated, in bytes
  658. );
  659. /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
  660. static ArrayBuffer New(
  661. napi_env env, ///< N-API environment
  662. void* externalData, ///< Pointer to the external buffer to be used by the array
  663. size_t byteLength ///< Length of the external buffer to be used by the array, in bytes
  664. );
  665. /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
  666. template <typename Finalizer>
  667. static ArrayBuffer New(
  668. napi_env env, ///< N-API environment
  669. void* externalData, ///< Pointer to the external buffer to be used by the array
  670. size_t byteLength, ///< Length of the external buffer to be used by the array,
  671. /// in bytes
  672. Finalizer finalizeCallback ///< Function to be called when the array buffer is destroyed;
  673. /// must implement `void operator()(Env env, void* externalData)`
  674. );
  675. /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
  676. template <typename Finalizer, typename Hint>
  677. static ArrayBuffer New(
  678. napi_env env, ///< N-API environment
  679. void* externalData, ///< Pointer to the external buffer to be used by the array
  680. size_t byteLength, ///< Length of the external buffer to be used by the array,
  681. /// in bytes
  682. Finalizer finalizeCallback, ///< Function to be called when the array buffer is destroyed;
  683. /// must implement `void operator()(Env env, void* externalData, Hint* hint)`
  684. Hint* finalizeHint ///< Hint (second parameter) to be passed to the finalize callback
  685. );
  686. ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
  687. ArrayBuffer(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  688. void* Data(); ///< Gets a pointer to the data buffer.
  689. size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
  690. };
  691. /// A JavaScript typed-array value with unknown array type.
  692. ///
  693. /// For type-specific operations, cast to a `TypedArrayOf<T>` instance using the `As()`
  694. /// method:
  695. ///
  696. /// Napi::TypedArray array = ...
  697. /// if (t.TypedArrayType() == napi_int32_array) {
  698. /// Napi::Int32Array int32Array = t.As<Napi::Int32Array>();
  699. /// }
  700. class TypedArray : public Object {
  701. public:
  702. TypedArray(); ///< Creates a new _empty_ TypedArray instance.
  703. TypedArray(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  704. napi_typedarray_type TypedArrayType() const; ///< Gets the type of this typed-array.
  705. Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
  706. uint8_t ElementSize() const; ///< Gets the size in bytes of one element in the array.
  707. size_t ElementLength() const; ///< Gets the number of elements in the array.
  708. size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
  709. size_t ByteLength() const; ///< Gets the length of the array in bytes.
  710. protected:
  711. /// !cond INTERNAL
  712. napi_typedarray_type _type;
  713. size_t _length;
  714. TypedArray(napi_env env, napi_value value, napi_typedarray_type type, size_t length);
  715. static const napi_typedarray_type unknown_array_type = static_cast<napi_typedarray_type>(-1);
  716. template <typename T>
  717. static
  718. #if defined(NAPI_HAS_CONSTEXPR)
  719. constexpr
  720. #endif
  721. napi_typedarray_type TypedArrayTypeForPrimitiveType() {
  722. return std::is_same<T, int8_t>::value ? napi_int8_array
  723. : std::is_same<T, uint8_t>::value ? napi_uint8_array
  724. : std::is_same<T, int16_t>::value ? napi_int16_array
  725. : std::is_same<T, uint16_t>::value ? napi_uint16_array
  726. : std::is_same<T, int32_t>::value ? napi_int32_array
  727. : std::is_same<T, uint32_t>::value ? napi_uint32_array
  728. : std::is_same<T, float>::value ? napi_float32_array
  729. : std::is_same<T, double>::value ? napi_float64_array
  730. #if NAPI_VERSION > 5
  731. : std::is_same<T, int64_t>::value ? napi_bigint64_array
  732. : std::is_same<T, uint64_t>::value ? napi_biguint64_array
  733. #endif // NAPI_VERSION > 5
  734. : unknown_array_type;
  735. }
  736. /// !endcond
  737. };
  738. /// A JavaScript typed-array value with known array type.
  739. ///
  740. /// Note while it is possible to create and access Uint8 "clamped" arrays using this class,
  741. /// the _clamping_ behavior is only applied in JavaScript.
  742. template <typename T>
  743. class TypedArrayOf : public TypedArray {
  744. public:
  745. /// Creates a new TypedArray instance over a new automatically-allocated array buffer.
  746. ///
  747. /// The array type parameter can normally be omitted (because it is inferred from the template
  748. /// parameter T), except when creating a "clamped" array:
  749. ///
  750. /// Uint8Array::New(env, length, napi_uint8_clamped_array)
  751. static TypedArrayOf New(
  752. napi_env env, ///< N-API environment
  753. size_t elementLength, ///< Length of the created array, as a number of elements
  754. #if defined(NAPI_HAS_CONSTEXPR)
  755. napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
  756. #else
  757. napi_typedarray_type type
  758. #endif
  759. ///< Type of array, if different from the default array type for the template parameter T.
  760. );
  761. /// Creates a new TypedArray instance over a provided array buffer.
  762. ///
  763. /// The array type parameter can normally be omitted (because it is inferred from the template
  764. /// parameter T), except when creating a "clamped" array:
  765. ///
  766. /// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
  767. static TypedArrayOf New(
  768. napi_env env, ///< N-API environment
  769. size_t elementLength, ///< Length of the created array, as a number of elements
  770. Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
  771. size_t bufferOffset, ///< Offset into the array buffer where the typed-array starts
  772. #if defined(NAPI_HAS_CONSTEXPR)
  773. napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType<T>()
  774. #else
  775. napi_typedarray_type type
  776. #endif
  777. ///< Type of array, if different from the default array type for the template parameter T.
  778. );
  779. TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
  780. TypedArrayOf(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  781. T& operator [](size_t index); ///< Gets or sets an element in the array.
  782. const T& operator [](size_t index) const; ///< Gets an element in the array.
  783. /// Gets a pointer to the array's backing buffer.
  784. ///
  785. /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer, because the
  786. /// typed-array may have a non-zero `ByteOffset()` into the `ArrayBuffer`.
  787. T* Data();
  788. /// Gets a pointer to the array's backing buffer.
  789. ///
  790. /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer, because the
  791. /// typed-array may have a non-zero `ByteOffset()` into the `ArrayBuffer`.
  792. const T* Data() const;
  793. private:
  794. T* _data;
  795. TypedArrayOf(napi_env env,
  796. napi_value value,
  797. napi_typedarray_type type,
  798. size_t length,
  799. T* data);
  800. };
  801. /// The DataView provides a low-level interface for reading/writing multiple
  802. /// number types in an ArrayBuffer irrespective of the platform's endianness.
  803. class DataView : public Object {
  804. public:
  805. static DataView New(napi_env env,
  806. Napi::ArrayBuffer arrayBuffer);
  807. static DataView New(napi_env env,
  808. Napi::ArrayBuffer arrayBuffer,
  809. size_t byteOffset);
  810. static DataView New(napi_env env,
  811. Napi::ArrayBuffer arrayBuffer,
  812. size_t byteOffset,
  813. size_t byteLength);
  814. DataView(); ///< Creates a new _empty_ DataView instance.
  815. DataView(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
  816. Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
  817. size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
  818. size_t ByteLength() const; ///< Gets the length of the array in bytes.
  819. void* Data() const;
  820. float GetFloat32(size_t byteOffset) const;
  821. double GetFloat64(size_t byteOffset) const;
  822. int8_t GetInt8(size_t byteOffset) const;
  823. int16_t GetInt16(size_t byteOffset) const;
  824. int32_t GetInt32(size_t byteOffset) const;
  825. uint8_t GetUint8(size_t byteOffset) const;
  826. uint16_t GetUint16(size_t byteOffset) const;
  827. uint32_t GetUint32(size_t byteOffset) const;
  828. void SetFloat32(size_t byteOffset, float value) const;
  829. void SetFloat64(size_t byteOffset, double value) const;
  830. void SetInt8(size_t byteOffset, int8_t value) const;
  831. void SetInt16(size_t byteOffset, int16_t value) const;
  832. void SetInt32(size_t byteOffset, int32_t value) const;
  833. void SetUint8(size_t byteOffset, uint8_t value) const;
  834. void SetUint16(size_t byteOffset, uint16_t value) const;
  835. void SetUint32(size_t byteOffset, uint32_t value) const;
  836. private:
  837. template <typename T>
  838. T ReadData(size_t byteOffset) const;
  839. template <typename T>
  840. void WriteData(size_t byteOffset, T value) const;
  841. void* _data;
  842. size_t _length;
  843. };
  844. class Function : public Object {
  845. public:
  846. typedef void (*VoidCallback)(const CallbackInfo& info);
  847. typedef Value (*Callback)(const CallbackInfo& info);
  848. template <VoidCallback cb>
  849. static Function New(napi_env env,
  850. const char* utf8name = nullptr,
  851. void* data = nullptr);
  852. template <Callback cb>
  853. static Function New(napi_env env,
  854. const char* utf8name = nullptr,
  855. void* data = nullptr);
  856. template <VoidCallback cb>
  857. static Function New(napi_env env,
  858. const std::string& utf8name,
  859. void* data = nullptr);
  860. template <Callback cb>
  861. static Function New(napi_env env,
  862. const std::string& utf8name,
  863. void* data = nullptr);
  864. /// Callable must implement operator() accepting a const CallbackInfo&
  865. /// and return either void or Value.
  866. template <typename Callable>
  867. static Function New(napi_env env,
  868. Callable cb,
  869. const char* utf8name = nullptr,
  870. void* data = nullptr);
  871. /// Callable must implement operator() accepting a const CallbackInfo&
  872. /// and return either void or Value.
  873. template <typename Callable>
  874. static Function New(napi_env env,
  875. Callable cb,
  876. const std::string& utf8name,
  877. void* data = nullptr);
  878. Function();
  879. Function(napi_env env, napi_value value);
  880. Value operator ()(const std::initializer_list<napi_value>& args) const;
  881. Value Call(const std::initializer_list<napi_value>& args) const;
  882. Value Call(const std::vector<napi_value>& args) const;
  883. Value Call(size_t argc, const napi_value* args) const;
  884. Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
  885. Value Call(napi_value recv, const std::vector<napi_value>& args) const;
  886. Value Call(napi_value recv, size_t argc, const napi_value* args) const;
  887. Value MakeCallback(napi_value recv,
  888. const std::initializer_list<napi_value>& args,
  889. napi_async_context context = nullptr) const;
  890. Value MakeCallback(napi_value recv,
  891. const std::vector<napi_value>& args,
  892. napi_async_context context = nullptr) const;
  893. Value MakeCallback(napi_value recv,
  894. size_t argc,
  895. const napi_value* args,
  896. napi_async_context context = nullptr) const;
  897. Object New(const std::initializer_list<napi_value>& args) const;
  898. Object New(const std::vector<napi_value>& args) const;
  899. Object New(size_t argc, const napi_value* args) const;
  900. };
  901. class Promise : public Object {
  902. public:
  903. class Deferred {
  904. public:
  905. static Deferred New(napi_env env);
  906. Deferred(napi_env env);
  907. Napi::Promise Promise() const;
  908. Napi::Env Env() const;
  909. void Resolve(napi_value value) const;
  910. void Reject(napi_value value) const;
  911. private:
  912. napi_env _env;
  913. napi_deferred _deferred;
  914. napi_value _promise;
  915. };
  916. Promise(napi_env env, napi_value value);
  917. };
  918. template <typename T>
  919. class Buffer : public Uint8Array {
  920. public:
  921. static Buffer<T> New(napi_env env, size_t length);
  922. static Buffer<T> New(napi_env env, T* data, size_t length);
  923. // Finalizer must implement `void operator()(Env env, T* data)`.
  924. template <typename Finalizer>
  925. static Buffer<T> New(napi_env env, T* data,
  926. size_t length,
  927. Finalizer finalizeCallback);
  928. // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
  929. template <typename Finalizer, typename Hint>
  930. static Buffer<T> New(napi_env env, T* data,
  931. size_t length,
  932. Finalizer finalizeCallback,
  933. Hint* finalizeHint);
  934. static Buffer<T> Copy(napi_env env, const T* data, size_t length);
  935. Buffer();
  936. Buffer(napi_env env, napi_value value);
  937. size_t Length() const;
  938. T* Data() const;
  939. private:
  940. mutable size_t _length;
  941. mutable T* _data;
  942. Buffer(napi_env env, napi_value value, size_t length, T* data);
  943. void EnsureInfo() const;
  944. };
  945. /// Holds a counted reference to a value; initially a weak reference unless otherwise specified,
  946. /// may be changed to/from a strong reference by adjusting the refcount.
  947. ///
  948. /// The referenced value is not immediately destroyed when the reference count is zero; it is
  949. /// merely then eligible for garbage-collection if there are no other references to the value.
  950. template <typename T>
  951. class Reference {
  952. public:
  953. static Reference<T> New(const T& value, uint32_t initialRefcount = 0);
  954. Reference();
  955. Reference(napi_env env, napi_ref ref);
  956. ~Reference();
  957. // A reference can be moved but cannot be copied.
  958. Reference(Reference<T>&& other);
  959. Reference<T>& operator =(Reference<T>&& other);
  960. NAPI_DISALLOW_ASSIGN(Reference<T>)
  961. operator napi_ref() const;
  962. bool operator ==(const Reference<T> &other) const;
  963. bool operator !=(const Reference<T> &other) const;
  964. Napi::Env Env() const;
  965. bool IsEmpty() const;
  966. // Note when getting the value of a Reference it is usually correct to do so
  967. // within a HandleScope so that the value handle gets cleaned up efficiently.
  968. T Value() const;
  969. uint32_t Ref();
  970. uint32_t Unref();
  971. void Reset();
  972. void Reset(const T& value, uint32_t refcount = 0);
  973. // Call this on a reference that is declared as static data, to prevent its destructor
  974. // from running at program shutdown time, which would attempt to reset the reference when
  975. // the environment is no longer valid.
  976. void SuppressDestruct();
  977. protected:
  978. Reference(const Reference<T>&);
  979. /// !cond INTERNAL
  980. napi_env _env;
  981. napi_ref _ref;
  982. /// !endcond
  983. private:
  984. bool _suppressDestruct;
  985. };
  986. class ObjectReference: public Reference<Object> {
  987. public:
  988. ObjectReference();
  989. ObjectReference(napi_env env, napi_ref ref);
  990. // A reference can be moved but cannot be copied.
  991. ObjectReference(Reference<Object>&& other);
  992. ObjectReference& operator =(Reference<Object>&& other);
  993. ObjectReference(ObjectReference&& other);
  994. ObjectReference& operator =(ObjectReference&& other);
  995. NAPI_DISALLOW_ASSIGN(ObjectReference)
  996. Napi::Value Get(const char* utf8name) const;
  997. Napi::Value Get(const std::string& utf8name) const;
  998. void Set(const char* utf8name, napi_value value);
  999. void Set(const char* utf8name, Napi::Value value);
  1000. void Set(const char* utf8name, const char* utf8value);
  1001. void Set(const char* utf8name, bool boolValue);
  1002. void Set(const char* utf8name, double numberValue);
  1003. void Set(const std::string& utf8name, napi_value value);
  1004. void Set(const std::string& utf8name, Napi::Value value);
  1005. void Set(const std::string& utf8name, std::string& utf8value);
  1006. void Set(const std::string& utf8name, bool boolValue);
  1007. void Set(const std::string& utf8name, double numberValue);
  1008. Napi::Value Get(uint32_t index) const;
  1009. void Set(uint32_t index, const napi_value value);
  1010. void Set(uint32_t index, const Napi::Value value);
  1011. void Set(uint32_t index, const char* utf8value);
  1012. void Set(uint32_t index, const std::string& utf8value);
  1013. void Set(uint32_t index, bool boolValue);
  1014. void Set(uint32_t index, double numberValue);
  1015. protected:
  1016. ObjectReference(const ObjectReference&);
  1017. };
  1018. class FunctionReference: public Reference<Function> {
  1019. public:
  1020. FunctionReference();
  1021. FunctionReference(napi_env env, napi_ref ref);
  1022. // A reference can be moved but cannot be copied.
  1023. FunctionReference(Reference<Function>&& other);
  1024. FunctionReference& operator =(Reference<Function>&& other);
  1025. FunctionReference(FunctionReference&& other);
  1026. FunctionReference& operator =(FunctionReference&& other);
  1027. NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
  1028. Napi::Value operator ()(const std::initializer_list<napi_value>& args) const;
  1029. Napi::Value Call(const std::initializer_list<napi_value>& args) const;
  1030. Napi::Value Call(const std::vector<napi_value>& args) const;
  1031. Napi::Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
  1032. Napi::Value Call(napi_value recv, const std::vector<napi_value>& args) const;
  1033. Napi::Value Call(napi_value recv, size_t argc, const napi_value* args) const;
  1034. Napi::Value MakeCallback(napi_value recv,
  1035. const std::initializer_list<napi_value>& args,
  1036. napi_async_context context = nullptr) const;
  1037. Napi::Value MakeCallback(napi_value recv,
  1038. const std::vector<napi_value>& args,
  1039. napi_async_context context = nullptr) const;
  1040. Napi::Value MakeCallback(napi_value recv,
  1041. size_t argc,
  1042. const napi_value* args,
  1043. napi_async_context context = nullptr) const;
  1044. Object New(const std::initializer_list<napi_value>& args) const;
  1045. Object New(const std::vector<napi_value>& args) const;
  1046. };
  1047. // Shortcuts to creating a new reference with inferred type and refcount = 0.
  1048. template <typename T> Reference<T> Weak(T value);
  1049. ObjectReference Weak(Object value);
  1050. FunctionReference Weak(Function value);
  1051. // Shortcuts to creating a new reference with inferred type and refcount = 1.
  1052. template <typename T> Reference<T> Persistent(T value);
  1053. ObjectReference Persistent(Object value);
  1054. FunctionReference Persistent(Function value);
  1055. /// A persistent reference to a JavaScript error object. Use of this class depends somewhat
  1056. /// on whether C++ exceptions are enabled at compile time.
  1057. ///
  1058. /// ### Handling Errors With C++ Exceptions
  1059. ///
  1060. /// If C++ exceptions are enabled, then the `Error` class extends `std::exception` and enables
  1061. /// integrated error-handling for C++ exceptions and JavaScript exceptions.
  1062. ///
  1063. /// If a N-API call fails without executing any JavaScript code (for example due to an invalid
  1064. /// argument), then the N-API wrapper automatically converts and throws the error as a C++
  1065. /// exception of type `Napi::Error`. Or if a JavaScript function called by C++ code via N-API
  1066. /// throws a JavaScript exception, then the N-API wrapper automatically converts and throws it as
  1067. /// a C++ exception of type `Napi::Error`.
  1068. ///
  1069. /// If a C++ exception of type `Napi::Error` escapes from a N-API C++ callback, then the N-API
  1070. /// wrapper automatically converts and throws it as a JavaScript exception. Therefore, catching
  1071. /// a C++ exception of type `Napi::Error` prevents a JavaScript exception from being thrown.
  1072. ///
  1073. /// #### Example 1A - Throwing a C++ exception:
  1074. ///
  1075. /// Napi::Env env = ...
  1076. /// throw Napi::Error::New(env, "Example exception");
  1077. ///
  1078. /// Following C++ statements will not be executed. The exception will bubble up as a C++
  1079. /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
  1080. /// automatically propataged as a JavaScript exception when the callback returns to JavaScript.
  1081. ///
  1082. /// #### Example 2A - Propagating a N-API C++ exception:
  1083. ///
  1084. /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
  1085. /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
  1086. ///
  1087. /// Following C++ statements will not be executed. The exception will bubble up as a C++
  1088. /// exception of type `Napi::Error`, until it is either caught while still in C++, or else
  1089. /// automatically propagated as a JavaScript exception when the callback returns to JavaScript.
  1090. ///
  1091. /// #### Example 3A - Handling a N-API C++ exception:
  1092. ///
  1093. /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
  1094. /// Napi::Value result;
  1095. /// try {
  1096. /// result = jsFunctionThatThrows({ arg1, arg2 });
  1097. /// } catch (const Napi::Error& e) {
  1098. /// cerr << "Caught JavaScript exception: " + e.what();
  1099. /// }
  1100. ///
  1101. /// Since the exception was caught here, it will not be propagated as a JavaScript exception.
  1102. ///
  1103. /// ### Handling Errors Without C++ Exceptions
  1104. ///
  1105. /// If C++ exceptions are disabled (by defining `NAPI_DISABLE_CPP_EXCEPTIONS`) then this class
  1106. /// does not extend `std::exception`, and APIs in the `Napi` namespace do not throw C++
  1107. /// exceptions when they fail. Instead, they raise _pending_ JavaScript exceptions and
  1108. /// return _empty_ `Value`s. Calling code should check `Value::IsEmpty()` before attempting
  1109. /// to use a returned value, and may use methods on the `Env` class to check for, get, and
  1110. /// clear a pending JavaScript exception. If the pending exception is not cleared, it will
  1111. /// be thrown when the native callback returns to JavaScript.
  1112. ///
  1113. /// #### Example 1B - Throwing a JS exception
  1114. ///
  1115. /// Napi::Env env = ...
  1116. /// Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException();
  1117. /// return;
  1118. ///
  1119. /// After throwing a JS exception, the code should generally return immediately from the native
  1120. /// callback, after performing any necessary cleanup.
  1121. ///
  1122. /// #### Example 2B - Propagating a N-API JS exception:
  1123. ///
  1124. /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
  1125. /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
  1126. /// if (result.IsEmpty()) return;
  1127. ///
  1128. /// An empty value result from a N-API call indicates an error occurred, and a JavaScript
  1129. /// exception is pending. To let the exception propagate, the code should generally return
  1130. /// immediately from the native callback, after performing any necessary cleanup.
  1131. ///
  1132. /// #### Example 3B - Handling a N-API JS exception:
  1133. ///
  1134. /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
  1135. /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
  1136. /// if (result.IsEmpty()) {
  1137. /// Napi::Error e = env.GetAndClearPendingException();
  1138. /// cerr << "Caught JavaScript exception: " + e.Message();
  1139. /// }
  1140. ///
  1141. /// Since the exception was cleared here, it will not be propagated as a JavaScript exception
  1142. /// after the native callback returns.
  1143. class Error : public ObjectReference
  1144. #ifdef NAPI_CPP_EXCEPTIONS
  1145. , public std::exception
  1146. #endif // NAPI_CPP_EXCEPTIONS
  1147. {
  1148. public:
  1149. static Error New(napi_env env);
  1150. static Error New(napi_env env, const char* message);
  1151. static Error New(napi_env env, const std::string& message);
  1152. static NAPI_NO_RETURN void Fatal(const char* location, const char* message);
  1153. Error();
  1154. Error(napi_env env, napi_value value);
  1155. // An error can be moved or copied.
  1156. Error(Error&& other);
  1157. Error& operator =(Error&& other);
  1158. Error(const Error&);
  1159. Error& operator =(const Error&);
  1160. const std::string& Message() const NAPI_NOEXCEPT;
  1161. void ThrowAsJavaScriptException() const;
  1162. #ifdef NAPI_CPP_EXCEPTIONS
  1163. const char* what() const NAPI_NOEXCEPT override;
  1164. #endif // NAPI_CPP_EXCEPTIONS
  1165. protected:
  1166. /// !cond INTERNAL
  1167. typedef napi_status (*create_error_fn)(napi_env envb, napi_value code, napi_value msg, napi_value* result);
  1168. template <typename TError>
  1169. static TError New(napi_env env,
  1170. const char* message,
  1171. size_t length,
  1172. create_error_fn create_error);
  1173. /// !endcond
  1174. private:
  1175. mutable std::string _message;
  1176. };
  1177. class TypeError : public Error {
  1178. public:
  1179. static TypeError New(napi_env env, const char* message);
  1180. static TypeError New(napi_env env, const std::string& message);
  1181. TypeError();
  1182. TypeError(napi_env env, napi_value value);
  1183. };
  1184. class RangeError : public Error {
  1185. public:
  1186. static RangeError New(napi_env env, const char* message);
  1187. static RangeError New(napi_env env, const std::string& message);
  1188. RangeError();
  1189. RangeError(napi_env env, napi_value value);
  1190. };
  1191. class CallbackInfo {
  1192. public:
  1193. CallbackInfo(napi_env env, napi_callback_info info);
  1194. ~CallbackInfo();
  1195. // Disallow copying to prevent multiple free of _dynamicArgs
  1196. NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
  1197. Napi::Env Env() const;
  1198. Value NewTarget() const;
  1199. bool IsConstructCall() const;
  1200. size_t Length() const;
  1201. const Value operator [](size_t index) const;
  1202. Value This() const;
  1203. void* Data() const;
  1204. void SetData(void* data);
  1205. private:
  1206. const size_t _staticArgCount = 6;
  1207. napi_env _env;
  1208. napi_callback_info _info;
  1209. napi_value _this;
  1210. size_t _argc;
  1211. napi_value* _argv;
  1212. napi_value _staticArgs[6];
  1213. napi_value* _dynamicArgs;
  1214. void* _data;
  1215. };
  1216. class PropertyDescriptor {
  1217. public:
  1218. typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
  1219. typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
  1220. #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
  1221. template <typename Getter>
  1222. static PropertyDescriptor Accessor(const char* utf8name,
  1223. Getter getter,
  1224. napi_property_attributes attributes = napi_default,
  1225. void* data = nullptr);
  1226. template <typename Getter>
  1227. static PropertyDescriptor Accessor(const std::string& utf8name,
  1228. Getter getter,
  1229. napi_property_attributes attributes = napi_default,
  1230. void* data = nullptr);
  1231. template <typename Getter>
  1232. static PropertyDescriptor Accessor(napi_value name,
  1233. Getter getter,
  1234. napi_property_attributes attributes = napi_default,
  1235. void* data = nullptr);
  1236. template <typename Getter>
  1237. static PropertyDescriptor Accessor(Name name,
  1238. Getter getter,
  1239. napi_property_attributes attributes = napi_default,
  1240. void* data = nullptr);
  1241. template <typename Getter, typename Setter>
  1242. static PropertyDescriptor Accessor(const char* utf8name,
  1243. Getter getter,
  1244. Setter setter,
  1245. napi_property_attributes attributes = napi_default,
  1246. void* data = nullptr);
  1247. template <typename Getter, typename Setter>
  1248. static PropertyDescriptor Accessor(const std::string& utf8name,
  1249. Getter getter,
  1250. Setter setter,
  1251. napi_property_attributes attributes = napi_default,
  1252. void* data = nullptr);
  1253. template <typename Getter, typename Setter>
  1254. static PropertyDescriptor Accessor(napi_value name,
  1255. Getter getter,
  1256. Setter setter,
  1257. napi_property_attributes attributes = napi_default,
  1258. void* data = nullptr);
  1259. template <typename Getter, typename Setter>
  1260. static PropertyDescriptor Accessor(Name name,
  1261. Getter getter,
  1262. Setter setter,
  1263. napi_property_attributes attributes = napi_default,
  1264. void* data = nullptr);
  1265. template <typename Callable>
  1266. static PropertyDescriptor Function(const char* utf8name,
  1267. Callable cb,
  1268. napi_property_attributes attributes = napi_default,
  1269. void* data = nullptr);
  1270. template <typename Callable>
  1271. static PropertyDescriptor Function(const std::string& utf8name,
  1272. Callable cb,
  1273. napi_property_attributes attributes = napi_default,
  1274. void* data = nullptr);
  1275. template <typename Callable>
  1276. static PropertyDescriptor Function(napi_value name,
  1277. Callable cb,
  1278. napi_property_attributes attributes = napi_default,
  1279. void* data = nullptr);
  1280. template <typename Callable>
  1281. static PropertyDescriptor Function(Name name,
  1282. Callable cb,
  1283. napi_property_attributes attributes = napi_default,
  1284. void* data = nullptr);
  1285. #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
  1286. template <GetterCallback Getter>
  1287. static PropertyDescriptor Accessor(const char* utf8name,
  1288. napi_property_attributes attributes = napi_default,
  1289. void* data = nullptr);
  1290. template <GetterCallback Getter>
  1291. static PropertyDescriptor Accessor(const std::string& utf8name,
  1292. napi_property_attributes attributes = napi_default,
  1293. void* data = nullptr);
  1294. template <GetterCallback Getter>
  1295. static PropertyDescriptor Accessor(Name name,
  1296. napi_property_attributes attributes = napi_default,
  1297. void* data = nullptr);
  1298. template <GetterCallback Getter, SetterCallback Setter>
  1299. static PropertyDescriptor Accessor(const char* utf8name,
  1300. napi_property_attributes attributes = napi_default,
  1301. void* data = nullptr);
  1302. template <GetterCallback Getter, SetterCallback Setter>
  1303. static PropertyDescriptor Accessor(const std::string& utf8name,
  1304. napi_property_attributes attributes = napi_default,
  1305. void* data = nullptr);
  1306. template <GetterCallback Getter, SetterCallback Setter>
  1307. static PropertyDescriptor Accessor(Name name,
  1308. napi_property_attributes attributes = napi_default,
  1309. void* data = nullptr);
  1310. template <typename Getter>
  1311. static PropertyDescriptor Accessor(Napi::Env env,
  1312. Napi::Object object,
  1313. const char* utf8name,
  1314. Getter getter,
  1315. napi_property_attributes attributes = napi_default,
  1316. void* data = nullptr);
  1317. template <typename Getter>
  1318. static PropertyDescriptor Accessor(Napi::Env env,
  1319. Napi::Object object,
  1320. const std::string& utf8name,
  1321. Getter getter,
  1322. napi_property_attributes attributes = napi_default,
  1323. void* data = nullptr);
  1324. template <typename Getter>
  1325. static PropertyDescriptor Accessor(Napi::Env env,
  1326. Napi::Object object,
  1327. Name name,
  1328. Getter getter,
  1329. napi_property_attributes attributes = napi_default,
  1330. void* data = nullptr);
  1331. template <typename Getter, typename Setter>
  1332. static PropertyDescriptor Accessor(Napi::Env env,
  1333. Napi::Object object,
  1334. const char* utf8name,
  1335. Getter getter,
  1336. Setter setter,
  1337. napi_property_attributes attributes = napi_default,
  1338. void* data = nullptr);
  1339. template <typename Getter, typename Setter>
  1340. static PropertyDescriptor Accessor(Napi::Env env,
  1341. Napi::Object object,
  1342. const std::string& utf8name,
  1343. Getter getter,
  1344. Setter setter,
  1345. napi_property_attributes attributes = napi_default,
  1346. void* data = nullptr);
  1347. template <typename Getter, typename Setter>
  1348. static PropertyDescriptor Accessor(Napi::Env env,
  1349. Napi::Object object,
  1350. Name name,
  1351. Getter getter,
  1352. Setter setter,
  1353. napi_property_attributes attributes = napi_default,
  1354. void* data = nullptr);
  1355. template <typename Callable>
  1356. static PropertyDescriptor Function(Napi::Env env,
  1357. Napi::Object object,
  1358. const char* utf8name,
  1359. Callable cb,
  1360. napi_property_attributes attributes = napi_default,
  1361. void* data = nullptr);
  1362. template <typename Callable>
  1363. static PropertyDescriptor Function(Napi::Env env,
  1364. Napi::Object object,
  1365. const std::string& utf8name,
  1366. Callable cb,
  1367. napi_property_attributes attributes = napi_default,
  1368. void* data = nullptr);
  1369. template <typename Callable>
  1370. static PropertyDescriptor Function(Napi::Env env,
  1371. Napi::Object object,
  1372. Name name,
  1373. Callable cb,
  1374. napi_property_attributes attributes = napi_default,
  1375. void* data = nullptr);
  1376. static PropertyDescriptor Value(const char* utf8name,
  1377. napi_value value,
  1378. napi_property_attributes attributes = napi_default);
  1379. static PropertyDescriptor Value(const std::string& utf8name,
  1380. napi_value value,
  1381. napi_property_attributes attributes = napi_default);
  1382. static PropertyDescriptor Value(napi_value name,
  1383. napi_value value,
  1384. napi_property_attributes attributes = napi_default);
  1385. static PropertyDescriptor Value(Name name,
  1386. Napi::Value value,
  1387. napi_property_attributes attributes = napi_default);
  1388. PropertyDescriptor(napi_property_descriptor desc);
  1389. operator napi_property_descriptor&();
  1390. operator const napi_property_descriptor&() const;
  1391. private:
  1392. template <GetterCallback Getter>
  1393. static napi_value GetterCallbackWrapper(napi_env env, napi_callback_info info);
  1394. template <SetterCallback Setter>
  1395. static napi_value SetterCallbackWrapper(napi_env env, napi_callback_info info);
  1396. napi_property_descriptor _desc;
  1397. };
  1398. /// Property descriptor for use with `ObjectWrap::DefineClass()`.
  1399. ///
  1400. /// This is different from the standalone `PropertyDescriptor` because it is specific to each
  1401. /// `ObjectWrap<T>` subclass. This prevents using descriptors from a different class when
  1402. /// defining a new class (preventing the callbacks from having incorrect `this` pointers).
  1403. template <typename T>
  1404. class ClassPropertyDescriptor {
  1405. public:
  1406. ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {}
  1407. operator napi_property_descriptor&() { return _desc; }
  1408. operator const napi_property_descriptor&() const { return _desc; }
  1409. private:
  1410. napi_property_descriptor _desc;
  1411. };
  1412. /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
  1413. /// "wrapped" by a JavaScript object that is managed by this class.
  1414. ///
  1415. /// At initialization time, the `DefineClass()` method must be used to
  1416. /// hook up the accessor and method callbacks. It takes a list of
  1417. /// property descriptors, which can be constructed via the various
  1418. /// static methods on the base class.
  1419. ///
  1420. /// #### Example:
  1421. ///
  1422. /// class Example: public Napi::ObjectWrap<Example> {
  1423. /// public:
  1424. /// static void Initialize(Napi::Env& env, Napi::Object& target) {
  1425. /// Napi::Function constructor = DefineClass(env, "Example", {
  1426. /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
  1427. /// InstanceMethod<&Example::DoSomething>("doSomething"),
  1428. /// });
  1429. /// target.Set("Example", constructor);
  1430. /// }
  1431. ///
  1432. /// Example(const Napi::CallbackInfo& info); // Constructor
  1433. /// Napi::Value GetSomething(const Napi::CallbackInfo& info);
  1434. /// void SetSomething(const Napi::CallbackInfo& info, const Napi::Value& value);
  1435. /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
  1436. /// }
  1437. template <typename T>
  1438. class ObjectWrap : public Reference<Object> {
  1439. public:
  1440. ObjectWrap(const CallbackInfo& callbackInfo);
  1441. virtual ~ObjectWrap();
  1442. static T* Unwrap(Object wrapper);
  1443. // Methods exposed to JavaScript must conform to one of these callback signatures.
  1444. typedef void (*StaticVoidMethodCallback)(const CallbackInfo& info);
  1445. typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
  1446. typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
  1447. typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
  1448. typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
  1449. typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
  1450. typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
  1451. typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
  1452. typedef ClassPropertyDescriptor<T> PropertyDescriptor;
  1453. static Function DefineClass(Napi::Env env,
  1454. const char* utf8name,
  1455. const std::initializer_list<PropertyDescriptor>& properties,
  1456. void* data = nullptr);
  1457. static Function DefineClass(Napi::Env env,
  1458. const char* utf8name,
  1459. const std::vector<PropertyDescriptor>& properties,
  1460. void* data = nullptr);
  1461. static PropertyDescriptor StaticMethod(const char* utf8name,
  1462. StaticVoidMethodCallback method,
  1463. napi_property_attributes attributes = napi_default,
  1464. void* data = nullptr);
  1465. static PropertyDescriptor StaticMethod(const char* utf8name,
  1466. StaticMethodCallback method,
  1467. napi_property_attributes attributes = napi_default,
  1468. void* data = nullptr);
  1469. static PropertyDescriptor StaticMethod(Symbol name,
  1470. StaticVoidMethodCallback method,
  1471. napi_property_attributes attributes = napi_default,
  1472. void* data = nullptr);
  1473. static PropertyDescriptor StaticMethod(Symbol name,
  1474. StaticMethodCallback method,
  1475. napi_property_attributes attributes = napi_default,
  1476. void* data = nullptr);
  1477. template <StaticVoidMethodCallback method>
  1478. static PropertyDescriptor StaticMethod(const char* utf8name,
  1479. napi_property_attributes attributes = napi_default,
  1480. void* data = nullptr);
  1481. template <StaticVoidMethodCallback method>
  1482. static PropertyDescriptor StaticMethod(Symbol name,
  1483. napi_property_attributes attributes = napi_default,
  1484. void* data = nullptr);
  1485. template <StaticMethodCallback method>
  1486. static PropertyDescriptor StaticMethod(const char* utf8name,
  1487. napi_property_attributes attributes = napi_default,
  1488. void* data = nullptr);
  1489. template <StaticMethodCallback method>
  1490. static PropertyDescriptor StaticMethod(Symbol name,
  1491. napi_property_attributes attributes = napi_default,
  1492. void* data = nullptr);
  1493. static PropertyDescriptor StaticAccessor(const char* utf8name,
  1494. StaticGetterCallback getter,
  1495. StaticSetterCallback setter,
  1496. napi_property_attributes attributes = napi_default,
  1497. void* data = nullptr);
  1498. static PropertyDescriptor StaticAccessor(Symbol name,
  1499. StaticGetterCallback getter,
  1500. StaticSetterCallback setter,
  1501. napi_property_attributes attributes = napi_default,
  1502. void* data = nullptr);
  1503. template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
  1504. static PropertyDescriptor StaticAccessor(const char* utf8name,
  1505. napi_property_attributes attributes = napi_default,
  1506. void* data = nullptr);
  1507. template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
  1508. static PropertyDescriptor StaticAccessor(Symbol name,
  1509. napi_property_attributes attributes = napi_default,
  1510. void* data = nullptr);
  1511. static PropertyDescriptor InstanceMethod(const char* utf8name,
  1512. InstanceVoidMethodCallback method,
  1513. napi_property_attributes attributes = napi_default,
  1514. void* data = nullptr);
  1515. static PropertyDescriptor InstanceMethod(const char* utf8name,
  1516. InstanceMethodCallback method,
  1517. napi_property_attributes attributes = napi_default,
  1518. void* data = nullptr);
  1519. static PropertyDescriptor InstanceMethod(Symbol name,
  1520. InstanceVoidMethodCallback method,
  1521. napi_property_attributes attributes = napi_default,
  1522. void* data = nullptr);
  1523. static PropertyDescriptor InstanceMethod(Symbol name,
  1524. InstanceMethodCallback method,
  1525. napi_property_attributes attributes = napi_default,
  1526. void* data = nullptr);
  1527. template <InstanceVoidMethodCallback method>
  1528. static PropertyDescriptor InstanceMethod(const char* utf8name,
  1529. napi_property_attributes attributes = napi_default,
  1530. void* data = nullptr);
  1531. template <InstanceMethodCallback method>
  1532. static PropertyDescriptor InstanceMethod(const char* utf8name,
  1533. napi_property_attributes attributes = napi_default,
  1534. void* data = nullptr);
  1535. template <InstanceVoidMethodCallback method>
  1536. static PropertyDescriptor InstanceMethod(Symbol name,
  1537. napi_property_attributes attributes = napi_default,
  1538. void* data = nullptr);
  1539. template <InstanceMethodCallback method>
  1540. static PropertyDescriptor InstanceMethod(Symbol name,
  1541. napi_property_attributes attributes = napi_default,
  1542. void* data = nullptr);
  1543. static PropertyDescriptor InstanceAccessor(const char* utf8name,
  1544. InstanceGetterCallback getter,
  1545. InstanceSetterCallback setter,
  1546. napi_property_attributes attributes = napi_default,
  1547. void* data = nullptr);
  1548. static PropertyDescriptor InstanceAccessor(Symbol name,
  1549. InstanceGetterCallback getter,
  1550. InstanceSetterCallback setter,
  1551. napi_property_attributes attributes = napi_default,
  1552. void* data = nullptr);
  1553. template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
  1554. static PropertyDescriptor InstanceAccessor(const char* utf8name,
  1555. napi_property_attributes attributes = napi_default,
  1556. void* data = nullptr);
  1557. template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
  1558. static PropertyDescriptor InstanceAccessor(Symbol name,
  1559. napi_property_attributes attributes = napi_default,
  1560. void* data = nullptr);
  1561. static PropertyDescriptor StaticValue(const char* utf8name,
  1562. Napi::Value value,
  1563. napi_property_attributes attributes = napi_default);
  1564. static PropertyDescriptor StaticValue(Symbol name,
  1565. Napi::Value value,
  1566. napi_property_attributes attributes = napi_default);
  1567. static PropertyDescriptor InstanceValue(const char* utf8name,
  1568. Napi::Value value,
  1569. napi_property_attributes attributes = napi_default);
  1570. static PropertyDescriptor InstanceValue(Symbol name,
  1571. Napi::Value value,
  1572. napi_property_attributes attributes = napi_default);
  1573. virtual void Finalize(Napi::Env env);
  1574. private:
  1575. using This = ObjectWrap<T>;
  1576. static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
  1577. static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
  1578. static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
  1579. static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
  1580. static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info);
  1581. static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
  1582. static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
  1583. static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
  1584. static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
  1585. static void FinalizeCallback(napi_env env, void* data, void* hint);
  1586. static Function DefineClass(Napi::Env env,
  1587. const char* utf8name,
  1588. const size_t props_count,
  1589. const napi_property_descriptor* props,
  1590. void* data = nullptr);
  1591. template <typename TCallback>
  1592. struct MethodCallbackData {
  1593. TCallback callback;
  1594. void* data;
  1595. };
  1596. typedef MethodCallbackData<StaticVoidMethodCallback> StaticVoidMethodCallbackData;
  1597. typedef MethodCallbackData<StaticMethodCallback> StaticMethodCallbackData;
  1598. typedef MethodCallbackData<InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
  1599. typedef MethodCallbackData<InstanceMethodCallback> InstanceMethodCallbackData;
  1600. template <typename TGetterCallback, typename TSetterCallback>
  1601. struct AccessorCallbackData {
  1602. TGetterCallback getterCallback;
  1603. TSetterCallback setterCallback;
  1604. void* data;
  1605. };
  1606. typedef AccessorCallbackData<StaticGetterCallback, StaticSetterCallback>
  1607. StaticAccessorCallbackData;
  1608. typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
  1609. InstanceAccessorCallbackData;
  1610. template <StaticVoidMethodCallback method>
  1611. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1612. template <StaticMethodCallback method>
  1613. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1614. template <InstanceVoidMethodCallback method>
  1615. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1616. template <InstanceMethodCallback method>
  1617. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1618. template <StaticSetterCallback method>
  1619. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1620. template <InstanceSetterCallback method>
  1621. static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
  1622. template <StaticGetterCallback getter> struct StaticGetterTag {};
  1623. template <StaticSetterCallback setter> struct StaticSetterTag {};
  1624. template <InstanceGetterCallback getter> struct GetterTag {};
  1625. template <InstanceSetterCallback setter> struct SetterTag {};
  1626. template <StaticGetterCallback getter>
  1627. static napi_callback WrapStaticGetter(StaticGetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
  1628. static napi_callback WrapStaticGetter(StaticGetterTag<nullptr>) noexcept { return nullptr; }
  1629. template <StaticSetterCallback setter>
  1630. static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
  1631. static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
  1632. template <InstanceGetterCallback getter>
  1633. static napi_callback WrapGetter(GetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
  1634. static napi_callback WrapGetter(GetterTag<nullptr>) noexcept { return nullptr; }
  1635. template <InstanceSetterCallback setter>
  1636. static napi_callback WrapSetter(SetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
  1637. static napi_callback WrapSetter(SetterTag<nullptr>) noexcept { return nullptr; }
  1638. bool _construction_failed = true;
  1639. };
  1640. class HandleScope {
  1641. public:
  1642. HandleScope(napi_env env, napi_handle_scope scope);
  1643. explicit HandleScope(Napi::Env env);
  1644. ~HandleScope();
  1645. // Disallow copying to prevent double close of napi_handle_scope
  1646. NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
  1647. operator napi_handle_scope() const;
  1648. Napi::Env Env() const;
  1649. private:
  1650. napi_env _env;
  1651. napi_handle_scope _scope;
  1652. };
  1653. class EscapableHandleScope {
  1654. public:
  1655. EscapableHandleScope(napi_env env, napi_escapable_handle_scope scope);
  1656. explicit EscapableHandleScope(Napi::Env env);
  1657. ~EscapableHandleScope();
  1658. // Disallow copying to prevent double close of napi_escapable_handle_scope
  1659. NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
  1660. operator napi_escapable_handle_scope() const;
  1661. Napi::Env Env() const;
  1662. Value Escape(napi_value escapee);
  1663. private:
  1664. napi_env _env;
  1665. napi_escapable_handle_scope _scope;
  1666. };
  1667. #if (NAPI_VERSION > 2)
  1668. class CallbackScope {
  1669. public:
  1670. CallbackScope(napi_env env, napi_callback_scope scope);
  1671. CallbackScope(napi_env env, napi_async_context context);
  1672. virtual ~CallbackScope();
  1673. // Disallow copying to prevent double close of napi_callback_scope
  1674. NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
  1675. operator napi_callback_scope() const;
  1676. Napi::Env Env() const;
  1677. private:
  1678. napi_env _env;
  1679. napi_callback_scope _scope;
  1680. };
  1681. #endif
  1682. class AsyncContext {
  1683. public:
  1684. explicit AsyncContext(napi_env env, const char* resource_name);
  1685. explicit AsyncContext(napi_env env, const char* resource_name, const Object& resource);
  1686. virtual ~AsyncContext();
  1687. AsyncContext(AsyncContext&& other);
  1688. AsyncContext& operator =(AsyncContext&& other);
  1689. NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
  1690. operator napi_async_context() const;
  1691. Napi::Env Env() const;
  1692. private:
  1693. napi_env _env;
  1694. napi_async_context _context;
  1695. };
  1696. class AsyncWorker {
  1697. public:
  1698. virtual ~AsyncWorker();
  1699. // An async worker can be moved but cannot be copied.
  1700. AsyncWorker(AsyncWorker&& other);
  1701. AsyncWorker& operator =(AsyncWorker&& other);
  1702. NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
  1703. operator napi_async_work() const;
  1704. Napi::Env Env() const;
  1705. void Queue();
  1706. void Cancel();
  1707. void SuppressDestruct();
  1708. ObjectReference& Receiver();
  1709. FunctionReference& Callback();
  1710. virtual void OnExecute(Napi::Env env);
  1711. virtual void OnWorkComplete(Napi::Env env,
  1712. napi_status status);
  1713. protected:
  1714. explicit AsyncWorker(const Function& callback);
  1715. explicit AsyncWorker(const Function& callback,
  1716. const char* resource_name);
  1717. explicit AsyncWorker(const Function& callback,
  1718. const char* resource_name,
  1719. const Object& resource);
  1720. explicit AsyncWorker(const Object& receiver,
  1721. const Function& callback);
  1722. explicit AsyncWorker(const Object& receiver,
  1723. const Function& callback,
  1724. const char* resource_name);
  1725. explicit AsyncWorker(const Object& receiver,
  1726. const Function& callback,
  1727. const char* resource_name,
  1728. const Object& resource);
  1729. explicit AsyncWorker(Napi::Env env);
  1730. explicit AsyncWorker(Napi::Env env,
  1731. const char* resource_name);
  1732. explicit AsyncWorker(Napi::Env env,
  1733. const char* resource_name,
  1734. const Object& resource);
  1735. virtual void Execute() = 0;
  1736. virtual void OnOK();
  1737. virtual void OnError(const Error& e);
  1738. virtual void Destroy();
  1739. virtual std::vector<napi_value> GetResult(Napi::Env env);
  1740. void SetError(const std::string& error);
  1741. private:
  1742. static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
  1743. static inline void OnAsyncWorkComplete(napi_env env,
  1744. napi_status status,
  1745. void* asyncworker);
  1746. napi_env _env;
  1747. napi_async_work _work;
  1748. ObjectReference _receiver;
  1749. FunctionReference _callback;
  1750. std::string _error;
  1751. bool _suppress_destruct;
  1752. };
  1753. #if (NAPI_VERSION > 3)
  1754. class ThreadSafeFunction {
  1755. public:
  1756. // This API may only be called from the main thread.
  1757. template <typename ResourceString>
  1758. static ThreadSafeFunction New(napi_env env,
  1759. const Function& callback,
  1760. ResourceString resourceName,
  1761. size_t maxQueueSize,
  1762. size_t initialThreadCount);
  1763. // This API may only be called from the main thread.
  1764. template <typename ResourceString, typename ContextType>
  1765. static ThreadSafeFunction New(napi_env env,
  1766. const Function& callback,
  1767. ResourceString resourceName,
  1768. size_t maxQueueSize,
  1769. size_t initialThreadCount,
  1770. ContextType* context);
  1771. // This API may only be called from the main thread.
  1772. template <typename ResourceString, typename Finalizer>
  1773. static ThreadSafeFunction New(napi_env env,
  1774. const Function& callback,
  1775. ResourceString resourceName,
  1776. size_t maxQueueSize,
  1777. size_t initialThreadCount,
  1778. Finalizer finalizeCallback);
  1779. // This API may only be called from the main thread.
  1780. template <typename ResourceString, typename Finalizer,
  1781. typename FinalizerDataType>
  1782. static ThreadSafeFunction New(napi_env env,
  1783. const Function& callback,
  1784. ResourceString resourceName,
  1785. size_t maxQueueSize,
  1786. size_t initialThreadCount,
  1787. Finalizer finalizeCallback,
  1788. FinalizerDataType* data);
  1789. // This API may only be called from the main thread.
  1790. template <typename ResourceString, typename ContextType, typename Finalizer>
  1791. static ThreadSafeFunction New(napi_env env,
  1792. const Function& callback,
  1793. ResourceString resourceName,
  1794. size_t maxQueueSize,
  1795. size_t initialThreadCount,
  1796. ContextType* context,
  1797. Finalizer finalizeCallback);
  1798. // This API may only be called from the main thread.
  1799. template <typename ResourceString, typename ContextType,
  1800. typename Finalizer, typename FinalizerDataType>
  1801. static ThreadSafeFunction New(napi_env env,
  1802. const Function& callback,
  1803. ResourceString resourceName,
  1804. size_t maxQueueSize,
  1805. size_t initialThreadCount,
  1806. ContextType* context,
  1807. Finalizer finalizeCallback,
  1808. FinalizerDataType* data);
  1809. // This API may only be called from the main thread.
  1810. template <typename ResourceString>
  1811. static ThreadSafeFunction New(napi_env env,
  1812. const Function& callback,
  1813. const Object& resource,
  1814. ResourceString resourceName,
  1815. size_t maxQueueSize,
  1816. size_t initialThreadCount);
  1817. // This API may only be called from the main thread.
  1818. template <typename ResourceString, typename ContextType>
  1819. static ThreadSafeFunction New(napi_env env,
  1820. const Function& callback,
  1821. const Object& resource,
  1822. ResourceString resourceName,
  1823. size_t maxQueueSize,
  1824. size_t initialThreadCount,
  1825. ContextType* context);
  1826. // This API may only be called from the main thread.
  1827. template <typename ResourceString, typename Finalizer>
  1828. static ThreadSafeFunction New(napi_env env,
  1829. const Function& callback,
  1830. const Object& resource,
  1831. ResourceString resourceName,
  1832. size_t maxQueueSize,
  1833. size_t initialThreadCount,
  1834. Finalizer finalizeCallback);
  1835. // This API may only be called from the main thread.
  1836. template <typename ResourceString, typename Finalizer,
  1837. typename FinalizerDataType>
  1838. static ThreadSafeFunction New(napi_env env,
  1839. const Function& callback,
  1840. const Object& resource,
  1841. ResourceString resourceName,
  1842. size_t maxQueueSize,
  1843. size_t initialThreadCount,
  1844. Finalizer finalizeCallback,
  1845. FinalizerDataType* data);
  1846. // This API may only be called from the main thread.
  1847. template <typename ResourceString, typename ContextType, typename Finalizer>
  1848. static ThreadSafeFunction New(napi_env env,
  1849. const Function& callback,
  1850. const Object& resource,
  1851. ResourceString resourceName,
  1852. size_t maxQueueSize,
  1853. size_t initialThreadCount,
  1854. ContextType* context,
  1855. Finalizer finalizeCallback);
  1856. // This API may only be called from the main thread.
  1857. template <typename ResourceString, typename ContextType,
  1858. typename Finalizer, typename FinalizerDataType>
  1859. static ThreadSafeFunction New(napi_env env,
  1860. const Function& callback,
  1861. const Object& resource,
  1862. ResourceString resourceName,
  1863. size_t maxQueueSize,
  1864. size_t initialThreadCount,
  1865. ContextType* context,
  1866. Finalizer finalizeCallback,
  1867. FinalizerDataType* data);
  1868. ThreadSafeFunction();
  1869. ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
  1870. operator napi_threadsafe_function() const;
  1871. // This API may be called from any thread.
  1872. napi_status BlockingCall() const;
  1873. // This API may be called from any thread.
  1874. template <typename Callback>
  1875. napi_status BlockingCall(Callback callback) const;
  1876. // This API may be called from any thread.
  1877. template <typename DataType, typename Callback>
  1878. napi_status BlockingCall(DataType* data, Callback callback) const;
  1879. // This API may be called from any thread.
  1880. napi_status NonBlockingCall() const;
  1881. // This API may be called from any thread.
  1882. template <typename Callback>
  1883. napi_status NonBlockingCall(Callback callback) const;
  1884. // This API may be called from any thread.
  1885. template <typename DataType, typename Callback>
  1886. napi_status NonBlockingCall(DataType* data, Callback callback) const;
  1887. // This API may only be called from the main thread.
  1888. void Ref(napi_env env) const;
  1889. // This API may only be called from the main thread.
  1890. void Unref(napi_env env) const;
  1891. // This API may be called from any thread.
  1892. napi_status Acquire() const;
  1893. // This API may be called from any thread.
  1894. napi_status Release();
  1895. // This API may be called from any thread.
  1896. napi_status Abort();
  1897. struct ConvertibleContext
  1898. {
  1899. template <class T>
  1900. operator T*() { return static_cast<T*>(context); }
  1901. void* context;
  1902. };
  1903. // This API may be called from any thread.
  1904. ConvertibleContext GetContext() const;
  1905. private:
  1906. using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>;
  1907. template <typename ResourceString, typename ContextType,
  1908. typename Finalizer, typename FinalizerDataType>
  1909. static ThreadSafeFunction New(napi_env env,
  1910. const Function& callback,
  1911. const Object& resource,
  1912. ResourceString resourceName,
  1913. size_t maxQueueSize,
  1914. size_t initialThreadCount,
  1915. ContextType* context,
  1916. Finalizer finalizeCallback,
  1917. FinalizerDataType* data,
  1918. napi_finalize wrapper);
  1919. napi_status CallInternal(CallbackWrapper* callbackWrapper,
  1920. napi_threadsafe_function_call_mode mode) const;
  1921. static void CallJS(napi_env env,
  1922. napi_value jsCallback,
  1923. void* context,
  1924. void* data);
  1925. napi_threadsafe_function _tsfn;
  1926. };
  1927. template <typename DataType>
  1928. class AsyncProgressWorkerBase : public AsyncWorker {
  1929. public:
  1930. virtual void OnWorkProgress(DataType* data) = 0;
  1931. class ThreadSafeData {
  1932. public:
  1933. ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
  1934. : _asyncprogressworker(asyncprogressworker), _data(data) {}
  1935. AsyncProgressWorkerBase* asyncprogressworker() { return _asyncprogressworker; };
  1936. DataType* data() { return _data; };
  1937. private:
  1938. AsyncProgressWorkerBase* _asyncprogressworker;
  1939. DataType* _data;
  1940. };
  1941. void OnWorkComplete(Napi::Env env, napi_status status) override;
  1942. protected:
  1943. explicit AsyncProgressWorkerBase(const Object& receiver,
  1944. const Function& callback,
  1945. const char* resource_name,
  1946. const Object& resource,
  1947. size_t queue_size = 1);
  1948. virtual ~AsyncProgressWorkerBase();
  1949. // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
  1950. // Refs: https://github.com/nodejs/node/pull/27791
  1951. #if NAPI_VERSION > 4
  1952. explicit AsyncProgressWorkerBase(Napi::Env env,
  1953. const char* resource_name,
  1954. const Object& resource,
  1955. size_t queue_size = 1);
  1956. #endif
  1957. static inline void OnAsyncWorkProgress(Napi::Env env,
  1958. Napi::Function jsCallback,
  1959. void* data);
  1960. napi_status NonBlockingCall(DataType* data);
  1961. private:
  1962. ThreadSafeFunction _tsfn;
  1963. bool _work_completed = false;
  1964. napi_status _complete_status;
  1965. static inline void OnThreadSafeFunctionFinalize(Napi::Env env, void* data, AsyncProgressWorkerBase* context);
  1966. };
  1967. template<class T>
  1968. class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
  1969. public:
  1970. virtual ~AsyncProgressWorker();
  1971. class ExecutionProgress {
  1972. friend class AsyncProgressWorker;
  1973. public:
  1974. void Signal() const;
  1975. void Send(const T* data, size_t count) const;
  1976. private:
  1977. explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {}
  1978. AsyncProgressWorker* const _worker;
  1979. };
  1980. void OnWorkProgress(void*) override;
  1981. protected:
  1982. explicit AsyncProgressWorker(const Function& callback);
  1983. explicit AsyncProgressWorker(const Function& callback,
  1984. const char* resource_name);
  1985. explicit AsyncProgressWorker(const Function& callback,
  1986. const char* resource_name,
  1987. const Object& resource);
  1988. explicit AsyncProgressWorker(const Object& receiver,
  1989. const Function& callback);
  1990. explicit AsyncProgressWorker(const Object& receiver,
  1991. const Function& callback,
  1992. const char* resource_name);
  1993. explicit AsyncProgressWorker(const Object& receiver,
  1994. const Function& callback,
  1995. const char* resource_name,
  1996. const Object& resource);
  1997. // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
  1998. // Refs: https://github.com/nodejs/node/pull/27791
  1999. #if NAPI_VERSION > 4
  2000. explicit AsyncProgressWorker(Napi::Env env);
  2001. explicit AsyncProgressWorker(Napi::Env env,
  2002. const char* resource_name);
  2003. explicit AsyncProgressWorker(Napi::Env env,
  2004. const char* resource_name,
  2005. const Object& resource);
  2006. #endif
  2007. virtual void Execute(const ExecutionProgress& progress) = 0;
  2008. virtual void OnProgress(const T* data, size_t count) = 0;
  2009. private:
  2010. void Execute() override;
  2011. void Signal() const;
  2012. void SendProgress_(const T* data, size_t count);
  2013. std::mutex _mutex;
  2014. T* _asyncdata;
  2015. size_t _asyncsize;
  2016. };
  2017. template<class T>
  2018. class AsyncProgressQueueWorker : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
  2019. public:
  2020. virtual ~AsyncProgressQueueWorker() {};
  2021. class ExecutionProgress {
  2022. friend class AsyncProgressQueueWorker;
  2023. public:
  2024. void Signal() const;
  2025. void Send(const T* data, size_t count) const;
  2026. private:
  2027. explicit ExecutionProgress(AsyncProgressQueueWorker* worker) : _worker(worker) {}
  2028. AsyncProgressQueueWorker* const _worker;
  2029. };
  2030. void OnWorkComplete(Napi::Env env, napi_status status) override;
  2031. void OnWorkProgress(std::pair<T*, size_t>*) override;
  2032. protected:
  2033. explicit AsyncProgressQueueWorker(const Function& callback);
  2034. explicit AsyncProgressQueueWorker(const Function& callback,
  2035. const char* resource_name);
  2036. explicit AsyncProgressQueueWorker(const Function& callback,
  2037. const char* resource_name,
  2038. const Object& resource);
  2039. explicit AsyncProgressQueueWorker(const Object& receiver,
  2040. const Function& callback);
  2041. explicit AsyncProgressQueueWorker(const Object& receiver,
  2042. const Function& callback,
  2043. const char* resource_name);
  2044. explicit AsyncProgressQueueWorker(const Object& receiver,
  2045. const Function& callback,
  2046. const char* resource_name,
  2047. const Object& resource);
  2048. // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
  2049. // Refs: https://github.com/nodejs/node/pull/27791
  2050. #if NAPI_VERSION > 4
  2051. explicit AsyncProgressQueueWorker(Napi::Env env);
  2052. explicit AsyncProgressQueueWorker(Napi::Env env,
  2053. const char* resource_name);
  2054. explicit AsyncProgressQueueWorker(Napi::Env env,
  2055. const char* resource_name,
  2056. const Object& resource);
  2057. #endif
  2058. virtual void Execute(const ExecutionProgress& progress) = 0;
  2059. virtual void OnProgress(const T* data, size_t count) = 0;
  2060. private:
  2061. void Execute() override;
  2062. void Signal() const;
  2063. void SendProgress_(const T* data, size_t count);
  2064. };
  2065. #endif
  2066. // Memory management.
  2067. class MemoryManagement {
  2068. public:
  2069. static int64_t AdjustExternalMemory(Env env, int64_t change_in_bytes);
  2070. };
  2071. // Version management
  2072. class VersionManagement {
  2073. public:
  2074. static uint32_t GetNapiVersion(Env env);
  2075. static const napi_node_version* GetNodeVersion(Env env);
  2076. };
  2077. } // namespace Napi
  2078. // Inline implementations of all the above class methods are included here.
  2079. #include "napi-inl.h"
  2080. #endif // SRC_NAPI_H_