1 | # Tools
|
---|
2 |
|
---|
3 | ## clang-format
|
---|
4 |
|
---|
5 | The clang-format checking tools is designed to check changed lines of code compared to given git-refs.
|
---|
6 |
|
---|
7 | ## Migration Script
|
---|
8 |
|
---|
9 | The migration tool is designed to reduce repetitive work in the migration process. However, the script is not aiming to convert every thing for you. There are usually some small fixes and major reconstruction required.
|
---|
10 |
|
---|
11 | ### How To Use
|
---|
12 |
|
---|
13 | To run the conversion script, first make sure you have the latest `node-addon-api` in your `node_modules` directory.
|
---|
14 | ```
|
---|
15 | npm install node-addon-api
|
---|
16 | ```
|
---|
17 |
|
---|
18 | Then run the script passing your project directory
|
---|
19 | ```
|
---|
20 | node ./node_modules/node-addon-api/tools/conversion.js ./
|
---|
21 | ```
|
---|
22 |
|
---|
23 | After finish, recompile and debug things that are missed by the script.
|
---|
24 |
|
---|
25 |
|
---|
26 | ### Quick Fixes
|
---|
27 | Here is the list of things that can be fixed easily.
|
---|
28 | 1. Change your methods' return value to void if it doesn't return value to JavaScript.
|
---|
29 | 2. Use `.` to access attribute or to invoke member function in Napi::Object instead of `->`.
|
---|
30 | 3. `Napi::New(env, value);` to `Napi::[Type]::New(env, value);
|
---|
31 |
|
---|
32 |
|
---|
33 | ### Major Reconstructions
|
---|
34 | The implementation of `Napi::ObjectWrap` is significantly different from NAN's. `Napi::ObjectWrap` takes a pointer to the wrapped object and creates a reference to the wrapped object inside ObjectWrap constructor. `Napi::ObjectWrap` also associates wrapped object's instance methods to Javascript module instead of static methods like NAN.
|
---|
35 |
|
---|
36 | So if you use Nan::ObjectWrap in your module, you will need to execute the following steps.
|
---|
37 |
|
---|
38 | 1. Convert your [ClassName]::New function to a constructor function that takes a `Napi::CallbackInfo`. Declare it as
|
---|
39 | ```
|
---|
40 | [ClassName](const Napi::CallbackInfo& info);
|
---|
41 | ```
|
---|
42 | and define it as
|
---|
43 | ```
|
---|
44 | [ClassName]::[ClassName](const Napi::CallbackInfo& info) : Napi::ObjectWrap<[ClassName]>(info){
|
---|
45 | ...
|
---|
46 | }
|
---|
47 | ```
|
---|
48 | This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instantiated and `Napi::ObjectWrap` can use the `this` pointer to create a reference to the wrapped object.
|
---|
49 |
|
---|
50 | 2. Move your original constructor code into the new constructor. Delete your original constructor.
|
---|
51 | 3. In your class initialization function, associate native methods in the following way.
|
---|
52 | ```
|
---|
53 | Napi::FunctionReference constructor;
|
---|
54 |
|
---|
55 | void [ClassName]::Init(Napi::Env env, Napi::Object exports, Napi::Object module) {
|
---|
56 | Napi::HandleScope scope(env);
|
---|
57 | Napi::Function ctor = DefineClass(env, "Canvas", {
|
---|
58 | InstanceMethod<&[ClassName]::Func1>("Func1"),
|
---|
59 | InstanceMethod<&[ClassName]::Func2>("Func2"),
|
---|
60 | InstanceAccessor<&[ClassName]::ValueGetter>("Value"),
|
---|
61 | StaticMethod<&[ClassName]::StaticMethod>("MethodName"),
|
---|
62 | InstanceValue("Value", Napi::[Type]::New(env, value)),
|
---|
63 | });
|
---|
64 |
|
---|
65 | constructor = Napi::Persistent(ctor);
|
---|
66 | constructor .SuppressDestruct();
|
---|
67 | exports.Set("[ClassName]", ctor);
|
---|
68 | }
|
---|
69 | ```
|
---|
70 | 4. In function where you need to Unwrap the ObjectWrap in NAN like `[ClassName]* native = Nan::ObjectWrap::Unwrap<[ClassName]>(info.This());`, use `this` pointer directly as the unwrapped object as each ObjectWrap instance is associated with a unique object instance.
|
---|
71 |
|
---|
72 |
|
---|
73 | If you still find issues after following this guide, please leave us an issue describing your problem and we will try to resolve it.
|
---|