Skip to content

Commit 870f766

Browse files
3y3tbranyen
authored andcommitted
Merge pull request #175 from 3y3/master
NodeJS v0.11.13 compatibility
2 parents 8ca18ce + fd2b822 commit 870f766

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3873
-3836
lines changed

build/codegen/templates/asyncFunction.cc.ejs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,26 @@
33
/**
44
<% include doc.cc.ejs -%>
55
*/
6-
Handle<Value> <%- cppClassName %>::<%- functionInfo.cppFunctionName %>(const Arguments& args) {
7-
HandleScope scope;
6+
NAN_METHOD(<%- cppClassName %>::<%- functionInfo.cppFunctionName %>) {
7+
NanScope();
88
<% var jsArg; -%>
99
<% include guardArguments.cc.ejs -%>
1010

1111
if (args.Length() == <%- jsArg %> || !args[<%- jsArg %>]->IsFunction()) {
12-
return ThrowException(Exception::Error(String::New("Callback is required and must be a Function.")));
12+
return NanThrowError("Callback is required and must be a Function.");
1313
}
1414

1515
<%- functionInfo.cppFunctionName %>Baton* baton = new <%- functionInfo.cppFunctionName %>Baton;
1616
baton->error_code = GIT_OK;
1717
baton->error = NULL;
18-
baton->request.data = baton;
1918
<%
2019
for (var cArg = 0, jsArg = 0; cArg < functionInfo.args.length; cArg++) {
2120
var arg = functionInfo.args[cArg];
2221
-%>
2322
<% if (!arg.isReturn) { -%>
2423
<% if (arg.isSelf) { -%>
25-
baton-><%- arg.name %>Reference = Persistent<Value>::New(args.This());
2624
baton-><%- arg.name %> = ObjectWrap::Unwrap<<%- cppClassName %>>(args.This())->GetValue();
2725
<% } else { -%>
28-
baton-><%- arg.name %>Reference = Persistent<Value>::New(args[<%- jsArg %>]);
2926
<% include convertFromV8.cc.ejs -%>
3027
<% if (!arg.isPayload) { -%>
3128
baton-><%- arg.name %> = from_<%- arg.name %>;
@@ -35,19 +32,32 @@ Handle<Value> <%- cppClassName %>::<%- functionInfo.cppFunctionName %>(const Arg
3532
<% } else { -%>
3633
<% if (arg.shouldAlloc) { -%>
3734
baton-><%- arg.name %> = (<%- arg.cType %>)malloc(sizeof(<%- arg.cType.replace('*', '') %>));
38-
<% } else { -%>
3935
<% } -%>
4036
<% } -%>
4137
<% } -%>
42-
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[<%- jsArg %>]));
4338

44-
uv_queue_work(uv_default_loop(), &baton->request, <%- functionInfo.cppFunctionName %>Work, (uv_after_work_cb)<%- functionInfo.cppFunctionName %>AfterWork);
39+
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[<%- jsArg %>]));
40+
<%- functionInfo.cppFunctionName %>Worker *worker = new <%- functionInfo.cppFunctionName %>Worker(baton, callback);
41+
<%
42+
for (var cArg = 0, jsArg = 0; cArg < functionInfo.args.length; cArg++) {
43+
var arg = functionInfo.args[cArg];
44+
-%>
45+
<% if (!arg.isReturn) { -%>
46+
<% if (arg.isSelf) { -%>
47+
worker->SaveToPersistent("<%- arg.name %>", args.This());
48+
<% } else { -%>
49+
if (!args[<%- jsArg %>]->IsUndefined() && !args[<%- jsArg %>]->IsNull())
50+
worker->SaveToPersistent("<%- arg.name %>", args[<%- jsArg %>]->ToObject());
51+
<% } -%>
52+
<% if (!(arg.isReturn || arg.isSelf || arg.isPayload)) jsArg++; -%>
53+
<% } -%>
54+
<% } -%>
4555

46-
return Undefined();
56+
NanAsyncQueueWorker(worker);
57+
NanReturnUndefined();
4758
}
4859

49-
void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>Work(uv_work_t *req) {
50-
<%- functionInfo.cppFunctionName %>Baton *baton = static_cast<<%- functionInfo.cppFunctionName %>Baton *>(req->data);
60+
void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>Worker::Execute() {
5161
<% if (functionInfo.return.cType != "void" || functionInfo.return.isErrorCode) { %><%- functionInfo.return.cType %> result = <% } %><%- functionInfo.cFunctionName %>(
5262
<%
5363
for (var i = 0; i < functionInfo.args.length; i++) {
@@ -66,46 +76,43 @@ void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>Work(uv_work_t *req
6676
<% } -%>
6777
}
6878

69-
void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>AfterWork(uv_work_t *req) {
70-
HandleScope scope;
71-
<%- functionInfo.cppFunctionName %>Baton *baton = static_cast<<%- functionInfo.cppFunctionName %>Baton *>(req->data);
72-
79+
void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>Worker::HandleOKCallback() {
7380
TryCatch try_catch;
7481
if (baton->error_code == GIT_OK) {
7582
<% if (!returns.length) { -%>
76-
Handle<Value> result = Local<Value>::New(Undefined());
83+
Handle<Value> result = NanUndefined();
7784
<% } else if (returns.length == 1) { -%>
7885
<% var to = {}; to.__proto__ = returns[0]; to.name = "baton->" + to.name; -%>
7986
Handle<Value> to;
8087
<% include convertToV8.cc.ejs -%>
8188
Handle<Value> result = to;
8289
<% } else { -%>
83-
Handle<Object> result = Object::New();
90+
Handle<Object> result = NanNew<Object>();
8491
Handle<Value> to;
8592
<%
8693
for (r in returns) {
8794
var to = returns[r];
8895
-%>
8996
<% include convertToV8.cc.ejs -%>
90-
result->Set(String::NewSymbol("<%- to.jsName || to.name %>"), to);
97+
result->Set(NanNew<String>("<%- to.jsName || to.name %>"), to);
9198
<% } -%>
9299
<% } -%>
93100
Handle<Value> argv[2] = {
94-
Local<Value>::New(Null()),
101+
NanNull(),
95102
result
96103
};
97-
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
104+
callback->Call(2, argv);
98105
} else {
99106
if (baton->error) {
100107
Handle<Value> argv[1] = {
101-
Exception::Error(String::New(baton->error->message))
108+
NanError(baton->error->message)
102109
};
103-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
110+
callback->Call(1, argv);
104111
if (baton->error->message)
105112
free((void *)baton->error->message);
106113
free((void *)baton->error);
107114
} else {
108-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
115+
callback->Call(0, NULL);
109116
}
110117
<%
111118
for (var i = 0; i < functionInfo.args.length; i++) {
@@ -119,14 +126,6 @@ void <%- cppClassName %>::<%- functionInfo.cppFunctionName %>AfterWork(uv_work_t
119126
if (try_catch.HasCaught()) {
120127
node::FatalException(try_catch);
121128
}
122-
<%
123-
for (var i = 0, j = 0; i < functionInfo.args.length; i++) {
124-
var arg = functionInfo.args[i];
125-
if (arg.isReturn) continue;
126-
-%>
127-
baton-><%- arg.name %>Reference.Dispose();
128-
<% } -%>
129-
baton->callback.Dispose();
130129
<%
131130
for (var i = 0; i < functionInfo.args.length; i++) {
132131
var arg = functionInfo.args[i];

build/codegen/templates/class.cc.ejs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
/**
2727
* This code is auto-generated; unless you know what you're doing, do not modify!
2828
**/
29-
#include <v8.h>
30-
#include <node.h>
29+
#include <nan.h>
3130
#include <string.h>
3231

3332
#include "git2.h"
@@ -56,12 +55,12 @@ using namespace node;
5655
}
5756
5857
void <%- cppClassName %>::Initialize(Handle<v8::Object> target) {
59-
HandleScope scope;
58+
NanScope();
6059
61-
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
60+
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
6261
6362
tpl->InstanceTemplate()->SetInternalFieldCount(1);
64-
tpl->SetClassName(String::NewSymbol("<%- jsClassName %>"));
63+
tpl->SetClassName(NanNew<String>("<%- jsClassName %>"));
6564
6665
<% if (typeof functions != 'undefined') { -%>
6766
<%
@@ -87,49 +86,49 @@ void <%- cppClassName %>::Initialize(Handle<v8::Object> target) {
8786
<% } -%>
8887
<% } -%>
8988
90-
constructor_template = Persistent<Function>::New(tpl->GetFunction());
91-
target->Set(String::NewSymbol("<%- jsClassName %>"), constructor_template);
89+
Local<Function> _constructor_template = tpl->GetFunction();
90+
NanAssignPersistent(constructor_template, _constructor_template);
91+
target->Set(NanNew<String>("<%- jsClassName %>"), _constructor_template);
9292
}
9393
94-
Handle<Value> <%- cppClassName %>::New(const Arguments& args) {
95-
HandleScope scope;
94+
NAN_METHOD(<%- cppClassName %>::New) {
95+
NanScope();
9696
9797
if (args.Length() == 0 || !args[0]->IsExternal()) {
98-
return ThrowException(Exception::Error(String::New("<%= cType %> is required.")));
98+
return NanThrowError("<%= cType %> is required.");
9999
}
100-
101-
<%- cppClassName %>* object = new <%- cppClassName %>((<%= cType%> *) External::Unwrap(args[0]));
100+
<%- cppClassName %>* object = new <%- cppClassName %>(static_cast<<%= cType%> *>(Handle<External>::Cast(args[0])->Value()));
102101
object->Wrap(args.This());
103102
104-
return scope.Close(args.This());
103+
NanReturnValue(args.This());
105104
}
106105
107106
Handle<Value> <%- cppClassName %>::New(void *raw) {
108-
HandleScope scope;
109-
Handle<Value> argv[1] = { External::New((void *)raw) };
110-
return scope.Close(<%- cppClassName %>::constructor_template->NewInstance(1, argv));
107+
NanEscapableScope();
108+
Handle<Value> argv[1] = { NanNew<External>((void *)raw) };
109+
return NanEscapeScope(NanNew<Function>(<%- cppClassName %>::constructor_template)->NewInstance(1, argv));
111110
}
112111
113112
<%- cType %> *<%- cppClassName %>::GetValue() {
114113
return this->raw;
115114
}
116115
<% } else { -%>
117116
void <%- cppClassName %>::Initialize(Handle<v8::Object> target) {
118-
HandleScope scope;
117+
NanScope();
119118
120-
Persistent<Object> object = Persistent<Object>::New(Object::New());
119+
Local<Object> object = NanNew<Object>();
121120
122121
<% if (typeof functions != 'undefined') { -%>
123122
<%
124123
for (var i in functions) {
125124
var functionInfo = functions[i];
126125
if (functionInfo.ignore) continue;
127126
-%>
128-
object->Set(String::NewSymbol("<%- functionInfo.jsFunctionName %>"), FunctionTemplate::New(<%- functionInfo.cppFunctionName %>)->GetFunction());
127+
NODE_SET_METHOD(object, "<%- functionInfo.jsFunctionName %>", <%- functionInfo.cppFunctionName %>);
129128
<% } -%>
130129
<% } -%>
131130
132-
target->Set(String::NewSymbol("<%- jsClassName %>"), object);
131+
target->Set(NanNew<String>("<%- jsClassName %>"), object);
133132
}
134133
<% } -%>
135134

build/codegen/templates/convertFromV8.cc.ejs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
<%
1414
// FIXME: should recursively call convertFromv8.
1515
%>
16-
from_<%- arg.name %>[i] = ObjectWrap::Unwrap<<%- arg.arrayElementCppClassName %>>(tmp_<%- arg.name %>->Get(Number::New(static_cast<double>(i)))->ToObject())->GetValue();
16+
from_<%- arg.name %>[i] = ObjectWrap::Unwrap<<%- arg.arrayElementCppClassName %>>(tmp_<%- arg.name %>->Get(NanNew<Number>(static_cast<double>(i)))->ToObject())->GetValue();
1717
}
1818
<% } else if (arg.cppClassName == "Function") { -%>
19-
Persistent<Function>::New(Local<Function>::Cast(args[<%- jsArg %>]));
2019
<% } else if (arg.cppClassName == 'Buffer') { -%>
2120
from_<%- arg.name %> = Buffer::Data(args[<%- jsArg %>]->ToObject());
2221
<% } else if (isV8Value(arg.cppClassName)) { -%>
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<% toName = to.name || 'result' -%>
22
<% if (to.cppClassName == "String") { -%>
33
<% if (typeof to.size != 'undefined') { -%>
4-
to = String::New(<%- toName %>, <%- to.size %>);
4+
to = NanNew<String>(<%- toName %>, <%- to.size %>);
55
<% } else { -%>
6-
to = String::New(<%- toName %>);
6+
to = NanNew<String>(<%- toName %>);
77
<% } -%>
88
<% if (to.freeFunctionName) { -%>
99
<%- to.freeFunctionName %>(<%- toName %>);
1010
<% } -%>
1111
<% } else if (isV8Value(to.cppClassName)) { -%>
12-
to = <%- to.cppClassName %>::New(<%- toName %>);
12+
<% if (~['Uint32', 'Int32'].indexOf(to.cppClassName)) { -%>
13+
<% var className = to.cppClassName.toLowerCase()+'_t' -%>
14+
to = NanNew<<%- to.cppClassName %>>((<%- className %>)<%- toName %>);
15+
<% } else { -%>
16+
to = NanNew<<%- to.cppClassName %>>(<%- toName %>);
17+
<% } -%>
1318
<% } else if (to.cppClassName == "External") { -%>
14-
to = External::New((void *)<%- toName %>);
19+
to = NanNew<External>((void *)<%- toName %>);
1520
<% } else if (to.cppClassName == 'Array') { -%>
1621
<%
1722
// FIXME this is not general purpose enough.
1823
%>
19-
Local<Array> tmpArray = Array::New(<%- toName %>-><%- to.size %>);
24+
Local<Array> tmpArray = NanNew<Array>(<%- toName %>-><%- to.size %>);
2025
for (unsigned int i = 0; i < <%- toName %>-><%- to.size %>; i++) {
21-
tmpArray->Set(Number::New(i), String::New(<%- toName %>-><%- to.key %>[i]));
26+
tmpArray->Set(NanNew<Number>(i), NanNew<String>(<%- toName %>-><%- to.key %>[i]));
2227
}
2328
to = tmpArray;
2429
<% } else { -%>
@@ -30,6 +35,6 @@
3035
if (<%- toName %> != NULL) {
3136
to = <%- to.cppClassName %>::New((void *)<%- toName %>);
3237
} else {
33-
to = Null();
38+
to = NanNull();
3439
}
3540
<% } -%>

build/codegen/templates/fields.cc.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
if (fieldInfo.ignore) continue;
66
-%>
77
8-
Handle<Value> <%- cppClassName %>::<%- fieldInfo.cppFunctionName %>(const Arguments& args) {
9-
HandleScope scope;
8+
NAN_METHOD(<%- cppClassName %>::<%- fieldInfo.cppFunctionName %>) {
9+
NanScope();
1010
<% var to = fieldInfo; -%>
1111
Handle<Value> to;
1212
1313
<%- fieldInfo.cType %> <% if (!isV8Value(fieldInfo.cppClassName)) { %>*<% } %><%- fieldInfo.name %> =
1414
<% if (!isV8Value(fieldInfo.cppClassName)) { %>&<% } %>ObjectWrap::Unwrap<<%- cppClassName %>>(args.This())->GetValue()-><%- fieldInfo.name %>;
1515
1616
<% include convertToV8.cc.ejs -%>
17-
return scope.Close(to);
17+
NanReturnValue(to);
1818
}
1919
<% } -%>
2020
<% } -%>

build/codegen/templates/guardArguments.cc.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-%>
77
<% if (!arg.isOptional) { -%>
88
if (args.Length() == <%- jsArg %> || !args[<%- jsArg %>]->Is<%- cppClassName2v8ValueClassName(arg.cppClassName) %>()) {
9-
return ThrowException(Exception::Error(String::New("<%- arg.jsClassName %> <%- arg.name %> is required.")));
9+
return NanThrowError("<%- arg.jsClassName %> <%- arg.name %> is required.");
1010
}
1111
<% } -%>
1212
<% jsArg++; -%>

build/codegen/templates/header.h.ejs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#ifndef <%- cppClassName.toUpperCase() %>_H
66
#define <%- cppClassName.toUpperCase() %>_H
77

8-
#include <v8.h>
9-
#include <node.h>
8+
#include <nan.h>
109
#include <string>
1110

1211
#include "git2.h"
@@ -32,31 +31,26 @@ class <%- cppClassName %> : public ObjectWrap {
3231
~<%- cppClassName %>();
3332
<% } -%>
3433

35-
static Handle<Value> New(const Arguments& args);
34+
static NAN_METHOD(New);
3635

3736
<% if (typeof fields != 'undefined') { -%>
3837
<%
3938
for (var i in fields) {
4039
var fieldInfo = fields[i];
4140
if (fieldInfo.ignore) continue;
4241
-%>
43-
static Handle<Value> <%- fieldInfo.cppFunctionName %>(const Arguments& args);
42+
static NAN_METHOD(<%- fieldInfo.cppFunctionName %>);
4443
<% } -%>
4544
<% } -%>
46-
4745
<% if (typeof functions != 'undefined') { -%>
4846
<%
4947
for (var i in functions) {
5048
var functionInfo = functions[i];
5149
if (functionInfo.ignore) continue;
5250
-%>
53-
static Handle<Value> <%- functionInfo.cppFunctionName %>(const Arguments& args);
5451
<% if (functionInfo.isAsync) { -%>
55-
static void <%- functionInfo.cppFunctionName %>Work(uv_work_t* req);
56-
static void <%- functionInfo.cppFunctionName %>AfterWork(uv_work_t* req);
5752
5853
struct <%- functionInfo.cppFunctionName %>Baton {
59-
uv_work_t request;
6054
int error_code;
6155
const git_error* error;
6256
<%
@@ -66,13 +60,26 @@ class <%- cppClassName %> : public ObjectWrap {
6660
<% if (arg.isReturn) { -%>
6761
<%- arg.cType.replace('**', '*') %> <%- arg.name %>;
6862
<% } else { -%>
69-
Persistent<Value> <%- arg.name %>Reference;
7063
<%- arg.cType %> <%- arg.name %>;
7164
<% } -%>
7265
<% } -%>
73-
Persistent<Function> callback;
66+
};
67+
class <%- functionInfo.cppFunctionName %>Worker : public NanAsyncWorker {
68+
public:
69+
<%- functionInfo.cppFunctionName %>Worker(
70+
<%- functionInfo.cppFunctionName %>Baton *_baton,
71+
NanCallback *callback
72+
) : NanAsyncWorker(callback)
73+
, baton(_baton) {};
74+
~<%- functionInfo.cppFunctionName %>Worker() {};
75+
void Execute();
76+
void HandleOKCallback();
77+
78+
private:
79+
<%- functionInfo.cppFunctionName %>Baton *baton;
7480
};
7581
<% } -%>
82+
static NAN_METHOD(<%- functionInfo.cppFunctionName %>);
7683
<% } -%>
7784
<% } -%>
7885
<% if (typeof cType != 'undefined') { -%>

0 commit comments

Comments
 (0)