Skip to content

Commit 1be9c37

Browse files
committed
Clean up spacing in templates
1 parent 4cc8301 commit 1be9c37

3 files changed

Lines changed: 200 additions & 166 deletions

File tree

generate/templates/filters/returns_info.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@ module.exports = function(fn, argReturnsOnly, isAsync) {
44
var result = [];
55
var args = fn.args || [];
66

7+
// We will use this to figure out the index
8+
// of arguments, because sync functions
9+
// need to respect ownership to an arbitrary
10+
// parameter that is labeled by name in the
11+
// descriptor, and we won't have access to
12+
// any sort of string to argument index
13+
// in the template.
14+
var nameToArgIndex = {};
15+
args.forEach(function (arg, index) {
16+
nameToArgIndex[arg.name] = index;
17+
});
18+
719
args.forEach(function (arg) {
820
if (!arg.isReturn) return;
921

1022
var return_info = {};
1123

1224
return_info.__proto__ = arg;
1325

26+
return_info.isAsync = isAsync;
1427
return_info.parsedName = isAsync ? "baton->" + return_info.name : return_info.name;
1528
return_info.isCppClassIntType = ~['Uint32', 'Int32'].indexOf(return_info.cppClassName);
1629
return_info.needsDereference
@@ -21,6 +34,15 @@ module.exports = function(fn, argReturnsOnly, isAsync) {
2134
return_info.returnNameOrName = return_info.returnName || return_info.name;
2235
return_info.jsOrCppClassName = return_info.jsClassName || return_info.cppClassName;
2336
return_info.isOutParam = true;
37+
return_info.hasOwner = return_info.ownedBy || return_info.ownedByThis;
38+
return_info.ownedByIndex = -1;
39+
40+
// Here we convert ownedBy, which is the name of the parameter
41+
// that owns this result to the argument index.
42+
// sync functions will need to know this.
43+
if (!isAsync && return_info.ownedBy) {
44+
return_info.ownedByIndex = nameToArgIndex[return_info.ownedBy];
45+
}
2446

2547
result.push(return_info);
2648
});
@@ -33,6 +55,8 @@ module.exports = function(fn, argReturnsOnly, isAsync) {
3355
var return_info = {};
3456

3557
return_info.__proto__ = fn.return;
58+
59+
return_info.isAsync = isAsync;
3660
return_info.parsedName = return_info.name && isAsync ? "baton->" + return_info.name : "result";
3761
return_info.isCppClassIntType = ~['Uint32', 'Int32'].indexOf(return_info.cppClassName);
3862
return_info.parsedClassName = (return_info.cppClassName || '').toLowerCase() + "_t";

generate/templates/partials/async_function.cc

Lines changed: 87 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
1313

1414
{%each args|argsInfo as arg %}
1515
{%if arg.globalPayload %}
16-
{{ cppFunctionName }}_globalPayload* globalPayload = new {{ cppFunctionName }}_globalPayload;
16+
{{ cppFunctionName }}_globalPayload* globalPayload = new {{ cppFunctionName }}_globalPayload;
1717
{%endif%}
1818
{%endeach%}
1919

2020
{%each args|argsInfo as arg %}
2121
{%if not arg.isReturn %}
2222
{%if arg.isSelf %}
23-
baton->{{ arg.name }} = Nan::ObjectWrap::Unwrap<{{ arg.cppClassName }}>(info.This())->GetValue();
23+
baton->{{ arg.name }} = Nan::ObjectWrap::Unwrap<{{ arg.cppClassName }}>(info.This())->GetValue();
2424
{%elsif arg.isCallbackFunction %}
25-
if (!info[{{ arg.jsArg }}]->IsFunction()) {
26-
baton->{{ arg.name }} = NULL;
25+
if (!info[{{ arg.jsArg }}]->IsFunction()) {
26+
baton->{{ arg.name }} = NULL;
2727
{%if arg.payload.globalPayload %}
28-
globalPayload->{{ arg.name }} = NULL;
28+
globalPayload->{{ arg.name }} = NULL;
2929
{%else%}
30-
baton->{{ arg.payload.name }} = NULL;
30+
baton->{{ arg.payload.name }} = NULL;
3131
{%endif%}
32-
}
33-
else {
34-
baton->{{ arg.name}} = {{ cppFunctionName }}_{{ arg.name }}_cppCallback;
35-
{%if arg.payload.globalPayload %}
36-
globalPayload->{{ arg.name }} = new Nan::Callback(info[{{ arg.jsArg }}].As<Function>());
37-
{%else%}
38-
baton->{{ arg.payload.name }} = new Nan::Callback(info[{{ arg.jsArg }}].As<Function>());
39-
{%endif%}
40-
}
32+
}
33+
else {
34+
baton->{{ arg.name}} = {{ cppFunctionName }}_{{ arg.name }}_cppCallback;
35+
{%if arg.payload.globalPayload %}
36+
globalPayload->{{ arg.name }} = new Nan::Callback(info[{{ arg.jsArg }}].As<Function>());
37+
{%else%}
38+
baton->{{ arg.payload.name }} = new Nan::Callback(info[{{ arg.jsArg }}].As<Function>());
39+
{%endif%}
40+
}
4141
{%elsif arg.payloadFor %}
4242
{%if arg.globalPayload %}
43-
baton->{{ arg.name }} = globalPayload;
43+
baton->{{ arg.name }} = globalPayload;
4444
{%endif%}
4545
{%elsif arg.name %}
46-
{%partial convertFromV8 arg%}
46+
{%partial convertFromV8 arg%}
4747
{%if not arg.payloadFor %}
48-
baton->{{ arg.name }} = from_{{ arg.name }};
48+
baton->{{ arg.name }} = from_{{ arg.name }};
4949
{%if arg | isOid %}
50-
baton->{{ arg.name }}NeedsFree = info[{{ arg.jsArg }}]->IsString();
50+
baton->{{ arg.name }}NeedsFree = info[{{ arg.jsArg }}]->IsString();
5151
{%endif%}
5252
{%endif%}
5353
{%endif%}
@@ -62,13 +62,14 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
6262

6363
Nan::Callback *callback = new Nan::Callback(v8::Local<Function>::Cast(info[{{args|jsArgsCount}}]));
6464
{{ cppFunctionName }}Worker *worker = new {{ cppFunctionName }}Worker(baton, callback);
65+
6566
{%each args|argsInfo as arg %}
6667
{%if not arg.isReturn %}
6768
{%if arg.isSelf %}
68-
worker->SaveToPersistent("{{ arg.name }}", info.This());
69+
worker->SaveToPersistent("{{ arg.name }}", info.This());
6970
{%elsif not arg.isCallbackFunction %}
70-
if (!info[{{ arg.jsArg }}]->IsUndefined() && !info[{{ arg.jsArg }}]->IsNull())
71-
worker->SaveToPersistent("{{ arg.name }}", info[{{ arg.jsArg }}]->ToObject());
71+
if (!info[{{ arg.jsArg }}]->IsUndefined() && !info[{{ arg.jsArg }}]->IsNull())
72+
worker->SaveToPersistent("{{ arg.name }}", info[{{ arg.jsArg }}]->ToObject());
7273
{%endif%}
7374
{%endif%}
7475
{%endeach%}
@@ -81,72 +82,78 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::Execute() {
8182
giterr_clear();
8283

8384
{
84-
LockMaster lockMaster(/*asyncAction: */true{%each args|argsInfo as arg %}
85-
{%if arg.cType|isPointer%}{%if not arg.cType|isDoublePointer%}
86-
,baton->{{ arg.name }}
87-
{%endif%}{%endif%}
88-
{%endeach%});
85+
LockMaster lockMaster(
86+
/*asyncAction: */true
87+
{%each args|argsInfo as arg %}
88+
{%if arg.cType|isPointer%}
89+
{%if not arg.cType|isDoublePointer%}
90+
,baton->{{ arg.name }}
91+
{%endif%}
92+
{%endif%}
93+
{%endeach%}
94+
);
8995

9096
{%if .|hasReturnType %}
91-
{{ return.cType }} result = {{ cFunctionName }}(
97+
{{ return.cType }} result = {{ cFunctionName }}(
9298
{%else%}
93-
{{ cFunctionName }}(
99+
{{ cFunctionName }}(
94100
{%endif%}
95101
{%-- Insert Function Arguments --%}
96102
{%each args|argsInfo as arg %}
97103
{%-- turn the pointer into a ref --%}
98-
{%if arg.isReturn|and arg.cType|isDoublePointer %}&{%endif%}baton->{{ arg.name }}{%if not arg.lastArg %},{%endif%}
99-
104+
{%if arg.isReturn|and arg.cType|isDoublePointer %}&{%endif%}baton->{{ arg.name }}{%if not arg.lastArg %},{%endif%}
100105
{%endeach%}
101-
);
106+
);
102107

103108
{%if return.isResultOrError %}
104-
baton->error_code = result;
105-
if (result < GIT_OK && giterr_last() != NULL) {
106-
baton->error = git_error_dup(giterr_last());
107-
}
109+
baton->error_code = result;
110+
if (result < GIT_OK && giterr_last() != NULL) {
111+
baton->error = git_error_dup(giterr_last());
112+
}
108113

109114
{%elsif return.isErrorCode %}
110-
baton->error_code = result;
115+
baton->error_code = result;
111116

112-
if (result != GIT_OK && giterr_last() != NULL) {
113-
baton->error = git_error_dup(giterr_last());
114-
}
117+
if (result != GIT_OK && giterr_last() != NULL) {
118+
baton->error = git_error_dup(giterr_last());
119+
}
115120

116121
{%elsif not return.cType == 'void' %}
117122

118-
baton->result = result;
123+
baton->result = result;
119124

120125
{%endif%}
121126
}
122127
}
123128

124129
void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
125130
{%if return.isResultOrError %}
126-
if (baton->error_code >= GIT_OK) {
131+
if (baton->error_code >= GIT_OK) {
127132
{%else%}
128-
if (baton->error_code == GIT_OK) {
133+
if (baton->error_code == GIT_OK) {
129134
{%endif%}
130-
{%if return.isResultOrError %}
135+
136+
{%if return.isResultOrError %}
131137
v8::Local<v8::Value> result = Nan::New<v8::Number>(baton->error_code);
132138

133-
{%elsif not .|returnsCount %}
139+
{%elsif not .|returnsCount %}
134140
v8::Local<v8::Value> result = Nan::Undefined();
135-
{%else%}
141+
{%else%}
136142
v8::Local<v8::Value> to;
143+
{%if .|returnsCount > 1 %}
144+
v8::Local<Object> result = Nan::New<Object>();
145+
{%endif%}
146+
{%each .|returnsInfo 0 1 as _return %}
147+
{%partial convertToV8 _return %}
137148
{%if .|returnsCount > 1 %}
138-
v8::Local<Object> result = Nan::New<Object>();
139-
{%endif%}
140-
{%each .|returnsInfo 0 1 as _return %}
141-
{%partial convertToV8 _return %}
142-
{%if .|returnsCount > 1 %}
143-
Nan::Set(result, Nan::New("{{ _return.returnNameOrName }}").ToLocalChecked(), to);
144-
{%endif%}
145-
{%endeach%}
146-
{%if .|returnsCount == 1 %}
147-
v8::Local<v8::Value> result = to;
149+
Nan::Set(result, Nan::New("{{ _return.returnNameOrName }}").ToLocalChecked(), to);
148150
{%endif%}
151+
{%endeach%}
152+
{%if .|returnsCount == 1 %}
153+
v8::Local<v8::Value> result = to;
149154
{%endif%}
155+
{%endif%}
156+
150157
v8::Local<v8::Value> argv[2] = {
151158
Nan::Null(),
152159
result
@@ -171,15 +178,15 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
171178
free((void *)baton->error);
172179
} else if (baton->error_code < 0) {
173180
std::queue< v8::Local<v8::Value> > workerArguments;
174-
{%each args|argsInfo as arg %}
175-
{%if not arg.isReturn %}
176-
{%if not arg.isSelf %}
177-
{%if not arg.isCallbackFunction %}
178-
workerArguments.push(GetFromPersistent("{{ arg.name }}"));
179-
{%endif%}
180-
{%endif%}
181-
{%endif%}
182-
{%endeach%}
181+
{%each args|argsInfo as arg %}
182+
{%if not arg.isReturn %}
183+
{%if not arg.isSelf %}
184+
{%if not arg.isCallbackFunction %}
185+
workerArguments.push(GetFromPersistent("{{ arg.name }}"));
186+
{%endif%}
187+
{%endif%}
188+
{%endif%}
189+
{%endeach%}
183190
bool callbackFired = false;
184191
while(!workerArguments.empty()) {
185192
v8::Local<v8::Value> node = workerArguments.front();
@@ -237,18 +244,18 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
237244
{%if arg.shouldAlloc %}
238245
{%if not arg.isCppClassStringOrArray %}
239246
{%elsif arg | isOid %}
240-
if (baton->{{ arg.name}}NeedsFree) {
241-
baton->{{ arg.name}}NeedsFree = false;
242-
free((void*)baton->{{ arg.name }});
243-
}
247+
if (baton->{{ arg.name}}NeedsFree) {
248+
baton->{{ arg.name}}NeedsFree = false;
249+
free((void*)baton->{{ arg.name }});
250+
}
244251
{%elsif arg.isCallbackFunction %}
245252
{%if not arg.payload.globalPayload %}
246-
delete baton->{{ arg.payload.name }};
253+
delete baton->{{ arg.payload.name }};
247254
{%endif%}
248255
{%elsif arg.globalPayload %}
249-
delete ({{ cppFunctionName}}_globalPayload*)baton->{{ arg.name }};
256+
delete ({{ cppFunctionName}}_globalPayload*)baton->{{ arg.name }};
250257
{%else%}
251-
free((void*)baton->{{ arg.name }});
258+
free((void*)baton->{{ arg.name }});
252259
{%endif%}
253260
{%endif%}
254261
{%endeach%}
@@ -257,21 +264,21 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
257264
{%each args|argsInfo as arg %}
258265
{%if arg.isCppClassStringOrArray %}
259266
{%if arg.freeFunctionName %}
260-
{{ arg.freeFunctionName }}(baton->{{ arg.name }});
267+
{{ arg.freeFunctionName }}(baton->{{ arg.name }});
261268
{%elsif not arg.isConst%}
262-
free((void *)baton->{{ arg.name }});
269+
free((void *)baton->{{ arg.name }});
263270
{%endif%}
264271
{%elsif arg | isOid %}
265-
if (baton->{{ arg.name}}NeedsFree) {
266-
baton->{{ arg.name}}NeedsFree = false;
267-
free((void *)baton->{{ arg.name }});
268-
}
272+
if (baton->{{ arg.name}}NeedsFree) {
273+
baton->{{ arg.name}}NeedsFree = false;
274+
free((void *)baton->{{ arg.name }});
275+
}
269276
{%elsif arg.isCallbackFunction %}
270277
{%if not arg.payload.globalPayload %}
271-
delete baton->{{ arg.payload.name }};
278+
delete baton->{{ arg.payload.name }};
272279
{%endif%}
273280
{%elsif arg.globalPayload %}
274-
delete ({{ cppFunctionName}}_globalPayload*)baton->{{ arg.name }};
281+
delete ({{ cppFunctionName}}_globalPayload*)baton->{{ arg.name }};
275282
{%endif%}
276283
{%if arg.cppClassName == "GitBuf" %}
277284
{%if cppFunctionName == "Set" %}

0 commit comments

Comments
 (0)