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 Getunmappedargs, 22 Ldobjbyindex, 23 Returnundefined, 24 Imm, 25 Lda, 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("arguments Keyword", function () { 35 it('arguments: Array-like object accessible inside functions', function () { 36 let snippetCompiler = new SnippetCompiler(); 37 snippetCompiler.compile(`function foo(a,b) {arguments[0];}`); 38 IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`function foo(a,b) {arguments[0];}`), 0, undefined); 39 let argumentsReg = new VReg(); 40 let temp1 = new VReg(); 41 let expected = [ 42 new Getunmappedargs(), 43 new Sta(argumentsReg), 44 new Lda(argumentsReg), 45 new Sta(temp1), 46 new Lda(temp1), 47 new Ldobjbyindex(new Imm(0), new Imm(0)), 48 new Returnundefined() 49 ]; 50 let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); 51 let insns = functionPg!.getInsns(); 52 53 expect(checkInstructions(insns, expected)).to.be.true; 54 }); 55 56 it('arguments as parameter shadows keyword', function () { 57 let snippetCompiler = new SnippetCompiler(); 58 snippetCompiler.compile(`function foo(arguments) {arguments[0];}`); 59 IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`function foo(arguments) {arguments[0];}`), 0, undefined); 60 let parameterArguments = new VReg(); 61 let temp1 = new VReg(); 62 let expected = [ 63 new Getunmappedargs(), 64 new Sta(new VReg()), 65 new Lda(parameterArguments), 66 new Sta(temp1), 67 new Lda(temp1), 68 new Ldobjbyindex(new Imm(0), new Imm(0)), 69 new Returnundefined() 70 ]; 71 let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); 72 let insns = functionPg!.getInsns(); 73 74 expect(checkInstructions(insns, expected)).to.be.true; 75 }); 76}); 77