• 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 { creatAstFromSnippet } from "../utils/asthelper"
21import { PandaGen } from '../../src/pandagen';
22import {
23    Add2,
24    Ldobjbyname,
25    Ldobjbyvalue,
26    Sttoglobalrecord,
27    Stobjbyname,
28    Tryldglobalbyname,
29    Imm,
30    Ldai,
31    Mov,
32    Sta,
33    VReg,
34    Lda,
35    IRNode
36} from "../../src/irnodes";
37import { checkInstructions, compileMainSnippet } from "../utils/base";
38
39describe("ElementAccess", function () {
40    it('get obj["property"]', function () {
41        let insns = compileMainSnippet(`let obj;
42                                obj["property"];`);
43        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
44
45        let objReg = new VReg();
46
47        let expected = [
48            new Sttoglobalrecord(new Imm(0), 'obj'),
49            new Tryldglobalbyname(new Imm(1), 'obj'),
50            new Sta(objReg),
51            new Lda(objReg),
52            new Ldobjbyname(new Imm(2), "property"),
53        ];
54
55        insns = insns.slice(1, insns.length - 1); // cut off let obj and return.
56        expect(checkInstructions(insns, expected)).to.be.true;
57    });
58
59    it('set obj["property"]', function () {
60        let insns = compileMainSnippet(`let obj;
61                                obj["property"] = 5;`);
62        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
63        let objReg = new VReg();
64        let tempObj = new VReg();
65
66        let expected = [
67            new Sttoglobalrecord(new Imm(0), 'obj'),
68            new Tryldglobalbyname(new Imm(1), 'obj'),
69            new Sta(tempObj),
70            new Mov(objReg, tempObj),
71            new Ldai(new Imm(5)),
72            new Stobjbyname(new Imm(2), "property", objReg),
73        ];
74
75        insns = insns.slice(1, insns.length - 1); // cut off let obj and return.
76        expect(checkInstructions(insns, expected)).to.be.true;
77    });
78
79    it('get obj[1 + 2]', function () {
80        let insns = compileMainSnippet(`let obj;
81                                obj[1 + 2];`);
82        IRNode.pg = new PandaGen("", creatAstFromSnippet(``), 0, undefined);
83        let prop1Reg = new VReg();
84        let objReg = new VReg();
85        let val = new VReg();
86
87        let expected = [
88            new Sttoglobalrecord(new Imm(0), 'obj'),
89            new Tryldglobalbyname(new Imm(1), 'obj'),
90            new Sta(objReg),
91            new Ldai(new Imm(1)),
92            new Sta(prop1Reg),
93            new Ldai(new Imm(2)),
94            new Add2(new Imm(2), prop1Reg),
95            new Sta(val),
96            new Lda(val),
97            new Ldobjbyvalue(new Imm(3), objReg)
98        ];
99        insns = insns.slice(1, insns.length - 1); // cut off let obj and return.
100        expect(checkInstructions(insns, expected)).to.be.true;
101    });
102});
103