• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 {
20    Createobjectwithexcludedkeys,
21    Ldobjbyname,
22    Returnundefined,
23    Imm,
24    Lda,
25    LdaStr,
26    Sta,
27    VReg,
28    IRNode
29} from "../../src/irnodes";
30import { checkInstructions, SnippetCompiler } from "../utils/base";
31import { creatAstFromSnippet } from "../utils/asthelper";
32import { PandaGen } from '../../src/pandagen';
33
34describe("object bindingPattern", function () {
35    it('object bindingPattern intialization', function () {
36        let snippetCompiler = new SnippetCompiler();
37        snippetCompiler.compile(`function foo() {const {_times: times, _values: values} = this;}`);
38        IRNode.pg = new PandaGen(
39            "foo",
40            creatAstFromSnippet(`function foo() {const {_times: times, _values: values} = this;}`), 0, undefined);
41        let thisArg = new VReg();
42        let temp = new VReg();
43        let obj = new VReg();
44        let times = new VReg();
45        let values = new VReg();
46        let expected = [
47            new Lda(thisArg),
48            new Sta(temp),
49            new Sta(obj),
50            new Lda(obj),
51            new Ldobjbyname(new Imm(0), "_times"),
52            new Sta(times),
53            new Lda(obj),
54            new Ldobjbyname(new Imm(2), "_values"),
55            new Sta(values),
56            new Lda(temp),
57            new Returnundefined()
58        ];
59        let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo");
60        let insns = functionPg!.getInsns();
61
62        expect(checkInstructions(insns, expected)).to.be.true;
63    });
64
65    it('object bindingPattern has restElement', function () {
66        let snippetCompiler = new SnippetCompiler();
67        snippetCompiler.compile(`function foo() {const {_times: times, ...values} = this;}`);
68        IRNode.pg = new PandaGen(
69            "foo",
70            creatAstFromSnippet(`function foo() {const {_times: times, ...values} = this;}`), 0, undefined);
71        let thisArg = new VReg();
72        let temp = new VReg();
73        let obj = new VReg();
74        let times = new VReg();
75        let values = new VReg();
76        let arg = new VReg();
77        let expected = [
78            new Lda(thisArg),
79            new Sta(temp),
80            new Sta(obj),
81            new Lda(obj),
82            new Ldobjbyname(new Imm(0), "_times"),
83            new Sta(times),
84            new LdaStr("_times"),
85            new Sta(arg),
86            new Createobjectwithexcludedkeys(new Imm(0), obj, [arg]),
87            new Sta(values),
88            new Lda(temp),
89            new Returnundefined()
90        ];
91        let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo");
92        let insns = functionPg!.getInsns();
93
94        expect(checkInstructions(insns, expected)).to.be.true;
95    });
96});
97