From 7636073e7ad6e9a945337e5291eb2b313579f0e6 Mon Sep 17 00:00:00 2001 From: kolegovilya Date: Tue, 28 Jan 2025 11:53:38 +0300 Subject: [PATCH] added arkts finalizable --- interop/a | 29 ----------------------------- interop/arktsconfig.json | 2 +- interop/src/arkts/Finalizable.ts | 32 ++++++++++++++++++++++++++++++++ interop/src/arkts/index.sts | 1 + 4 files changed, 34 insertions(+), 30 deletions(-) delete mode 100644 interop/a create mode 100644 interop/src/arkts/Finalizable.ts diff --git a/interop/a b/interop/a deleted file mode 100644 index e8532404f..000000000 --- a/interop/a +++ /dev/null @@ -1,29 +0,0 @@ -diff --git a/interop/src/interop/loadLibraries.ts b/interop/src/interop/loadLibraries.ts -index be37ebb..6c16dee 100644 ---- a/interop/src/interop/loadLibraries.ts -+++ b/interop/src/interop/loadLibraries.ts -@@ -1,16 +1,15 @@ --import * as path from "path" --import * as process from "process" --import * as os from "os" -- - const nativeModulesToLoad: Map = new Map() - const lateInitNativeModules: Map = new Map() - -+function loaderString(name: string): string { -+ if (process) -+ return `let exports = {}; process.dlopen({ exports }, path.resolve("${name}" + ".node"), 2); exports` -+ else -+ return `requireNapi(${name}, false)` -+} -+ - function loadLibrary(name: string): Record { -- const libPath = path.resolve(name + ".node"); // TODO lib dir, support es6 modules, etc. -- console.debug("Loading native library from " + libPath) -- const exports = {} -- ;(process as any).dlopen({ exports }, libPath, (os.constants as any).dlopen.RTLD_NOW) ; //TODO fix @types/node -- return exports -+ return eval(loaderString(name))() - } - - export function loadLibraries(libraries: string[]) { diff --git a/interop/arktsconfig.json b/interop/arktsconfig.json index f2491e241..7ec727ac3 100644 --- a/interop/arktsconfig.json +++ b/interop/arktsconfig.json @@ -9,7 +9,7 @@ "#common/wrappers/*": ["./../../src/napi/wrappers/*"] } }, - "include": ["src/arkts/*.sts", "src/arkts/ResourceManager.ts", "./index.ts"], + "include": ["src/arkts/*.sts", "src/arkts/ResourceManager.ts", "src/arkts/Finalizable.ts", "./index.ts"], "references": [ { "path": "../incremental/common" }, { "path": "../incremental/compat" } diff --git a/interop/src/arkts/Finalizable.ts b/interop/src/arkts/Finalizable.ts new file mode 100644 index 000000000..50b494929 --- /dev/null +++ b/interop/src/arkts/Finalizable.ts @@ -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. + */ + +import { pointer, nullptr } from "./InteropTypes" + +export class Finalizable { + public ptr: pointer + public finalizerPtr: pointer + + constructor(ptr: pointer, finalizerPtr: pointer) { + this.ptr = ptr + this.finalizerPtr = finalizerPtr + } + + release(): pointer { + let result = this.ptr + this.ptr = nullptr + return result + } +} \ No newline at end of file diff --git a/interop/src/arkts/index.sts b/interop/src/arkts/index.sts index bcd894b42..1b31a23fb 100644 --- a/interop/src/arkts/index.sts +++ b/interop/src/arkts/index.sts @@ -21,3 +21,4 @@ export * from "./NativeBuffer" export * from "./InteropNativeModule" export * from "./SerializerBase" export * from "./DeserializerBase" +export * from "./Finalizable" -- Gitee