summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/22_strict_mode
blob: 73f399c13c402eb3049b52d70d8b471661ed61dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Ucode borrows the `"use strict";` statement from ECMA script to enable
strict variable semantics for the entire script or for the enclosing
function.

With strict mode enabled, attempts to use undeclared local variables
or attempts to read global variables which have not been assigned yet
will raise an exception.


1. To enable strict mode for the entire script, it should be the first
statement of the program.

-- Expect stderr --
Reference error: access to undeclared variable x
In line 4, byte 8:

 `    print(x);`
            ^-- Near here


-- End --

-- Testcase --
{%
	"use strict";

	print(x);
%}
-- End --


2. To enable strict mode for a single function, the "use strict" expression
should be the first statement of the function body.

-- Expect stdout --
a() = null
-- End --

-- Expect stderr --
Reference error: access to undeclared variable x
In b(), line 9, byte 24:
  called from anonymous function ([stdin]:13:4)

 `        printf("b() = %J\n", x);`
  Near here -------------------^


-- End --

-- Testcase --
{%
	function a() {
		printf("a() = %J\n", x);
	}

	function b() {
		"use strict";

		printf("b() = %J\n", x);
	}

	a();
	b();
%}
-- End --


3. When "use strict" is not the first statement, it has no effect.

-- Expect stdout --
b=null
c=null
-- End --

-- Testcase --
{%
	function t() {
		a = 1;

		"use strict";

		printf("b=%J\n", b);
	}

	t();

	"use strict";

	printf("c=%J\n", c);

%}
-- End --