diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 834157fe02a9145ceeb74b86e289a7fc35836fe6..a3685c876469756d7b43e347b06f464494c93e6f 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -36,29 +36,8 @@ InteropProfiler* InteropProfiler::_instance = nullptr; using std::string; #ifdef KOALA_NAPI -static Napi::Reference g_koalaNapiCallbackDispatcher; - -// TODO: shall we pass name in globalThis instead of object reference? -void impl_SetCallbackDispatcher(Napi::Object dispatcher) { - g_koalaNapiCallbackDispatcher = Napi::Reference::New(dispatcher.As(), 1); - g_koalaNapiCallbackDispatcher.SuppressDestruct(); -} -KOALA_INTEROP_V1(SetCallbackDispatcher, Napi::Object) - -void impl_CleanCallbackDispatcher() { - if (!g_koalaNapiCallbackDispatcher.IsEmpty()) { - g_koalaNapiCallbackDispatcher.Reset(); - } -} -KOALA_INTEROP_V0(CleanCallbackDispatcher) - -napi_value getKoalaNapiCallbackDispatcher() { - if (g_koalaNapiCallbackDispatcher.IsEmpty()) { - abort(); - } - return (napi_value)g_koalaNapiCallbackDispatcher.Value(); -} - +// Callback dispatcher MOVED to convertors-napi.cc. +// Let's keep platform-specific parts of the code together typedef void (*hold_t)(KInt); diff --git a/interop/src/cpp/napi/convertors-napi.cc b/interop/src/cpp/napi/convertors-napi.cc index f39b72561b59ab3f9855b6a23257a4820c45807f..4df172e882a470ba8ae293ec0df9c1b07ec4e1dd 100644 --- a/interop/src/cpp/napi/convertors-napi.cc +++ b/interop/src/cpp/napi/convertors-napi.cc @@ -13,13 +13,14 @@ * limitations under the License. */ #include +#include +#include #ifndef KOALA_INTEROP_MODULE #define KOALA_INTEROP_MODULE #endif #include "interop-logging.h" -#include "napi.h" #include "convertors-napi.h" @@ -30,62 +31,81 @@ } \ NAPI_MODULE(modname, __napi_##regfunc) -KBoolean getBoolean(Napi::Env env, Napi::Value value) { - if (value.IsBoolean()) { - return static_cast(value.As().Value()); - } - return static_cast(getInt32(env, value) != 0); +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); + return type; } -KInt getInt32(Napi::Env env, Napi::Value value) { - if (!value.IsNumber()) { - Napi::Error::New(env, "Expected Number") - .ThrowAsJavaScriptException(); - return 0; - } +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); + return result; +} - return value.As().Int32Value(); +KBoolean getBoolean(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) == napi_valuetype::napi_boolean) { + bool result = false; + napi_get_value_bool(env, value, &result); + return static_cast(result); + } + return static_cast(getInt32(env, value) != 0); } -KUInt getUInt32(Napi::Env env, Napi::Value value) { - if (!value.IsNumber()) { - Napi::Error::New(env, "Expected Number") - .ThrowAsJavaScriptException(); - return 0; - } +KInt getInt32(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_number) { + napi_throw_error(env, nullptr, "Expected Number"); + return 0; + } + int32_t result = false; + napi_get_value_int32(env, value, &result); + return static_cast(result); +} - return value.As().Uint32Value(); +KUInt getUInt32(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_number) { + napi_throw_error(env, nullptr, "Expected Number"); + return 0; + } + uint32_t result = false; + napi_get_value_uint32(env, value, &result); + return static_cast(result); } -KFloat getFloat32(Napi::Env env, Napi::Value value) { - if (!value.IsNumber()) { - Napi::Error::New(env, "Expected Number") - .ThrowAsJavaScriptException(); - return 0.0f; - } - return value.As().FloatValue(); +KFloat getFloat32(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_number) { + napi_throw_error(env, nullptr, "Expected Number"); + return 0.0f; + } + double result = false; + napi_get_value_double(env, value, &result); + return static_cast(static_cast(result)); } -KDouble getFloat64(Napi::Env env, Napi::Value value) { - if (!value.IsNumber()) { - Napi::Error::New(env, "Expected Number") - .ThrowAsJavaScriptException(); - return 0.0; - } - return value.As().DoubleValue(); +KDouble getFloat64(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_number) { + napi_throw_error(env, nullptr, "Expected Number"); + return 0.0; + } + double result = false; + napi_get_value_double(env, value, &result); + return static_cast(result); } -KStringPtr getString(Napi::Env env, Napi::Value value) { - KStringPtr result; - if (value.IsNull() || value.IsUndefined()) { +KStringPtr getString(napi_env env, napi_value value) { + KStringPtr result {}; + napi_valuetype valueType = getValueTypeChecked(env, value); + if (valueType == napi_valuetype::napi_null || valueType == napi_valuetype::napi_undefined) { return result; } - if (!value.IsString()) { - Napi::Error::New(env, "Expected String") - .ThrowAsJavaScriptException(); + if (valueType != napi_valuetype::napi_string) { + napi_throw_error(env, nullptr, "Expected String"); return result; } + size_t length = 0; napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &length); if (status != 0) return result; @@ -94,90 +114,129 @@ KStringPtr getString(Napi::Env env, Napi::Value value) { return result; } -KNativePointer getPointer(Napi::Env env, Napi::Value value) { - if (!value.IsBigInt() && !value.IsExternal()) { - Napi::Error::New(env, "cannot be coerced to pointer") - .ThrowAsJavaScriptException(); - return nullptr; +KNativePointer getPointer(napi_env env, napi_value value) { + napi_valuetype valueType = getValueTypeChecked(env, 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); + return result; } - if (value.IsExternal()) { - return value.As>().Data(); + + if (valueType != napi_valuetype::napi_bigint) { + napi_throw_error(env, nullptr, "cannot be coerced to pointer"); + return nullptr; } + bool isWithinRange = true; - uint64_t ptrU64 = value.As().Uint64Value(&isWithinRange); + uint64_t ptrU64 = 0; + napi_status status = napi_get_value_bigint_uint64(env, value, &ptrU64, &isWithinRange); + NAPI_THROW_IF_FAILED(env, status, nullptr); if (!isWithinRange) { - Napi::Error::New(env, "cannot be coerced to pointer, value is too large") - .ThrowAsJavaScriptException(); + napi_throw_error(env, nullptr, "cannot be coerced to uint64, value is too large"); return nullptr; } return reinterpret_cast(ptrU64); } -KLong getInt64(Napi::Env env, Napi::Value value) { - if (!value.IsBigInt()) { - Napi::Error::New(env, "cannot be coerced to int64") - .ThrowAsJavaScriptException(); +KLong getInt64(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_bigint) { + napi_throw_error(env, nullptr, "cannot be coerced to int64"); return -1; } bool isWithinRange = true; - int64_t ptr64 = value.As().Int64Value(&isWithinRange); + int64_t ptr64 = 0; + napi_get_value_bigint_int64(env, value, &ptr64, &isWithinRange); if (!isWithinRange) { - Napi::Error::New(env, "cannot be coerced to int64, value is too large") - .ThrowAsJavaScriptException(); + napi_throw_error(env, nullptr, "cannot be coerced to int64, value is too large"); return -1; } return static_cast(ptr64); } -Napi::Object getObject(Napi::Env env, Napi::Value value) { - if (!value.IsObject()) { - Napi::Error::New(env, "Expected Object") - .ThrowAsJavaScriptException(); - return env.Global(); - } - - return value.As(); -} - -Napi::Value makeString(const Napi::CallbackInfo& info, const KStringPtr& value) { - return Napi::String::New(info.Env(), value.isNull() ? "" : value.data()); -} - -Napi::Value makeString(const Napi::CallbackInfo& info, const std::string& value) { - return Napi::String::New(info.Env(), value); +// napi_value getObject(napi_env env, napi_value value) { +// if (getValueTypeChecked(env, value) != napi_valuetype::napi_object) { +// Napi::Error::New(env, "Expected Object") +// .ThrowAsJavaScriptException(); +// napi_value result; +// napi_status status = napi_get_global(env, &result); +// NAPI_THROW_IF_FAILED(env, status, result); +// } + +// return value; +// } + +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); + return result; } -Napi::Value makeBoolean(const Napi::CallbackInfo& info, int8_t value) { - return Napi::Number::New(info.Env(), value); +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); + return result; } -Napi::Value makeInt32(const Napi::CallbackInfo& info, int32_t value) { - return Napi::Number::New(info.Env(), value); +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); + return result; } -Napi::Value makeUInt32(const Napi::CallbackInfo& info, uint32_t value) { - return Napi::Number::New(info.Env(), value); +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); + return result; } -Napi::Value makeFloat32(const Napi::CallbackInfo& info, float value) { - return Napi::Number::New(info.Env(), value); +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); + return result; } -Napi::Value makePointer(const Napi::CallbackInfo& info, void* value) { - return makePointer(info.Env(), value); +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); + return result; } -Napi::Value makePointer(Napi::Env env, void* value) { - return Napi::BigInt::New(env, static_cast(reinterpret_cast(value))); +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); + return result; } -Napi::Value makeVoid(const Napi::CallbackInfo& info) { - return info.Env().Undefined(); +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); + return result; } -Napi::Object makeObject(const Napi::CallbackInfo& info, napi_value object) { - return Napi::Object(info.Env(), object); +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); + return result; } #if _MSC_VER >= 1932 // Visual Studio 2022 version 17.2+ @@ -219,42 +278,88 @@ const std::vector>& Exports::getMethods(cons return it->second; } +// +// Callback dispatcher +// +// TODO Should we get rid of explicit Node_* declrations and hide the naming convention behind the macro definitions? + +static napi_ref g_koalaNapiCallbackDispatcher = nullptr; + +// TODO: shall we pass name in globalThis instead of object reference? +napi_value Node_SetCallbackDispatcher(napi_env env, napi_callback_info cbinfo) { + CallbackInfo info(env, 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); + + return result; +} +MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, SetCallbackDispatcher) + +napi_value Node_CleanCallbackDispatcher(napi_env env, napi_callback_info cbinfo) { + napi_value result = makeVoid(env); + if (g_koalaNapiCallbackDispatcher) { + napi_status status = napi_delete_reference(env, g_koalaNapiCallbackDispatcher); + g_koalaNapiCallbackDispatcher = nullptr; + NAPI_THROW_IF_FAILED(env, status, result); + } + return result; +} +MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, CleanCallbackDispatcher) + +napi_value getKoalaNapiCallbackDispatcher(napi_env env) { + if (!g_koalaNapiCallbackDispatcher) { + abort(); + } + napi_value value; + napi_status status = napi_get_reference_value(env, g_koalaNapiCallbackDispatcher, &value); + NAPI_THROW_IF_FAILED(env, status, makeVoid(env)); + return value; +} + +// +// Module initialization +// + +using ModuleRegisterCallback = napi_value (*)(napi_env env, napi_value exports); + /** * Sets a new callback and returns its previous value. */ -Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterCallback value = nullptr) { - static const Napi::ModuleRegisterCallback DEFAULT_CB = [](Napi::Env env, Napi::Object exports) { return exports; }; - static Napi::ModuleRegisterCallback curCallback = DEFAULT_CB; +ModuleRegisterCallback ProvideModuleRegisterCallback(ModuleRegisterCallback value = nullptr) { + static const ModuleRegisterCallback DEFAULT_CB = [](napi_env env, napi_value exports) { return exports; }; + static ModuleRegisterCallback curCallback = DEFAULT_CB; - Napi::ModuleRegisterCallback prevCallback = curCallback; + ModuleRegisterCallback prevCallback = curCallback; curCallback = value ? value : DEFAULT_CB; return prevCallback; } static constexpr bool splitModules = true; -static Napi::Object InitModule(Napi::Env env, Napi::Object exports) { +static napi_value InitModule(napi_env env, napi_value exports) { LOG("InitModule: " QUOTE(INTEROP_LIBRARY_NAME) "\n"); Exports* inst = Exports::getInstance(); - if (splitModules) { - for (const auto &module : inst->getModules()) { - Napi::Object byModule = Napi::Object::New(env); - exports.Set(module, byModule); - for (const auto &impl : inst->getMethods(module)) { - byModule.Set( - Napi::String::New(env, impl.first.c_str()), - Napi::Function::New(env, impl.second, impl.first.c_str())); - } + napi_status status; + napi_value target = exports; + for (const auto &module : inst->getModules()) { + if (splitModules) { + status = napi_create_object(env, &target); + 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); } - } else { - for (const auto& module: inst->getModules()) { - for (const auto& impl: inst->getMethods(module)) { - exports.Set(Napi::String::New(env, impl.first.c_str()), - Napi::Function::New(env, impl.second, impl.first.c_str())); - } + + 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); + status = napi_set_named_property(env, target, impl.first.c_str(), implFunc); + NAPI_THROW_IF_FAILED(env, status, exports); } } return ProvideModuleRegisterCallback()(env, exports); } -NODE_API_MODULE_ADAPTER(INTEROP_LIBRARY_NAME, InitModule) +NAPI_MODULE(INTEROP_LIBRARY_NAME, InitModule) diff --git a/interop/src/cpp/napi/convertors-napi.h b/interop/src/cpp/napi/convertors-napi.h index 096dc871e1033c206bddf248b5f533ba5561542c..12c15084b513c90874d79c2284f1eca5f1eabfbc 100644 --- a/interop/src/cpp/napi/convertors-napi.h +++ b/interop/src/cpp/napi/convertors-napi.h @@ -19,18 +19,20 @@ #ifdef KOALA_NAPI #include +#include +#include -#include "napi.h" - +// #include "napi.h" +#include #include "koala-types.h" // TODO: switch to more generic convertors eventually. template struct InteropTypeConverter { using InteropType = T; - static T convertFrom(Napi::Env env, InteropType value) { return value; } - static InteropType convertTo(Napi::Env env, T value) { return value; } - static void release(Napi::Env env, InteropType value, T converted) {} + static T convertFrom(napi_env env, InteropType value) { return value; } + static InteropType convertTo(napi_env env, T value) { return value; } + static void release(napi_env env, InteropType value, T converted) {} }; template @@ -44,7 +46,7 @@ inline Type getArgument(napi_env env, typename InteropTypeConverter::Inter } template -inline void releaseArgument(Napi::Env env, typename InteropTypeConverter::InteropType arg, Type data) { +inline void releaseArgument(napi_env env, typename InteropTypeConverter::InteropType arg, Type data) { InteropTypeConverter::release(env, arg, data); } @@ -159,12 +161,65 @@ struct InteropTypeConverter { #define NAPI_ASSERT_INDEX(info, index, result) \ do { \ if (static_cast(index) >= info.Length()) { \ - Napi::Error::New(info.Env(), "No such element") \ - .ThrowAsJavaScriptException(); \ - return result; \ + napi_throw_error(info.Env(), nullptr, "No such element");\ + return result; \ } \ } while (0) +// Helpers from node-addon-api +#ifndef NAPI_THROW_IF_FAILED +#define 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) \ + 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: + CallbackInfo(napi_env env, napi_callback_info info) : _env(env) { + 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); + 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); + } + } + + napi_value operator[](size_t idx) const { + if (idx >= Length()) { + napi_value result; + napi_get_undefined(_env, &result); + return result; + } + return args[idx]; + } + + napi_env Env() const { + return _env; + } + + size_t Length() const { + return args.size(); + } +private: + napi_env _env; + // napi_callback_info _info; + std::vector args; +}; + template inline napi_typedarray_type getNapiType() = delete; @@ -208,17 +263,19 @@ inline napi_typedarray_type getNapiType() { return napi_biguint64_array; } +napi_valuetype getValueTypeChecked(napi_env env, napi_value value); +bool isTypedArray(napi_env env, napi_value value); + template -inline ElemType* getTypedElements(Napi::Env env, Napi::Value value) { - if (value.IsNull()) { +inline ElemType* getTypedElements(napi_env env, napi_value value) { + napi_valuetype valueType = getValueTypeChecked(env, value); + if (valueType == napi_null) { return nullptr; } - if (!value.IsTypedArray()) { - Napi::Error::New(env, "Expected TypedArray") - .ThrowAsJavaScriptException(); + if (!isTypedArray(env, value)) { + napi_throw_error(env, nullptr, "Expected TypedArray"); return nullptr; } - Napi::TypedArray array [[maybe_unused]] = value.As(); napi_value arrayBuffer; void* data = nullptr; size_t byteLength; @@ -231,130 +288,130 @@ inline ElemType* getTypedElements(Napi::Env env, Napi::Value value) { &data, &arrayBuffer, &byteOffset); - if (type != getNapiType()) { + NAPI_THROW_IF_FAILED(env, status, nullptr); + if (type != getNapiType()) { printf("Array type mismatch. Expected %d got %d\n", getNapiType(), type); - Napi::Error::New(env, "Array type mismatch") - .ThrowAsJavaScriptException(); + napi_throw_error(env, nullptr, "Array type mismatch"); return nullptr; } - if (status != 0) return nullptr; return reinterpret_cast(data); } template -inline ElemType* getTypedElements(const Napi::CallbackInfo& info, int index) { +inline ElemType* getTypedElements(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, nullptr); return getTypedElements(info.Env(), info[index]); } -inline uint8_t* getUInt8Elements(const Napi::CallbackInfo& info, int index) { +inline uint8_t* getUInt8Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline int8_t* getInt8Elements(const Napi::CallbackInfo& info, int index) { +inline int8_t* getInt8Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline uint16_t* getUInt16Elements(const Napi::CallbackInfo& info, int index) { +inline uint16_t* getUInt16Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline int16_t* getInt16Elements(const Napi::CallbackInfo& info, int index) { +inline int16_t* getInt16Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline uint32_t* getUInt32Elements(const Napi::CallbackInfo& info, int index) { +inline uint32_t* getUInt32Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline uint32_t* getUInt32Elements(Napi::Env env, Napi::Value value) { +inline uint32_t* getUInt32Elements(napi_env env, napi_value value) { return getTypedElements(env, value); } -inline int32_t* getInt32Elements(const Napi::CallbackInfo& info, int index) { +inline int32_t* getInt32Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline float* getFloat32Elements(const Napi::CallbackInfo& info, int index) { +inline float* getFloat32Elements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -inline KNativePointer* getPointerElements(const Napi::CallbackInfo& info, int index) { +inline KNativePointer* getPointerElements(const CallbackInfo& info, int index) { return getTypedElements(info, index); } -KInt getInt32(Napi::Env env, Napi::Value value); -inline int32_t getInt32(const Napi::CallbackInfo& info, int index) { +KInt getInt32(napi_env env, napi_value value); +inline int32_t getInt32(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0); return getInt32(info.Env(), info[index]); } -KUInt getUInt32(Napi::Env env, Napi::Value value); -inline uint32_t getUInt32(const Napi::CallbackInfo& info, int index) { +KUInt getUInt32(napi_env env, napi_value value); +inline uint32_t getUInt32(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0); return getUInt32(info.Env(), info[index]); } -KFloat getFloat32(Napi::Env env, Napi::Value value); -inline float getFloat32(const Napi::CallbackInfo& info, int index) { +KFloat getFloat32(napi_env env, napi_value value); +inline float getFloat32(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0.0f); return getFloat32(info.Env(), info[index]); } -KDouble getFloat64(Napi::Env env, Napi::Value value); -inline KDouble getFloat64(const Napi::CallbackInfo& info, int index) { +KDouble getFloat64(napi_env env, napi_value value); +inline KDouble getFloat64(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0.0); return getFloat64(info.Env(), info[index]); } -KStringPtr getString(Napi::Env env, Napi::Value value); -inline KStringPtr getString(const Napi::CallbackInfo& info, int index) { +KStringPtr getString(napi_env env, napi_value value); +inline KStringPtr getString(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, KStringPtr()); return getString(info.Env(), info[index]); } -void* getPointer(Napi::Env env, Napi::Value value); -inline void* getPointer(const Napi::CallbackInfo& info, int index) { +void* getPointer(napi_env env, napi_value value); +inline void* getPointer(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, nullptr); return getPointer(info.Env(), info[index]); } -KLong getInt64(Napi::Env env, Napi::Value value); -inline KLong getInt64(const Napi::CallbackInfo& info, int index) { +KLong getInt64(napi_env env, napi_value value); +inline KLong getInt64(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0); return getInt64(info.Env(), info[index]); } -KBoolean getBoolean(Napi::Env env, Napi::Value value); -inline KBoolean getBoolean(const Napi::CallbackInfo& info, int index) { +KBoolean getBoolean(napi_env env, napi_value value); +inline KBoolean getBoolean(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, false); return getBoolean(info.Env(), info[index]); } -Napi::Object getObject(Napi::Env env, Napi::Value value); -inline Napi::Object getObject(const Napi::CallbackInfo& info, int index) { - NAPI_ASSERT_INDEX(info, index, info.Env().Global()); - return getObject(info.Env(), info[index]); -} +// TODO should we keep supporting conversion to and from raw JS object values? +// napi_value getObject(napi_env env, napi_value value); +// inline napi_value getObject(const CallbackInfo& info, int index) { +// NAPI_ASSERT_INDEX(info, index, info.Env().Global()); +// return getObject(info.Env(), info[index]); +// } template -inline Type getArgument(const Napi::CallbackInfo& info, int index) = delete; +inline Type getArgument(const CallbackInfo& info, int index) = delete; template <> -inline KBoolean getArgument(const Napi::CallbackInfo& info, int index) { +inline KBoolean getArgument(const CallbackInfo& info, int index) { return getBoolean(info, index); } template <> -inline KUInt getArgument(const Napi::CallbackInfo& info, int index) { +inline KUInt getArgument(const CallbackInfo& info, int index) { return getUInt32(info, index); } template <> -inline KInt getArgument(const Napi::CallbackInfo& info, int index) { +inline KInt getArgument(const CallbackInfo& info, int index) { return getInt32(info, index); } template <> -inline KInteropNumber getArgument(const Napi::CallbackInfo& info, int index) { +inline KInteropNumber getArgument(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, { 0 }); - return getArgument((napi_env)info.Env(), (napi_value)info[index]); + return getArgument(info.Env(), info[index]); } template <> -inline KLength getArgument(const Napi::CallbackInfo& info, int index) { +inline KLength getArgument(const CallbackInfo& info, int index) { KLength result = { 0 }; NAPI_ASSERT_INDEX(info, index, result); auto value = info[index]; @@ -363,7 +420,7 @@ inline KLength getArgument(const Napi::CallbackInfo& info, int index) { if (status != 0) return result; switch (type) { case napi_number: { - result.value = value.As().FloatValue(); + result.value = getFloat32(info.Env(), value); result.unit = 1; result.type = 0; break; @@ -397,156 +454,159 @@ inline KLength getArgument(const Napi::CallbackInfo& info, int index) { template <> -inline KInteropBuffer getArgument(const Napi::CallbackInfo& info, int index) { +inline KInteropBuffer getArgument(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, { 0 }); return getArgument((napi_env)info.Env(), (napi_value)info[index]); } template <> -inline KFloat getArgument(const Napi::CallbackInfo& info, int index) { +inline KFloat getArgument(const CallbackInfo& info, int index) { return getFloat32(info, index); } template <> -inline KDouble getArgument(const Napi::CallbackInfo& info, int index) { +inline KDouble getArgument(const CallbackInfo& info, int index) { return getFloat64(info, index); } template <> -inline KNativePointer getArgument(const Napi::CallbackInfo& info, int index) { +inline KNativePointer getArgument(const CallbackInfo& info, int index) { return getPointer(info, index); } template <> -inline KLong getArgument(const Napi::CallbackInfo& info, int index) { +inline KLong getArgument(const CallbackInfo& info, int index) { return getInt64(info, index); } template <> -inline KNativePointerArray getArgument(const Napi::CallbackInfo& info, int index) { +inline KNativePointerArray getArgument(const CallbackInfo& info, int index) { return getPointerElements(info, index); } -template <> -inline Napi::Object getArgument(const Napi::CallbackInfo& info, int index) { - return getObject(info, index); -} +// template <> +// inline napi_value getArgument(const CallbackInfo& info, int index) { +// return getObject(info, index); +// } template <> -inline uint8_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline uint8_t* getArgument(const CallbackInfo& info, int index) { return getUInt8Elements(info, index); } template <> -inline const uint8_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline const uint8_t* getArgument(const CallbackInfo& info, int index) { return getUInt8Elements(info, index); } template <> -inline int8_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline int8_t* getArgument(const CallbackInfo& info, int index) { return getInt8Elements(info, index); } template <> -inline int16_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline int16_t* getArgument(const CallbackInfo& info, int index) { return getInt16Elements(info, index); } template <> -inline uint16_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline uint16_t* getArgument(const CallbackInfo& info, int index) { return getUInt16Elements(info, index); } template <> -inline int32_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline int32_t* getArgument(const CallbackInfo& info, int index) { return getInt32Elements(info, index); } template <> -inline uint32_t* getArgument(const Napi::CallbackInfo& info, int index) { +inline uint32_t* getArgument(const CallbackInfo& info, int index) { return getUInt32Elements(info, index); } template <> -inline float* getArgument(const Napi::CallbackInfo& info, int index) { +inline float* getArgument(const CallbackInfo& info, int index) { return getFloat32Elements(info, index); } template <> -inline KStringPtr getArgument(const Napi::CallbackInfo& info, int index) { +inline KStringPtr getArgument(const CallbackInfo& info, int index) { return getString(info, index); } -Napi::Value makeString(const Napi::CallbackInfo& info, KStringPtr value); -Napi::Value makeString(const Napi::CallbackInfo& info, const std::string& value); -Napi::Value makeBoolean(const Napi::CallbackInfo& info, KBoolean value); -Napi::Value makeInt32(const Napi::CallbackInfo& info, int32_t value); -Napi::Value makeUInt32(const Napi::CallbackInfo& info, uint32_t value); -Napi::Value makeFloat32(const Napi::CallbackInfo& info, float value); -Napi::Value makePointer(const Napi::CallbackInfo& info, void* value); -Napi::Value makePointer(Napi::Env env, void* value); -Napi::Value makeVoid(const Napi::CallbackInfo& info); -Napi::Object makeObject(const Napi::CallbackInfo& info, napi_value object); +napi_value makeString(napi_env env, KStringPtr value); +napi_value makeString(napi_env env, const std::string& value); +napi_value makeBoolean(napi_env env, KBoolean value); +napi_value makeInt32(napi_env env, int32_t value); +napi_value makeUInt32(napi_env env, uint32_t value); +napi_value makeFloat32(napi_env env, float value); +napi_value makePointer(napi_env env, void* value); +napi_value makeVoid(napi_env env); +// napi_value makeObject(napi_env env, napi_value object); + +inline napi_value makeVoid(const CallbackInfo& info) { + return makeVoid(info.Env()); +} template -inline Napi::Value makeResult(const Napi::CallbackInfo& info, Type value) = delete; +inline napi_value makeResult(const CallbackInfo& info, Type value) = delete; template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KBoolean value) { - return makeBoolean(info, value); +inline napi_value makeResult(const CallbackInfo& info, KBoolean value) { + return makeBoolean(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, int32_t value) { - return makeInt32(info, value); +inline napi_value makeResult(const CallbackInfo& info, int32_t value) { + return makeInt32(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, uint32_t value) { - return makeUInt32(info, value); +inline napi_value makeResult(const CallbackInfo& info, uint32_t value) { + return makeUInt32(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, float value) { - return makeFloat32(info, value); +inline napi_value makeResult(const CallbackInfo& info, float value) { + return makeFloat32(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KNativePointer value) { - return makePointer(info, value); +inline napi_value makeResult(const CallbackInfo& info, KNativePointer value) { + return makePointer(info.Env(), value); } -template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, Napi::Object value) { - return value; -} +// template <> +// inline napi_value makeResult(const CallbackInfo& info, napi_value value) { +// return value; +// } -template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, napi_value value) { - return makeObject(info, value); -} +// template <> +// inline napi_value makeResult(const CallbackInfo& info, napi_value value) { +// return makeObject(info, value); +// } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KVMObjectHandle value) { - return Napi::Value(info.Env(), InteropTypeConverter::convertTo(info.Env(), value)); +inline napi_value makeResult(const CallbackInfo& info, KVMObjectHandle value) { + return InteropTypeConverter::convertTo(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KStringPtr value) { - return Napi::Value(info.Env(), InteropTypeConverter::convertTo(info.Env(), value)); +inline napi_value makeResult(const CallbackInfo& info, KStringPtr value) { + return InteropTypeConverter::convertTo(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KInteropBuffer value) { - return Napi::Value(info.Env(), InteropTypeConverter::convertTo(info.Env(), value)); +inline napi_value makeResult(const CallbackInfo& info, KInteropBuffer value) { + return InteropTypeConverter::convertTo(info.Env(), value); } template <> -inline Napi::Value makeResult(const Napi::CallbackInfo& info, KInteropNumber value) { - return Napi::Value(info.Env(), InteropTypeConverter::convertTo(info.Env(), value)); +inline napi_value makeResult(const CallbackInfo& info, KInteropNumber value) { + return InteropTypeConverter::convertTo(info.Env(), value); } -typedef Napi::Value (*napi_type_t)(const Napi::CallbackInfo&); +typedef napi_value (*napi_type_t)(napi_env, napi_callback_info); class Exports { std::unordered_map>& getMethods(const std::string& module); }; -Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterCallback value); +// Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterCallback value); #define __QUOTE(x) #x #define QUOTE(x) __QUOTE(x) @@ -590,23 +650,26 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC #define MAKE_INTEROP_NODE_EXPORT(name) MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_0(name, Ret) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ - KOALA_MAYBE_LOG(name) \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ + KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ return makeResult(info, impl_##name()); \ } \ MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_1(name, Ret, P0) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ return makeResult(info, impl_##name(p0)); \ } \ MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_2(name, Ret, P0, P1) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ return makeResult(info, impl_##name(p0, p1)); \ @@ -614,8 +677,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_3(name, Ret, P0, P1, P2) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -624,8 +688,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_4(name, Ret, P0, P1, P2, P3) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -635,8 +700,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_5(name, Ret, P0, P1, P2, P3, P4) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -647,8 +713,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_6(name, Ret, P0, P1, P2, P3, P4, P5) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -660,8 +727,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_7(name, Ret, P0, P1, P2, P3, P4, P5, P6) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -674,8 +742,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_8(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -689,8 +758,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_9(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -705,8 +775,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_10(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -722,8 +793,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_11(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -740,8 +812,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_12(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -759,8 +832,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_13(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -779,8 +853,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_14(name, Ret, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -800,16 +875,18 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V0(name) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ impl_##name(); \ return makeVoid(info); \ } \ MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V1(name, P0) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ impl_##name(p0); \ return makeVoid(info); \ @@ -817,8 +894,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V2(name, P0, P1) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ impl_##name(p0, p1); \ @@ -827,8 +905,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V3(name, P0, P1, P2) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -838,8 +917,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V4(name, P0, P1, P2, P3) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -850,8 +930,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V5(name, P0, P1, P2, P3, P4) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -863,8 +944,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V6(name, P0, P1, P2, P3, P4, P5) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -877,8 +959,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V7(name, P0, P1, P2, P3, P4, P5, P6) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -892,8 +975,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V8(name, P0, P1, P2, P3, P4, P5, P6, P7) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -908,8 +992,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V9(name, P0, P1, P2, P3, P4, P5, P6, P7, P8) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -925,8 +1010,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V10(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -943,8 +1029,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V11(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -962,8 +1049,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V12(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -982,8 +1070,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V13(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -1003,8 +1092,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V14(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -1025,8 +1115,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_V15(name, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ P2 p2 = getArgument(info, 2); \ @@ -1048,16 +1139,18 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_0(name, Ret) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ return makeResult(info, impl_##name(ctx)); \ } \ MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_1(name, Ret, P0) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(impl_##name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ return makeResult(info, impl_##name(ctx, p0)); \ @@ -1065,8 +1158,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_2(name, Ret, P0, P1) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ @@ -1075,8 +1169,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_3(name, Ret, P0, P1, P2) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ @@ -1086,8 +1181,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_4(name, Ret, P0, P1, P2, P3) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ @@ -1098,8 +1194,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_V0(name) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ impl_##name(ctx); \ return makeVoid(info); \ @@ -1108,8 +1205,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC #define KOALA_INTEROP_CTX_V1(name, P0) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ impl_##name(ctx, p0); \ @@ -1118,8 +1216,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_V2(name, P0, P1) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ @@ -1129,8 +1228,9 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC MAKE_NODE_EXPORT(KOALA_INTEROP_MODULE, name) #define KOALA_INTEROP_CTX_V3(name, P0, P1, P2) \ - Napi::Value Node_##name(const Napi::CallbackInfo& info) { \ + napi_value Node_##name(napi_env env, napi_callback_info cbinfo) { \ KOALA_MAYBE_LOG(name) \ + CallbackInfo info(env, cbinfo); \ KVMContext ctx = reinterpret_cast((napi_env)info.Env()); \ P0 p0 = getArgument(info, 0); \ P1 p1 = getArgument(info, 1); \ @@ -1155,13 +1255,13 @@ Napi::ModuleRegisterCallback ProvideModuleRegisterCallback(Napi::ModuleRegisterC } \ } while (0) -napi_value getKoalaNapiCallbackDispatcher(); +napi_value getKoalaNapiCallbackDispatcher(napi_env env); // TODO: can/shall we cache bridge reference? #define KOALA_INTEROP_CALL_VOID(venv, id, length, args) \ { \ napi_env env = reinterpret_cast(venv); \ - napi_value bridge = getKoalaNapiCallbackDispatcher(), \ + napi_value bridge = getKoalaNapiCallbackDispatcher(env), \ global = nullptr, return_val = nullptr; \ napi_handle_scope scope = nullptr; \ napi_open_handle_scope(env, &scope); \ @@ -1182,7 +1282,7 @@ napi_value getKoalaNapiCallbackDispatcher(); #define KOALA_INTEROP_CALL_INT(venv, id, length, args) \ { \ napi_env env = reinterpret_cast(venv); \ - napi_value bridge = getKoalaNapiCallbackDispatcher(), \ + napi_value bridge = getKoalaNapiCallbackDispatcher(env), \ global = nullptr, return_val = nullptr; \ napi_handle_scope scope = nullptr; \ napi_open_handle_scope(env, &scope); \