forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_function.cc
More file actions
136 lines (117 loc) · 3.69 KB
/
Copy pathasync_function.cc
File metadata and controls
136 lines (117 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{%partial doc .%}
NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
NanScope();
{%partial guardArguments .%}
if (args.Length() == {{args|jsArgsCount}} || !args[{{args|jsArgsCount}}]->IsFunction()) {
return NanThrowError("Callback is required and must be a Function.");
}
{{ cppFunctionName }}Baton* baton = new {{ cppFunctionName }}Baton;
baton->error_code = GIT_OK;
baton->error = NULL;
{%each args|argsInfo as arg %}
{%if not arg.isReturn %}
{%if arg.isSelf %}
baton->{{ arg.name }} = ObjectWrap::Unwrap<{{ arg.cppClassName }}>(args.This())->GetValue();
{%elsif arg.name %}
{%partial convertFromV8 arg%}
{%if not arg.isPayload %}
baton->{{ arg.name }} = from_{{ arg.name }};
{%endif%}
{%endif%}
{%elsif arg.shouldAlloc %}
baton->{{ arg.name }} = ({{ arg.cType }})malloc(sizeof({{ arg.cType|replace '*' '' }}));
{%endif%}
{%endeach%}
NanCallback *callback = new NanCallback(Local<Function>::Cast(args[{{args|jsArgsCount}}]));
{{ cppFunctionName }}Worker *worker = new {{ cppFunctionName }}Worker(baton, callback);
{%each args|argsInfo as arg %}
{%if not arg.isReturn %}
{%if arg.isSelf %}
worker->SaveToPersistent("{{ arg.name }}", args.This());
{%else%}
if (!args[{{ arg.jsArg }}]->IsUndefined() && !args[{{ arg.jsArg }}]->IsNull())
worker->SaveToPersistent("{{ arg.name }}", args[{{ arg.jsArg }}]->ToObject());
{%endif%}
{%endif%}
{%endeach%}
NanAsyncQueueWorker(worker);
NanReturnUndefined();
}
void {{ cppClassName }}::{{ cppFunctionName }}Worker::Execute() {
{%if .|hasReturnType %}
{{ return.cType }} result = {{ cFunctionName }}(
{%else%}
{{ cFunctionName }}(
{%endif%}
{%-- Insert Function Arguments --%}
{%each args|argsInfo as arg %}
{%-- turn the pointer into a ref --%}
{%if arg.isReturn|and arg.cType|isDoublePointer %}&{%endif%}baton->{{ arg.name }}{%if not arg.lastArg %},{%endif%}
{%endeach%}
);
{%if return.isErrorCode %}
baton->error_code = result;
if (result != GIT_OK && giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}
{%elsif not return.cType == 'void' %}
baton->result = result;
{%endif%}
}
void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
TryCatch try_catch;
if (baton->error_code == GIT_OK) {
{%if not .|returnsCount %}
Handle<Value> result = NanUndefined();
{%else%}
Handle<Value> to;
{%if .|returnsCount > 1 %}
Handle<Object> result = NanNew<Object>();
{%endif%}
{%each .|returnsInfo 0 1 as _return %}
{%partial convertToV8 _return %}
{%if .|returnsCount > 1 %}
result->Set(NanNew<String>("{{ _return.jsNameOrName }}"), to);
{%endif%}
{%endeach%}
{%if .|returnsCount == 1 %}
Handle<Value> result = to;
{%endif%}
{%endif%}
Handle<Value> argv[2] = {
NanNull(),
result
};
callback->Call(2, argv);
} else {
if (baton->error) {
Handle<Value> argv[1] = {
NanError(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else {
callback->Call(0, NULL);
}
{%each args as arg %}
{%if arg.shouldAlloc %}
free(baton->{{ arg.name }});
{%endif%}
{%endeach%}
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
{%each args|argsInfo as arg %}
{%if arg.isCppClassStringOrArray %}
{%if arg.freeFunctionName %}
{{ arg.freeFunctionName }}(baton->{{ arg.name }});
{%else%}
free((void *)baton->{{ arg.name }});
{%endif%}
{%endif%}
{%endeach%}
delete baton;
}