Skip to content

Commit

Permalink
Merge pull request #26 from gagern/nan2
Browse files Browse the repository at this point in the history
Support nan 2
  • Loading branch information
albanm committed Oct 15, 2015
2 parents 8a22510 + 64a30d9 commit 006dcb8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"license": "MIT",
"dependencies": {
"bindings": "~1.2.1",
"libxmljs-mt": "0.14.3",
"nan": "~1.2.0"
"libxmljs-mt": "0.14.5",
"nan": "^2.1.0"
},
"devDependencies": {
"async": "~0.9.0",
Expand Down
10 changes: 5 additions & 5 deletions src/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using namespace v8;

Persistent<Function> Document::constructor;
Nan::Persistent<Function> Document::constructor;

Document::Document(xmlDocumentPtr documentPtr) : document_obj(documentPtr) {}

Expand All @@ -20,14 +20,14 @@ void Document::Init(Handle<Object> exports) {
tpl->SetClassName(String::NewSymbol("Document"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);

constructor = Persistent<Function>::New(tpl->GetFunction());
constructor = Nan::Persistent<Function>::New(tpl->GetFunction());
}

// not called from node, private api
Local<Object> Document::New(xmlDocumentPtr documentPtr) {
NanEscapableScope();
Local<Object> wrapper = NanNew(constructor)->NewInstance();
Nan::EscapableHandleScope scope;
Local<Object> wrapper = Nan::New(constructor).ToLocalChecked()->NewInstance();
Document* Document = new Document(documentPtr);
Document->Wrap(wrapper);
return NanEscapeScope(wrapper);
return scope.Escape(wrapper);
}
4 changes: 2 additions & 2 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>

class Document : public node::ObjectWrap {
class Document : public Nan::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);
static v8::Local<v8::Object> New(xmlDocumentPtr DocumentPtr);
Expand All @@ -18,7 +18,7 @@ class Document : public node::ObjectWrap {
private:
explicit Document(xmlDocumentPtr DocumentPtr);
~Document();
static v8::Persistent<v8::Function> constructor;
static Nan::Persistent<v8::Function> constructor;
};

#endif // SRC_DOCUMENT_H_
88 changes: 44 additions & 44 deletions src/node_libxslt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@
using namespace v8;

NAN_METHOD(StylesheetSync) {
NanScope();
Nan::HandleScope scope;

// From libxml document
libxmljs::XmlDocument* doc = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[0]->ToObject());
libxmljs::XmlDocument* doc = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(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<Object> 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.
Expand All @@ -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<Value> argv[] = { NanError("Failed to parse stylesheet") };
Local<Value> argv[] = { Nan::Error("Failed to parse stylesheet") };
callback->Call(2, argv);
} else {
Local<Object> resultWrapper = Stylesheet::New(result);
Local<Value> argv[] = { NanNull(), resultWrapper };
Local<Value> argv[] = { Nan::Null(), resultWrapper };
callback->Call(2, argv);
}
};
Expand All @@ -72,11 +72,11 @@ class StylesheetWorker : public NanAsyncWorker {
};

NAN_METHOD(StylesheetAsync) {
NanScope();
libxmljs::XmlDocument* doc = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[0]->ToObject());
NanCallback *callback = new NanCallback(args[1].As<Function>());
NanAsyncQueueWorker(new StylesheetWorker(doc, callback));
NanReturnUndefined();
Nan::HandleScope scope;
libxmljs::XmlDocument* doc = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(info[0]->ToObject());
Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
Nan::AsyncQueueWorker(new StylesheetWorker(doc, callback));
return;
}

// duplicate from https://github.com/bsuh/node_xslt/blob/master/node_xslt.cc
Expand All @@ -93,27 +93,27 @@ char** PrepareParams(Handle<Array> 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<String> param = array->Get(NanNew<Integer>(i))->ToString();
Local<String> param = array->Get(Nan::New<Integer>(i))->ToString();
params[i] = (char *)malloc(sizeof(char) * (param->Utf8Length() + 1));
param->WriteUtf8(params[i]);
}
return params;
}

NAN_METHOD(ApplySync) {
NanScope();
Nan::HandleScope scope;

Stylesheet* stylesheet = node::ObjectWrap::Unwrap<Stylesheet>(args[0]->ToObject());
libxmljs::XmlDocument* docSource = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[1]->ToObject());
Handle<Array> paramsArray = Handle<Array>::Cast(args[2]);
libxmljs::XmlDocument* docResult = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[3]->ToObject());
Stylesheet* stylesheet = Nan::ObjectWrap::Unwrap<Stylesheet>(info[0]->ToObject());
libxmljs::XmlDocument* docSource = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(info[1]->ToObject());
Handle<Array> paramsArray = Handle<Array>::Cast(info[2]);
libxmljs::XmlDocument* docResult = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(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,
Expand All @@ -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.
Expand All @@ -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<Value> argv[] = { NanError("Failed to apply stylesheet") };
Local<Value> argv[] = { Nan::Error("Failed to apply stylesheet") };
freeArray(params, paramsLength);
callback->Call(2, argv);
} else {
Local<Value> argv[] = { NanNull() };
Local<Value> 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
Expand All @@ -184,32 +184,32 @@ class ApplyWorker : public NanAsyncWorker {
};

NAN_METHOD(ApplyAsync) {
NanScope();
Nan::HandleScope scope;

Stylesheet* stylesheet = node::ObjectWrap::Unwrap<Stylesheet>(args[0]->ToObject());
libxmljs::XmlDocument* docSource = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[1]->ToObject());
Handle<Array> paramsArray = Handle<Array>::Cast(args[2]);
libxmljs::XmlDocument* docResult = node::ObjectWrap::Unwrap<libxmljs::XmlDocument>(args[3]->ToObject());
NanCallback *callback = new NanCallback(args[4].As<Function>());
Stylesheet* stylesheet = Nan::ObjectWrap::Unwrap<Stylesheet>(info[0]->ToObject());
libxmljs::XmlDocument* docSource = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(info[1]->ToObject());
Handle<Array> paramsArray = Handle<Array>::Cast(info[2]);
libxmljs::XmlDocument* docResult = Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(info[3]->ToObject());
Nan::Callback *callback = new Nan::Callback(info[4].As<Function>());

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<Object> exports) {
Stylesheet::Init(exports);
exports->Set(NanNew<String>("stylesheetSync"), NanNew<FunctionTemplate>(StylesheetSync)->GetFunction());
exports->Set(NanNew<String>("stylesheetAsync"), NanNew<FunctionTemplate>(StylesheetAsync)->GetFunction());
exports->Set(NanNew<String>("applySync"), NanNew<FunctionTemplate>(ApplySync)->GetFunction());
exports->Set(NanNew<String>("applyAsync"), NanNew<FunctionTemplate>(ApplyAsync)->GetFunction());
exports->Set(NanNew<String>("registerEXSLT"), NanNew<FunctionTemplate>(RegisterEXSLT)->GetFunction());
exports->Set(Nan::New<String>("stylesheetSync").ToLocalChecked(), Nan::New<FunctionTemplate>(StylesheetSync)->GetFunction());
exports->Set(Nan::New<String>("stylesheetAsync").ToLocalChecked(), Nan::New<FunctionTemplate>(StylesheetAsync)->GetFunction());
exports->Set(Nan::New<String>("applySync").ToLocalChecked(), Nan::New<FunctionTemplate>(ApplySync)->GetFunction());
exports->Set(Nan::New<String>("applyAsync").ToLocalChecked(), Nan::New<FunctionTemplate>(ApplyAsync)->GetFunction());
exports->Set(Nan::New<String>("registerEXSLT").ToLocalChecked(), Nan::New<FunctionTemplate>(RegisterEXSLT)->GetFunction());
}
NODE_MODULE(node_libxslt, InitAll);
14 changes: 7 additions & 7 deletions src/stylesheet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using namespace v8;

Persistent<Function> Stylesheet::constructor;
Nan::Persistent<Function> Stylesheet::constructor;

Stylesheet::Stylesheet(xsltStylesheetPtr stylesheetPtr) : stylesheet_obj(stylesheetPtr) {}

Expand All @@ -19,18 +19,18 @@ Stylesheet::~Stylesheet()

void Stylesheet::Init(Handle<Object> exports) {
// Prepare constructor template
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>();
tpl->SetClassName(NanNew<String>("Stylesheet"));
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>();
tpl->SetClassName(Nan::New<String>("Stylesheet").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

NanAssignPersistent(constructor, tpl->GetFunction());
constructor.Reset(tpl->GetFunction());
}

// not called from node, private api
Local<Object> Stylesheet::New(xsltStylesheetPtr stylesheetPtr) {
NanEscapableScope();
Local<Object> wrapper = NanNew(constructor)->NewInstance();
Nan::EscapableHandleScope scope;
Local<Object> wrapper = Nan::New(constructor)->NewInstance();
Stylesheet* stylesheet = new Stylesheet(stylesheetPtr);
stylesheet->Wrap(wrapper);
return NanEscapeScope(wrapper);
return scope.Escape(wrapper);
}
4 changes: 2 additions & 2 deletions src/stylesheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>

class Stylesheet : public node::ObjectWrap {
class Stylesheet : public Nan::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> exports);
static v8::Local<v8::Object> New(xsltStylesheetPtr stylesheetPtr);
Expand All @@ -18,7 +18,7 @@ class Stylesheet : public node::ObjectWrap {
private:
explicit Stylesheet(xsltStylesheetPtr stylesheetPtr);
~Stylesheet();
static v8::Persistent<v8::Function> constructor;
static Nan::Persistent<v8::Function> constructor;
};

#endif // SRC_STYLESHEET_H_

0 comments on commit 006dcb8

Please sign in to comment.