Serenity Operating System
1test("basic eval() functionality", () => {
2 expect(eval("1 + 2")).toBe(3);
3
4 function foo(a) {
5 var x = 5;
6 eval("x += a");
7 return x;
8 }
9 expect(foo(7)).toBe(12);
10});
11
12test("returns value of last value-producing statement", () => {
13 // See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
14 expect(eval("")).toBeUndefined();
15 expect(eval("1;;;;;")).toBe(1);
16 expect(eval("1;{}")).toBe(1);
17 expect(eval("1;var a;")).toBe(1);
18});
19
20test("syntax error", () => {
21 expect(() => {
22 eval("{");
23 }).toThrowWithMessage(
24 SyntaxError,
25 "Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
26 );
27});
28
29test("returns 1st argument unless 1st argument is a string", () => {
30 var stringObject = new String("1 + 2");
31 expect(eval(stringObject)).toBe(stringObject);
32});
33
34// These eval scope tests use function expressions due to bug #8198
35var testValue = "outer";
36test("eval only touches locals if direct use", function () {
37 var testValue = "inner";
38 expect(globalThis.eval("testValue")).toEqual("outer");
39});
40
41test("alias to eval works as a global eval", function () {
42 var testValue = "inner";
43 var eval1 = globalThis.eval;
44 expect(eval1("testValue")).toEqual("outer");
45});
46
47test("eval evaluates all args", function () {
48 var i = 0;
49 expect(eval("testValue", i++, i++, i++)).toEqual("outer");
50 expect(i).toEqual(3);
51});
52
53test("eval tests for exceptions", function () {
54 var i = 0;
55 expect(function () {
56 eval("testValue", i++, i++, j, i++);
57 }).toThrowWithMessage(ReferenceError, "'j' is not defined");
58 expect(i).toEqual(2);
59});
60
61test("direct eval inherits non-strict evaluation", function () {
62 expect(eval("01")).toEqual(1);
63});
64
65test("direct eval inherits strict evaluation", function () {
66 "use strict";
67 expect(() => {
68 eval("01");
69 }).toThrowWithMessage(SyntaxError, "Unprefixed octal number not allowed in strict mode");
70});
71
72test("global eval evaluates as non-strict", function () {
73 "use strict";
74 expect(globalThis.eval("01"));
75});