• 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 {
22    Dynamicimport,
23    Callarg0,
24    Callarg1,
25    Apply,
26    Createarraywithbuffer,
27    Createemptyarray,
28    Ldobjbyname,
29    Ldobjbyvalue,
30    Returnundefined,
31    Starrayspread,
32    Stconsttoglobalrecord,
33    Sttoglobalrecord,
34    Tryldglobalbyname,
35    Imm,
36    Lda,
37    Ldai,
38    Sta,
39    VReg,
40    Callthis0,
41    LdaStr,
42    IRNode
43} from "../../src/irnodes";
44import { checkInstructions, compileMainSnippet } from "../utils/base";
45import { creatAstFromSnippet } from "../utils/asthelper";
46import { PandaGen } from '../../src/pandagen';
47
48describe("CallTest", function () {
49    it("no arg call of a global standalone function", function () {
50        let insns = compileMainSnippet(`foo();`);
51        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`foo();`), 0, undefined);
52        let arg0 = new VReg();
53        let expected = [
54            new Tryldglobalbyname(new Imm(0), "foo"),
55            new Sta(arg0),
56            new Lda(arg0),
57            new Callarg0(new Imm(1)),
58
59            new Returnundefined()
60        ];
61        expect(checkInstructions(insns, expected)).to.be.true;
62    });
63
64    it("one arg call of a global standalone function", function () {
65        let insns = compileMainSnippet(`let i = 5;foo(i);`);
66        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`let i = 5;foo(i);`), 0, undefined);
67        let arg0 = new VReg();
68        let arg2 = new VReg();
69        let expected = [
70            new Ldai(new Imm(5)),
71            new Sttoglobalrecord(new Imm(0), 'i'),
72            new Tryldglobalbyname(new Imm(1), "foo"),
73            new Sta(arg0),
74            new Tryldglobalbyname(new Imm(2), 'i'),
75            new Sta(arg2),
76            new Lda(arg0),
77            new Callarg1(new Imm(3), arg2),
78            new Returnundefined()
79        ];
80        expect(checkInstructions(insns, expected)).to.be.true;
81    });
82
83    it("call method", function () {
84        let insns = compileMainSnippet(`
85      Foo.method();
86      `);
87        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`Foo.method();`), 0, undefined);
88        let obj = new VReg();
89        let arg0 = new VReg();
90        let arg1 = new VReg();
91        let expected = [
92            new Tryldglobalbyname(new Imm(0), "Foo"),
93            new Sta(arg0),
94            new Lda(arg0),
95            new Ldobjbyname(new Imm(1), "method"),
96            new Sta(arg1),
97            new Lda(arg0),
98            new Callthis0(new Imm(3), obj),
99
100            new Returnundefined()
101        ];
102        expect(checkInstructions(insns, expected)).to.be.true;
103    });
104
105    it("spread element call of a global standalone function", function () {
106        CmdOptions.parseUserCmd([""]);
107        let insns = compileMainSnippet(`
108       const args = [1, 2];
109       myFunction(...args);
110      `);
111        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`const args = [1, 2];myFunction(...args);`), 0, undefined);
112        let arg0 = new VReg();
113        let globalEnv = new VReg();
114        let lengthReg = new VReg();
115        let arrayInstance = new VReg();
116
117        let expected = [
118            new Createarraywithbuffer(new Imm(0), "snippet_1"),
119            new Sta(arrayInstance),
120            new Lda(arrayInstance),
121            new Stconsttoglobalrecord(new Imm(1), 'args'),
122
123            new Tryldglobalbyname(new Imm(2), "myFunction"),
124            new Sta(arg0),
125
126            new Createemptyarray(new Imm(3), ),
127            new Sta(arrayInstance),
128            new Ldai(new Imm(0)),
129            new Sta(lengthReg),
130            new Tryldglobalbyname(new Imm(4), 'args'),
131            new Starrayspread(arrayInstance, lengthReg),
132            new Sta(lengthReg),
133            new Lda(arrayInstance),
134
135            new Lda(arg0),
136            new Apply(new Imm(5), globalEnv, arrayInstance),
137            new Returnundefined()
138        ];
139        expect(checkInstructions(insns, expected)).to.be.true;
140    });
141
142    it("call by element access", function () {
143        let insns = compileMainSnippet(`
144            Foo[method]();
145            `);
146        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`Foo[method]();`), 0, undefined);
147        let obj = new VReg();
148        let prop = new VReg();
149        let arg0 = new VReg();
150        let arg1 = new VReg();
151        let expected = [
152            new Tryldglobalbyname(new Imm(0), "Foo"),
153            new Sta(arg0),
154            new Tryldglobalbyname(new Imm(1), "method"),
155            new Sta(prop),
156            new Lda(prop),
157            new Ldobjbyvalue(new Imm(2), arg0),
158            new Sta(arg1),
159            new Lda(arg1),
160            new Callthis0(new Imm(4), arg0),
161
162            new Returnundefined()
163        ];
164        expect(checkInstructions(insns, expected)).to.be.true;
165    });
166
167    it("import calls", function () {
168        let insns = compileMainSnippet(`import('./test.js');`);
169        let expected = [
170            new LdaStr("./test.js"),
171            new Dynamicimport(),
172            new Returnundefined()
173        ];
174        expect(checkInstructions(insns, expected)).to.be.true;
175    })
176});
177