From 90204abccbe12d71c35a5ba8b695d536f0e8e9b9 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 13:44:30 +0300 Subject: [PATCH 1/8] Move callbacks where they belong --- arkoala-arkts/libarkts/native/meson.build | 191 +++++++++--------- arkoala/framework/native/meson.build | 5 +- .../src => interop/src/cpp}/callbacks.cc | 7 +- .../src => interop/src/cpp}/callbacks.h | 0 4 files changed, 105 insertions(+), 98 deletions(-) rename {arkoala/framework/native/src => interop/src/cpp}/callbacks.cc (90%) rename {arkoala/framework/native/src => interop/src/cpp}/callbacks.h (100%) diff --git a/arkoala-arkts/libarkts/native/meson.build b/arkoala-arkts/libarkts/native/meson.build index 115f7ef9e..30d4b9859 100644 --- a/arkoala-arkts/libarkts/native/meson.build +++ b/arkoala-arkts/libarkts/native/meson.build @@ -1,95 +1,96 @@ -project('Es2panda interop', 'cpp', - version: '0.1', - default_options: ['cpp_std=c++17', 'buildtype=debug'], -) - -source_dir = meson.current_source_dir() -interop_src = '../../../interop/src/cpp' -es2panda_header = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/public' -es2panda_gen = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/' -is_msvc = meson.get_compiler('cpp').get_id() == 'msvc' - -implementation = './src/es2panda_lib.cc' - -os = target_machine.system() - -sources = [ - implementation, - interop_src / 'common-interop.cc' -] -include_dirs = [ - es2panda_header, - es2panda_gen -] -library_use_name = 'Es2panda' -cflags = ['-DINTEROP_LIBRARY_NAME=' + library_use_name] -ldflags = [] -deps = [] - -if os == 'windows' - cflags += ['-DKOALA_WINDOWS'] - if is_msvc - # apply node.exe symbol loading hook - sources += [ - interop_src / 'napi/win-dynamic-node.cc' - ] - endif -endif - -if os == 'linux' - cflags += ['-DKOALA_LINUX'] -endif - -include_dirs += [ - interop_src, - interop_src / 'types', -] - -sources += [ - interop_src / 'napi/convertors-napi.cc', -] - -node_addon_api = run_command('node', '-p', 'require.resolve("node-addon-api/package.json").slice(0, -12)', check: true).stdout().strip() -node_api_headers = run_command('node', '-p', 'require.resolve("node-api-headers/package.json").slice(0, -12)', check: true).stdout().strip() - -include_dirs += [ - interop_src / 'napi', - node_api_headers / 'include', - node_addon_api, -] -cflags += [ - '-DKOALA_USE_NODE_VM', - '-DKOALA_NAPI' -] - -module_prefix = '' -module_suffix = 'node' - -shared_library( - library_use_name, - sources, - override_options: [ - 'b_lundef=false', - ], - install: true, - name_prefix: module_prefix, - name_suffix: module_suffix, - include_directories: include_dirs, - install_dir: source_dir / '..' / 'build', - cpp_args: cflags, - link_args: ldflags, - dependencies: deps -) - -playground_exec_name = 'playground_exec' - -sources = [ - './src/playground.cc' -] - -executable( - playground_exec_name, - sources, - include_directories: include_dirs, - cpp_args: cflags -) +project('Es2panda interop', 'cpp', + version: '0.1', + default_options: ['cpp_std=c++17', 'buildtype=debug'], +) + +source_dir = meson.current_source_dir() +interop_src = '../../../interop/src/cpp' +es2panda_header = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/public' +es2panda_gen = '../node_modules/@koalaui/plugin-api-panda-sdk/@panda/sdk/ohos_arm64/include/tools/es2panda/' +is_msvc = meson.get_compiler('cpp').get_id() == 'msvc' + +implementation = './src/es2panda_lib.cc' + +os = target_machine.system() + +sources = [ + implementation, + interop_src / 'common-interop.cc', + interop_src / 'callbacks.cc', +] +include_dirs = [ + es2panda_header, + es2panda_gen +] +library_use_name = 'Es2panda' +cflags = ['-DINTEROP_LIBRARY_NAME=' + library_use_name] +ldflags = [] +deps = [] + +if os == 'windows' + cflags += ['-DKOALA_WINDOWS'] + if is_msvc + # apply node.exe symbol loading hook + sources += [ + interop_src / 'napi/win-dynamic-node.cc' + ] + endif +endif + +if os == 'linux' + cflags += ['-DKOALA_LINUX'] +endif + +include_dirs += [ + interop_src, + interop_src / 'types', +] + +sources += [ + interop_src / 'napi/convertors-napi.cc', +] + +node_addon_api = run_command('node', '-p', 'require.resolve("node-addon-api/package.json").slice(0, -12)', check: true).stdout().strip() +node_api_headers = run_command('node', '-p', 'require.resolve("node-api-headers/package.json").slice(0, -12)', check: true).stdout().strip() + +include_dirs += [ + interop_src / 'napi', + node_api_headers / 'include', + node_addon_api, +] +cflags += [ + '-DKOALA_USE_NODE_VM', + '-DKOALA_NAPI' +] + +module_prefix = '' +module_suffix = 'node' + +shared_library( + library_use_name, + sources, + override_options: [ + 'b_lundef=false', + ], + install: true, + name_prefix: module_prefix, + name_suffix: module_suffix, + include_directories: include_dirs, + install_dir: source_dir / '..' / 'build', + cpp_args: cflags, + link_args: ldflags, + dependencies: deps +) + +playground_exec_name = 'playground_exec' + +sources = [ + './src/playground.cc' +] + +executable( + playground_exec_name, + sources, + include_directories: include_dirs, + cpp_args: cflags +) diff --git a/arkoala/framework/native/meson.build b/arkoala/framework/native/meson.build index b66656ac7..c5f2f4d9c 100644 --- a/arkoala/framework/native/meson.build +++ b/arkoala/framework/native/meson.build @@ -26,7 +26,8 @@ arkoala_sources = [ ] sources = [ - interop_src / 'common-interop.cc' + interop_src / 'common-interop.cc', + interop_src / 'callbacks.cc' ] include_dirs = [ @@ -250,7 +251,7 @@ shared_library(library_use_name, ) if get_option('vmloader') == true - + vmloader_cflags = ['-DKOALA_' + os.to_upper()] vmloader_ldflags = [] diff --git a/arkoala/framework/native/src/callbacks.cc b/interop/src/cpp/callbacks.cc similarity index 90% rename from arkoala/framework/native/src/callbacks.cc rename to interop/src/cpp/callbacks.cc index d2911bbfd..279bde13d 100644 --- a/arkoala/framework/native/src/callbacks.cc +++ b/interop/src/cpp/callbacks.cc @@ -53,4 +53,9 @@ KOALA_INTEROP_0(GetManagedResourceHolder, KNativePointer) KNativePointer impl_GetManagedResourceReleaser() { return 0; } -KOALA_INTEROP_0(GetManagedResourceReleaser, KNativePointer) \ No newline at end of file +KOALA_INTEROP_0(GetManagedResourceReleaser, KNativePointer) + +KNativePointer impl_GetManagerCallbackCaller() { + return 0; +} +KOALA_INTEROP_0(GetManagerCallbackCaller, KNativePointer) \ No newline at end of file diff --git a/arkoala/framework/native/src/callbacks.h b/interop/src/cpp/callbacks.h similarity index 100% rename from arkoala/framework/native/src/callbacks.h rename to interop/src/cpp/callbacks.h -- Gitee From c6ed11855522659a653e6b4c9af06ce31901c469 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 15:19:29 +0300 Subject: [PATCH 2/8] Rework --- arkoala-arkts/libarkts/native/meson.build | 1 - interop/src/cpp/callbacks.cc | 61 ----------------------- interop/src/cpp/callbacks.h | 32 ------------ interop/src/cpp/common-interop.cc | 15 ++++++ interop/src/cpp/common-interop.h | 3 ++ 5 files changed, 18 insertions(+), 94 deletions(-) delete mode 100644 interop/src/cpp/callbacks.cc delete mode 100644 interop/src/cpp/callbacks.h diff --git a/arkoala-arkts/libarkts/native/meson.build b/arkoala-arkts/libarkts/native/meson.build index 30d4b9859..24e3059ea 100644 --- a/arkoala-arkts/libarkts/native/meson.build +++ b/arkoala-arkts/libarkts/native/meson.build @@ -16,7 +16,6 @@ os = target_machine.system() sources = [ implementation, interop_src / 'common-interop.cc', - interop_src / 'callbacks.cc', ] include_dirs = [ es2panda_header, diff --git a/interop/src/cpp/callbacks.cc b/interop/src/cpp/callbacks.cc deleted file mode 100644 index 279bde13d..000000000 --- a/interop/src/cpp/callbacks.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "callbacks.h" -#include "common-interop.h" - -static std::deque callbackQueue; -void enqueueArkoalaCallback(const CallbackBuffer* event) { - callbackQueue.push_back(*event); -} - -KInt impl_GetFrontCallback(KByte* result, KInt size) { - if (((size_t)size) < sizeof(CallbackBuffer::buffer)) - throw std::invalid_argument("Expected buffer size be not less than sizeof(EventBuffer) bytes"); - - if (!callbackQueue.size()) - return 0; - - memcpy(result, callbackQueue.front().buffer, sizeof(CallbackBuffer::buffer)); - return 1; -} -KOALA_INTEROP_2(GetFrontCallback, KInt, KByte*, KInt) - -KInt impl_PopFrontCallback() { - if (!callbackQueue.size()) - return 0; - - callbackQueue.front().resourceHolder.release(); - callbackQueue.pop_front(); - return 1; -} -KOALA_INTEROP_0(PopFrontCallback, KInt) - -KNativePointer impl_GetManagedResourceHolder() { - return 0; -} -KOALA_INTEROP_0(GetManagedResourceHolder, KNativePointer) - -KNativePointer impl_GetManagedResourceReleaser() { - return 0; -} -KOALA_INTEROP_0(GetManagedResourceReleaser, KNativePointer) - -KNativePointer impl_GetManagerCallbackCaller() { - return 0; -} -KOALA_INTEROP_0(GetManagerCallbackCaller, KNativePointer) \ No newline at end of file diff --git a/interop/src/cpp/callbacks.h b/interop/src/cpp/callbacks.h deleted file mode 100644 index d4a47240d..000000000 --- a/interop/src/cpp/callbacks.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _ARKOALA_CALLBACKS_H -#define _ARKOALA_CALLBACKS_H - -#include -#include -#include "arkoala_api_generated.h" -#include "callback_kind.h" -#include "SerializerBase.h" - -struct CallbackBuffer { - CallbackKind kind; - uint8_t buffer[60 * 4]; - CallbackResourceHolder resourceHolder; -}; -void enqueueArkoalaCallback(const CallbackBuffer* event); - -#endif \ No newline at end of file diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 2de0d74f8..e83d97a50 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -335,3 +335,18 @@ void impl_NativeLog(const KStringPtr& str) { #endif } KOALA_INTEROP_V1(NativeLog, KStringPtr) + +CallCallback_t g_callbackMethod = nullptr; + +void impl_SetCallbackMethod(KPointer method) { + g_callbackMethod = reinterpret_cast(method); +} +KOALA_INTEROP_V1(SetCallbackMethod, KNativePointer) + + +void impl_CallCallback(KInt callbackKind, KNativePointer caller, KInt resourceId, KByte* args, Kint argsSize) { + if (g_callbackMethod) { + g_callbackMethod(callbackKind, caller, resourceId, argsSize); + } +} +KOALA_INTEROP_V4(CallCallback, KInt, KNativePointer, KInt, KByte*, KInt) \ No newline at end of file diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index fa0f8ae14..6a17b5346 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -44,6 +44,9 @@ std::vector makeStringVector(KStringArray strArray); std::vector makeStringVector(KNativePointerArray arr, KInt size); +// Externally provided method to deserialize and call particular callback. +typedef void (*CallCallback_t)(KInt callbackKind, KNativePointer caller, KInt resourceId, KByte* args, KInt argsSize); + // Grouped logs. void startGroupedLog(KInt kind); void stopGroupedLog(KInt kind); -- Gitee From be0766f1c88ae850ec76d4cf9e1f3a20f5f407eb Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 15:22:12 +0300 Subject: [PATCH 3/8] Restore --- arkoala/framework/native/src/callbacks.cc | 46 +++++++++++++++++++++++ arkoala/framework/native/src/callbacks.h | 32 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 arkoala/framework/native/src/callbacks.cc create mode 100644 arkoala/framework/native/src/callbacks.h diff --git a/arkoala/framework/native/src/callbacks.cc b/arkoala/framework/native/src/callbacks.cc new file mode 100644 index 000000000..59c6ec056 --- /dev/null +++ b/arkoala/framework/native/src/callbacks.cc @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "callbacks.h" +#include "common-interop.h" + +static std::deque callbackQueue; +void enqueueArkoalaCallback(const CallbackBuffer* event) { + callbackQueue.push_back(*event); +} + +KInt impl_GetFrontCallback(KByte* result, KInt size) { + if (((size_t)size) < sizeof(CallbackBuffer::buffer)) + throw std::invalid_argument("Expected buffer size be not less than sizeof(EventBuffer) bytes"); + + if (!callbackQueue.size()) + return 0; + + memcpy(result, callbackQueue.front().buffer, sizeof(CallbackBuffer::buffer)); + return 1; +} +KOALA_INTEROP_2(GetFrontCallback, KInt, KByte*, KInt) + +KInt impl_PopFrontCallback() { + if (!callbackQueue.size()) + return 0; + + callbackQueue.front().resourceHolder.release(); + callbackQueue.pop_front(); + return 1; +} +KOALA_INTEROP_0(PopFrontCallback, KInt) diff --git a/arkoala/framework/native/src/callbacks.h b/arkoala/framework/native/src/callbacks.h new file mode 100644 index 000000000..744b2386b --- /dev/null +++ b/arkoala/framework/native/src/callbacks.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _ARKOALA_CALLBACKS_H +#define _ARKOALA_CALLBACKS_H + +#include +#include +#include "arkoala_api_generated.h" +#include "callback_kind.h" +#include "SerializerBase.h" + +struct CallbackBuffer { + CallbackKind kind; + uint8_t buffer[60 * 4]; + CallbackResourceHolder resourceHolder; +}; +void enqueueArkoalaCallback(const CallbackBuffer* event); + +#endif // _ARKOALA_CALLBACKS_H \ No newline at end of file -- Gitee From 3585b2e17d3d52503c8ca87bd310aa1569be6fa5 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 16:54:43 +0300 Subject: [PATCH 4/8] Update --- arkoala/arkui/src/peers/DeserializerBase.ts | 2 +- arkoala/arkui/src/peers/SerializerBase.ts | 5 +++-- .../framework/native/src/DeserializerBase.cc | 14 +++++++++++++- arkoala/framework/native/src/DeserializerBase.h | 17 ++++++++++++++--- .../src/generated/arkoala_api_generated.h | 1 - interop/src/cpp/common-interop.cc | 14 ++++++-------- interop/src/cpp/common-interop.h | 3 ++- 7 files changed, 39 insertions(+), 17 deletions(-) diff --git a/arkoala/arkui/src/peers/DeserializerBase.ts b/arkoala/arkui/src/peers/DeserializerBase.ts index 3e00af795..acc906e54 100644 --- a/arkoala/arkui/src/peers/DeserializerBase.ts +++ b/arkoala/arkui/src/peers/DeserializerBase.ts @@ -104,7 +104,7 @@ export class DeserializerBase { return value == 1 } - readFunction(): any { + readFunction(): any // TODO: not exactly correct. const id = this.readInt32() return id diff --git a/arkoala/arkui/src/peers/SerializerBase.ts b/arkoala/arkui/src/peers/SerializerBase.ts index 92d38fde1..5f8683cbf 100644 --- a/arkoala/arkui/src/peers/SerializerBase.ts +++ b/arkoala/arkui/src/peers/SerializerBase.ts @@ -211,8 +211,9 @@ export class SerializerBase { const resourceId = ResourceManager.registerAndHold(resource) this.heldResources.push(resourceId) this.writeInt32(resourceId) - this.writePointer(nativeModule()._GetManagedResourceHolder()) - this.writePointer(nativeModule()._GetManagedResourceReleaser()) + this.writePointer(0) // holder + this.writePointer(0) // releaser + this.writePointer(0) // caller } private releaseResources() { for (const resourceId of this.heldResources) diff --git a/arkoala/framework/native/src/DeserializerBase.cc b/arkoala/framework/native/src/DeserializerBase.cc index 1c2d6389b..a4850ca8a 100644 --- a/arkoala/framework/native/src/DeserializerBase.cc +++ b/arkoala/framework/native/src/DeserializerBase.cc @@ -1,3 +1,15 @@ #include "DeserializerBase.h" -CustomDeserializer* DeserializerBase::customDeserializers = nullptr; \ No newline at end of file +CustomDeserializer* DeserializerBase::customDeserializers = nullptr; + +KNativePointer getDefaultHoldMethod() { + return reinterpret_cast(1); +} + +KNativePointer getDefaultReleaseMethod() { + return reinterpret_cast(2); +} + +KNativePointer getDefaultCallMethod() { + return reinterpret_cast(3); +} \ No newline at end of file diff --git a/arkoala/framework/native/src/DeserializerBase.h b/arkoala/framework/native/src/DeserializerBase.h index a80ca4294..514fa17bd 100644 --- a/arkoala/framework/native/src/DeserializerBase.h +++ b/arkoala/framework/native/src/DeserializerBase.h @@ -23,6 +23,10 @@ using namespace std; +void* getDefaultHoldMethod(); +void* getDefaultReleaseMethod(); +void* getDefaultCallMethod(); + inline const char *tagName(Ark_Tag tag) { switch (tag) @@ -395,12 +399,12 @@ public: strcat(result.kind, kind.c_str()); return result; } - + Ark_CallbackResource readCallbackResource() { Ark_CallbackResource result = {}; result.resourceId = readInt32(); - result.hold = reinterpret_cast(readPointer()); - result.release = reinterpret_cast(readPointer()); + result.hold = reinterpret_cast(readPointerOrDefault(getDefaultHoldMethod())); + result.release = reinterpret_cast(readPointerOrDefault(getDefaultReleaseMethod())); return result; } @@ -450,6 +454,13 @@ public: position += 8; return reinterpret_cast(value); } + Ark_NativePointer readPointerOrDefault(Ark_NativePointer defaultValue) + { + check(8); + int64_t value = *(int64_t *)(data + position); + position += 8; + return value ? reinterpret_cast(value) : defaultValue; + } Ark_Number readNumber() { check(5); diff --git a/arkoala/framework/native/src/generated/arkoala_api_generated.h b/arkoala/framework/native/src/generated/arkoala_api_generated.h index ca7257a1f..d63d6ca3b 100644 --- a/arkoala/framework/native/src/generated/arkoala_api_generated.h +++ b/arkoala/framework/native/src/generated/arkoala_api_generated.h @@ -20703,4 +20703,3 @@ struct Ark_AnyAPI { /* clang-format on */ #endif // GENERATED_FOUNDATION_ACE_FRAMEWORKS_CORE_INTERFACES_ARKOALA_API_H - diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index e83d97a50..9039d1f89 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -336,17 +336,15 @@ void impl_NativeLog(const KStringPtr& str) { } KOALA_INTEROP_V1(NativeLog, KStringPtr) -CallCallback_t g_callbackMethod = nullptr; +CallTypedCallback_t g_callbackMethod = nullptr; -void impl_SetCallbackMethod(KPointer method) { - g_callbackMethod = reinterpret_cast(method); +void setCallbackMethod(CallTypedCallback_t method) { + g_callbackMethod = method; } -KOALA_INTEROP_V1(SetCallbackMethod, KNativePointer) - -void impl_CallCallback(KInt callbackKind, KNativePointer caller, KInt resourceId, KByte* args, Kint argsSize) { +void impl_CallCallback(KInt callbackKind, KByte* args, KInt argsSize) { if (g_callbackMethod) { - g_callbackMethod(callbackKind, caller, resourceId, argsSize); + g_callbackMethod(callbackKind, args, argsSize); } } -KOALA_INTEROP_V4(CallCallback, KInt, KNativePointer, KInt, KByte*, KInt) \ No newline at end of file +KOALA_INTEROP_V3(CallCallback, KInt, KByte*, KInt) \ No newline at end of file diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index 6a17b5346..d377b2f38 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -45,7 +45,8 @@ std::vector makeStringVector(KStringArray strArray); std::vector makeStringVector(KNativePointerArray arr, KInt size); // Externally provided method to deserialize and call particular callback. -typedef void (*CallCallback_t)(KInt callbackKind, KNativePointer caller, KInt resourceId, KByte* args, KInt argsSize); +typedef void (*CallTypedCallback_t)(KInt callbackKind, KByte* args, KInt argsSize); +void setCallbackMethod(CallTypedCallback_t method); // Grouped logs. void startGroupedLog(KInt kind); -- Gitee From a41b9b39639f74577658d6b7de8bce61a7a0ee2b Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 17:02:26 +0300 Subject: [PATCH 5/8] Simplify more --- .../framework/native/src/DeserializerBase.cc | 12 ---------- interop/src/cpp/common-interop.cc | 24 +++++++++++++++---- interop/src/cpp/common-interop.h | 5 +++- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/arkoala/framework/native/src/DeserializerBase.cc b/arkoala/framework/native/src/DeserializerBase.cc index a4850ca8a..17e2d599f 100644 --- a/arkoala/framework/native/src/DeserializerBase.cc +++ b/arkoala/framework/native/src/DeserializerBase.cc @@ -1,15 +1,3 @@ #include "DeserializerBase.h" CustomDeserializer* DeserializerBase::customDeserializers = nullptr; - -KNativePointer getDefaultHoldMethod() { - return reinterpret_cast(1); -} - -KNativePointer getDefaultReleaseMethod() { - return reinterpret_cast(2); -} - -KNativePointer getDefaultCallMethod() { - return reinterpret_cast(3); -} \ No newline at end of file diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 9039d1f89..d1fa2f449 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -336,10 +336,14 @@ void impl_NativeLog(const KStringPtr& str) { } KOALA_INTEROP_V1(NativeLog, KStringPtr) -CallTypedCallback_t g_callbackMethod = nullptr; +static CallTypedCallback_t g_callMethod = nullptr; +static CallHoldCallback_t g_holdMethod = nullptr; +static CallReleaseCallback_t g_releaseMethod = nullptr; -void setCallbackMethod(CallTypedCallback_t method) { - g_callbackMethod = method; +void setCallbackMethods(CallTypedCallback_t call, CallHoldCallback_t hold, CallReleaseCallback_t release) { + g_callMethod = call; + g_holdMethod = hold; + g_releaseMethod = release; } void impl_CallCallback(KInt callbackKind, KByte* args, KInt argsSize) { @@ -347,4 +351,16 @@ void impl_CallCallback(KInt callbackKind, KByte* args, KInt argsSize) { g_callbackMethod(callbackKind, args, argsSize); } } -KOALA_INTEROP_V3(CallCallback, KInt, KByte*, KInt) \ No newline at end of file +KOALA_INTEROP_V3(CallCallback, KInt, KByte*, KInt) + +KNativePointer getDefaultHoldMethod() { + return reinterpret_cast(g_holdMethod); +} + +KNativePointer getDefaultReleaseMethod() { + return reinterpret_cast(g_releaseMethod); +} + +KNativePointer getDefaultCallMethod() { + return reinterpret_cast(g_callMethod); +} \ No newline at end of file diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index d377b2f38..1410bebec 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -46,7 +46,10 @@ std::vector makeStringVector(KNativePointerArray arr, KInt size); // Externally provided method to deserialize and call particular callback. typedef void (*CallTypedCallback_t)(KInt callbackKind, KByte* args, KInt argsSize); -void setCallbackMethod(CallTypedCallback_t method); +typedef void (*HoldCallback_t)(KInt id); +typedef void (*ReleaseCallback_t)(KInt id); + +void setCallbackMethods(CallTypedCallback_t call, HoldCallback_t hold, ReleaseCallback_t release); // Grouped logs. void startGroupedLog(KInt kind); -- Gitee From b9716644bb748cb18f206aad970499cb0d357d74 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 17:36:12 +0300 Subject: [PATCH 6/8] Simplify --- interop/src/cpp/common-interop.cc | 15 +++++++-------- interop/src/cpp/common-interop.h | 8 ++++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index d1fa2f449..55779a991 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -262,7 +262,6 @@ void impl_StopGroupedLog(KInt index) { } KOALA_INTEROP_V1(StopGroupedLog, KInt) - typedef KInt (*LoadVirtualMachine_t)(KInt vmKind, const char* appClassPath, const char* appLibPath); typedef KInt (*StartApplication_t)(); typedef KInt (*RunApplication_t)(const KInt arg0, const KInt arg1); @@ -334,21 +333,21 @@ void impl_NativeLog(const KStringPtr& str) { fflush(stdout); #endif } -KOALA_INTEROP_V1(NativeLog, KStringPtr) +KOALA_INTEROP_V1(NativeLog, KStringPtr); -static CallTypedCallback_t g_callMethod = nullptr; -static CallHoldCallback_t g_holdMethod = nullptr; -static CallReleaseCallback_t g_releaseMethod = nullptr; +CallCallback_t g_callMethod = nullptr; +HoldCallback_t g_holdMethod = nullptr; +ReleaseCallback_t g_releaseMethod = nullptr; -void setCallbackMethods(CallTypedCallback_t call, CallHoldCallback_t hold, CallReleaseCallback_t release) { +void setCallbackMethods(CallCallback_t call, HoldCallback_t hold, ReleaseCallback_t release) { g_callMethod = call; g_holdMethod = hold; g_releaseMethod = release; } void impl_CallCallback(KInt callbackKind, KByte* args, KInt argsSize) { - if (g_callbackMethod) { - g_callbackMethod(callbackKind, args, argsSize); + if (g_callMethod) { + g_callMethod(callbackKind, args, argsSize); } } KOALA_INTEROP_V3(CallCallback, KInt, KByte*, KInt) diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index 1410bebec..1ad0cde94 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -45,11 +45,11 @@ std::vector makeStringVector(KStringArray strArray); std::vector makeStringVector(KNativePointerArray arr, KInt size); // Externally provided method to deserialize and call particular callback. -typedef void (*CallTypedCallback_t)(KInt callbackKind, KByte* args, KInt argsSize); -typedef void (*HoldCallback_t)(KInt id); -typedef void (*ReleaseCallback_t)(KInt id); +typedef void (*CallCallback_t)(KInt, KByte*, KInt); +typedef void (*HoldCallback_t)(KInt); +typedef void (*ReleaseCallback_t)(KInt); -void setCallbackMethods(CallTypedCallback_t call, HoldCallback_t hold, ReleaseCallback_t release); +void setCallbackMethods(CallCallback_t call, HoldCallback_t hold, ReleaseCallback_t release); // Grouped logs. void startGroupedLog(KInt kind); -- Gitee From 84a2fed96106f05d66c16d62c1d5ede2095efced Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 17:46:22 +0300 Subject: [PATCH 7/8] Update --- arkoala/arkui/src/peers/DeserializerBase.ts | 2 +- arkoala/framework/native/src/DeserializerBase.h | 4 ---- interop/src/cpp/common-interop.h | 8 ++++++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arkoala/arkui/src/peers/DeserializerBase.ts b/arkoala/arkui/src/peers/DeserializerBase.ts index acc906e54..3e00af795 100644 --- a/arkoala/arkui/src/peers/DeserializerBase.ts +++ b/arkoala/arkui/src/peers/DeserializerBase.ts @@ -104,7 +104,7 @@ export class DeserializerBase { return value == 1 } - readFunction(): any + readFunction(): any { // TODO: not exactly correct. const id = this.readInt32() return id diff --git a/arkoala/framework/native/src/DeserializerBase.h b/arkoala/framework/native/src/DeserializerBase.h index 514fa17bd..9f0376805 100644 --- a/arkoala/framework/native/src/DeserializerBase.h +++ b/arkoala/framework/native/src/DeserializerBase.h @@ -23,10 +23,6 @@ using namespace std; -void* getDefaultHoldMethod(); -void* getDefaultReleaseMethod(); -void* getDefaultCallMethod(); - inline const char *tagName(Ark_Tag tag) { switch (tag) diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index 1ad0cde94..f7700f056 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -46,11 +46,15 @@ std::vector makeStringVector(KNativePointerArray arr, KInt size); // Externally provided method to deserialize and call particular callback. typedef void (*CallCallback_t)(KInt, KByte*, KInt); -typedef void (*HoldCallback_t)(KInt); -typedef void (*ReleaseCallback_t)(KInt); +typedef void (*HoldCallback_t)(KInt, KNativePointer); +typedef void (*ReleaseCallback_t)(KInt, KNativePointer); void setCallbackMethods(CallCallback_t call, HoldCallback_t hold, ReleaseCallback_t release); +KNativePointer getDefaultHoldMethod(); +KNativePointer getDefaultReleaseMethod(); +KNativePointer getDefaultCallMethod(); + // Grouped logs. void startGroupedLog(KInt kind); void stopGroupedLog(KInt kind); -- Gitee From d7d42e7117f453e10e8962c1e09752d423d85347 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 30 Oct 2024 18:26:27 +0300 Subject: [PATCH 8/8] WIP --- arkoala/framework/native/src/DeserializerBase.h | 17 +++++++++++++++-- interop/src/cpp/common-interop.cc | 4 ++-- interop/src/cpp/common-interop.h | 4 ---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/arkoala/framework/native/src/DeserializerBase.h b/arkoala/framework/native/src/DeserializerBase.h index 9f0376805..c060fe133 100644 --- a/arkoala/framework/native/src/DeserializerBase.h +++ b/arkoala/framework/native/src/DeserializerBase.h @@ -23,6 +23,19 @@ using namespace std; +// TODO: provide real impl +inline KNativePointer getCallbackCaller(KInt kind) { + return nullptr; +} + +inline KNativePointer getDefaultHoldMethod() { + return nullptr; +} + +inline KNativePointer getDefaultReleaseMethod() { + return nullptr; +} + inline const char *tagName(Ark_Tag tag) { switch (tag) @@ -399,8 +412,8 @@ public: Ark_CallbackResource readCallbackResource() { Ark_CallbackResource result = {}; result.resourceId = readInt32(); - result.hold = reinterpret_cast(readPointerOrDefault(getDefaultHoldMethod())); - result.release = reinterpret_cast(readPointerOrDefault(getDefaultReleaseMethod())); + result.hold = reinterpret_cast(readPointerOrDefault(&holdMethod)); + result.release = reinterpret_cast(readPointerOrDefault(&releaseMethod)); return result; } diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 55779a991..91744bc17 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -360,6 +360,6 @@ KNativePointer getDefaultReleaseMethod() { return reinterpret_cast(g_releaseMethod); } -KNativePointer getDefaultCallMethod() { - return reinterpret_cast(g_callMethod); +KNativePointer getDefaultCallMethod(KInt kind) { + return g_callMethodBySignature(kind); } \ No newline at end of file diff --git a/interop/src/cpp/common-interop.h b/interop/src/cpp/common-interop.h index f7700f056..0312a7be5 100644 --- a/interop/src/cpp/common-interop.h +++ b/interop/src/cpp/common-interop.h @@ -51,10 +51,6 @@ typedef void (*ReleaseCallback_t)(KInt, KNativePointer); void setCallbackMethods(CallCallback_t call, HoldCallback_t hold, ReleaseCallback_t release); -KNativePointer getDefaultHoldMethod(); -KNativePointer getDefaultReleaseMethod(); -KNativePointer getDefaultCallMethod(); - // Grouped logs. void startGroupedLog(KInt kind); void stopGroupedLog(KInt kind); -- Gitee