Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Last active October 13, 2016 08:04
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thlorenz/7e9d8ad15566c99fd116 to your computer and use it in GitHub Desktop.
Save thlorenz/7e9d8ad15566c99fd116 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

Thanks, fixed.

@julianduque
Copy link

@thlorenz Add the following replacement

 "NanAsciiString/Nan::Utf8String"

@narqo
Copy link

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
Copy link

@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