• 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 { Context, Config } from './types';
17import { global } from './global';
18import { throwError } from './utils';
19import { Es2pandaContextState } from './generated/Es2pandaEnums';
20import { withStringResult } from './Platform';
21import { KBoolean, KNativePointer, KPointer } from './InteropTypes';
22
23export class DriverHelper {
24  constructor(filePath: string, cmd: string[]) {
25    this._filePath = filePath;
26    global.filePath = this._filePath;
27    this._cfg = Config.create(cmd, filePath);
28  }
29  private _ctx: Context | undefined;
30  private _cfg: Config;
31  private _filePath: string;
32
33  public createCtx(source: string): KPointer {
34    this._ctx = Context.createFromString(source);
35    return this._ctx.peer;
36  }
37
38  public toString(): string {
39    return `DriverHelper (filepath = ${this._filePath}, config = ${this._cfg}, context = ${this._ctx})`;
40  }
41
42  public proceedToState(state: Es2pandaContextState): void {
43    if (this._ctx === undefined) {
44      throwError('Trying to proceed to state while cts is undefined');
45    }
46    if (state <= global.es2panda._ContextState(this._ctx.peer)) {
47      return;
48    }
49
50    try {
51      global.es2panda._ProceedToState(this._ctx.peer, state);
52      if (global.es2panda._ContextState(this._ctx.peer) === Es2pandaContextState.ES2PANDA_STATE_ERROR) {
53        const errMsg = withStringResult(global.es2panda._ContextErrorMessage(this._ctx.peer));
54        if (errMsg === undefined) {
55          throwError(`Couldn't get context error msg`);
56        } else {
57          throwError('Failed proceed to: ' + Es2pandaContextState[state] + '\n' + errMsg);
58        }
59      }
60    } catch (e) {
61      global.es2panda._DestroyContext(this._ctx.peer);
62      throw e;
63    }
64  }
65
66  public finalize(): void {
67    if (this._cfg === undefined) {
68      throwError('Call finalize before initialized config');
69    }
70    if (this._ctx === undefined) {
71      throwError('Call finalize before initialized context');
72    }
73    global.es2panda._DestroyContext(this._ctx.peer);
74    global.es2panda._DestroyConfig(this._cfg.peer);
75    this._ctx = undefined;
76    global.destroyCfg();
77  }
78
79  public generateTsDecl(declOutPath: string, etsOutPath: string, exportAll: boolean, recordFile: string): void {
80    let exportAll_: KBoolean = exportAll ? 1 : 0;
81    global.es2panda._GenerateTsDeclarationsFromContext(this._cfg.peer, declOutPath, etsOutPath, exportAll_, recordFile);
82  }
83}
84
85export class LspDriverHelper {
86  public createCfg(cmd: string[], filePath: string, pandaLibPath: string): Config {
87    return Config.create(cmd, filePath, pandaLibPath, true);
88  }
89
90  public createCtx(source: string, filePath: string, cfg: Config): KNativePointer {
91    return Context.lspCreateFromString(source, filePath, cfg);
92  }
93
94  public proceedToState(ctx: KNativePointer, state: Es2pandaContextState): void {
95    if (ctx === undefined) {
96      throwError('Trying to proceed to state while cts is undefined');
97    }
98    if (state <= global.es2pandaPublic._ContextState(ctx)) {
99      return;
100    }
101
102    try {
103      global.es2pandaPublic._ProceedToState(ctx, state);
104    } catch (e) {
105      global.es2pandaPublic._DestroyContext(ctx);
106      throw e;
107    }
108  }
109
110  public destroyContext(ctx: KNativePointer): void {
111    if (ctx === undefined) {
112      return;
113    }
114    global.es2pandaPublic._DestroyContext(ctx);
115  }
116
117  public destroyConfig(cfg: Config): void {
118    if (cfg === undefined) {
119      return;
120    }
121    global.es2pandaPublic._DestroyConfig(cfg.peer);
122  }
123}
124