• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { int32 } from "@koalaui/common"
17import {
18    Access,
19    ArrayDecoder,
20    CallbackRegistry,
21    NativeStringBase,
22    nullptr,
23    pointer,
24    providePlatformDefinedData,
25    withByteArray
26} from "@koalaui/interop"
27import { global } from "../static/global"
28
29class NativeString extends NativeStringBase {
30    constructor(ptr: pointer) {
31        super(ptr)
32    }
33    protected bytesLength(): int32 {
34        return global.interop._StringLength(this.ptr)
35    }
36    protected getData(data: Uint8Array): void {
37        withByteArray(data, Access.WRITE, (dataPtr: pointer) => {
38            global.interop._StringData(this.ptr, dataPtr, data.length)
39        })
40    }
41    close(): void {
42        global.interop._InvokeFinalizer(this.ptr, global.interop._GetStringFinalizer())
43        this.ptr = nullptr
44    }
45}
46
47providePlatformDefinedData({
48    nativeString(ptr: pointer): NativeStringBase {
49        return new NativeString(ptr)
50    },
51    nativeStringArrayDecoder(): ArrayDecoder<NativeStringBase> {
52        throw new Error("Not yet implemented")
53    },
54    callbackRegistry(): CallbackRegistry | undefined {
55        return undefined
56    }
57})
58
59export class NativePtrDecoder extends ArrayDecoder<pointer> {
60    getArraySize(blob: pointer) {
61        return global.interop._GetPtrVectorSize(blob)
62    }
63    disposeArray(blob: pointer): void {
64        // TODO
65    }
66    getArrayElement(blob: pointer, index: int32): pointer {
67        return global.interop._GetPtrVectorElement(blob, index)
68    }
69}