• 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    Delobjprop,
22    Returnundefined,
23    Sttoglobalrecord,
24    Tryldglobalbyname,
25    Imm,
26    Lda,
27    Ldai,
28    LdaStr,
29    Sta,
30    VReg,
31    IRNode
32} from "../../src/irnodes";
33import { checkInstructions, compileMainSnippet, SnippetCompiler } from "../utils/base";
34import { creatAstFromSnippet } from "../utils/asthelper";
35import { PandaGen } from '../../src/pandagen';
36
37describe("deleteExpressionTest", function () {
38    it("deleteElementFromArray", function () {
39        let insns = compileMainSnippet("let arr = [1, 2]; delete arr[1];");
40        IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let arr = [1, 2]; delete arr[1];"), 0, undefined);
41        IRNode.pg.updateIcSize(1);
42        let objReg = new VReg();
43
44        let expected = [
45            new Sttoglobalrecord(new Imm(1), 'arr'),
46            new Tryldglobalbyname(new Imm(2), 'arr'),
47            new Sta(objReg),
48            new Ldai(new Imm(1)),
49            new Delobjprop(new VReg()),
50            new Returnundefined()
51        ];
52
53        insns = insns.slice(insns.length - 6, insns.length);
54        expect(checkInstructions(insns, expected)).to.be.true;
55    });
56
57    it("deletePropFromObj", function () {
58        // this Snippet code isn't supported by TS
59        let insns = compileMainSnippet(`let obj = {
60                                  a: 1,
61                                  b: 2};
62                                  delete obj.b;`);
63        IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let arr = [1, 2]; delete arr[1];"), 0, undefined);
64        IRNode.pg.updateIcSize(1);
65        let objReg = new VReg();
66
67        let expected = [
68            // delete obj.b;
69            new Sttoglobalrecord(new Imm(1), 'obj'),
70            new Tryldglobalbyname(new Imm(2), 'obj'),
71            new Sta(objReg),
72            new LdaStr("b"),
73            new Delobjprop(new VReg()),
74            new Returnundefined()
75        ];
76
77        insns = insns.slice(insns.length - 6, insns.length);
78        expect(checkInstructions(insns, expected)).to.be.true;
79    });
80
81    // delete function call won't use delObjProp
82    it("deleteFunctionCall", function () {
83        let snippetCompiler = new SnippetCompiler();
84        snippetCompiler.compile(`var foo = function() {
85                                  bIsFooCalled = true;
86                              };
87                              let a = delete foo();`);
88        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`var foo = function() {
89            bIsFooCalled = true;
90        };
91        let a = delete foo();`), 0, undefined);
92        IRNode.pg.updateIcSize(6);
93
94        let insns = snippetCompiler.getGlobalInsns();
95        let expected = [
96            // function call insns
97            new Lda(new VReg()),
98            new Sttoglobalrecord(new Imm(6), 'a'),
99            new Returnundefined()
100        ];
101
102        insns = insns.slice(insns.length - 3, insns.length);
103        expect(checkInstructions(insns, expected)).to.be.true;
104    });
105
106    // delete keywords won't use delObjProp
107    it("deleteKeywords", function () {
108        let insns = compileMainSnippet(`let a = delete false;`);
109        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`let a = delete false;`), 0, undefined);
110
111        let expected = [
112            new Lda(new VReg()),
113            new Lda(new VReg()),
114            new Sttoglobalrecord(new Imm(0), 'a'),
115            new Returnundefined()
116        ];
117
118        expect(checkInstructions(insns, expected)).to.be.true;
119    });
120
121    it("deleteUnresolvable", function () {
122        let insns = compileMainSnippet(`delete a;`);
123
124        let expected = [
125            new LdaStr("a"),
126            new Delobjprop(new VReg()),
127            new Returnundefined()
128        ];
129
130        expect(checkInstructions(insns, expected)).to.be.true;
131    });
132
133    it("double delete", function () {
134        let insns = compileMainSnippet(`delete delete a;`);
135
136        let expected = [
137            new LdaStr("a"),
138            new Delobjprop(new VReg()),
139            new Lda(new VReg()),
140            new Returnundefined()
141        ];
142
143        expect(checkInstructions(insns, expected)).to.be.true;
144    });
145});