• 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 arrays_1 = require("../../interop/arrays");
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;
30const nullptr = 0;
31// with string as array of utf8 data headed by length
32function withString(data, exec) {
33    if (data === undefined)
34        return exec(nullptr);
35    let array = encoder.encode(data, true);
36    return withUint8Array(array, arrays_1.Access.READ, exec);
37}
38exports.withString = withString;
39function withStringArray(strings, exec) {
40    if (strings === undefined || strings.length === 0) {
41        return exec(nullptr);
42    }
43    let array = encoder.encodeArray(strings);
44    return withUint8Array(array, arrays_1.Access.READ, exec);
45}
46exports.withStringArray = withStringArray;
47function withArray(data, access, exec, bytesPerElement, ctor) {
48    if (data === undefined || data.length === 0) {
49        return exec(nullptr, 0);
50    }
51    let ptr = _malloc(data.length * bytesPerElement);
52    let wasmArray = ctor(ptr, data.length);
53    if ((0, arrays_1.isRead)(access)) {
54        wasmArray.set(data);
55    }
56    let result = exec(ptr, data.length);
57    if ((0, arrays_1.isWrite)(access)) {
58        data.set(wasmArray);
59    }
60    _free(ptr);
61    return result;
62}
63function withPtrArray(data, access, exec) {
64    return withArray(data, access, exec, Uint32Array.BYTES_PER_ELEMENT, (ptr, length) => {
65        return new Uint32Array(_heaps.HEAPU8().buffer, ptr, length);
66    });
67}
68exports.withPtrArray = withPtrArray;
69function toPtrArray(data) {
70    var _a;
71    if (data === undefined || data.length === 0) {
72        return new Uint32Array(0);
73    }
74    const array = new Uint32Array(data.length);
75    for (let i = 0; i < data.length; i++) {
76        array[i] = (_a = data[i]) === null || _a === void 0 ? void 0 : _a.ptr;
77    }
78    return array;
79}
80exports.toPtrArray = toPtrArray;
81function fromPtrArray(array, factory) {
82    const result = new Array(array.length);
83    for (let i = 0; i < array.length; i++) {
84        let v = array[i];
85        if (v == 0) {
86            result[i] = undefined;
87        }
88        else {
89            result[i] = factory(v);
90        }
91    }
92    return result;
93}
94exports.fromPtrArray = fromPtrArray;
95function withUint8Array(data, access, exec) {
96    return withArray(data, access, exec, Uint8Array.BYTES_PER_ELEMENT, (ptr, length) => {
97        return new Uint8Array(_heaps.HEAPU8().buffer, ptr, length);
98    });
99}
100exports.withUint8Array = withUint8Array;
101function withInt8Array(data, access, exec) {
102    return withArray(data, access, exec, Int8Array.BYTES_PER_ELEMENT, (ptr, length) => {
103        return new Int8Array(_heaps.HEAPU8().buffer, ptr, length);
104    });
105}
106exports.withInt8Array = withInt8Array;
107function withUint16Array(data, access, exec) {
108    return withArray(data, access, exec, Uint16Array.BYTES_PER_ELEMENT, (ptr, length) => {
109        return new Uint16Array(_heaps.HEAPU8().buffer, ptr, length);
110    });
111}
112exports.withUint16Array = withUint16Array;
113function withInt16Array(data, access, exec) {
114    return withArray(data, access, exec, Int16Array.BYTES_PER_ELEMENT, (ptr, length) => {
115        return new Int16Array(_heaps.HEAPU8().buffer, ptr, length);
116    });
117}
118exports.withInt16Array = withInt16Array;
119function withUint32Array(data, access, exec) {
120    return withArray(data, access, exec, Uint32Array.BYTES_PER_ELEMENT, (ptr, length) => {
121        return new Uint32Array(_heaps.HEAPU8().buffer, ptr, length);
122    });
123}
124exports.withUint32Array = withUint32Array;
125function withInt32Array(data, access, exec) {
126    return withArray(data, access, exec, Int32Array.BYTES_PER_ELEMENT, (ptr, length) => {
127        return new Int32Array(_heaps.HEAPU8().buffer, ptr, length);
128    });
129}
130exports.withInt32Array = withInt32Array;
131function withFloat32Array(data, access, exec) {
132    return withArray(data, access, exec, Float32Array.BYTES_PER_ELEMENT, (ptr, length) => {
133        return new Float32Array(_heaps.HEAPU8().buffer, ptr, length);
134    });
135}
136exports.withFloat32Array = withFloat32Array;
137function withFloat64Array(data, access, exec) {
138    return withArray(data, access, exec, Float64Array.BYTES_PER_ELEMENT, (ptr, length) => {
139        return new Float64Array(_heaps.HEAPU8().buffer, ptr, length);
140    });
141}
142exports.withFloat64Array = withFloat64Array;
143function wasmHeap() {
144    return _heaps.HEAP32().buffer;
145}
146exports.wasmHeap = wasmHeap;
147//# sourceMappingURL=arrays.js.map