• 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    LdaStr,
33    Callarg1,
34    Stconsttoglobalrecord,
35    Defineclasswithbuffer,
36    Ldobjbyname,
37    Definemethod,
38    Stownbyvaluewithnameset,
39    Newobjrange,
40    Ldobjbyvalue,
41    Callthis0
42} from "../../src/irnodes";
43import { checkInstructions, SnippetCompiler } from "../utils/base";
44import { creatAstFromSnippet } from "../utils/asthelper"
45import { PandaGen } from '../../src/pandagen';
46
47describe("PartiallyEmittedExpressionTest", function () {
48    it("createdPartiallyEmittedExprTest", function () {
49        let snippetCompiler = new SnippetCompiler();
50        snippetCompiler.compileAfter(`let a; let b; (a.name as string) = b`, 'test.ts');
51        IRNode.pg = new PandaGen("", creatAstFromSnippet("let a; let b; (a.name as string) = b"), 0, undefined);
52        let insns = snippetCompiler.getGlobalInsns();
53        let expected = [
54            new Lda(new VReg()),
55            new Sttoglobalrecord(new Imm(0), 'a'),
56            new Lda(new VReg()),
57            new Sttoglobalrecord(new Imm(0), 'b'),
58            new Tryldglobalbyname(new Imm(1), 'a'),
59            new Sta(new VReg()),
60            new Mov(new VReg(), new VReg()),
61            new Tryldglobalbyname(new Imm(1), 'b'),
62            new Stobjbyname(new Imm(2), "name", new VReg()),
63            new Returnundefined()
64        ];
65        expect(checkInstructions(insns, expected)).to.be.true;
66    });
67
68    it("nestingParenthesizedPartiallyEmittedExprTest", function () {
69        let snippetCompiler = new SnippetCompiler();
70        snippetCompiler.compileAfter(
71            `
72                function reindexEdgeList(e: any, u: number):void {
73                    --(((((e) as number)) as number) as number);
74                }
75            `, 'test.ts');
76        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
77        let insns = snippetCompiler.getPandaGenByName("UnitTest.reindexEdgeList").getInsns();
78        let expected = [
79            new Lda(new VReg()),
80            new Sta(new VReg()),
81            new Lda(new VReg()),
82            new Dec(new Imm(1)),
83            new Sta(new VReg()),
84            new Returnundefined()
85        ];
86        expect(checkInstructions(insns, expected)).to.be.true;
87    });
88
89    it("callPartiallyEmittedExprTest", function () {
90        let snippetCompiler = new SnippetCompiler();
91        snippetCompiler.compileAfter(
92            `
93                const sayHello = Symbol('sayHello')
94                class A {
95                    [sayHello]() {
96                    }
97                }
98                let a = new A();
99                a[sayHello]!();
100            `, 'test.ts');
101        IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined);
102        let insns = snippetCompiler.getGlobalInsns();
103        let expected = [
104            new Tryldglobalbyname(new Imm(1), 'Symbol'),
105            new Sta(new VReg()),
106            new LdaStr('sayHello'),
107            new Sta(new VReg()),
108            new Lda(new VReg()),
109            new Callarg1(new Imm(1), new VReg()),
110            new Stconsttoglobalrecord(new Imm(1), 'sayHello'),
111            new Mov(new VReg(), new VReg()),
112            new Defineclasswithbuffer(new Imm(0), 'UnitTest.#1#A', 'test_1', new Imm(0), new VReg()),
113            new Sta(new VReg()),
114            new Tryldglobalbyname(new Imm(1), 'sayHello'),
115            new Sta(new VReg()),
116            new Sta(new VReg()),
117            new Lda(new VReg()),
118            new Ldobjbyname(new Imm(1), 'prototype'),
119            new Sta(new VReg()),
120            new Lda(new VReg()),
121            new Lda(new VReg()),
122            new Definemethod(new Imm(0), 'UnitTest.#2#', new Imm(0)),
123            new Sta(new VReg()),
124            new Stownbyvaluewithnameset(new Imm(1), new VReg(), new VReg()),
125            new Lda(new VReg()),
126            new Sttoglobalrecord(new Imm(1), 'A'),
127            new Tryldglobalbyname(new Imm(1), 'A'),
128            new Sta(new VReg()),
129            new Newobjrange(new Imm(1), new Imm(1), [new VReg()]),
130            new Sttoglobalrecord(new Imm(1), 'a'),
131            new Tryldglobalbyname(new Imm(1), 'a'),
132            new Sta(new VReg()),
133            new Tryldglobalbyname(new Imm(1), 'sayHello'),
134            new Sta(new VReg()),
135            new Lda(new VReg()),
136            new Ldobjbyvalue(new Imm(1), new VReg()),
137            new Sta(new VReg()),
138            new Lda(new VReg()),
139            new Callthis0(new Imm(1), new VReg()),
140            new Returnundefined()
141        ];
142        expect(checkInstructions(insns, expected)).to.be.true;
143
144    });
145});
146