• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.wasmHeap = exports.withFloat64Array = exports.withFloat32Array = exports.withInt32Array = exports.withUint32Array = exports.withInt16Array = exports.withUint16Array = exports.withInt8Array = exports.withUint8Array = exports.fromPtrArray = exports.toPtrArray = exports.withPtrArray = exports.withStringArray = exports.withString = exports.encodeToData = exports.decodeToString = void 0;
18const common_1 = require("#koalaui/common");
19const Wrapper_1 = require("./Wrapper");
20const encoder = new common_1.CustomTextEncoder();
21const decoder = new common_1.CustomTextDecoder();
22function decodeToString(array) {
23    return decoder.decode(array);
24}
25exports.decodeToString = decodeToString;
26function encodeToData(string) {
27    return encoder.encode(string, false);
28}
29exports.encodeToData = encodeToData;
30function withString(data, exec) {
31    return exec(data === undefined ? null : data);
32}
33exports.withString = withString;
34function withStringArray(strings, exec) {
35    if (strings === undefined || strings.length === 0) {
36        return exec(null);
37    }
38    let array = encoder.encodeArray(strings);
39    return exec(array);
40}
41exports.withStringArray = withStringArray;
42function withArray(data, exec) {
43    var _a;
44    return exec(data !== null && data !== void 0 ? data : null, (_a = data === null || data === void 0 ? void 0 : data.length) !== null && _a !== void 0 ? _a : 0);
45}
46function withPtrArray(data, access, exec) {
47    var _a;
48    return exec(data !== null && data !== void 0 ? data : null, (_a = data === null || data === void 0 ? void 0 : data.length) !== null && _a !== void 0 ? _a : 0); // TODO rethink
49}
50exports.withPtrArray = withPtrArray;
51function toPtrArray(data) {
52    if (data == undefined || data.length === 0) {
53        return new BigUint64Array(0);
54    }
55    const array = new BigUint64Array(data.length);
56    for (let i = 0; i < data.length; i++) {
57        let item = data[i];
58        array[i] = item != undefined ? item.ptr : Wrapper_1.nullptr;
59    }
60    return array;
61}
62exports.toPtrArray = toPtrArray;
63function fromPtrArray(array, factory) {
64    if (array.length === 0) {
65        return new Array(0);
66    }
67    const result = new Array(array.length);
68    for (let i = 0; i < array.length; i++) {
69        let ptr = array[i];
70        if (ptr == Wrapper_1.nullptr) {
71            result[i] = undefined;
72        }
73        else {
74            result[i] = factory(ptr);
75        }
76    }
77    return result;
78}
79exports.fromPtrArray = fromPtrArray;
80function withUint8Array(data, access, exec) {
81    return withArray(data, exec);
82}
83exports.withUint8Array = withUint8Array;
84function withInt8Array(data, access, exec) {
85    return withArray(data, exec);
86}
87exports.withInt8Array = withInt8Array;
88function withUint16Array(data, access, exec) {
89    return withArray(data, exec);
90}
91exports.withUint16Array = withUint16Array;
92function withInt16Array(data, access, exec) {
93    return withArray(data, exec);
94}
95exports.withInt16Array = withInt16Array;
96function withUint32Array(data, access, exec) {
97    return withArray(data, exec);
98}
99exports.withUint32Array = withUint32Array;
100function withInt32Array(data, access, exec) {
101    return withArray(data, exec);
102}
103exports.withInt32Array = withInt32Array;
104function withFloat32Array(data, access, exec) {
105    return withArray(data, exec);
106}
107exports.withFloat32Array = withFloat32Array;
108function withFloat64Array(data, access, exec) {
109    return withArray(data, exec);
110}
111exports.withFloat64Array = withFloat64Array;
112function wasmHeap() {
113    throw new Error("Unused");
114}
115exports.wasmHeap = wasmHeap;
116//# sourceMappingURL=arrays.js.map