• 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
16import {
17    expect
18} from 'chai';
19import 'mocha';
20import {
21    Returnundefined,
22    Sttoglobalrecord,
23    Tryldglobalbyname,
24    Lda,
25    VReg,
26    Imm,
27    IRNode,
28    Sta,
29    Mov,
30    Stobjbyname,
31    Dec,
32    Definefunc,
33    Label,
34    Stglobalvar,
35    Newobjrange,
36    Createobjectwithbuffer,
37    Ldglobalvar,
38    Eq,
39    Jeqz,
40    Ldai,
41    Jmp,
42    Trystglobalbyname,
43    LdaStr,
44    Defineclasswithbuffer,
45    Stlexvar,
46    Createemptyarray,
47    Callarg1,
48    Stownbyindex,
49    Callargs2
50} from "../src/irnodes";
51import { checkInstructions, SnippetCompiler } from "./utils/base";
52import { creatAstFromSnippet } from "./utils/asthelper"
53import { PandaGen } from '../src/pandagen';
54
55describe("AutoGeneratedIdNameTest", function () {
56    it("noNameForAutoGeneratedId", function () {
57        let snippetCompiler = new SnippetCompiler();
58        snippetCompiler.compileAfter(
59            `
60                let kk = new Date();
61                let list;
62                let hh = {name:2};
63
64                function fu(a) {
65                    return a;
66                }
67                let uu = fu(((list = kk) == null ? void 0 : hh) ?? []);
68            `, 'test.js');
69        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
70        let insns = snippetCompiler.getGlobalInsns();
71        let expected = [
72            new Definefunc(new Imm(0), 'UnitTest.fu', new Imm(1)),
73            new Stglobalvar(new Imm(0), 'fu'),
74            new Tryldglobalbyname(new Imm(0), 'Date'),
75            new Sta(new VReg()),
76            new Newobjrange(new Imm(0), new Imm(1), [new VReg()]),
77            new Sttoglobalrecord(new Imm(0), 'kk'),
78            new Lda(new VReg()),
79            new Sttoglobalrecord(new Imm(0), 'list'),
80            new Createobjectwithbuffer(new Imm(0), 'test_0'),
81            new Sta(new VReg()),
82            new Lda(new VReg()),
83            new Sttoglobalrecord(new Imm(0), 'hh'),
84            new Ldglobalvar(new Imm(0), 'fu'),
85            new Sta(new VReg()),
86            new Tryldglobalbyname(new Imm(0), 'kk'),
87            new Trystglobalbyname(new Imm(0), 'list'),
88            new Sta(new VReg()),
89            new Lda(new VReg()),
90            new Eq(new Imm(0), new VReg()),
91            new Jeqz(new Label()),
92            new Ldai(new Imm(0)),
93            new Lda(new VReg()),
94            new Jmp(new Label()),
95            new Label(),
96            new Tryldglobalbyname(new Imm(0), 'hh'),
97            new Label(),
98            new Stglobalvar(new Imm(0), '#a')
99        ];
100        expect(checkInstructions(insns.slice(0, 27), expected)).to.be.true;
101    });
102
103    it("sameNameForDifferentNodeWithSameOrigin", function () {
104        let snippetCompiler = new SnippetCompiler();
105        snippetCompiler.compileAfter(
106            `
107                enum hiddenItemAction {
108                    str = "string"
109                }
110                let a = hiddenItemAction.str;
111            `, 'test.js');
112        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
113        let insns = snippetCompiler.getPandaGenByName("UnitTest.#1#").getInsns();
114        let expected = [
115            new Lda(new VReg()),
116            new Sta(new VReg()),
117            new Mov(new VReg(), new VReg()),
118            new LdaStr('string'),
119            new Stobjbyname(new Imm(0), 'str', new VReg()),
120            new Returnundefined()
121        ];
122        expect(checkInstructions(insns, expected)).to.be.true;
123    });
124
125    it("allocUniqueNameForAutoGenratedIdWithName", function () {
126        let snippetCompiler = new SnippetCompiler();
127        snippetCompiler.compileAfter(
128            `
129                function ccclass(name: string) {
130                    return function ccclass(target :any) {
131                        target.prototype.name = name;
132                    }
133                }
134
135                @ccclass('cc.ClickEvent')
136                class EventHandler {
137                    public static emitEvent(event: EventHandler): boolean {
138                        if (!(event instanceof EventHandler)) {
139                            return false;
140                        }
141                        return true;
142                    }
143                }
144            `, 'test.ts');
145        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
146        let insns = snippetCompiler.getGlobalInsns();
147        let expected = [
148            new Definefunc(new Imm(0), 'UnitTest.#2#ccclass', new Imm(1)),
149            new Stglobalvar(new Imm(0), 'ccclass'),
150            new Mov(new VReg(), new VReg()),
151            new Defineclasswithbuffer(new Imm(0), 'UnitTest.#3#EventHandler', 'test_1', new Imm(0), new VReg()),
152            new Sta(new VReg()),
153            new Lda(new VReg()),
154            new Sta(new VReg()),
155            new Lda(new VReg()),
156            new Stlexvar(new Imm(0), new Imm(0)),
157            new Lda(new VReg()),
158            new Stglobalvar(new Imm(0), 'EventHandler#a'),
159            new Sttoglobalrecord(new Imm(0), 'EventHandler'),
160            new Tryldglobalbyname(new Imm(0), '__decorate'),
161            new Sta(new VReg()),
162            new Createemptyarray(new Imm(0)),
163            new Sta(new VReg()),
164            new Ldglobalvar(new Imm(0), 'ccclass'),
165            new Sta(new VReg()),
166            new LdaStr('cc.ClickEvent'),
167            new Sta(new VReg()),
168            new Lda(new VReg()),
169            new Callarg1(new Imm(0), new VReg()),
170            new Stownbyindex(new Imm(0), new VReg(), new Imm(0)),
171            new Lda(new VReg()),
172            new Sta(new VReg()),
173            new Tryldglobalbyname(new Imm(0), 'EventHandler'),
174            new Sta(new VReg()),
175            new Lda(new VReg()),
176            new Callargs2(new Imm(0), new VReg(), new VReg()),
177            new Stglobalvar(new Imm(0), 'EventHandler#a'),
178            new Trystglobalbyname(new Imm(0), 'EventHandler'),
179            new Returnundefined()
180        ];
181        expect(checkInstructions(insns, expected)).to.be.true;
182    });
183});
184