summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/22_strict_mode
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-05-04 14:15:20 +0200
committerJo-Philipp Wich <jo@mein.io>2021-05-04 15:31:15 +0200
commita36e0dfd8432a0c345ab3a710280f6d4f663bddc (patch)
tree73e08e12a669b2c3afbbaca58f123b59b589b96c /tests/custom/00_syntax/22_strict_mode
parent776dc9e3166dfc3735bd72be3acd26ebc6a591f5 (diff)
syntax: implement support for 'use strict' pragma
Support per-file and per-function `"use strict";` statement to opt into strict variable handling from ucode source code. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/00_syntax/22_strict_mode')
-rw-r--r--tests/custom/00_syntax/22_strict_mode92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/22_strict_mode b/tests/custom/00_syntax/22_strict_mode
new file mode 100644
index 0000000..73f399c
--- /dev/null
+++ b/tests/custom/00_syntax/22_strict_mode
@@ -0,0 +1,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 --