Skip to content

Commit 27e9952

Browse files
Sampson Gaomhdawson
authored andcommitted
Improve the conversion script
Cover some more cases for Nan::New and FunctionTemplate Cover case where class name has digit characters Add conversion for info.GetReturnValue().SetUndefined() and value->XXXValue() fix missing escape for brackets fix missing quote for string fix Nan::Get/Set/Has regexp for nested call add option v8:: prefix to types PR-URL: #77 Reviewed-By: Michael Dawson <[email protected]>
1 parent 28fad43 commit 27e9952

File tree

1 file changed

+159
-123
lines changed

1 file changed

+159
-123
lines changed

tools/conversion.js

Lines changed: 159 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -10,182 +10,217 @@ if (!dir) {
1010
const NodeApiVersion = require('../package.json').version;
1111

1212
var ConfigFileOperations = {
13-
// ex. String::Utf8Value str(info[0]) to Napi::String str(env, info[0])
1413
'package.json': [
15-
[/"nan": *"[^"]+"/g, '"node-api": "' + NodeApiVersion + '"'],
14+
[ /"nan": *"[^"]+"/g, '"node-addon-api": "' + NodeApiVersion + '"' ]
1615
],
17-
// TODO: Add 'cflags!': [ '-fno-exceptions' ],
18-
// 'cflags_cc!': [ '-fno-exceptions' ]
1916
'binding.gyp': [
20-
[/node -e \\"require\('nan'\)\\"/g, 'node -p \\"require(\'node-api\').include\\"'],
17+
[ /\(node -e \\("|')require\(("|')nan("|')\)\\("|')\)/g, '@(node -p \\$1require(\$2node-addon-api\$3).include\\$4)' ],
18+
[ /("|')target_name("|'): ("|')(.+?)("|'),/g, '$1target_name$2: $3$4$5,\n $1cflags!$1: [ $1-fno-exceptions$1 ],\n $1cflags_cc!$1: [ $1-fno-exceptions$1 ],' ],
2119
]
2220
};
2321

2422
var SourceFileOperations = [
25-
[/Local<FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::New\("(\w+)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$2", {'],
26-
[/v8::Local<v8:FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<v8::FunctionTemplate>\([\w:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::New\("(\w+)"\)\);/gm, 'Napi::Function $1 = DefineClass(env, "$2", {'],
27-
[/Nan::SetPrototypeMethod\(\w+, "(\w+)", (\w+)\);/g, ' InstanceMethod("$1", &$2),'],
28-
[/(?:\w+\.Reset\(\w+\);\s+)?\(target\)\.Set\("(\w+)",\s*Nan::GetFunction\((\w+)\)\);/gm,
23+
[ /v8::Local<v8::FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::String::New\("(\w+)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$2", {' ],
24+
[ /Local<FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);\s+(\w+)\.Reset\((\1)\);\s+\1->SetClassName\((Nan::String::New|Nan::New<(v8::)*String>)\("(.+?)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$6", {'],
25+
[ /Local<FunctionTemplate>\s+(\w+)\s*=\s*Nan::New<FunctionTemplate>\([\w\d:]+\);(?:\w+->Reset\(\1\))?\s+\1->SetClassName\(Nan::String::New\("(\w+)"\)\);/g, 'Napi::Function $1 = DefineClass(env, "$2", {' ],
26+
[ /Nan::New<v8::FunctionTemplate>\(([\w\d:]+)\)->GetFunction\(\)/g, 'Napi::Function::New(env, $1)' ],
27+
[ /Nan::New<FunctionTemplate>\(([\w\d:]+)\)->GetFunction()/g, 'Napi::Function::New(env, $1);' ],
28+
[ /Nan::New<v8::FunctionTemplate>\(([\w\d:]+)\)/g, 'Napi::Function::New(env, $1)' ],
29+
[ /Nan::New<FunctionTemplate>\(([\w\d:]+)\)/g, 'Napi::Function::New(env, $1)' ],
30+
31+
// FunctionTemplate to FunctionReference
32+
[ /Nan::Persistent<(v8::)*FunctionTemplate>/g, 'Napi::FunctionReference' ],
33+
[ /Nan::Persistent<(v8::)*Function>/g, 'Napi::FunctionReference' ],
34+
[ /v8::Local<v8::FunctionTemplate>/g, 'Napi::FunctionReference' ],
35+
[ /Local<FunctionTemplate>/g, 'Napi::FunctionReference' ],
36+
[ /v8::FunctionTemplate/g, 'Napi::FunctionReference' ],
37+
[ /FunctionTemplate/g, 'Napi::FunctionReference' ],
38+
39+
40+
[ /Nan::SetPrototypeMethod\(\w+, "(\w+)", (\w+)\);/g, ' InstanceMethod("$1", &$2),' ],
41+
[ /(?:\w+\.Reset\(\w+\);\s+)?\(target\)\.Set\("(\w+)",\s*Nan::GetFunction\((\w+)\)\);/gm,
2942
'});\n\n' +
3043
' constructor = Napi::Persistent($2);\n' +
3144
' constructor.SuppressDestruct();\n' +
32-
' target.Set("$1", $2);'],
33-
[/constructor_template/g, 'constructor'],
45+
' target.Set("$1", $2);' ],
3446

35-
[/([\w:]+?)::Cast\((.+?)\)/g, '$2.As<$1>()'],
3647

37-
[/\*Nan::Utf8String\(([^)]+)\)/g, '$1->As<Napi::String>().Utf8Value().c_str()'],
38-
[/Nan::Utf8String +(\w+)\(([^)]+)\)/g, 'std::string $1 = $2.As<Napi::String>()'],
39-
[/Nan::Utf8String/g, 'std::string'],
40-
41-
[/v8::String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)'],
42-
[/String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)'],
43-
[/\.length\(\)/g, '.Length()'],
48+
// TODO: Other attribute combinations
49+
[ /static_cast<PropertyAttribute>\(ReadOnly\s*\|\s*DontDelete\)/gm,
50+
'static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)' ],
4451

45-
[/Nan::MakeCallback\(([^,]+),[\s\\]+([^,]+),/gm, '$2.MakeCallback($1,'],
52+
[ /([\w\d:<>]+?)::Cast\((.+?)\)/g, '$2.As<$1>()' ],
4653

47-
[/class\s+(\w+)\s*:\s*public\s+Nan::ObjectWrap/g, 'class $1 : public Napi::ObjectWrap<$1>'],
48-
[/(\w+)\(([^\)]*)\)\s*:\s*Nan::ObjectWrap\(\)\s*(,)?/gm, '$1($2) : Napi::ObjectWrap<$1>()$3'],
54+
[ /\*Nan::Utf8String\(([^)]+)\)/g, '$1->As<Napi::String>().Utf8Value().c_str()' ],
55+
[ /Nan::Utf8String +(\w+)\(([^)]+)\)/g, 'std::string $1 = $2.As<Napi::String>()' ],
56+
[ /Nan::Utf8String/g, 'std::string' ],
4957

50-
// HandleOKCallback to OnOK
51-
[/HandleOKCallback/g, 'OnOK'],
52-
// HandleErrorCallback to OnError
53-
[/HandleErrorCallback/g, 'OnError'],
58+
[ /v8::String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)' ],
59+
[ /String::Utf8Value (.+?)\((.+?)\)/g, 'Napi::String $1(env, $2)' ],
60+
[ /\.length\(\)/g, '.Length()' ],
5461

62+
[ /Nan::MakeCallback\(([^,]+),[\s\\]+([^,]+),/gm, '$2.MakeCallback($1,' ],
5563

56-
[/Nan::Callback/g, 'Napi::Function'],
57-
[/Nan::Persistent<(v8::)*FunctionTemplate>/g, 'Napi::FunctionReference'],
58-
[/Nan::Persistent<(v8::)*Function>/g, 'Napi::FunctionReference'],
59-
[/Nan::Persistent<(v8::)*Object>/g, 'Napi::ObjectReference'],
60-
[/(v8::)*Persistent<(v8::)*FunctionTemplate>/g, 'Napi::FunctionReference'],
61-
[/(v8::)*Persistent<(v8::)*Function>/g, 'Napi::FunctionReference'],
62-
[/(v8::)*Persistent<(v8::)*Object>/g, 'Napi::FunctionReference'],
63-
[/Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target/g, 'Napi::Env& env, Napi::Object& target'],
64-
[/v8::FunctionTemplate/g, 'Napi::FunctionReference'],
65-
[/FunctionTemplate/g, 'Napi::FunctionReference'],
64+
[ /class\s+(\w+)\s*:\s*public\s+Nan::ObjectWrap/g, 'class $1 : public Napi::ObjectWrap<$1>' ],
65+
[ /(\w+)\(([^\)]*)\)\s*:\s*Nan::ObjectWrap\(\)\s*(,)?/gm, '$1($2) : Napi::ObjectWrap<$1>()$3' ],
6666

67+
// HandleOKCallback to OnOK
68+
[ /HandleOKCallback/g, 'OnOK' ],
69+
// HandleErrorCallback to OnError
70+
[ /HandleErrorCallback/g, 'OnError' ],
6771

68-
// ex. Local<Value> to Napi::Value
69-
[/v8::Local<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1'],
70-
[/Local<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1'],
71-
[/v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)/g, 'Napi::$1'],
7272
// ex. .As<Function>() to .As<Napi::Object>()
73-
[/\.As<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()'],
74-
[/\.As<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()'],
73+
[ /\.As<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()' ],
74+
[ /\.As<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>\(\)/g, '.As<Napi::$1>()' ],
7575

7676
// ex. Nan::New<Number>(info[0]) to Napi::Number::New(info[0])
77-
[/Nan::New<Integer>\((.+?)\)/g, 'Napi::Number::New(env, $1)'],
78-
[/Nan::New\(([0-9\.]+)\)/g, 'Napi::Number::New(env, $1)'],
79-
[/Nan::New\("(.+?)"\)/g, 'Napi::String::New(env, "$1")'],
80-
[/Nan::New<(.+?)>\(\)/g, 'Napi::$1::New(env)'],
81-
[/Nan::New<(.+?)>\(/g, 'Napi::$1::New(env, '],
82-
[/Nan::NewBuffer\(/g, 'Napi::Buffer<char>::New(env, '],
77+
[ /Nan::New<(v8::)*Integer>\((.+?)\)/g, 'Napi::Number::New(env, $2)' ],
78+
[ /Nan::New\(([0-9\.]+)\)/g, 'Napi::Number::New(env, $1)' ],
79+
[ /Nan::New<(v8::)*String>\("(.+?)"\)/g, 'Napi::String::New(env, "$2")' ],
80+
[ /Nan::New\("(.+?)"\)/g, 'Napi::String::New(env, "$1")' ],
81+
[ /Nan::New<(v8::)*(.+?)>\(\)/g, 'Napi::$2::New(env)' ],
82+
[ /Nan::New<(.+?)>\(\)/g, 'Napi::$1::New(env)' ],
83+
[ /Nan::New<(v8::)*(.+?)>\(/g, 'Napi::$2::New(env, ' ],
84+
[ /Nan::New<(.+?)>\(/g, 'Napi::$1::New(env, ' ],
85+
[ /Nan::NewBuffer\(/g, 'Napi::Buffer<char>::New(env, ' ],
8386
// TODO: Properly handle this
84-
[/Nan::New\(/g, 'Napi::New(env, '],
85-
86-
[/.IsInt32\(\)/g, '.IsNumber()'],
87-
87+
[ /Nan::New\(/g, 'Napi::New(env, ' ],
8888

89-
// ex. Nan::To<bool>(info[0]) to info[0].Value()
90-
[/Nan::To<(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()'],
91-
[/Nan::To<v8::(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()'],
92-
// ex. Nan::To<bool>(info[0]) to info[0].Value()
93-
[/Nan::To<bool>\((.+?)\)/g, '$1.As<Napi::Boolean>().Value()'],
94-
// ex. Nan::To<int>(info[0]) to info[0].Int32Value()
95-
[/Nan::To<int>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()'],
96-
// ex. Nan::To<int32_t>(info[0]) to info[0].Int32Value()
97-
[/Nan::To<int32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()'],
98-
// ex. Nan::To<uint32_t>(info[0]) to info[0].Uint32Value()
99-
[/Nan::To<uint32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Uint32Value()'],
100-
// ex. Nan::To<int64_t>(info[0]) to info[0].Int64Value()
101-
[/Nan::To<int64_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int64Value()'],
102-
// ex. Nan::To<float>(info[0]) to info[0].FloatValue()
103-
[/Nan::To<float>\((.+?)\)/g, '$1.As<Napi::Number>().FloatValue()'],
104-
// ex. Nan::To<double>(info[0]) to info[0].DoubleValue()
105-
[/Nan::To<double>\((.+?)\)/g, '$1.As<Napi::Number>().DoubleValue()'],
89+
[ /\.IsInt32\(\)/g, '.IsNumber()' ],
90+
[ /->IsInt32\(\)/g, '.IsNumber()' ],
10691

107-
[/Nan::New\((\w+)\)->HasInstance\((\w+)\)/g, '$2.InstanceOf($1.Value())'],
10892

109-
[/Nan::Get\(([^)]+),\s*/gm, '($1).Get('],
110-
[/\.Get\([\s|\\]*Nan::New\(([^)]+)\)\)/gm, '.Get($1)'],
93+
[ /(.+?)->BooleanValue\(\)/g, '$1.As<Napi::Boolean>().BooleanValue()' ],
94+
[ /(.+?)->Int32Value\(\)/g, '$1.As<Napi::Number>().Int32Value()' ],
95+
[ /(.+?)->Uint32Value\(\)/g, '$1.As<Napi::Number>().Uint32Value()' ],
96+
[ /(.+?)->IntegerValue\(\)/g, '$1.As<Napi::Number>().Int64Value()' ],
97+
[ /(.+?)->NumberValue\(\)/g, '$1.As<Napi::Number>().DoubleValue()' ],
11198

112-
[/Nan::Set\(([^,]+),\s*/gm, '($1).Set('],
113-
[/\.Set\([\s|\\]*Nan::New\(([^)]+)\),/gm, '.Set($1,'],
99+
// ex. Nan::To<bool>(info[0]) to info[0].Value()
100+
[ /Nan::To<v8::(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()' ],
101+
[ /Nan::To<(Boolean|String|Number|Object|Array|Symbol|Function)>\((.+?)\)/g, '$2.To<Napi::$1>()' ],
102+
// ex. Nan::To<bool>(info[0]) to info[0].As<Napi::Boolean>().Value()
103+
[ /Nan::To<bool>\((.+?)\)/g, '$1.As<Napi::Boolean>().Value()' ],
104+
// ex. Nan::To<int>(info[0]) to info[0].As<Napi::Number>().Int32Value()
105+
[ /Nan::To<int>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()' ],
106+
// ex. Nan::To<int32_t>(info[0]) to info[0].As<Napi::Number>().Int32Value()
107+
[ /Nan::To<int32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int32Value()' ],
108+
// ex. Nan::To<uint32_t>(info[0]) to info[0].As<Napi::Number>().Uint32Value()
109+
[ /Nan::To<uint32_t>\((.+?)\)/g, '$1.As<Napi::Number>().Uint32Value()' ],
110+
// ex. Nan::To<int64_t>(info[0]) to info[0].As<Napi::Number>().Int64Value()
111+
[ /Nan::To<int64_t>\((.+?)\)/g, '$1.As<Napi::Number>().Int64Value()' ],
112+
// ex. Nan::To<float>(info[0]) to info[0].As<Napi::Number>().FloatValue()
113+
[ /Nan::To<float>\((.+?)\)/g, '$1.As<Napi::Number>().FloatValue()' ],
114+
// ex. Nan::To<double>(info[0]) to info[0].As<Napi::Number>().DoubleValue()
115+
[ /Nan::To<double>\((.+?)\)/g, '$1.As<Napi::Number>().DoubleValue()' ],
116+
117+
[ /Nan::New\((\w+)\)->HasInstance\((\w+)\)/g, '$2.InstanceOf($1.Value())' ],
118+
119+
[ /Nan::Has\(([^,]+),\s*/gm, '($1).Has(' ],
120+
[ /\.Has\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\)/gm, '.Has($1)' ],
121+
[ /\.Has\([\s|\\]*Nan::New\(([^)]+)\)\)/gm, '.Has($1)' ],
122+
123+
[ /Nan::Get\(([^,]+),\s*/gm, '($1).Get(' ],
124+
[ /\.Get\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\)/gm, '.Get($1)' ],
125+
[ /\.Get\([\s|\\]*Nan::New\(([^)]+)\)\)/gm, '.Get($1)' ],
126+
127+
[ /Nan::Set\(([^,]+),\s*/gm, '($1).Set(' ],
128+
[ /\.Set\([\s|\\]*Nan::New<(v8::)*String>\(([^)]+)\)\s*,/gm, '.Set($1,' ],
129+
[ /\.Set\([\s|\\]*Nan::New\(([^)]+)\)\s*,/gm, '.Set($1,' ],
114130

115131

116132
// ex. node::Buffer::HasInstance(info[0]) to info[0].IsBuffer()
117-
[/node::Buffer::HasInstance\((.+?)\)/g, '$1.IsBuffer()'],
133+
[ /node::Buffer::HasInstance\((.+?)\)/g, '$1.IsBuffer()' ],
118134
// ex. node::Buffer::Length(info[0]) to info[0].Length()
119-
[/node::Buffer::Length\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Length()'],
135+
[ /node::Buffer::Length\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Length()' ],
120136
// ex. node::Buffer::Data(info[0]) to info[0].Data()
121-
[/node::Buffer::Data\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Data()'],
122-
[/Nan::CopyBuffer\(/g, 'Napi::Buffer::Copy(env, '],
137+
[ /node::Buffer::Data\((.+?)\)/g, '$1.As<Napi::Buffer<char>>().Data()' ],
138+
[ /Nan::CopyBuffer\(/g, 'Napi::Buffer::Copy(env, ' ],
123139

124140
// Nan::AsyncQueueWorker(worker)
125-
[/Nan::AsyncQueueWorker\((.+)\);/g, '$1.Queue();'],
126-
[/Nan::(Undefined|Null|True|False)\(\)/g, 'env.$1()'],
141+
[ /Nan::AsyncQueueWorker\((.+)\);/g, '$1.Queue();' ],
142+
[ /Nan::(Undefined|Null|True|False)\(\)/g, 'env.$1()' ],
127143

128144
// Nan::ThrowError(error) to Napi::Error::New(env, error).ThrowAsJavaScriptException()
129-
[/Nan::Throw(\w*?)Error\((.+?)\);/g, 'Napi::$1Error::New(env, $2).ThrowAsJavaScriptException();'],
145+
[ /return Nan::Throw(\w*?)Error\((.+?)\);/g, 'Napi::$1Error::New(env, $2).ThrowAsJavaScriptException();\n return env.Null();' ],
146+
[ /Nan::Throw(\w*?)Error\((.+?)\);/g, 'Napi::$1Error::New(env, $2).ThrowAsJavaScriptException();\n return env.Null();' ],
130147
// Nan::RangeError(error) to Napi::RangeError::New(env, error)
131-
[/Nan::(\w*?)Error\((.+)\)/g, 'Napi::$1Error::New(env, $2)'],
148+
[ /Nan::(\w*?)Error\((.+)\)/g, 'Napi::$1Error::New(env, $2)' ],
132149

133-
[/Nan::Set\((.+?),\n* *(.+?),\n* *(.+?),\n* *(.+?)\)/g, '$1.Set($2, $3, $4)'],
150+
[ /Nan::Set\((.+?),\n* *(.+?),\n* *(.+?),\n* *(.+?)\)/g, '$1.Set($2, $3, $4)' ],
134151

135-
[/info\[(\w+)\]->/g, 'info[$1].'],
136-
[/info\.This\(\)->/g, 'info.This().'],
137-
[/->Is(Object|String|Int32|Number)\(\)/g, '.Is$1()'],
138-
[/info\.GetReturnValue\(\)\.Set\(((\n|.)+?)\);/g, 'return $1;'],
152+
[ /Nan::(Escapable)?HandleScope\s+(\w+)\s*;/g, 'Napi::$1HandleScope $2(env);' ],
153+
[ /Nan::(Escapable)?HandleScope/g, 'Napi::$1HandleScope' ],
154+
[ /Nan::ForceSet\(([^,]+), ?/g, '$1->DefineProperty(' ],
155+
[ /\.ForceSet\(Napi::String::New\(env, "(\w+)"\),\s*?/g, '.DefineProperty("$1", ' ],
156+
// [ /Nan::GetPropertyNames\(([^,]+)\)/, '$1->GetPropertyNames()' ],
157+
[ /Nan::Equals\(([^,]+),/g, '$1.StrictEquals(' ],
139158

140-
[/Nan::(Escapable)?HandleScope\s+(\w+)\s*;/g, 'Napi::$1HandleScope $2(env);'],
141-
[/Nan::(Escapable)?HandleScope/g, 'Napi::$1HandleScope'],
142-
[/Nan::ForceSet\(([^,]+), ?/g, '$1->DefineProperty('],
143-
[/\.ForceSet\(Napi::String::New\(env, "(\w+)"\),\s*?/g, '.DefineProperty("$1", '],
144-
// [/Nan::GetPropertyNames\(([^,]+)\)/, '$1->GetPropertyNames()'],
145-
[/Nan::Equals\(([^,]+),/g, '$1.Equals('],
146159

147160

161+
[ /Nan::Callback/g, 'Napi::FunctionReference' ],
148162

149-
[/(\w+)\*\s+(\w+)\s*=\s*Nan::ObjectWrap::Unwrap<\w+>\(info\.This\(\)\);/g, '$1* $2 = this;'],
150-
[/Nan::ObjectWrap::Unwrap<(\w+)>\((.*)\);/g, '$2.Unwrap<$1>();'],
151163

152-
[/static\s*NAN_METHOD\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info) {\n Napi::Env env = info.Env();'],
153-
[/NAN_METHOD\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info) {\n Napi::Env env = info.Env();'],
154-
[/static\s*NAN_GETTER\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info) {\n Napi::Env env = info.Env();'],
155-
[/NAN_GETTER\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info) {\n Napi::Env env = info.Env();'],
156-
[/static\s*NAN_SETTER\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info, const Napi::Value& value) {\n Napi::Env env = info.Env();'],
157-
[/NAN_SETTER\s*\((\w+?)\)\s*{/g, 'Napi::Value $1(const Napi::CallbackInfo& info, const Napi::Value& value) {\n Napi::Env env = info.Env();'],
158-
[/NAN_MODULE_INIT\s*\(([\w:]+?)\)/g, 'void $1(Napi::Env env, Napi::Object exports, Napi::Object module)'],
159-
[/Nan::NAN_METHOD_ARGS_TYPE/g, 'const Napi::CallbackInfo&'],
160-
[/(Nan::)*FunctionCallbackInfo<(.+?)*>&*/g, 'Napi::CallbackInfo&'],
161-
[/::(Init(?:ialize)?)\(target\)/g, '::$1(env, target, module)'],
164+
[ /Nan::Persistent<Object>/g, 'Napi::ObjectReference' ],
165+
[ /Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target/g, 'Napi::Env& env, Napi::Object& target' ],
162166

163-
// TODO: Other attribute combinations
164-
[/static_cast<PropertyAttribute>\(ReadOnly\s*\|\s*DontDelete\)/gm,
165-
'static_cast<napi_property_attributes>(napi_enumerable | napi_configurable)'],
167+
[ /(\w+)\*\s+(\w+)\s*=\s*Nan::ObjectWrap::Unwrap<\w+>\(info\.This\(\)\);/g, '$1* $2 = this;' ],
168+
[ /Nan::ObjectWrap::Unwrap<(\w+)>\((.*)\);/g, '$2.Unwrap<$1>();' ],
169+
170+
[ /Nan::NAN_METHOD_ARGS_TYPE/g, 'const Napi::CallbackInfo&' ],
171+
[ /NAN_METHOD\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)'],
172+
[ /static\s*NAN_GETTER\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)' ],
173+
[ /NAN_GETTER\(([\w\d:]+?)\)/g, 'Napi::Value $1(const Napi::CallbackInfo& info)' ],
174+
[ /static\s*NAN_SETTER\(([\w\d:]+?)\)/g, 'void $1(const Napi::CallbackInfo& info, const Napi::Value& value)' ],
175+
[ /NAN_SETTER\(([\w\d:]+?)\)/g, 'void $1(const Napi::CallbackInfo& info, const Napi::Value& value)' ],
176+
[ /void Init\((v8::)*Local<(v8::)*Object> exports\)/g, [
177+
[ /exports->/g, 'exports.' ],
178+
[ /void Init\((v8::)*Local<(v8::)*Object> exports\)/g, 'void Init(Napi::Env env, Napi::Object exports, Napi::Object module)' ],
179+
]],
180+
[ /NAN_MODULE_INIT\(([\w\d:]+?)\);/g, 'void $1(Napi::Env env, Napi::Object exports, Napi::Object module);' ],
181+
[ /NAN_MODULE_INIT\(([\w\d:]+?)\)/g, 'void $1(Napi::Env env, Napi::Object exports, Napi::Object module)' ],
182+
183+
[ /::(Init(?:ialize)?)\(target\)/g, '::$1(env, target, module)' ],
184+
[ /constructor_template/g, 'constructor' ],
185+
186+
[ /Nan::FunctionCallbackInfo<(v8::)*Value>\s*&\s*info\)\s*{/g, 'Napi::CallbackInfo& info) {\n Napi::Env env = info.Env();' ],
187+
188+
189+
[ /info\[(\d+)\]->/g, 'info[$1].' ],
190+
[ /info\[([\w\d]+)\]->/g, 'info[$1].' ],
191+
[ /info\.This\(\)->/g, 'info.This().' ],
192+
[ /->Is(Object|String|Int32|Number)\(\)/g, '.Is$1()' ],
193+
[ /info.GetReturnValue\(\).SetUndefined\(\)/g, 'return env.Undefined()' ],
194+
[ /info\.GetReturnValue\(\)\.Set\(((\n|.)+?)\);/g, 'return $1;' ],
195+
196+
197+
// ex. Local<Value> to Napi::Value
198+
[ /v8::Local<v8::(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1' ],
199+
[ /Local<(Value|Boolean|String|Number|Object|Array|Symbol|External|Function)>/g, 'Napi::$1' ],
166200

167201
// Declare an env in helper functions that take a Napi::Value
168-
[/(\w+)\(Napi::Value (\w+)(,\s*[^\()]+)?\)\s*{/g, '$1(Napi::Value $2$3) {\n Napi::Env env = $2.Env();'],
202+
[ /(\w+)\(Napi::Value (\w+)(,\s*[^\()]+)?\)\s*{/g, '$1(Napi::Value $2$3) {\n Napi::Env env = $2.Env();' ],
169203

170204
// delete #include <node.h> and/or <v8.h>
171-
[/#include +(<|")(?:node|nan).h("|>)/g, "#include $1napi.h$2\n#include $1uv.h$2"],
205+
[ /#include +(<|")(?:node|nan).h("|>)/g, "#include $1napi.h$2\n#include $1uv.h$2" ],
172206
// NODE_MODULE to NODE_API_MODULE
173-
[/NODE_MODULE/g, 'NODE_API_MODULE'],
174-
[/Nan::/g, 'Napi::'],
175-
[/nan.h/g, 'napi.h'],
207+
[ /NODE_MODULE/g, 'NODE_API_MODULE' ],
208+
[ /Nan::/g, 'Napi::' ],
209+
[ /nan.h/g, 'napi.h' ],
176210

177211
// delete .FromJust()
178-
[/\.FromJust\(\)/g, ''],
212+
[ /\.FromJust\(\)/g, '' ],
179213
// delete .ToLocalCheck()
180-
[/\.ToLocalChecked\(\)/g, ''],
181-
[/^.*->SetInternalFieldCount\(.*$/gm, ''],
214+
[ /\.ToLocalChecked\(\)/g, '' ],
215+
[ /^.*->SetInternalFieldCount\(.*$/gm, '' ],
182216

183217
// replace using node; and/or using v8; to using Napi;
184-
[/using (node|v8);/g, 'using Napi;'],
185-
// delete using v8::XXX;
186-
[/using v8::Local;\n/g, ''],
187-
// delete using v8::XXX;
188-
[/using v8::([A-Za-z]+);/g, 'using Napi::$1;'],
218+
[ /using (node|v8);/g, 'using Napi;' ],
219+
[ /using namespace (node|Nan|v8);/g, 'using namespace Napi;' ],
220+
// delete using v8::Local;
221+
[ /using v8::Local;\n/g, '' ],
222+
// replace using v8::XXX; with using Napi::XXX
223+
[ /using v8::([A-Za-z]+);/g, 'using Napi::$1;' ],
189224

190225
];
191226

@@ -218,9 +253,10 @@ function listFiles(dir, filelist) {
218253
}
219254

220255
function convert(content, operations) {
221-
operations.forEach(function(operation) {
256+
for (let i = 0; i < operations.length; i ++) {
257+
let operation = operations[i];
222258
content = content.replace(operation[0], operation[1]);
223-
});
259+
}
224260
return content;
225261
}
226262

0 commit comments

Comments
 (0)