Last active
October 13, 2016 08:04
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
@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
@thlorenz look like there are several small bug here:
As far as I understand the code above should become:
The same goes to this part:
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 inNanAssignPersistent
regexp.