• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 * as fs from 'fs';
17import { CompileFileInfo } from './artkts-config';
18import * as arkts from '@koalaui/libarkts';
19
20function initGlobal(fileInfo: CompileFileInfo, isDebug: boolean = true): void {
21    const config = [
22        '_',
23        '--extension',
24        'ets',
25        '--arktsconfig',
26        fileInfo.arktsConfigFile,
27        '--output',
28        fileInfo.abcFilePath,
29    ];
30
31    if (isDebug) {
32        config.push('--debug-info');
33    }
34    config.push(fileInfo.filePath);
35
36    arkts.arktsGlobal.filePath = fileInfo.filePath;
37    resetConfig(config);
38
39    const source: string = fs.readFileSync(fileInfo.filePath).toString();
40    resetContext(source);
41}
42
43function resetContext(source: string): void {
44    try {
45        arkts.arktsGlobal.context;
46    } catch (e) {
47        // Do nothing
48    } finally {
49        const context: arkts.Context = arkts.Context.createFromString(source);
50        arkts.arktsGlobal.compilerContext = context;
51    }
52}
53
54function resetConfig(cmd: string[]): void {
55    try {
56        arkts.arktsGlobal.config;
57        destroyConfig();
58    } catch (e) {
59        // Do nothing
60    } finally {
61        const arkTSConfig: arkts.Config = arkts.Config.create(cmd);
62        arkts.arktsGlobal.config = arkTSConfig.peer;
63    }
64}
65
66function destroyContext(): void {
67    try {
68        arkts.arktsGlobal.clearContext();
69    } catch (e) {
70        // Do nothing
71    }
72}
73
74function destroyConfig(): void {
75    try {
76        arkts.destroyConfig(arkts.arktsGlobal.config);
77    } catch (e) {
78        // Do nothing
79    }
80}
81
82function canProceedToState(state: arkts.Es2pandaContextState): boolean {
83    const stateToSkip: arkts.Es2pandaContextState[] = [
84        arkts.Es2pandaContextState.ES2PANDA_STATE_SCOPE_INITED,
85        arkts.Es2pandaContextState.ES2PANDA_STATE_BOUND,
86        arkts.Es2pandaContextState.ES2PANDA_STATE_LOWERED,
87        arkts.Es2pandaContextState.ES2PANDA_STATE_ASM_GENERATED,
88        arkts.Es2pandaContextState.ES2PANDA_STATE_ERROR,
89    ];
90    if (state in stateToSkip) {
91        return false;
92    }
93
94    const currState = arkts.arktsGlobal.es2panda._ContextState(arkts.arktsGlobal.context);
95    return currState < state;
96}
97
98export { initGlobal, resetContext, resetConfig, destroyContext, destroyConfig, canProceedToState };
99