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];
0 commit comments