diff --git a/incremental/graphics/native/platform/cpp/node/platform-api.cc b/incremental/graphics/native/platform/cpp/node/platform-api.cc index 3508596499779371786e12ba56b8dff6c9fc3cb4..f9c80c02fd69b7134e5a313725d27a5fb311ddc8 100644 --- a/incremental/graphics/native/platform/cpp/node/platform-api.cc +++ b/incremental/graphics/native/platform/cpp/node/platform-api.cc @@ -30,12 +30,16 @@ static peerFactory_t peerFactory = nullptr; static void* peerFactoryArg = nullptr; static bool isPlatformApiSet = false; -void impl_skoala_setPlatformAPI(Napi::Object apiObject) { - if (isPlatformApiSet) return; +// void impl_skoala_setPlatformAPI(Napi::Object apiObject) { +napi_value Node_skoala_setPlatformAPI(napi_env env, napi_callback_info cbinfo) { + napi_value result = makeVoid(env); + if (isPlatformApiSet) return result; isPlatformApiSet = true; #ifndef SK_BUILD_FOR_ANDROID + CallbackInfo info(env, cbinfo); + Napi::Object apiObject(env, info[0]); if (apiObject.IsNull()) { - return; + return result; } v8::Isolate* isolate = v8::Isolate::GetCurrent(); auto nodeState = new NodeScopedState(isolate, isolate->GetCurrentContext()); @@ -49,10 +53,14 @@ void impl_skoala_setPlatformAPI(Napi::Object apiObject) { peerFactory = getSystemPeerFactory(nodeState); peerFactoryArg = getSystemPeerFactoryArg(); #endif + return result; } -KOALA_INTEROP_V1(skoala_setPlatformAPI, Napi::Object) +MAKE_INTEROP_NODE_EXPORT(skoala_setPlatformAPI) -KNativePointer impl_skoala_createRedrawerPeer(Napi::Object redrawerObject) { +// KNativePointer impl_skoala_createRedrawerPeer(Napi::Object redrawerObject) { +napi_value Node_skoala_createRedrawerPeer(napi_env env, napi_callback_info cbinfo) { + CallbackInfo info(env, cbinfo); + Napi::Object redrawerObject(env, info[0]); // TODO: implement release of peer. RedrawerPeer* peer = nullptr; assert(peerFactory != nullptr); @@ -65,9 +73,9 @@ KNativePointer impl_skoala_createRedrawerPeer(Napi::Object redrawerObject) { } peer->callOnInit(); - return peer; + return makePointer(env, peer); } -KOALA_INTEROP_1(skoala_createRedrawerPeer, KNativePointer, Napi::Object) +MAKE_INTEROP_NODE_EXPORT(skoala_createRedrawerPeer) void impl_skoala_providePeerFactory(KNativePointer peerFactoryPtr, KNativePointer peerFactoryArgPtr) { peerFactory = reinterpret_cast)>(peerFactoryPtr); @@ -109,7 +117,8 @@ void doCallVoidCallback(KNativePointer cb) { callCallback(cb); } -Napi::Value Node_skoala_registerCallback(const Napi::CallbackInfo& info) { +napi_value Node_skoala_registerCallback(napi_env env, napi_callback_info cbinfo) { + Napi::CallbackInfo info(env, cbinfo); static std::once_flag flag; std::call_once(flag, []() { initCallbacks( @@ -155,7 +164,7 @@ Napi::Value Node_skoala_registerCallback(const Napi::CallbackInfo& info) { NodeCallback* callback = new NodeCallback(isolate, isolate->GetCurrentContext(), receiver.As(), cb.As()); - return makePointer(info, callback); + return makePointer(env, callback); } MAKE_INTEROP_NODE_EXPORT(skoala_registerCallback) @@ -172,14 +181,21 @@ KBoolean impl_skoala_checkEvents(KNativePointer dataPtr) { } KOALA_INTEROP_1(skoala_checkEvents, KBoolean, KNativePointer) -void impl_skoala_drawPicture(KNativePointer picturePtr, KNativePointer dataPtr, Napi::Object completerObject, KBoolean sync) { +napi_value Node_skoala_drawPicture(napi_env env, napi_callback_info cbinfo) { + CallbackInfo info(env, cbinfo); + KNativePointer picturePtr = getArgument(info, 0); + KNativePointer dataPtr = getArgument(info, 1); + Napi::Object completerObject(env, info[0]); + KBoolean sync = getArgument(info, 3); + SkPicture* picture = ptr(picturePtr); RedrawerData* data = ptr(dataPtr); Napi::Function completer = completerObject.As(); performDrawOperation(completer, data, picture, sync); + return makeVoid(env); } -KOALA_INTEROP_V4(skoala_drawPicture, KNativePointer, KNativePointer, Napi::Object, KBoolean) +MAKE_INTEROP_NODE_EXPORT(skoala_drawPicture) void impl_skoala_abandonRedrawer(KNativePointer dataPtr) { auto data = ptr(dataPtr); diff --git a/interop/src/cpp/napi/convertors-napi.cc b/interop/src/cpp/napi/convertors-napi.cc index 30ebc87cb4a7c5d3394db151bbe7a89fecd51655..21a9eb41bfa28d7c6d7f6a02b4455a3c94fe45c7 100644 --- a/interop/src/cpp/napi/convertors-napi.cc +++ b/interop/src/cpp/napi/convertors-napi.cc @@ -31,14 +31,14 @@ napi_valuetype getValueTypeChecked(napi_env env, napi_value value) { napi_valuetype type; napi_status status = napi_typeof(env, value, &type); - NAPI_THROW_IF_FAILED(env, status, napi_undefined); + KOALA_NAPI_THROW_IF_FAILED(env, status, napi_undefined); return type; } bool isTypedArray(napi_env env, napi_value value) { bool result = false; napi_status status = napi_is_typedarray(env, value, &result); - NAPI_THROW_IF_FAILED(env, status, false); + KOALA_NAPI_THROW_IF_FAILED(env, status, false); return result; } @@ -116,7 +116,7 @@ KNativePointer getPointer(napi_env env, napi_value value) { if (valueType == napi_valuetype::napi_external) { KNativePointer result = nullptr; napi_status status = napi_get_value_external(env, value, &result); - NAPI_THROW_IF_FAILED(env, status, nullptr); + KOALA_NAPI_THROW_IF_FAILED(env, status, nullptr); return result; } @@ -128,7 +128,7 @@ KNativePointer getPointer(napi_env env, napi_value value) { bool isWithinRange = true; uint64_t ptrU64 = 0; napi_status status = napi_get_value_bigint_uint64(env, value, &ptrU64, &isWithinRange); - NAPI_THROW_IF_FAILED(env, status, nullptr); + KOALA_NAPI_THROW_IF_FAILED(env, status, nullptr); if (!isWithinRange) { napi_throw_error(env, nullptr, "cannot be coerced to uint64, value is too large"); return nullptr; @@ -158,7 +158,7 @@ KLong getInt64(napi_env env, napi_value value) { // .ThrowAsJavaScriptException(); // napi_value result; // napi_status status = napi_get_global(env, &result); -// NAPI_THROW_IF_FAILED(env, status, result); +// KOALA_NAPI_THROW_IF_FAILED(env, status, result); // } // return value; @@ -168,7 +168,7 @@ napi_value makeString(napi_env env, const KStringPtr& value) { napi_value result; napi_status status; status = napi_create_string_utf8(env, value.isNull() ? "" : value.data(), value.length(), &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -176,7 +176,7 @@ napi_value makeString(napi_env env, const std::string& value) { napi_value result; napi_status status; status = napi_create_string_utf8(env, value.c_str(), value.length(), &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -184,7 +184,7 @@ napi_value makeBoolean(napi_env env, int8_t value) { napi_value result; napi_status status; status = napi_get_boolean(env, value != 0, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -192,7 +192,7 @@ napi_value makeInt32(napi_env env, int32_t value) { napi_value result; napi_status status; status = napi_create_int32(env, value, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -200,7 +200,7 @@ napi_value makeUInt32(napi_env env, uint32_t value) { napi_value result; napi_status status; status = napi_create_uint32(env, value, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -208,7 +208,7 @@ napi_value makeFloat32(napi_env env, float value) { napi_value result; napi_status status; status = napi_create_double(env, value, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -216,7 +216,7 @@ napi_value makePointer(napi_env env, void* value) { napi_value result; napi_status status; status = napi_create_bigint_uint64(env, static_cast(reinterpret_cast(value)), &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -224,7 +224,7 @@ napi_value makeVoid(napi_env env) { napi_value result; napi_status status; status = napi_get_undefined(env, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -232,7 +232,7 @@ napi_value makeObject(napi_env env, napi_value object) { napi_value result; napi_status status; status = napi_create_object(env, &result); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -288,7 +288,7 @@ napi_value Node_SetCallbackDispatcher(napi_env env, napi_callback_info cbinfo) { napi_value dispatcher = info[0]; napi_value result = makeVoid(env); napi_status status = napi_create_reference(env, dispatcher, 1, &g_koalaNapiCallbackDispatcher); - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); return result; } @@ -299,7 +299,7 @@ napi_value Node_CleanCallbackDispatcher(napi_env env, napi_callback_info cbinfo) if (g_koalaNapiCallbackDispatcher) { napi_status status = napi_delete_reference(env, g_koalaNapiCallbackDispatcher); g_koalaNapiCallbackDispatcher = nullptr; - NAPI_THROW_IF_FAILED(env, status, result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); } return result; } @@ -311,7 +311,7 @@ napi_value getKoalaNapiCallbackDispatcher(napi_env env) { } napi_value value; napi_status status = napi_get_reference_value(env, g_koalaNapiCallbackDispatcher, &value); - NAPI_THROW_IF_FAILED(env, status, makeVoid(env)); + KOALA_NAPI_THROW_IF_FAILED(env, status, makeVoid(env)); return value; } @@ -343,17 +343,17 @@ static napi_value InitModule(napi_env env, napi_value exports) { for (const auto &module : inst->getModules()) { if (splitModules) { status = napi_create_object(env, &target); - NAPI_THROW_IF_FAILED(env, status, exports); + KOALA_NAPI_THROW_IF_FAILED(env, status, exports); status = napi_set_named_property(env, exports, module.c_str(), target); - NAPI_THROW_IF_FAILED(env, status, exports); + KOALA_NAPI_THROW_IF_FAILED(env, status, exports); } for (const auto &impl : inst->getMethods(module)) { napi_value implFunc; status = napi_create_function(env, impl.first.c_str(), 0, impl.second, nullptr, &implFunc); - NAPI_THROW_IF_FAILED(env, status, exports); + KOALA_NAPI_THROW_IF_FAILED(env, status, exports); status = napi_set_named_property(env, target, impl.first.c_str(), implFunc); - NAPI_THROW_IF_FAILED(env, status, exports); + KOALA_NAPI_THROW_IF_FAILED(env, status, exports); } } return ProvideModuleRegisterCallback()(env, exports); diff --git a/interop/src/cpp/napi/convertors-napi.h b/interop/src/cpp/napi/convertors-napi.h index 17dc6c02268ec6162010648268a9b7b56d695b3c..4f648b3d854d363ed893103eae95eb9bf1b43e74 100644 --- a/interop/src/cpp/napi/convertors-napi.h +++ b/interop/src/cpp/napi/convertors-napi.h @@ -171,22 +171,20 @@ struct InteropTypeConverter { } while (0) // Helpers from node-addon-api -#ifndef NAPI_THROW_IF_FAILED -#define NAPI_THROW_IF_FAILED(env, status, ...) \ +#define KOALA_NAPI_THROW_IF_FAILED(env, status, ...) \ if ((status) != napi_ok) { \ const napi_extended_error_info* errorInfo; \ napi_get_last_error_info(env, &errorInfo); \ napi_throw_error(env, nullptr, errorInfo->error_message); \ return __VA_ARGS__; \ } -#define NAPI_THROW_IF_FAILED_VOID(env, status) \ +#define KOALA_NAPI_THROW_IF_FAILED_VOID(env, status) \ if ((status) != napi_ok) { \ const napi_extended_error_info* errorInfo; \ napi_get_last_error_info(env, &errorInfo); \ napi_throw_error(env, nullptr, errorInfo->error_message); \ return; \ } -#endif class CallbackInfo { public: @@ -194,11 +192,11 @@ public: size_t size = 0; napi_status status; status = napi_get_cb_info(env, info, &size, nullptr, nullptr, nullptr); - NAPI_THROW_IF_FAILED_VOID(env, status); + KOALA_NAPI_THROW_IF_FAILED_VOID(env, status); if (size > 0) { args.resize(size); // TODO statically allocate small array for common case with few arguments passed status = napi_get_cb_info(env, info, &size, args.data(), nullptr, nullptr); - NAPI_THROW_IF_FAILED_VOID(env, status); + KOALA_NAPI_THROW_IF_FAILED_VOID(env, status); } } @@ -292,7 +290,7 @@ inline ElemType* getTypedElements(napi_env env, napi_value value) { &data, &arrayBuffer, &byteOffset); - NAPI_THROW_IF_FAILED(env, status, nullptr); + KOALA_NAPI_THROW_IF_FAILED(env, status, nullptr); if (type != getNapiType()) { printf("Array type mismatch. Expected %d got %d\n", getNapiType(), type); napi_throw_error(env, nullptr, "Array type mismatch");