1/* 2 * Copyright (c) 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 { throwError } from './utils'; 17import { KNativePointer, nullptr } from './InteropTypes'; 18import { withString, withStringArray } from './arrays'; 19import { NativePtrDecoder, withStringResult } from './Platform'; 20import { LspDiagsNode, LspNode } from './lspNode'; 21 22export function lspData(peer: KNativePointer): LspNode { 23 return new LspDiagsNode(peer); 24} 25 26export enum VariantTypes { 27 VARIANT_INT = 0, 28 VARIANT_STRING = 1 29} 30 31export function unpackNonNullableNode<T extends LspNode>(peer: KNativePointer): T { 32 if (peer === nullptr) { 33 throwError('peer is NULLPTR (maybe you should use unpackNode)'); 34 } 35 return lspData(peer) as T; 36} 37 38export function unpackNode<T extends LspNode>(peer: KNativePointer): T | undefined { 39 if (peer === nullptr) { 40 return undefined; 41 } 42 return undefined; 43} 44 45export function unpackNodeArray<T extends LspNode>(nodesPtr: KNativePointer): readonly T[] { 46 if (nodesPtr === nullptr) { 47 throwError('nodesPtr is NULLPTR (maybe you should use unpackNodeArray)'); 48 } 49 return new NativePtrDecoder().decode(nodesPtr).map((peer: KNativePointer) => unpackNonNullableNode(peer)); 50} 51 52export function unpackString(peer: KNativePointer): string { 53 return withStringResult(peer) ?? throwError(`failed to unpack (peer shouldn't be NULLPTR)`); 54} 55 56export function passString(str: string | undefined): string { 57 if (str === undefined) { 58 return ''; 59 } 60 return withString(str, (it: string) => it); 61} 62 63export function passStringArray(strings: string[]): Uint8Array { 64 return withStringArray(strings); 65} 66