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 { ArktsObject } from './ArktsObject'; 17import { global } from '../static/global'; 18import { throwError, filterSource } from '../../utils'; 19import { passString, passStringArray } from '../utilities/private'; 20import { KBoolean, KInt, KNativePointer } from '@koalaui/interop'; 21import { AstNode } from './AstNode'; 22import { Program } from './Program'; 23export class Context extends ArktsObject { 24 constructor(peer: KNativePointer) { 25 super(peer); 26 } 27 28 static createFromString(source: string): Context { 29 if (!global.configIsInitialized()) { 30 throwError(`Config not initialized`); 31 } 32 return new Context( 33 global.es2panda._CreateContextFromString(global.config, passString(source), passString(global.filePath)) 34 ); 35 } 36 static destroyAndRecreate(ast: AstNode): Context { 37 console.log('[TS WRAPPER] DESTROY AND RECREATE'); 38 const source = filterSource(ast.dumpSrc()); 39 global.es2panda._DestroyContext(global.context); 40 global.compilerContext = Context.createFromString(source); 41 42 return new Context(global.context); 43 } 44 45 static createContextGenerateAbcForExternalSourceFiles( 46 filenames: string[]): Context { 47 if (!global.configIsInitialized()) { 48 throwError(`Config not initialized`); 49 } 50 return new Context( 51 global.es2panda._CreateContextGenerateAbcForExternalSourceFiles(global.config, filenames.length, passStringArray(filenames)) 52 ); 53 } 54 55 get program(): Program { 56 return new Program(global.es2panda._ContextProgram(this.peer)); 57 } 58} 59