• 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 { CmdOptions } from '../../src/cmdOptions';
21import { DiagnosticCode, DiagnosticError } from '../../src/diagnostic';
22import { creatAstFromSnippet } from "../utils/asthelper"
23import { PandaGen } from '../../src/pandagen';
24import {
25    Add2,
26    Createarraywithbuffer,
27    Createemptyarray,
28    Createemptyobject,
29    Createobjectwithbuffer,
30    Inc,
31    Returnundefined,
32    Starrayspread,
33    Sttoglobalrecord,
34    Stownbyindex,
35    Stownbyname,
36    Stownbyvalue,
37    Tryldglobalbyname,
38    Imm,
39    Lda,
40    Ldai,
41    LdaStr,
42    Sta,
43    VReg,
44    IRNode
45} from "../../src/irnodes";
46import { checkInstructions, compileMainSnippet } from "../utils/base";
47
48describe("LiteralTest", function () {
49    it("5", function () {
50        let insns = compileMainSnippet("5");
51        let expected = [
52            new Ldai(new Imm(5)),
53            new Returnundefined()
54        ];
55        expect(checkInstructions(insns, expected)).to.be.true;
56    });
57
58    it("\"stringLiteral\"", function () {
59        let insns = compileMainSnippet("\"stringLiteral\"");
60        let expected = [
61            new LdaStr("stringLiteral"),
62            new Returnundefined()
63        ];
64        expect(checkInstructions(insns, expected)).to.be.true;
65    });
66
67    it("true", function () {
68        let insns = compileMainSnippet("true");
69        let expected = [
70            new Lda(new VReg()),
71            new Returnundefined()
72        ];
73        expect(checkInstructions(insns, expected)).to.be.true;
74    });
75
76    it("false", function () {
77        let insns = compileMainSnippet("false");
78        let expected = [
79            new Lda(new VReg()),
80            new Returnundefined()
81        ];
82        expect(checkInstructions(insns, expected)).to.be.true;
83    });
84
85    it("null", function () {
86        let insns = compileMainSnippet("null");
87        let expected = [
88            new Lda(new VReg()),
89            new Returnundefined()
90        ];
91        expect(checkInstructions(insns, expected)).to.be.true;
92    });
93
94    it("let arr = [1]", function () {
95        CmdOptions.parseUserCmd([""]);
96        let insns = compileMainSnippet("let arr = [1]");
97        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
98        let arrayInstance = new VReg();
99
100        let expected = [
101            new Createarraywithbuffer(new Imm(0), "snippet_1"),
102            new Sta(arrayInstance),
103            new Lda(arrayInstance),
104            new Sttoglobalrecord(new Imm(1), 'arr'),
105            new Returnundefined()
106        ];
107        expect(checkInstructions(insns, expected)).to.be.true;
108    });
109
110    it("let arr = []", function () {
111        let insns = compileMainSnippet("let arr = []");
112        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
113        let arrayInstance = new VReg();
114
115        let expected = [
116            new Createemptyarray(new Imm(0)),
117            new Sta(arrayInstance),
118            new Sttoglobalrecord(new Imm(1), 'arr'),
119            new Returnundefined()
120        ];
121        expect(checkInstructions(insns, expected)).to.be.true;
122    });
123
124    it("let arr = [1, 2]", function () {
125        CmdOptions.parseUserCmd([""]);
126        let insns = compileMainSnippet("let arr = [1, 2]");
127        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
128        let arrayInstance = new VReg();
129
130        let expected = [
131            new Createarraywithbuffer(new Imm(0), "snippet_1"),
132            new Sta(arrayInstance),
133            new Lda(arrayInstance),
134            new Sttoglobalrecord(new Imm(1), 'arr'),
135            new Returnundefined()
136        ];
137        insns = insns.slice(0, insns.length);
138        expect(checkInstructions(insns, expected)).to.be.true;
139    });
140
141    it("let arr = [, 1]", function () {
142        let insns = compileMainSnippet("let arr = [, 1]");
143        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
144        let arrayInstance = new VReg();
145
146        let expected = [
147            new Createemptyarray(new Imm(0)),
148            new Sta(arrayInstance),
149            new Ldai(new Imm(1)),
150            new Stownbyindex(new Imm(1), arrayInstance, new Imm(1)),
151            new Lda(arrayInstance),
152            new Sttoglobalrecord(new Imm(3), 'arr'),
153            new Returnundefined()
154        ];
155        insns = insns.slice(0, insns.length);
156        expect(checkInstructions(insns, expected)).to.be.true;
157    });
158
159    it("let arr = [1, , 3]", function () {
160        CmdOptions.parseUserCmd([""]);
161        let insns = compileMainSnippet("let arr = [1,, 3]");
162        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
163        let arrayInstance = new VReg();
164
165        let expected = [
166            new Createarraywithbuffer(new Imm(0), "snippet_1"),
167            new Sta(arrayInstance),
168            new Ldai(new Imm(3)),
169            new Stownbyindex(new Imm(1), arrayInstance, new Imm(2)),
170            new Lda(arrayInstance),
171            new Sttoglobalrecord(new Imm(3), 'arr'),
172            new Returnundefined()
173        ];
174
175        insns = insns.slice(0, insns.length);
176        expect(checkInstructions(insns, expected)).to.be.true;
177    });
178
179    it("let arr = [1, ...arr1, 3]", function () {
180        CmdOptions.parseUserCmd([""]);
181        let insns = compileMainSnippet(`let arr1 = [1, 2];
182                                let arr = [1, ...arr1, 3]`);
183        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
184        let elemIdxReg = new VReg();
185        let arrayInstance = new VReg();
186
187        let expected = [
188            new Createarraywithbuffer(new Imm(0), "snippet_1"),
189            new Sta(arrayInstance),
190            new Lda(arrayInstance),
191            new Sttoglobalrecord(new Imm(1), 'arr1'),
192
193            new Createarraywithbuffer(new Imm(2), "snippet_2"),
194            new Sta(arrayInstance),
195            new Ldai(new Imm(1)),
196            new Sta(elemIdxReg),
197            new Tryldglobalbyname(new Imm(3), 'arr1'),
198            new Starrayspread(arrayInstance, elemIdxReg),
199            new Sta(elemIdxReg),
200            new Ldai(new Imm(3)),
201            new Stownbyvalue(new Imm(4), arrayInstance, elemIdxReg),
202            new Lda(elemIdxReg),
203            new Inc(new Imm(6)),
204            new Sta(elemIdxReg),
205            new Lda(arrayInstance),
206            new Sttoglobalrecord(new Imm(7), 'arr'),
207        ];
208        insns = insns.slice(0, insns.length - 1);
209        expect(checkInstructions(insns, expected)).to.be.true;
210    });
211
212    it("let obj = {}", function () {
213        let insns = compileMainSnippet("let obj = {}");
214        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
215        let objInstance = new VReg();
216
217        let expected = [
218            new Createemptyobject(),
219            new Sta(objInstance),
220
221            new Sttoglobalrecord(new Imm(0), 'obj'),
222            new Returnundefined()
223        ];
224        expect(checkInstructions(insns, expected)).to.be.true;
225    });
226
227    it("let obj = {a: 1}", function () {
228        CmdOptions.parseUserCmd([""]);
229        let insns = compileMainSnippet("let obj = {a: 1}");
230        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
231        let objInstance = new VReg();
232        let expected = [
233            new Createobjectwithbuffer(new Imm(0), "snippet_1"),
234            new Sta(objInstance),
235            new Lda(objInstance),
236            new Sttoglobalrecord(new Imm(1), 'obj'),
237            new Returnundefined()
238        ];
239        expect(checkInstructions(insns, expected)).to.be.true;
240    });
241
242    it("let obj = {0: 1 + 2}", function () {
243        CmdOptions.parseUserCmd([""]);
244        let insns = compileMainSnippet("let obj = {0: 1 + 2}");
245        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
246        let objInstance = new VReg();
247        let lhs = new VReg();
248
249        let expected = [
250            new Createobjectwithbuffer(new Imm(0), "snippet_1"),
251            new Sta(objInstance),
252            new Ldai(new Imm(1)),
253            new Sta(lhs),
254            new Ldai(new Imm(2)),
255            new Add2(new Imm(1), lhs),
256            new Stownbyindex(new Imm(2), objInstance, new Imm(0)),
257            new Lda(objInstance),
258            new Sttoglobalrecord(new Imm(4), 'obj'),
259            new Returnundefined()
260        ];
261        expect(checkInstructions(insns, expected)).to.be.true;
262    });
263
264    it("let obj = {\"str\": 1}", function () {
265        CmdOptions.parseUserCmd([""]);
266        let insns = compileMainSnippet("let obj = {\"str\": 1}");
267        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
268        let objInstance = new VReg();
269
270        let expected = [
271            new Createobjectwithbuffer(new Imm(0), "snippet_1"),
272            new Sta(objInstance),
273            new Lda(objInstance),
274            new Sttoglobalrecord(new Imm(1), 'obj'),
275            new Returnundefined()
276        ];
277        expect(checkInstructions(insns, expected)).to.be.true;
278    });
279
280    it("let a; let obj = {a}", function () {
281        CmdOptions.parseUserCmd([""]);
282        let insns = compileMainSnippet("let a; let obj = {a}");
283        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
284        (<PandaGen>(IRNode.pg)).updateIcSize(1);
285        let objInstance = new VReg();
286
287        let expected = [
288            new Createobjectwithbuffer(new Imm(0), "snippet_1"),
289            new Sta(objInstance),
290            new Tryldglobalbyname(new Imm(1), 'a'),
291            new Stownbyname(new Imm(2), "a", objInstance),
292            new Lda(objInstance),
293            new Sttoglobalrecord(new Imm(4), 'obj')
294        ];
295        insns = insns.slice(2, insns.length - 1);
296        expect(checkInstructions(insns, expected)).to.be.true;
297    });
298
299    it("duplicate __proto__", function () {
300        let errorThrown = false;
301        try {
302            compileMainSnippet("({__proto__: null,other: null,'__proto__': null});");
303        } catch (err) {
304            expect(err instanceof DiagnosticError).to.be.true;
305            expect((<DiagnosticError>err).code).to.equal(DiagnosticCode.Duplicate_identifier_0);
306            errorThrown = true;
307        }
308        expect(errorThrown).to.be.true;
309    });
310});
311
312