summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/function-strict-mode.js
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-05-27 22:22:08 -0700
committerAndreas Kling <kling@serenityos.org>2020-05-28 17:18:42 +0200
commit786722149b5084dc52be0bdecf2d5628662d0941 (patch)
tree9a5afc8eac66b0602a4341e449ea404161d81100 /Libraries/LibJS/Tests/function-strict-mode.js
parent5ae9419a069f8180197df14447b1a23f1cf9411d (diff)
downloadserenity-786722149b5084dc52be0bdecf2d5628662d0941.zip
LibJS: Add strict mode
Adds the ability for a scope (either a function or the entire program) to be in strict mode. Scopes default to non-strict mode. There are two ways to determine the strict-ness of the JS engine: 1. In the parser, this can be accessed with the parser_state variable m_is_strict_mode boolean. If true, the Parser is currently parsing in strict mode. This is done so that the Parser can generate syntax errors at parse time, which is required in some cases. 2. With Interpreter.is_strict_mode(). This allows strict mode checking at runtime as opposed to compile time. Additionally, in order to test this, a global isStrictMode() function has been added to the JS ReplObject under the test-mode flag.
Diffstat (limited to 'Libraries/LibJS/Tests/function-strict-mode.js')
-rw-r--r--Libraries/LibJS/Tests/function-strict-mode.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/function-strict-mode.js b/Libraries/LibJS/Tests/function-strict-mode.js
new file mode 100644
index 0000000000..382327d9bb
--- /dev/null
+++ b/Libraries/LibJS/Tests/function-strict-mode.js
@@ -0,0 +1,52 @@
+load("test-common.js");
+
+try {
+ (function() {
+ assert(!isStrictMode());
+ })();
+
+ (function() {
+ 'use strict';
+ assert(isStrictMode());
+ })();
+
+ (function() {
+ "use strict";
+ assert(isStrictMode());
+ })();
+
+ (function() {
+ `use strict`;
+ assert(!isStrictMode());
+ })();
+
+ (function() {
+ ;'use strict';
+ assert(!isStrictMode());
+ })();
+
+ (function() {
+ ;"use strict";
+ assert(!isStrictMode());
+ })();
+
+ (function() {
+ "use strict";
+ (function() {
+ assert(isStrictMode());
+ })();
+ })();
+
+ (function() {
+ assert(!isStrictMode());
+ (function(){
+ "use strict";
+ assert(isStrictMode());
+ })();
+ assert(!isStrictMode());
+ })();
+
+ console.log("PASS");
+} catch (e) {
+ console.log("FAIL: " + e);
+}