Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to update Node.js addons to work with nan 2.0.x and thus with iojs v3.x (gets you 90% there)
#!/bin/bash
replacements=(
"NanAsyncWorker/Nan::AsyncWorker"
"NanAsyncQueueWorker/Nan::AsyncQueueWorker"
"NanCallback/Nan::Callback"
"NanSetInternalFieldPointer/Nan::SetInternalFieldPointer"
"NanGetInternalFieldPointer/Nan::GetInternalFieldPointer"
"NanNewBufferHandle\\(([^;]+);/Nan::NewBuffer(\\1.ToLocalChecked();"
"(NanNew(<(v8::)?String>)?\\(\"[^\"]*\"\\))/\\1.ToLocalChecked()"
"(NanNew<(v8::)?String>\\([^\"][^\;]*);/\\1.ToLocalChecked();"
"NanNew/Nan::New"
"NODE_SET_PROTOTYPE_METHOD/Nan::SetPrototypeMethod"
"NODE_SET_METHOD/Nan::SetMethod"
"_NAN_METHOD_ARGS_TYPE/Nan::NAN_METHOD_ARGS_TYPE"
"(\\W)?args(\\W|\\.|\\[)/\\1info\\2"
"(^|\\s)(v8::)?Persistent/\\1Nan::Persistent"
"NanAssignPersistent(<\w+>)?\\(([^,]+),\\s*([^)]+)\\)/\\2.Reset(\\3)"
"NanDisposePersistent\\(([^\\)]+)\\)/\\1.Reset()"
"NanReturnValue/info.GetReturnValue().Set"
"NanReturnNull\\(\\)/info.GetReturnValue().Set(Nan::Null())"
"NanScope\\(\\)/Nan::HandleScope\ scope"
"NanEscapableScope\\(\\)/Nan::EscapableHandleScope scope"
"NanEscapeScope/scope.Escape"
"NanReturnUndefined\\(\\);/return;"
"NanUtf8String/Nan::Utf8String"
"NanObjectWrapHandle\\(([^\\)]+)\\)/\\1->handle()"
"(node::)?ObjectWrap/Nan::ObjectWrap"
"NanMakeCallback/Nan::MakeCallback"
"NanNull/Nan::Null"
"NanUndefined/Nan::Undefined"
"NanFalse/Nan::False"
"NanTrue/Nan::True"
"NanThrow(\w+)?Error/Nan::Throw\\1Error"
"NanThrowTypeError/Nan::ThrowTypeError"
"NanError/Nan::Error"
"NanGetCurrentContext/Nan::GetCurrentContext"
"([a-zA-Z0-9_]+)->SetAccessor\\(/Nan::SetAccessor(\\1, "
"NanAdjustExternalMemory/Nan::AdjustExternalMemory"
"NanSetTemplate/Nan::SetTemplate"
"NanHasInstance\\(([^,]+),\\s*([^)]+)\\)/Nan::New(\\1)->HasInstance(\\2)"
)
os=`uname`
if [ $os == 'Darwin' ];
then sed_flag='-E'
else sed_flag='-r'
fi
for file in "$@"; do
echo $file
for replacement in "${replacements[@]}"; do
cat $file | sed $sed_flag "s/${replacement}/g" > $file.$$ && mv $file.$$ $file
done
done
@bahamas10

This comment has been minimized.

Show comment
Hide comment
@bahamas10

bahamas10 Aug 20, 2015

files=$@

makes $files a string, not an array, so it'll break on files with spaces, tabs, or newlines in them... get rid of files=$@ and do

for file in "$@"; do
...
done

bahamas10 commented Aug 20, 2015

files=$@

makes $files a string, not an array, so it'll break on files with spaces, tabs, or newlines in them... get rid of files=$@ and do

for file in "$@"; do
...
done
@thlorenz

This comment has been minimized.

Show comment
Hide comment
@thlorenz

thlorenz Aug 20, 2015

Thanks, fixed.

Owner

thlorenz commented Aug 20, 2015

Thanks, fixed.

@julianduque

This comment has been minimized.

Show comment
Hide comment
@julianduque

julianduque Aug 28, 2015

@thlorenz Add the following replacement

 "NanAsciiString/Nan::Utf8String"

julianduque commented Aug 28, 2015

@thlorenz Add the following replacement

 "NanAsciiString/Nan::Utf8String"
@narqo

This comment has been minimized.

Show comment
Hide comment
@narqo

narqo Sep 23, 2015

@thlorenz look like there are several small bug here:

-       tpl->SetClassName(NanNew<v8::String>(className));
+       tpl->SetClassName(Nan::New<v8::String>(className)).ToLocalChecked();

As far as I understand the code above should become:

tpl->SetClassName(Nan::New<v8::String>(className).ToLocalChecked());  // `ToLocalChecked()` results should be passed to `SetClassName()`

The same goes to this part:

-       target->Set(NanNew<v8::String>(className), tpl->GetFunction());
+       target->Set(Nan::New<v8::String>(className), tpl->GetFunction()).ToLocalChecked();

It should become

target->Set(Nan::New<v8::String>(className).ToLocalChecked(), tpl->GetFunction());

The code:

NanAssignPersistent<v8::Function>(constructor, tpl->GetFunction());

stays untouched and I believe it's because of this \w+ rule in NanAssignPersistent regexp.

narqo commented Sep 23, 2015

@thlorenz look like there are several small bug here:

-       tpl->SetClassName(NanNew<v8::String>(className));
+       tpl->SetClassName(Nan::New<v8::String>(className)).ToLocalChecked();

As far as I understand the code above should become:

tpl->SetClassName(Nan::New<v8::String>(className).ToLocalChecked());  // `ToLocalChecked()` results should be passed to `SetClassName()`

The same goes to this part:

-       target->Set(NanNew<v8::String>(className), tpl->GetFunction());
+       target->Set(Nan::New<v8::String>(className), tpl->GetFunction()).ToLocalChecked();

It should become

target->Set(Nan::New<v8::String>(className).ToLocalChecked(), tpl->GetFunction());

The code:

NanAssignPersistent<v8::Function>(constructor, tpl->GetFunction());

stays untouched and I believe it's because of this \w+ rule in NanAssignPersistent regexp.

@joelpurra

This comment has been minimized.

Show comment
Hide comment
@joelpurra

joelpurra Jul 24, 2016

@thlorenz: came here from nodejs/nan#376 -- wonderful, thanks! It did get me 90% there, and with some small patches implemented in my fork of this script I got 99% of the way. Fixed the .ToLocalChecked() bug @narqo mentioned (at least in my test cases); also replacing "Handle<...> with Local<...>" was a great help. Feel free to pull in the changes =)

Note: the script was used to upgrade getdns for node from running NAN v1.8.0 on Node v0.12, to NAN v2.4.0 on Node v6.3.1. Apart from compiling, getdns-node has 27 tests at the time of writing; all pass after the upgrade and a few minimal patches. See getdnsapi/getdns-node#6 and getdns-developer-support.

joelpurra commented Jul 24, 2016

@thlorenz: came here from nodejs/nan#376 -- wonderful, thanks! It did get me 90% there, and with some small patches implemented in my fork of this script I got 99% of the way. Fixed the .ToLocalChecked() bug @narqo mentioned (at least in my test cases); also replacing "Handle<...> with Local<...>" was a great help. Feel free to pull in the changes =)

Note: the script was used to upgrade getdns for node from running NAN v1.8.0 on Node v0.12, to NAN v2.4.0 on Node v6.3.1. Apart from compiling, getdns-node has 27 tests at the time of writing; all pass after the upgrade and a few minimal patches. See getdnsapi/getdns-node#6 and getdns-developer-support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment