Skip to content

Commit 7f3ca03

Browse files
Sampson Gaomhdawson
authored andcommitted
Create a doc for migration
PR-URL: #118 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 0a2177d commit 7f3ca03

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tools/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Migration Script
2+
3+
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.
4+
5+
### How To Use
6+
7+
To run the conversion script, first make sure you have the latest `node-addon-api` in your `node_modules` directory.
8+
```
9+
npm install node-addon-api
10+
```
11+
12+
Then run the script passing your project directory
13+
```
14+
node ./node_modules/node-addon-api/tools/conversion.js ./
15+
```
16+
17+
After finish, recompile and debug things that are missed by the script.
18+
19+
20+
### Quick Fixes
21+
Here is the list of things that can be fixed easily.
22+
1. Change your methods' return value to void if it doesn't return value to JavaScript.
23+
2. Use `.` to access attribute or to invoke member function in Napi::Object instead of `->`.
24+
3. `Napi::New(env, value);` to `Napi::[Type]::New(env, value);
25+
26+
27+
### Major Reconstructions
28+
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 associated wrapped object's instance methods to Javascript module instead of static methods like NAN.
29+
30+
So if you use Nan::ObjectWrap in your module, you will need to execute the following steps.
31+
32+
1. Convert your [ClassName]::New function to a constructor function that takes a `Napi::CallbackInfo`. Declare it as
33+
```
34+
[ClassName](const Napi::CallbackInfo& info);
35+
```
36+
and define it as
37+
```
38+
[ClassName]::[ClassName](const Napi::CallbackInfo& info) : Napi::ObjectWrap<[ClassName]>(info){
39+
...
40+
}
41+
```
42+
This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instanciated and `Napi::ObjectWrap` can use the `this` pointer to create reference to the wrapped object.
43+
44+
2. Move your original constructor code into the new constructor. Delete your original constructor.
45+
3. In your class initialization function, associate native methods in the following way. The `&` character before methods is required because they are not static methods but instance methods.
46+
```
47+
Napi::FunctionReference constructor;
48+
49+
void [ClassName]::Init(Napi::Env env, Napi::Object exports, Napi::Object module) {
50+
Napi::HandleScope scope(env);
51+
Napi::Function ctor = DefineClass(env, "Canvas", {
52+
InstanceMethod("Func1", &[ClassName]::Func1),
53+
InstanceMethod("Func2", &[ClassName]::Func2),
54+
InstanceAccessor("Value", &[ClassName]::ValueGetter),
55+
StaticMethod("MethodName", &[ClassName]::StaticMethod),
56+
InstanceValue("Value", Napi::[Type]::New(env, value)),
57+
});
58+
59+
constructor = Napi::Persistent(ctor);
60+
constructor .SuppressDestruct();
61+
exports.Set("[ClassName]", ctor);
62+
}
63+
```
64+
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.
65+
66+
67+
If you still find issues after following this guide, please leave us an issue describing your problem and we will try to resolve it.

0 commit comments

Comments
 (0)