|
1 | | -{%each args as cbFunction %} |
2 | | - {%if cbFunction.isCallbackFunction %} |
3 | | - |
4 | | -{{ cbFunction.return.type }} {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_cppCallback ( |
5 | | - {% each cbFunction.args|argsInfo as arg %} |
6 | | - {{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %} |
7 | | - {% endeach %} |
8 | | -) { |
9 | | - {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton(); |
10 | | - |
11 | | - {% each cbFunction.args|argsInfo as arg %} |
12 | | - baton->{{ arg.name }} = {{ arg.name }}; |
13 | | - {% endeach %} |
14 | | - |
15 | | - baton->result = 0; |
16 | | - baton->req.data = baton; |
17 | | - baton->done = false; |
18 | | - |
19 | | - uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter); |
20 | | - |
21 | | - while(!baton->done) { |
22 | | - this_thread::sleep_for(chrono::milliseconds(1)); |
23 | | - } |
24 | | - |
25 | | - {% each cbFunction|returnsInfo true false as _return %} |
26 | | - *{{ _return.name }} = *baton->{{ _return.name }}; |
27 | | - {% endeach %} |
28 | | - |
29 | | - return baton->result; |
30 | | -} |
31 | | - |
32 | | -void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork(uv_work_t* req) { |
33 | | - // We aren't doing any work on a seperate thread, just need to |
34 | | - // access the main node thread in the async after method. |
35 | | - // However, this worker method is still needed |
36 | | -} |
37 | | - |
38 | | -void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter(uv_work_t* req, int status) { |
39 | | - NanScope(); |
40 | | - |
41 | | - {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data); |
42 | | - |
43 | | - NanCallback* callback = (NanCallback *)baton->payload; |
44 | | - |
45 | | - Local<Value> argv[{{ cbFunction.args|jsArgsCount }}] = { |
46 | | - {% each cbFunction.args|argsInfo as arg %} |
47 | | - {% if arg.name == "payload" %} |
48 | | - {%-- payload is always the last arg --%} |
49 | | - // payload is null because we can use closure scope in javascript |
50 | | - NanUndefined() |
51 | | - {% elsif arg.isJsArg %} |
52 | | - {% if arg.isEnum %} |
53 | | - NanNew((int)baton->{{ arg.name }}), |
54 | | - {% elsif arg.isLibgitType %} |
55 | | - NanNew({{ arg.cppClassName }}::New(&baton->{{ arg.name }}, false)), |
56 | | - {% elsif arg.cType == "size_t" %} |
57 | | - // HACK: NAN should really have an overload for NanNew to support size_t |
58 | | - NanNew((unsigned int)baton->{{ arg.name }}), |
59 | | - {% else %} |
60 | | - NanNew(baton->{{ arg.name }}), |
61 | | - {% endif %} |
62 | | - {% endif %} |
63 | | - {% endeach %} |
64 | | - }; |
65 | | - |
66 | | - TryCatch tryCatch; |
67 | | - Handle<Value> result = callback->Call({{ cbFunction.args|jsArgsCount }}, argv); |
68 | | - |
69 | | - if (result->IsObject() && result->ToObject()->Has(NanNew("then"))) { |
70 | | - Handle<Value> thenProp = result->ToObject()->Get(NanNew("then")); |
71 | | - |
72 | | - if (thenProp->IsFunction()) { |
73 | | - // we can be reasonbly certain that the result is a promise |
74 | | - Local<Object> promise = result->ToObject(); |
75 | | - |
76 | | - NanAssignPersistent(baton->promise, promise); |
77 | | - |
78 | | - uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling); |
79 | | - return; |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - {{ cbFunction.return.type }} resultStatus; |
84 | | - |
85 | | - {% each cbFunction|returnsInfo true false as _return %} |
86 | | - if (result.IsEmpty() || result->IsNativeError()) { |
87 | | - baton->result = {{ cbFunction.return.error }}; |
88 | | - } |
89 | | - else if (!result->IsNull() && !result->IsUndefined()) { |
90 | | - {{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject()); |
91 | | - wrapper->selfFreeing = false; |
92 | | - |
93 | | - baton->{{ _return.name }} = wrapper->GetRefValue(); |
94 | | - baton->result = {{ cbFunction.return.success }}; |
95 | | - } |
96 | | - else { |
97 | | - baton->result = {{ cbFunction.return.noResults }}; |
98 | | - } |
99 | | - {% endeach %} |
100 | | - baton->done = true; |
101 | | -} |
102 | | - |
103 | | -void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling(uv_work_t* req, int status) { |
104 | | - NanScope(); |
105 | | - |
106 | | - {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data); |
107 | | - Local<Object> promise = NanNew<Object>(baton->promise); |
108 | | - NanCallback* isPendingFn = new NanCallback(promise->Get(NanNew("isPending")).As<Function>()); |
109 | | - Local<Value> argv[1]; // MSBUILD won't assign an array of length 0 |
110 | | - Local<Boolean> isPending = isPendingFn->Call(0, argv)->ToBoolean(); |
111 | | - |
112 | | - if (isPending->Value()) { |
113 | | - uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling); |
114 | | - return; |
115 | | - } |
116 | | - |
117 | | - NanCallback* isFulfilledFn = new NanCallback(promise->Get(NanNew("isFulfilled")).As<Function>()); |
118 | | - Local<Boolean> isFulfilled = isFulfilledFn->Call(0, argv)->ToBoolean(); |
119 | | - |
120 | | - if (isFulfilled->Value()) { |
121 | | - NanCallback* resultFn = new NanCallback(promise->Get(NanNew("value")).As<Function>()); |
122 | | - Handle<Value> result = resultFn->Call(0, argv); |
123 | | - {{ cbFunction.return.type }} resultStatus; |
124 | | - |
125 | | - {% each cbFunction|returnsInfo true false as _return %} |
126 | | - if (result.IsEmpty() || result->IsNativeError()) { |
127 | | - baton->result = {{ cbFunction.return.error }}; |
128 | | - } |
129 | | - else if (!result->IsNull() && !result->IsUndefined()) { |
130 | | - {{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject()); |
131 | | - wrapper->selfFreeing = false; |
132 | | - |
133 | | - baton->{{ _return.name }} = wrapper->GetRefValue(); |
134 | | - baton->result = {{ cbFunction.return.success }}; |
135 | | - } |
136 | | - else { |
137 | | - baton->result = {{ cbFunction.return.noResults }}; |
138 | | - } |
139 | | - {% endeach %} |
140 | | - baton->done = true; |
141 | | - } |
142 | | - else { |
143 | | - // promise was rejected |
144 | | - baton->result = {{ cbFunction.return.error }}; |
145 | | - baton->done = false; |
146 | | - } |
147 | | -} |
148 | | - {%endif%} |
149 | | -{%endeach%} |
| 1 | +{%each args as cbFunction %} |
| 2 | + {%if cbFunction.isCallbackFunction %} |
| 3 | + |
| 4 | +{{ cbFunction.return.type }} {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_cppCallback ( |
| 5 | + {% each cbFunction.args|argsInfo as arg %} |
| 6 | + {{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %} |
| 7 | + {% endeach %} |
| 8 | +) { |
| 9 | + {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton(); |
| 10 | + |
| 11 | + {% each cbFunction.args|argsInfo as arg %} |
| 12 | + baton->{{ arg.name }} = {{ arg.name }}; |
| 13 | + {% endeach %} |
| 14 | + |
| 15 | + baton->result = 0; |
| 16 | + baton->req.data = baton; |
| 17 | + baton->done = false; |
| 18 | + |
| 19 | + uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter); |
| 20 | + |
| 21 | + while(!baton->done) { |
| 22 | + this_thread::sleep_for(chrono::milliseconds(1)); |
| 23 | + } |
| 24 | + |
| 25 | + {% each cbFunction|returnsInfo true false as _return %} |
| 26 | + *{{ _return.name }} = *baton->{{ _return.name }}; |
| 27 | + {% endeach %} |
| 28 | + |
| 29 | + return baton->result; |
| 30 | +} |
| 31 | + |
| 32 | +void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork(uv_work_t* req) { |
| 33 | + // We aren't doing any work on a seperate thread, just need to |
| 34 | + // access the main node thread in the async after method. |
| 35 | + // However, this worker method is still needed |
| 36 | +} |
| 37 | + |
| 38 | +void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter(uv_work_t* req, int status) { |
| 39 | + NanScope(); |
| 40 | + |
| 41 | + {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data); |
| 42 | + |
| 43 | + NanCallback* callback = (NanCallback *)baton->payload; |
| 44 | + |
| 45 | + Local<Value> argv[{{ cbFunction.args|jsArgsCount }}] = { |
| 46 | + {% each cbFunction.args|argsInfo as arg %} |
| 47 | + {% if arg.name == "payload" %} |
| 48 | + {%-- payload is always the last arg --%} |
| 49 | + // payload is null because we can use closure scope in javascript |
| 50 | + NanUndefined() |
| 51 | + {% elsif arg.isJsArg %} |
| 52 | + {% if arg.isEnum %} |
| 53 | + NanNew((int)baton->{{ arg.name }}), |
| 54 | + {% elsif arg.isLibgitType %} |
| 55 | + NanNew({{ arg.cppClassName }}::New(&baton->{{ arg.name }}, false)), |
| 56 | + {% elsif arg.cType == "size_t" %} |
| 57 | + // HACK: NAN should really have an overload for NanNew to support size_t |
| 58 | + NanNew((unsigned int)baton->{{ arg.name }}), |
| 59 | + {% else %} |
| 60 | + NanNew(baton->{{ arg.name }}), |
| 61 | + {% endif %} |
| 62 | + {% endif %} |
| 63 | + {% endeach %} |
| 64 | + }; |
| 65 | + |
| 66 | + TryCatch tryCatch; |
| 67 | + Handle<Value> result = callback->Call({{ cbFunction.args|jsArgsCount }}, argv); |
| 68 | + |
| 69 | + if (result->IsObject() && result->ToObject()->Has(NanNew("then"))) { |
| 70 | + Handle<Value> thenProp = result->ToObject()->Get(NanNew("then")); |
| 71 | + |
| 72 | + if (thenProp->IsFunction()) { |
| 73 | + // we can be reasonbly certain that the result is a promise |
| 74 | + Local<Object> promise = result->ToObject(); |
| 75 | + |
| 76 | + NanAssignPersistent(baton->promise, promise); |
| 77 | + |
| 78 | + uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + {{ cbFunction.return.type }} resultStatus; |
| 84 | + |
| 85 | + {% each cbFunction|returnsInfo true false as _return %} |
| 86 | + if (result.IsEmpty() || result->IsNativeError()) { |
| 87 | + baton->result = {{ cbFunction.return.error }}; |
| 88 | + } |
| 89 | + else if (!result->IsNull() && !result->IsUndefined()) { |
| 90 | + {{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject()); |
| 91 | + wrapper->selfFreeing = false; |
| 92 | + |
| 93 | + baton->{{ _return.name }} = wrapper->GetRefValue(); |
| 94 | + baton->result = {{ cbFunction.return.success }}; |
| 95 | + } |
| 96 | + else { |
| 97 | + baton->result = {{ cbFunction.return.noResults }}; |
| 98 | + } |
| 99 | + {% endeach %} |
| 100 | + baton->done = true; |
| 101 | +} |
| 102 | + |
| 103 | +void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling(uv_work_t* req, int status) { |
| 104 | + NanScope(); |
| 105 | + |
| 106 | + {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data); |
| 107 | + Local<Object> promise = NanNew<Object>(baton->promise); |
| 108 | + NanCallback* isPendingFn = new NanCallback(promise->Get(NanNew("isPending")).As<Function>()); |
| 109 | + Local<Value> argv[1]; // MSBUILD won't assign an array of length 0 |
| 110 | + Local<Boolean> isPending = isPendingFn->Call(0, argv)->ToBoolean(); |
| 111 | + |
| 112 | + if (isPending->Value()) { |
| 113 | + uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + NanCallback* isFulfilledFn = new NanCallback(promise->Get(NanNew("isFulfilled")).As<Function>()); |
| 118 | + Local<Boolean> isFulfilled = isFulfilledFn->Call(0, argv)->ToBoolean(); |
| 119 | + |
| 120 | + if (isFulfilled->Value()) { |
| 121 | + NanCallback* resultFn = new NanCallback(promise->Get(NanNew("value")).As<Function>()); |
| 122 | + Handle<Value> result = resultFn->Call(0, argv); |
| 123 | + {{ cbFunction.return.type }} resultStatus; |
| 124 | + |
| 125 | + {% each cbFunction|returnsInfo true false as _return %} |
| 126 | + if (result.IsEmpty() || result->IsNativeError()) { |
| 127 | + baton->result = {{ cbFunction.return.error }}; |
| 128 | + } |
| 129 | + else if (!result->IsNull() && !result->IsUndefined()) { |
| 130 | + {{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject()); |
| 131 | + wrapper->selfFreeing = false; |
| 132 | + |
| 133 | + baton->{{ _return.name }} = wrapper->GetRefValue(); |
| 134 | + baton->result = {{ cbFunction.return.success }}; |
| 135 | + } |
| 136 | + else { |
| 137 | + baton->result = {{ cbFunction.return.noResults }}; |
| 138 | + } |
| 139 | + {% endeach %} |
| 140 | + baton->done = true; |
| 141 | + } |
| 142 | + else { |
| 143 | + // promise was rejected |
| 144 | + baton->result = {{ cbFunction.return.error }}; |
| 145 | + baton->done = false; |
| 146 | + } |
| 147 | +} |
| 148 | + {%endif%} |
| 149 | +{%endeach%} |
0 commit comments