• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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
16
17import {
18    expect
19} from 'chai';
20import 'mocha';
21import { creatAstFromSnippet } from "./utils/asthelper";
22import { checkInstructions, SnippetCompiler } from "./utils/base";
23import { PandaGen } from '../src/pandagen';
24
25import {
26    Callarg1,
27    Createemptyobject,
28    Defineclasswithbuffer,
29    Definefunc,
30    Imm,
31    IRNode,
32    Isfalse,
33    Jeqz,
34    Jmp,
35    Label,
36    Lda,
37    Ldglobalvar,
38    Mov,
39    Returnundefined,
40    Sta,
41    Stglobalvar,
42    Sttoglobalrecord,
43    ThrowUndefinedifholewithname,
44    Tryldglobalbyname,
45    VReg,
46} from "../src/irnodes";
47
48describe("preserveConstEnumsTest", function () {
49    it("preserveConstEnumsInSingleFileTest", function() {
50        let snippetCompiler = new SnippetCompiler();
51        snippetCompiler.compileAfter(
52            `
53                const enum LanguageType {
54                    JS,
55                    TS,
56                    ETS,
57                }
58            `, 'test.ts');
59        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
60        let insns = snippetCompiler.getGlobalInsns();
61        let expected = [
62            new Lda(new VReg()),
63            new Stglobalvar(new Imm(0), 'LanguageType'),
64            new Definefunc(new Imm(0), 'UnitTest.#1#', new Imm(1)),
65            new Sta(new VReg()),
66            new Ldglobalvar(new Imm(0), 'LanguageType'),
67            new Sta(new VReg()),
68            new Isfalse(),
69            new Jeqz(new Label()),
70            new Createemptyobject(),
71            new Sta(new VReg()),
72            new Stglobalvar(new Imm(0), 'LanguageType'),
73            new Jmp(new Label()),
74            new Label(),
75            new Lda(new VReg()),
76            new Label(),
77            new Sta(new VReg()),
78            new Lda(new VReg()),
79            new Callarg1(new Imm(0), new VReg()),
80            new Returnundefined()
81        ];
82        expect(checkInstructions(insns, expected)).to.be.true;
83    });
84    it("preserveConstEnumsInModuleTest", function () {
85        let snippetCompiler = new SnippetCompiler();
86        snippetCompiler.compileAfter(
87            `
88                class foo {};
89                module foo {
90                    const enum LanguageType {
91                        JS,
92                        TS,
93                        ETS,
94                    }
95                };
96            `, 'test.ts');
97        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
98        let insns = snippetCompiler.getGlobalInsns();
99        let expected = [
100            new Mov(new VReg(), new VReg()),
101            new Defineclasswithbuffer(new Imm(0), 'UnitTest.#1#foo', 'test_1', new Imm(0), new VReg()),
102            new Sta(new VReg()),
103            new Lda(new VReg()),
104            new Sttoglobalrecord(new Imm(0), 'foo'),
105            new Definefunc(new Imm(0), 'UnitTest.#3#', new Imm(1)),
106            new Sta(new VReg()),
107            new Tryldglobalbyname(new Imm(0), 'foo'),
108            new Sta(new VReg()),
109            new Isfalse(),
110            new Jeqz(new Label()),
111            new Createemptyobject(),
112            new Sta(new VReg()),
113            new Sta(new VReg()),
114            new Lda(new VReg()),
115            new ThrowUndefinedifholewithname("foo"),
116            new Lda(new VReg()),
117            new Sta(new VReg()),
118            new Jmp(new Label()),
119            new Label(),
120            new Lda(new VReg()),
121            new Label(),
122            new Sta(new VReg()),
123            new Lda(new VReg()),
124            new Callarg1(new Imm(0), new VReg()),
125            new Returnundefined()
126        ];
127        expect(checkInstructions(insns, expected)).to.be.true;
128    });
129});
130
131