• 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    EcmaCallarg1dyn,
22    EcmaLdobjbyname,
23    EcmaReturnundefined,
24    EcmaTryldglobalbyname,
25    EcmaTypeofdyn,
26    Imm,
27    LdaiDyn,
28    LdaStr,
29    ResultType,
30    StaDyn,
31    VReg
32} from "../../src/irnodes";
33import { checkInstructions, compileMainSnippet } from "../utils/base";
34
35describe("TypeOfTest", function () {
36    it("typeof 12", function () {
37        let insns = compileMainSnippet("typeof 5");
38        let expected = [
39            new LdaiDyn(new Imm(5)),
40            new EcmaTypeofdyn(),
41            new EcmaReturnundefined()
42        ];
43
44        expect(checkInstructions(insns, expected)).to.be.true;
45    });
46
47    it("typeof Number(\"12\")", function () {
48        let insns = compileMainSnippet("typeof Number(\"5\")");
49        let arg1 = new VReg();
50        let arg3 = new VReg();
51        let expected = [
52            new EcmaTryldglobalbyname("Number"),
53            new StaDyn(arg1),
54
55            new LdaStr("5"),
56            new StaDyn(arg3),
57            new EcmaCallarg1dyn(arg1, arg3),
58            new EcmaTypeofdyn(),
59            new EcmaReturnundefined()
60        ];
61
62        expect(checkInstructions(insns, expected)).to.be.true;
63    });
64
65    it("typeof x", function () {
66        let insns = compileMainSnippet("typeof x");
67
68        let expected = [
69            new EcmaLdobjbyname("x", new VReg()),
70            new EcmaTypeofdyn(),
71            new EcmaReturnundefined()
72        ];
73
74        expect(checkInstructions(insns, expected)).to.be.true;
75    });
76
77    it("typeof(x)", function () {
78        let insns = compileMainSnippet("typeof(x)");
79
80        let expected = [
81            new EcmaLdobjbyname("x", new VReg()),
82            new EcmaTypeofdyn(),
83            new EcmaReturnundefined()
84        ];
85
86        expect(checkInstructions(insns, expected)).to.be.true;
87    });
88});
89