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 { KNativePointer as KPtr } from './InteropTypes'; 17import { global } from './global'; 18import { throwError } from './utils'; 19import { passString, passStringArray, unpackString } from './private'; 20import { isNullPtr } from './Wrapper'; 21 22export const arrayOfNullptr = new BigUint64Array([BigInt(0)]); 23 24export abstract class ArktsObject { 25 protected constructor(peer: KPtr) { 26 this.peer = peer; 27 } 28 29 readonly peer: KPtr; 30} 31 32export abstract class Node extends ArktsObject { 33 protected constructor(peer: KPtr) { 34 if (isNullPtr(peer)) { 35 throw new Error('trying to create new Node on NULLPTR'); 36 } 37 super(peer); 38 } 39 40 public get originalPeer(): KPtr { 41 return global.es2panda._AstNodeOriginalNodeConst(global.context, this.peer); 42 } 43 44 public set originalPeer(peer: KPtr) { 45 global.es2panda._AstNodeSetOriginalNode(global.context, this.peer, peer); 46 } 47 48 protected dumpMessage(): string { 49 return ``; 50 } 51 52 public dumpJson(): string { 53 return unpackString(global.es2panda._AstNodeDumpJsonConst(global.context, this.peer)); 54 } 55 56 public dumpSrc(): string { 57 return unpackString(global.es2panda._AstNodeDumpEtsSrcConst(global.context, this.peer)); 58 } 59} 60 61export class Config extends ArktsObject { 62 readonly path: string; 63 constructor(peer: KPtr, fpath: string) { 64 super(peer); 65 // TODO: wait for getter from api 66 this.path = fpath; 67 } 68 69 public toString(): string { 70 return `Config (peer = ${this.peer}, path = ${this.path})`; 71 } 72 73 static create(input: string[], fpath: string, pandaLibPath: string = '', isEditingMode: boolean = false): Config { 74 if (isEditingMode) { 75 let cfg = global.es2pandaPublic._CreateConfig(input.length, passStringArray(input), pandaLibPath); 76 return new Config(cfg, fpath); 77 } 78 if (!global.configIsInitialized()) { 79 let cfg = global.es2panda._CreateConfig(input.length, passStringArray(input), pandaLibPath); 80 global.config = cfg; 81 return new Config(cfg, fpath); 82 } else { 83 return new Config(global.config, fpath); 84 } 85 } 86} 87 88export class Context extends ArktsObject { 89 constructor(peer: KPtr) { 90 super(peer); 91 } 92 93 public toString(): string { 94 return `Context (peer = ${this.peer})`; 95 } 96 97 static createFromString(source: string): Context { 98 if (!global.configIsInitialized()) { 99 throwError(`Config not initialized`); 100 } 101 return new Context( 102 global.es2panda._CreateContextFromString(global.config, passString(source), passString(global.filePath)) 103 ); 104 } 105 106 static lspCreateFromString(source: string, filePath: string, cfg: Config): KPtr { 107 if (cfg === undefined) { 108 throwError(`Config not initialized`); 109 } 110 return global.es2pandaPublic._CreateContextFromString(cfg.peer, passString(source), passString(filePath)); 111 } 112} 113 114// ProjectConfig begins 115export interface PluginsConfig { 116 [pluginName: string]: string; 117} 118 119export interface BuildBaseConfig { 120 buildType: 'build' | 'preview' | 'hotreload' | 'coldreload'; 121 buildMode: 'Debug' | 'Release'; 122 hasMainModule: boolean; 123 arkts: object; 124 arktsGlobal: object; 125} 126 127export interface ModuleConfig { 128 packageName: string; 129 moduleType: string; 130 moduleRootPath: string; 131 sourceRoots: string[]; 132} 133 134export interface PathConfig { 135 loaderOutPath: string; 136 declgenDtsOutPath: string; 137 declgenTsOutPath: string; 138 cachePath: string; 139 buildSdkPath: string; 140 pandaSdkPath?: string; // path to panda sdk lib/bin, for local test 141 pandaStdlibPath?: string; // path to panda sdk stdlib, for local test 142 abcLinkerPath?: string; 143} 144 145export interface DeclgenConfig { 146 enableDeclgenEts2Ts: boolean; 147 declgenV1OutPath?: string; 148 declgenBridgeCodePath?: string; 149} 150 151export interface LoggerConfig { 152 getHvigorConsoleLogger?: Function; 153} 154 155export interface DependentModuleConfig { 156 packageName: string; 157 moduleName: string; 158 moduleType: string; 159 modulePath: string; 160 sourceRoots: string[]; 161 entryFile: string; 162 language: string; 163 declFilesPath?: string; 164 dependencies?: string[]; 165} 166 167export interface BuildConfig extends BuildBaseConfig, DeclgenConfig, LoggerConfig, ModuleConfig, PathConfig { 168 plugins: PluginsConfig; 169 compileFiles: string[]; 170 dependentModuleList: DependentModuleConfig[]; 171} 172// ProjectConfig ends 173 174export interface CompileFileInfo { 175 filePath: string; 176 dependentFiles: string[]; 177 abcFilePath: string; 178 arktsConfigFile: string; 179 packageName: string; 180} 181 182export interface ModuleInfo { 183 isMainModule: boolean; 184 packageName: string; 185 moduleRootPath: string; 186 moduleType: string; 187 sourceRoots: string[]; 188 entryFile: string; 189 arktsConfigFile: string; 190 compileFileInfos: CompileFileInfo[]; 191 declgenV1OutPath: string | undefined; 192 declgenBridgeCodePath: string | undefined; 193 dependencies?: string[]; 194 staticDepModuleInfos: Map<string, ModuleInfo>; 195 dynamicDepModuleInfos: Map<string, ModuleInfo>; 196 language?: string; 197 declFilesPath?: string; 198} 199