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