Skip to content

Commit dee311e

Browse files
committed
feat: update napi ih api doc
1 parent c7fc69c commit dee311e

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

docs/develop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ The node env of this project is based on [v14.17.0](https://github.com/toyobayas
3232

3333
### NAPI_IH
3434

35-
The goal of the project architecture is to make the structure of the napi layer consistent with the cpp layer. But NAPI do not support inheritance, so we make a pitch to NAPI, that is [NAPI_IH](./napi_ih_api.md).
35+
The goal of the project architecture is to make the structure of the napi layer consistent with the cpp layer. But NAPI do not support inheritance, so we make a patch to NAPI, that is [NAPI_IH](./napi_ih_api.md).

docs/napi_ih_api.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# Warning
66

77
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!
99
If you want to use napi with inheritance more safe and realiable, please try https://github.com/ajihyf/node-addon-api-helper.
1010

1111
# Usage
1212

13-
`napi_ih` only supports be used in inheritance.
13+
`napi_ih` only be used in inheritance.
1414

1515
## Inheritance
1616

@@ -78,9 +78,12 @@ enum class ClassVisibility{
7878

7979
`Napi_IH::IHCallbackInfo` is a wrapper of `Napi::CallbackInfo`.
8080

81-
### IHCallbackInfo(const Napi::CallbackInfo &info);
81+
### IHCallbackInfo(info, user_data);
8282

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`.
8487

8588
### Napi::Env Env() const;
8689

@@ -243,6 +246,34 @@ The `method` must be of the form
243246
void MethodName(const Napi::CallbackInfo& info);
244247
```
245248
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+
246277
### InstanceAccessor\<T, getter, setter = nullptr\>(..., vector<PropertyDescriptor>, ...)
247278
248279
```cpp
@@ -304,7 +335,7 @@ Return the cpp object pointer of the cpp object wrapped by js object. If T is no
304335

305336
## Napi_IH::FunctionWrapper
306337

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.
308339

309340
### Constructor
310341

@@ -356,8 +387,6 @@ private:
356387
};
357388
```
358389
359-
WARN: this api MUST be used in SYNC!
360-
361390
## Napi_IH::Error
362391
363392
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);
409438
- `[in] format`: Pointer to a null-terminated byte string specifying how to interpret the data.
410439
- `[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.
411440

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+
412471
# Thanks
413472

414473
- Based on [node-addon-api](https://github.com/nodejs/node-addon-api/tree/main).

0 commit comments

Comments
 (0)