|
5 | 5 | # Warning |
6 | 6 |
|
7 | 7 | This is a **VERY SIMPLE TRY** to make an inheritance patch of napi, and this only implements necessary apis for JSAudio-Android project! |
8 | | -This patch **DOESN'T SUPPORT** multi-thread, **DOESN'T COMPATIABLE WITH** origin `napi_api_module()` api and **DOESN'T SUPPORT** multi-inheritance. This is designed to **ONLY BE USED** in JSAudio-Android PROJECT! |
| 8 | +This patch **DOESN'T SUPPORT** multi-thread, **DOESN'T COMPATIABLE WITH** origin `napi_api_module()` api and **DOESN'T SUPPORT** multi-inheritance. This is designed to **ONLY BE USED** in JSAudio-Android project! |
9 | 9 | If you want to use napi with inheritance more safe and realiable, please try https://github.com/ajihyf/node-addon-api-helper. |
10 | 10 |
|
11 | 11 | # Usage |
12 | 12 |
|
13 | | -`napi_ih` only supports be used in inheritance. |
| 13 | +`napi_ih` only be used in inheritance. |
14 | 14 |
|
15 | 15 | ## Inheritance |
16 | 16 |
|
@@ -78,9 +78,12 @@ enum class ClassVisibility{ |
78 | 78 |
|
79 | 79 | `Napi_IH::IHCallbackInfo` is a wrapper of `Napi::CallbackInfo`. |
80 | 80 |
|
81 | | -### IHCallbackInfo(const Napi::CallbackInfo &info); |
| 81 | +### IHCallbackInfo(info, user_data); |
82 | 82 |
|
83 | | -You can only create `IHCallbackInfo` by a `Napi::CallbackInfo`, you cannot assign an `IHCallbackInfo` object to other. |
| 83 | +You can only create `IHCallbackInfo` by a `Napi::CallbackInfo`, you cannot assign an `IHCallbackInfo` object to another `IHCallbackInfo` object. |
| 84 | + |
| 85 | +- `[in] info`: A `Napi::CallbackInfo` type object. |
| 86 | +- `[in] data (optional)`: An optional `void *` pointer, which is used to pass user data to constructor. Default is `nullptr`. |
84 | 87 |
|
85 | 88 | ### Napi::Env Env() const; |
86 | 89 |
|
@@ -243,6 +246,34 @@ The `method` must be of the form |
243 | 246 | void MethodName(const Napi::CallbackInfo& info); |
244 | 247 | ``` |
245 | 248 |
|
| 249 | +template <typename T, IHObjectWrap::InstanceMethodCallback<T> method> |
| 250 | +
|
| 251 | +### InstanceMethod\<T, method\>(utf8name, attributes = napi_default, data = nullptr); |
| 252 | +
|
| 253 | +```cpp |
| 254 | +template <typename T, IHObjectWrap::InstanceMethodCallback<T> method> |
| 255 | +static Napi::ClassPropertyDescriptor<MethodWrapper> |
| 256 | +InstanceMethod(const char *utf8name, |
| 257 | + napi_property_attributes attributes = napi_default, |
| 258 | + void *data = nullptr); |
| 259 | +``` |
| 260 | + |
| 261 | +- `[in] T`: Current cpp class type which the `method` belongs to. |
| 262 | +- `[in] method`: The native function that represents a method provided by the |
| 263 | + add-on. |
| 264 | +- `[in] utf8name`: Null-terminated string that represents the name of the method |
| 265 | + provided by instances of the class. |
| 266 | +- `[in] attributes`: The attributes associated with the property. One or more of |
| 267 | + `napi_property_attributes`. |
| 268 | +- `[in] data`: User-provided data passed into the method when it is invoked. |
| 269 | + |
| 270 | +Returns a `Napi::ClassPropertyDescriptor<T>` object that represents a method |
| 271 | +provided by instances of the class. The method must be of the form: |
| 272 | + |
| 273 | +```cpp |
| 274 | +Napi::Value MethodName(const Napi::CallbackInfo& info); |
| 275 | +``` |
| 276 | +
|
246 | 277 | ### InstanceAccessor\<T, getter, setter = nullptr\>(..., vector<PropertyDescriptor>, ...) |
247 | 278 |
|
248 | 279 | ```cpp |
@@ -304,7 +335,7 @@ Return the cpp object pointer of the cpp object wrapped by js object. If T is no |
304 | 335 |
|
305 | 336 | ## Napi_IH::FunctionWrapper |
306 | 337 |
|
307 | | -A wrapper of `Napi::Function` to make sure you can pass parameters other than `const Napi::CallbackInfo&` type. |
| 338 | +A wrapper of `Napi::Function` to make sure you can new NAPI object with parameters other than `const Napi::CallbackInfo&` type. |
308 | 339 |
|
309 | 340 | ### Constructor |
310 | 341 |
|
@@ -356,8 +387,6 @@ private: |
356 | 387 | }; |
357 | 388 | ``` |
358 | 389 |
|
359 | | -WARN: this api MUST be used in SYNC! |
360 | | -
|
361 | 390 | ## Napi_IH::Error |
362 | 391 |
|
363 | 392 | Class `Napi_IH::Error` is a `Napi::Error::New` function wrapper to help developer throwing error message with format form like `printf()` function in c/cpp. |
@@ -409,6 +438,36 @@ static Napi::RangeError New(Napi::Env env, const char *format, Args... args); |
409 | 438 | - `[in] format`: Pointer to a null-terminated byte string specifying how to interpret the data. |
410 | 439 | - `[in] args`: Arguments specifying data to print. If any argument after default argument promotions is not the type expected by the corresponding conversion specifier, or if there are fewer arguments than required by format, the behavior is undefined. If there are more arguments than required by format, the extraneous arguments are evaluated and ignored. |
411 | 440 |
|
| 441 | +## Helper API |
| 442 | + |
| 443 | +### NAPI_IH_API_MODULE(modname, regfunc) |
| 444 | + |
| 445 | +- `[in] modname`: A c string which represents the module name of created napi module. |
| 446 | +- `[in] regfunc`: A function which is type of `Napi::Object Init(Napi::Env env, Napi::Object exports)`. An initializer of created napi module. |
| 447 | + |
| 448 | +Register napi module with `modname`. |
| 449 | + |
| 450 | +### NAPI_IH_VERIFY_INSTANCE_OF(napi_obj, napi_type_name) |
| 451 | + |
| 452 | +- `[in] napi_obj`: A `Napi::Object` object. |
| 453 | +- `[in] napi_type_name`: A c string or `std::string` which represent the object type specified in `DefineClass()` or js built-in type. |
| 454 | + |
| 455 | +Check if `napi_obj` is a type of `napi_type_name`, if yes, return true; otherwise return false. |
| 456 | + |
| 457 | +### bool VerifyExactInstanceType(object, napi_type_name) |
| 458 | + |
| 459 | +- `[in] object`: A `Napi::Object` object. |
| 460 | +- `[in] napi_type_name`: A c string or `std::string` which represent the object type specified in `DefineClass()`. |
| 461 | + |
| 462 | +Check if `object` is the exactly type of `napi_type_name`, if yes, return true; otherwise return false. |
| 463 | + |
| 464 | +### bool VerifyInstanceOf<T>(const Napi::Object &object); |
| 465 | + |
| 466 | +- `[in] T`: Class name which inherits from IHObjectWrap. |
| 467 | +- `[in] object`: A `Napi::Object` object. |
| 468 | + |
| 469 | +Check if `object` is type of `T`, if yes, return true; otherwise return false. |
| 470 | + |
412 | 471 | # Thanks |
413 | 472 |
|
414 | 473 | - Based on [node-addon-api](https://github.com/nodejs/node-addon-api/tree/main). |
|
0 commit comments