From 211995a67aeb0c168eae0fcbc42ce41ce25355ad Mon Sep 17 00:00:00 2001 From: Martin von Gagern Date: Wed, 14 Oct 2015 14:09:00 +0200 Subject: [PATCH 1/2] Automatically upgrade to nan 2 using upgrade-utils@1.0.3 This is just the automatic changes applied, and the package dep upgraded. --- package.json | 2 +- src/document.cc | 10 +++--- src/document.h | 4 +-- src/node_libxslt.cc | 88 ++++++++++++++++++++++----------------------- src/stylesheet.cc | 14 ++++---- src/stylesheet.h | 4 +-- 6 files changed, 61 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index 1034446..a7bf493 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "dependencies": { "bindings": "~1.2.1", "libxmljs-mt": "0.14.3", - "nan": "~1.2.0" + "nan": "^2.1.0" }, "devDependencies": { "async": "~0.9.0", diff --git a/src/document.cc b/src/document.cc index b9aa2cd..35c5855 100644 --- a/src/document.cc +++ b/src/document.cc @@ -5,7 +5,7 @@ using namespace v8; -Persistent Document::constructor; +Nan::Persistent Document::constructor; Document::Document(xmlDocumentPtr documentPtr) : document_obj(documentPtr) {} @@ -20,14 +20,14 @@ void Document::Init(Handle exports) { tpl->SetClassName(String::NewSymbol("Document")); tpl->InstanceTemplate()->SetInternalFieldCount(1); - constructor = Persistent::New(tpl->GetFunction()); + constructor = Nan::Persistent::New(tpl->GetFunction()); } // not called from node, private api Local Document::New(xmlDocumentPtr documentPtr) { - NanEscapableScope(); - Local wrapper = NanNew(constructor)->NewInstance(); + Nan::EscapableHandleScope scope; + Local wrapper = Nan::New(constructor).ToLocalChecked()->NewInstance(); Document* Document = new Document(documentPtr); Document->Wrap(wrapper); - return NanEscapeScope(wrapper); + return scope.Escape(wrapper); } diff --git a/src/document.h b/src/document.h index f0a49c8..356281b 100644 --- a/src/document.h +++ b/src/document.h @@ -8,7 +8,7 @@ #include #include -class Document : public node::ObjectWrap { +class Document : public Nan::ObjectWrap { public: static void Init(v8::Handle exports); static v8::Local New(xmlDocumentPtr DocumentPtr); @@ -18,7 +18,7 @@ class Document : public node::ObjectWrap { private: explicit Document(xmlDocumentPtr DocumentPtr); ~Document(); - static v8::Persistent constructor; + static Nan::Persistent constructor; }; #endif // SRC_DOCUMENT_H_ \ No newline at end of file diff --git a/src/node_libxslt.cc b/src/node_libxslt.cc index 62e49cc..0cd417c 100644 --- a/src/node_libxslt.cc +++ b/src/node_libxslt.cc @@ -16,29 +16,29 @@ using namespace v8; NAN_METHOD(StylesheetSync) { - NanScope(); + Nan::HandleScope scope; // From libxml document - libxmljs::XmlDocument* doc = node::ObjectWrap::Unwrap(args[0]->ToObject()); + libxmljs::XmlDocument* doc = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); // From string - //libxmljs::XmlDocument* doc = libxmljs::XmlDocument::FromXml(args); + //libxmljs::XmlDocument* doc = libxmljs::XmlDocument::FromXml(info); xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(doc->xml_obj); // TODO fetch actual error. if (!stylesheet) { - return NanThrowError("Could not parse XML string as XSLT stylesheet"); + return Nan::ThrowError("Could not parse XML string as XSLT stylesheet"); } Local stylesheetWrapper = Stylesheet::New(stylesheet); - NanReturnValue(stylesheetWrapper); + info.GetReturnValue().Set(stylesheetWrapper); } // for memory the segfault i previously fixed were due to xml documents being deleted // by garbage collector before their associated stylesheet. -class StylesheetWorker : public NanAsyncWorker { +class StylesheetWorker : public Nan::AsyncWorker { public: - StylesheetWorker(libxmljs::XmlDocument* doc, NanCallback *callback) - : NanAsyncWorker(callback), doc(doc) {} + StylesheetWorker(libxmljs::XmlDocument* doc, Nan::Callback *callback) + : Nan::AsyncWorker(callback), doc(doc) {} ~StylesheetWorker() {} // Executed inside the worker-thread. @@ -54,13 +54,13 @@ class StylesheetWorker : public NanAsyncWorker { // this function will be run inside the main event loop // so it is safe to use V8 again void HandleOKCallback () { - NanScope(); + Nan::HandleScope scope; if (!result) { - Local argv[] = { NanError("Failed to parse stylesheet") }; + Local argv[] = { Nan::Error("Failed to parse stylesheet") }; callback->Call(2, argv); } else { Local resultWrapper = Stylesheet::New(result); - Local argv[] = { NanNull(), resultWrapper }; + Local argv[] = { Nan::Null(), resultWrapper }; callback->Call(2, argv); } }; @@ -72,11 +72,11 @@ class StylesheetWorker : public NanAsyncWorker { }; NAN_METHOD(StylesheetAsync) { - NanScope(); - libxmljs::XmlDocument* doc = node::ObjectWrap::Unwrap(args[0]->ToObject()); - NanCallback *callback = new NanCallback(args[1].As()); - NanAsyncQueueWorker(new StylesheetWorker(doc, callback)); - NanReturnUndefined(); + Nan::HandleScope scope; + libxmljs::XmlDocument* doc = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); + Nan::Callback *callback = new Nan::Callback(info[1].As()); + Nan::AsyncQueueWorker(new StylesheetWorker(doc, callback)); + return; } // duplicate from https://github.com/bsuh/node_xslt/blob/master/node_xslt.cc @@ -93,7 +93,7 @@ char** PrepareParams(Handle array) { char** params = (char **)malloc(sizeof(char *) * (arrayLen + 1)); memset(params, 0, sizeof(char *) * (array->Length() + 1)); for (int i = 0; i < array->Length(); i++) { - Local param = array->Get(NanNew(i))->ToString(); + Local param = array->Get(Nan::New(i))->ToString(); params[i] = (char *)malloc(sizeof(char) * (param->Utf8Length() + 1)); param->WriteUtf8(params[i]); } @@ -101,19 +101,19 @@ char** PrepareParams(Handle array) { } NAN_METHOD(ApplySync) { - NanScope(); + Nan::HandleScope scope; - Stylesheet* stylesheet = node::ObjectWrap::Unwrap(args[0]->ToObject()); - libxmljs::XmlDocument* docSource = node::ObjectWrap::Unwrap(args[1]->ToObject()); - Handle paramsArray = Handle::Cast(args[2]); - libxmljs::XmlDocument* docResult = node::ObjectWrap::Unwrap(args[3]->ToObject()); + Stylesheet* stylesheet = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); + libxmljs::XmlDocument* docSource = Nan::ObjectWrap::Unwrap(info[1]->ToObject()); + Handle paramsArray = Handle::Cast(info[2]); + libxmljs::XmlDocument* docResult = Nan::ObjectWrap::Unwrap(info[3]->ToObject()); char** params = PrepareParams(paramsArray); xmlDoc* result = xsltApplyStylesheet(stylesheet->stylesheet_obj, docSource->xml_obj, (const char **)params); if (!result) { freeArray(params, paramsArray->Length()); - return NanThrowError("Failed to apply stylesheet"); + return Nan::ThrowError("Failed to apply stylesheet"); } // for some obscure reason I didn't manage to create a new libxmljs document in applySync, @@ -126,15 +126,15 @@ NAN_METHOD(ApplySync) { freeArray(params, paramsArray->Length()); - NanReturnUndefined(); + return; } // for memory the segfault i previously fixed were due to xml documents being deleted // by garbage collector before their associated stylesheet. -class ApplyWorker : public NanAsyncWorker { +class ApplyWorker : public Nan::AsyncWorker { public: - ApplyWorker(Stylesheet* stylesheet, libxmljs::XmlDocument* docSource, char** params, int paramsLength, libxmljs::XmlDocument* docResult, NanCallback *callback) - : NanAsyncWorker(callback), stylesheet(stylesheet), docSource(docSource), params(params), paramsLength(paramsLength), docResult(docResult) {} + ApplyWorker(Stylesheet* stylesheet, libxmljs::XmlDocument* docSource, char** params, int paramsLength, libxmljs::XmlDocument* docResult, Nan::Callback *callback) + : Nan::AsyncWorker(callback), stylesheet(stylesheet), docSource(docSource), params(params), paramsLength(paramsLength), docResult(docResult) {} ~ApplyWorker() {} // Executed inside the worker-thread. @@ -150,14 +150,14 @@ class ApplyWorker : public NanAsyncWorker { // this function will be run inside the main event loop // so it is safe to use V8 again void HandleOKCallback () { - NanScope(); + Nan::HandleScope scope; if (!result) { - Local argv[] = { NanError("Failed to apply stylesheet") }; + Local argv[] = { Nan::Error("Failed to apply stylesheet") }; freeArray(params, paramsLength); callback->Call(2, argv); } else { - Local argv[] = { NanNull() }; + Local argv[] = { Nan::Null() }; // for some obscure reason I didn't manage to create a new libxmljs document in applySync, // but passing a document by reference and modifying its content works fine @@ -184,32 +184,32 @@ class ApplyWorker : public NanAsyncWorker { }; NAN_METHOD(ApplyAsync) { - NanScope(); + Nan::HandleScope scope; - Stylesheet* stylesheet = node::ObjectWrap::Unwrap(args[0]->ToObject()); - libxmljs::XmlDocument* docSource = node::ObjectWrap::Unwrap(args[1]->ToObject()); - Handle paramsArray = Handle::Cast(args[2]); - libxmljs::XmlDocument* docResult = node::ObjectWrap::Unwrap(args[3]->ToObject()); - NanCallback *callback = new NanCallback(args[4].As()); + Stylesheet* stylesheet = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); + libxmljs::XmlDocument* docSource = Nan::ObjectWrap::Unwrap(info[1]->ToObject()); + Handle paramsArray = Handle::Cast(info[2]); + libxmljs::XmlDocument* docResult = Nan::ObjectWrap::Unwrap(info[3]->ToObject()); + Nan::Callback *callback = new Nan::Callback(info[4].As()); char** params = PrepareParams(paramsArray); - NanAsyncQueueWorker(new ApplyWorker(stylesheet, docSource, params, paramsArray->Length(), docResult, callback)); - NanReturnUndefined(); + Nan::AsyncQueueWorker(new ApplyWorker(stylesheet, docSource, params, paramsArray->Length(), docResult, callback)); + return; } NAN_METHOD(RegisterEXSLT) { exsltRegisterAll(); - NanReturnUndefined(); + return; } // Compose the module by assigning the methods previously prepared void InitAll(Handle exports) { Stylesheet::Init(exports); - exports->Set(NanNew("stylesheetSync"), NanNew(StylesheetSync)->GetFunction()); - exports->Set(NanNew("stylesheetAsync"), NanNew(StylesheetAsync)->GetFunction()); - exports->Set(NanNew("applySync"), NanNew(ApplySync)->GetFunction()); - exports->Set(NanNew("applyAsync"), NanNew(ApplyAsync)->GetFunction()); - exports->Set(NanNew("registerEXSLT"), NanNew(RegisterEXSLT)->GetFunction()); + exports->Set(Nan::New("stylesheetSync").ToLocalChecked(), Nan::New(StylesheetSync)->GetFunction()); + exports->Set(Nan::New("stylesheetAsync").ToLocalChecked(), Nan::New(StylesheetAsync)->GetFunction()); + exports->Set(Nan::New("applySync").ToLocalChecked(), Nan::New(ApplySync)->GetFunction()); + exports->Set(Nan::New("applyAsync").ToLocalChecked(), Nan::New(ApplyAsync)->GetFunction()); + exports->Set(Nan::New("registerEXSLT").ToLocalChecked(), Nan::New(RegisterEXSLT)->GetFunction()); } NODE_MODULE(node_libxslt, InitAll); diff --git a/src/stylesheet.cc b/src/stylesheet.cc index 35b003f..4d455e7 100644 --- a/src/stylesheet.cc +++ b/src/stylesheet.cc @@ -5,7 +5,7 @@ using namespace v8; -Persistent Stylesheet::constructor; +Nan::Persistent Stylesheet::constructor; Stylesheet::Stylesheet(xsltStylesheetPtr stylesheetPtr) : stylesheet_obj(stylesheetPtr) {} @@ -19,18 +19,18 @@ Stylesheet::~Stylesheet() void Stylesheet::Init(Handle exports) { // Prepare constructor template - Local tpl = NanNew(); - tpl->SetClassName(NanNew("Stylesheet")); + Local tpl = Nan::New(); + tpl->SetClassName(Nan::New("Stylesheet").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NanAssignPersistent(constructor, tpl->GetFunction()); + constructor.Reset(tpl->GetFunction()); } // not called from node, private api Local Stylesheet::New(xsltStylesheetPtr stylesheetPtr) { - NanEscapableScope(); - Local wrapper = NanNew(constructor)->NewInstance(); + Nan::EscapableHandleScope scope; + Local wrapper = Nan::New(constructor).ToLocalChecked()->NewInstance(); Stylesheet* stylesheet = new Stylesheet(stylesheetPtr); stylesheet->Wrap(wrapper); - return NanEscapeScope(wrapper); + return scope.Escape(wrapper); } diff --git a/src/stylesheet.h b/src/stylesheet.h index b268222..a85d286 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -8,7 +8,7 @@ #include #include -class Stylesheet : public node::ObjectWrap { +class Stylesheet : public Nan::ObjectWrap { public: static void Init(v8::Handle exports); static v8::Local New(xsltStylesheetPtr stylesheetPtr); @@ -18,7 +18,7 @@ class Stylesheet : public node::ObjectWrap { private: explicit Stylesheet(xsltStylesheetPtr stylesheetPtr); ~Stylesheet(); - static v8::Persistent constructor; + static Nan::Persistent constructor; }; #endif // SRC_STYLESHEET_H_ \ No newline at end of file From 64a30d9eeea6b903ce27e60d2918f11ef231faf5 Mon Sep 17 00:00:00 2001 From: Martin von Gagern Date: Wed, 14 Oct 2015 14:57:52 +0200 Subject: [PATCH 2/2] Make code compile The Nan::New overload used to turn a Nan::Persistent into a v8::Local doesn't return a MaybeLocal, so the call to ToLocalChecked is not required. --- package.json | 2 +- src/stylesheet.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a7bf493..f582564 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "license": "MIT", "dependencies": { "bindings": "~1.2.1", - "libxmljs-mt": "0.14.3", + "libxmljs-mt": "0.14.5", "nan": "^2.1.0" }, "devDependencies": { diff --git a/src/stylesheet.cc b/src/stylesheet.cc index 4d455e7..584bc76 100644 --- a/src/stylesheet.cc +++ b/src/stylesheet.cc @@ -29,7 +29,7 @@ void Stylesheet::Init(Handle exports) { // not called from node, private api Local Stylesheet::New(xsltStylesheetPtr stylesheetPtr) { Nan::EscapableHandleScope scope; - Local wrapper = Nan::New(constructor).ToLocalChecked()->NewInstance(); + Local wrapper = Nan::New(constructor)->NewInstance(); Stylesheet* stylesheet = new Stylesheet(stylesheetPtr); stylesheet->Wrap(wrapper); return scope.Escape(wrapper);