• 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 { CustomTextDecoder, CustomTextEncoder } from "@koalaui/common"
17import { Access, Exec, ExecWithLength, PtrArray, TypedArray } from "../../interop/arrays"
18import { nullptr } from "./Wrapper"
19import { Wrapper } from "../../interop/Wrapper"
20import { KPointer, KStringArrayPtr } from "../../interop"
21
22const encoder = new CustomTextEncoder()
23const decoder = new CustomTextDecoder()
24
25export function decodeToString(array: Uint8Array): string {
26    return decoder.decode(array)
27}
28
29export function encodeToData(string: string): Uint8Array {
30    return encoder.encode(string, false)
31}
32
33export function withString<R>(data: string | undefined, exec: Exec<string|null, R>): R {
34    return exec(data === undefined ? null : data)
35}
36
37export function withStringArray<R>(strings: Array<string> | undefined, exec: Exec<KStringArrayPtr, R>): R {
38    if (strings === undefined || strings.length === 0) {
39        return exec(null)
40    }
41
42    let array = encoder.encodeArray(strings)
43    return exec(array)
44}
45
46function withArray<C extends TypedArray, R>(
47    data: C | undefined,
48    exec: ExecWithLength<C | null, R>
49): R {
50    return exec(data ?? null, data?.length ?? 0)
51}
52
53export function withPtrArray<R>(data: BigUint64Array, access: Access, exec: ExecWithLength<BigUint64Array | null, R>) {
54    return exec(data ?? null, data?.length ?? 0) // TODO rethink
55}
56
57export function toPtrArray<T extends Wrapper>(data: Array<T | undefined> | undefined): BigUint64Array {
58    if (data == undefined || data.length === 0) {
59        return new BigUint64Array(0)
60    }
61    const array = new BigUint64Array(data.length)
62    for (let i = 0; i < data.length; i++) {
63        let item = data[i]
64        array[i] = item != undefined ? item.ptr as bigint : nullptr
65    }
66    return array
67}
68
69export function fromPtrArray<T extends Wrapper>(array: PtrArray, factory: (ptr: KPointer) => T) : Array<T | undefined> {
70    if (array.length === 0) {
71        return new Array<T>(0)
72    }
73    const result = new Array<T|undefined>(array.length)
74    for (let i = 0; i < array.length; i++) {
75        let ptr = array[i]
76        if (ptr == nullptr) {
77            result[i] = undefined
78        } else {
79            result[i] = factory(ptr)
80        }
81    }
82    return result
83}
84
85export function withUint8Array<T>(data: Uint8Array | undefined, access: Access, exec: ExecWithLength<Uint8Array | null, T>) {
86    return withArray(data, exec)
87}
88export function withInt8Array<T>(data: Int8Array | undefined, access: Access, exec: ExecWithLength<Int8Array | null, T>) {
89    return withArray(data, exec)
90}
91export function withUint16Array<T>(data: Uint16Array | undefined, access: Access, exec: ExecWithLength<Uint16Array | null, T>) {
92    return withArray(data, exec)
93}
94export function withInt16Array<T>(data: Int16Array | undefined, access: Access, exec: ExecWithLength<Int16Array | null, T>) {
95    return withArray(data, exec)
96}
97export function withUint32Array<T>(data: Uint32Array | undefined, access: Access, exec: ExecWithLength<Uint32Array | null, T>) {
98    return withArray(data, exec)
99}
100export function withInt32Array<T>(data: Int32Array | undefined, access: Access, exec: ExecWithLength<Int32Array | null, T>) {
101    return withArray(data, exec)
102}
103export function withFloat32Array<T>(data: Float32Array | undefined, access: Access, exec: ExecWithLength<Float32Array | null, T>) {
104    return withArray(data, exec)
105}
106export function withFloat64Array<T>(data: Float64Array | undefined, access: Access, exec: ExecWithLength<Float64Array | null, T>) {
107    return withArray(data, exec)
108}
109export function wasmHeap(): ArrayBuffer {
110    throw new Error("Unused")
111}