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 { PluginDriver } from './plugin-driver'; 17import { PluginContext, PluginExecutor } from '../../common/plugin-context'; 18import { EtsglobalRemover } from '../../common/etsglobal-remover'; 19import * as arkts from '@koalaui/libarkts'; 20 21function restartCompilerUptoState(state: arkts.Es2pandaContextState, restart: boolean): boolean { 22 try { 23 const ast: arkts.EtsScript | undefined = arkts.EtsScript.fromContext(); 24 if (!ast) { 25 return false; 26 } 27 28 if (restart) { 29 const srcText = new EtsglobalRemover().visitor(ast).dumpSrc(); 30 arkts.arktsGlobal.es2panda._DestroyContext(arkts.arktsGlobal.context); 31 arkts.arktsGlobal.compilerContext = arkts.Context.createFromString(srcText); 32 } 33 34 arkts.proceedToState(state); 35 return true; 36 } catch (e) { 37 return false; 38 } 39} 40 41function insertPlugin( 42 driver: PluginDriver, 43 plugin: PluginExecutor | undefined, 44 state: arkts.Es2pandaContextState 45): boolean { 46 arkts.proceedToState(state); 47 const pluginContext: PluginContext = driver.getPluginContext(); 48 const ast: arkts.EtsScript | undefined = arkts.EtsScript.fromContext(); 49 50 if (!ast) { 51 return false; 52 } 53 54 if (plugin) { 55 plugin.handler.apply(pluginContext); 56 } 57 return true; 58} 59 60export { restartCompilerUptoState, insertPlugin }; 61