• 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    Createemptyarray,
22    Ldobjbyname,
23    Newobjrange,
24    Newobjapply,
25    Returnundefined,
26    Starrayspread,
27    Tryldglobalbyname,
28    Imm,
29    Lda,
30    Ldai,
31    Mov,
32    Sta,
33    VReg,
34    IRNode
35} from "../../src/irnodes";
36import { checkInstructions, compileMainSnippet } from "../utils/base";
37import { creatAstFromSnippet } from "../utils/asthelper"
38import { PandaGen } from '../../src/pandagen';
39
40describe("NewTest", function () {
41    it("new Object", function () {
42        let insns = compileMainSnippet("new Object");
43        IRNode.pg = new PandaGen("", creatAstFromSnippet("new Object"), 0, undefined);
44        let arg0 = new VReg();
45        let expected = [
46            new Tryldglobalbyname(new Imm(0), "Object"),
47            new Sta(arg0),
48
49            new Newobjrange(new Imm(1), new Imm(1), [arg0]),
50
51            new Returnundefined()
52        ];
53
54        expect(checkInstructions(insns, expected)).to.be.true;
55    });
56
57    it("new Object()", function () {
58        let insns = compileMainSnippet("new Object()");
59        IRNode.pg = new PandaGen("", creatAstFromSnippet("new Object()"), 0, undefined);
60        let arg0 = new VReg();
61
62        let expected = [
63            new Tryldglobalbyname(new Imm(0), "Object"),
64            new Sta(arg0),
65
66            new Newobjrange(new Imm(1), new Imm(1), [arg0]),
67
68            new Returnundefined()
69        ];
70
71        expect(checkInstructions(insns, expected)).to.be.true;
72    });
73
74    it("new Object(2)", function () {
75        let insns = compileMainSnippet("new Object(2)");
76        IRNode.pg = new PandaGen("", creatAstFromSnippet("new Object(2)"), 0, undefined);
77        let arg0 = new VReg();
78        let arg1 = new VReg();
79
80        let expected = [
81            new Tryldglobalbyname(new Imm(0), "Object"),
82            new Sta(arg0),
83
84            new Ldai(new Imm(2)),
85            new Sta(arg1),
86
87            new Newobjrange(new Imm(1), new Imm(2), [arg0, arg1]),
88
89            new Returnundefined()
90        ];
91
92        expect(checkInstructions(insns, expected)).to.be.true;
93    });
94
95    it("new obj.ctor()", function () {
96        let insns = compileMainSnippet("let obj; new obj.ctor()");
97        IRNode.pg = new PandaGen("", creatAstFromSnippet("let obj; new obj.ctor()"), 0, undefined);
98        IRNode.pg.updateIcSize(1);
99        let obj = new VReg();
100        let arg0 = new VReg();
101        let temp = new VReg();
102
103        let expected = [
104            new Tryldglobalbyname(new Imm(0), 'obj'),
105            new Sta(temp),
106
107            new Lda(obj),
108            new Ldobjbyname(new Imm(1), "ctor"),
109            new Sta(arg0),
110
111            new Newobjrange(new Imm(3), new Imm(1), [arg0]),
112        ];
113
114        insns = insns.slice(2, insns.length - 1);
115        expect(checkInstructions(insns, expected)).to.be.true;
116    });
117
118    it("new Object(...args)", function () {
119        let insns = compileMainSnippet(`new Object(...args);`);
120        IRNode.pg = new PandaGen("", creatAstFromSnippet("new Object(...args);"), 0, undefined);
121        let arg0 = new VReg();
122        let elemIdxReg = new VReg();
123        let arrayInstance = new VReg();
124
125        let expected = [
126            new Tryldglobalbyname(new Imm(0), "Object"),
127            new Sta(arg0),
128
129            new Createemptyarray(new Imm(1)),
130            new Sta(arrayInstance),
131            new Ldai(new Imm(0)),
132            new Sta(elemIdxReg),
133
134            new Tryldglobalbyname(new Imm(2), "args"),
135            new Starrayspread(arrayInstance, elemIdxReg),
136            new Sta(elemIdxReg),
137            new Lda(arrayInstance),
138
139            new Newobjapply(new Imm(3), arg0),
140
141            new Returnundefined()
142        ];
143
144        expect(checkInstructions(insns, expected)).to.be.true;
145    });
146});
147