• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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    EcmaAdd2dyn,
22    EcmaAshr2dyn,
23    EcmaDiv2dyn,
24    EcmaExpdyn,
25    EcmaMod2dyn,
26    EcmaMul2dyn,
27    EcmaReturnundefined,
28    EcmaShl2dyn,
29    EcmaShr2dyn,
30    EcmaStlettoglobalrecord,
31    EcmaSub2dyn,
32    EcmaTryldglobalbyname,
33    EcmaTrystglobalbyname,
34    Imm,
35    LdaiDyn,
36    ResultType,
37    StaDyn,
38    VReg
39} from "../../src/irnodes";
40import { checkInstructions, compileMainSnippet } from "../utils/base";
41
42describe("OperationEqualTest", function () {
43    it("plusEqual", function () {
44        let insns = compileMainSnippet("let a = 2;\n" +
45            "a += 3;");
46        let lhs = new VReg();
47
48        let expected = [
49            new LdaiDyn(new Imm(2)),
50            new EcmaStlettoglobalrecord('a'),
51            new EcmaTryldglobalbyname('a'),
52            new StaDyn(lhs),
53            new LdaiDyn(new Imm(3)),
54            new EcmaAdd2dyn(lhs),
55            new EcmaTrystglobalbyname('a'),
56            new EcmaReturnundefined()
57        ];
58        expect(checkInstructions(insns, expected)).to.be.true;
59    });
60
61    it("minusEqual", function () {
62        let insns = compileMainSnippet("let a = 5;\n" +
63            "a -= 7;");
64        let lhs = new VReg();
65
66        let expected = [
67            new LdaiDyn(new Imm(5)),
68            new EcmaStlettoglobalrecord('a'),
69            new EcmaTryldglobalbyname('a'),
70            new StaDyn(lhs),
71            new LdaiDyn(new Imm(7)),
72            new EcmaSub2dyn(lhs),
73            new EcmaTrystglobalbyname('a'),
74            new EcmaReturnundefined()
75        ];
76        expect(checkInstructions(insns, expected)).to.be.true;
77    });
78
79    it("asteriskEqual", function () {
80        let insns = compileMainSnippet("let a = 2;\n" +
81            "a *= 4;");
82        let lhs = new VReg();
83
84        let expected = [
85            new LdaiDyn(new Imm(2)),
86            new EcmaStlettoglobalrecord('a'),
87            new EcmaTryldglobalbyname('a'),
88            new StaDyn(lhs),
89            new LdaiDyn(new Imm(4)),
90            new EcmaMul2dyn(lhs),
91            new EcmaTrystglobalbyname('a'),
92            new EcmaReturnundefined()
93        ];
94        expect(checkInstructions(insns, expected)).to.be.true;
95    });
96
97    it("AsteriskAsteriskEqualsToken", function () {
98        let insns = compileMainSnippet("let a = 2;\n" +
99            "a **= 3;");
100        let lhs = new VReg();
101
102        let expected = [
103            new LdaiDyn(new Imm(2)),
104            new EcmaStlettoglobalrecord('a'),
105            new EcmaTryldglobalbyname('a'),
106            new StaDyn(lhs),
107            new LdaiDyn(new Imm(3)),
108            new EcmaExpdyn(lhs),
109            new EcmaTrystglobalbyname('a'),
110            new EcmaReturnundefined()
111        ]
112        expect(checkInstructions(insns, expected)).to.be.true;
113    });
114
115    it("slashEqual", function () {
116        let insns = compileMainSnippet("let a = 5;\n" +
117            "a /= 3;");
118        let lhs = new VReg();
119
120        let expected = [
121            new LdaiDyn(new Imm(5)),
122            new EcmaStlettoglobalrecord('a'),
123            new EcmaTryldglobalbyname('a'),
124            new StaDyn(lhs),
125            new LdaiDyn(new Imm(3)),
126            new EcmaDiv2dyn(lhs),
127            new EcmaTrystglobalbyname('a'),
128            new EcmaReturnundefined()
129        ];
130        expect(checkInstructions(insns, expected)).to.be.true;
131    });
132
133    it("percentEqual", function () {
134        let insns = compileMainSnippet("let a = 15;\n" +
135            "a %= 7;");
136        let lhs = new VReg();
137
138        let expected = [
139            new LdaiDyn(new Imm(15)),
140            new EcmaStlettoglobalrecord('a'),
141            new EcmaTryldglobalbyname('a'),
142            new StaDyn(lhs),
143            new LdaiDyn(new Imm(7)),
144            new EcmaMod2dyn(lhs),
145            new EcmaTrystglobalbyname('a'),
146            new EcmaReturnundefined()
147        ];
148        expect(checkInstructions(insns, expected)).to.be.true;
149    });
150
151    it("lessThanLessThanEqual", function () {
152        let insns = compileMainSnippet("let a = 8;\n" +
153            "a <<= 3;");
154        let lhs = new VReg();
155
156        let expected = [
157            new LdaiDyn(new Imm(8)),
158            new EcmaStlettoglobalrecord('a'),
159            new EcmaTryldglobalbyname('a'),
160            new StaDyn(lhs),
161            new LdaiDyn(new Imm(3)),
162            new EcmaShl2dyn(lhs),
163            new EcmaTrystglobalbyname('a'),
164            new EcmaReturnundefined()
165        ];
166        expect(checkInstructions(insns, expected)).to.be.true;
167    });
168
169    it("greaterThanGreaterThanEqual", function () {
170        let insns = compileMainSnippet("let a = 4;\n" +
171            "a >>= 1;");
172        let lhs = new VReg();
173
174        let expected = [
175            new LdaiDyn(new Imm(4)),
176            new EcmaStlettoglobalrecord('a'),
177            new EcmaTryldglobalbyname('a'),
178            new StaDyn(lhs),
179            new LdaiDyn(new Imm(1)),
180            new EcmaShr2dyn(lhs),
181            new EcmaTrystglobalbyname('a'),
182            new EcmaReturnundefined()
183        ];
184        expect(checkInstructions(insns, expected)).to.be.true;
185    });
186
187    it("greaterThanGreaterThanGreaterThanEqual", function () {
188        let insns = compileMainSnippet("let a = 8;\n" +
189            "a >>>= 2;");
190        let lhs = new VReg();
191
192        let expected = [
193            new LdaiDyn(new Imm(8)),
194            new EcmaStlettoglobalrecord('a'),
195            new EcmaTryldglobalbyname('a'),
196            new StaDyn(lhs),
197            new LdaiDyn(new Imm(2)),
198            new EcmaAshr2dyn(lhs),
199            new EcmaTrystglobalbyname('a'),
200            new EcmaReturnundefined()
201        ];
202        expect(checkInstructions(insns, expected)).to.be.true;
203    });
204});
205