1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 05:38:15 +00:00

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.
This commit is contained in:
Matthew Olsson 2020-05-27 22:22:08 -07:00 committed by Andreas Kling
parent 5ae9419a06
commit 786722149b
8 changed files with 172 additions and 2 deletions

View file

@ -120,6 +120,9 @@ public:
void add_variables(NonnullRefPtrVector<VariableDeclaration>); void add_variables(NonnullRefPtrVector<VariableDeclaration>);
const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; } const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
bool in_strict_mode() const { return m_strict_mode; }
void set_strict_mode() { m_strict_mode = true; }
protected: protected:
ScopeNode() { } ScopeNode() { }
@ -127,6 +130,7 @@ private:
virtual bool is_scope_node() const final { return true; } virtual bool is_scope_node() const final { return true; }
NonnullRefPtrVector<Statement> m_children; NonnullRefPtrVector<Statement> m_children;
NonnullRefPtrVector<VariableDeclaration> m_variables; NonnullRefPtrVector<VariableDeclaration> m_variables;
bool m_strict_mode { false };
}; };
class Program : public ScopeNode { class Program : public ScopeNode {

View file

@ -31,6 +31,7 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibJS/AST.h>
#include <LibJS/Console.h> #include <LibJS/Console.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/Heap.h> #include <LibJS/Heap/Heap.h>
@ -123,6 +124,8 @@ public:
const LexicalEnvironment* current_environment() const { return m_call_stack.last().environment; } const LexicalEnvironment* current_environment() const { return m_call_stack.last().environment; }
LexicalEnvironment* current_environment() { return m_call_stack.last().environment; } LexicalEnvironment* current_environment() { return m_call_stack.last().environment; }
bool in_strict_mode() const { return m_scope_stack.last().scope_node->in_strict_mode(); }
size_t argument_count() const size_t argument_count() const
{ {
if (m_call_stack.is_empty()) if (m_call_stack.is_empty())

View file

@ -206,9 +206,20 @@ NonnullRefPtr<Program> Parser::parse_program()
{ {
ScopePusher scope(*this, ScopePusher::Var | ScopePusher::Let); ScopePusher scope(*this, ScopePusher::Var | ScopePusher::Let);
auto program = adopt(*new Program); auto program = adopt(*new Program);
bool first = true;
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::Looking;
while (!done()) { while (!done()) {
if (match_statement()) { if (match_statement()) {
program->append(parse_statement()); program->append(parse_statement());
if (first) {
if (m_parser_state.m_use_strict_directive == UseStrictDirectiveState::Found) {
program->set_strict_mode();
m_parser_state.m_strict_mode = true;
}
first = false;
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::None;
}
} else { } else {
expected("statement"); expected("statement");
consume(); consume();
@ -594,6 +605,15 @@ NonnullRefPtr<StringLiteral> Parser::parse_string_literal(Token token)
m_parser_state.m_current_token.line_number(), m_parser_state.m_current_token.line_number(),
m_parser_state.m_current_token.line_column()); m_parser_state.m_current_token.line_column());
} }
if (m_parser_state.m_use_strict_directive == UseStrictDirectiveState::Looking) {
if (string == "use strict" && token.type() != TokenType::TemplateLiteralString) {
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::Found;
} else {
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::None;
}
}
return create_ast_node<StringLiteral>(string); return create_ast_node<StringLiteral>(string);
} }
@ -910,16 +930,37 @@ NonnullRefPtr<BlockStatement> Parser::parse_block_statement()
ScopePusher scope(*this, ScopePusher::Let); ScopePusher scope(*this, ScopePusher::Let);
auto block = create_ast_node<BlockStatement>(); auto block = create_ast_node<BlockStatement>();
consume(TokenType::CurlyOpen); consume(TokenType::CurlyOpen);
bool first = true;
bool initial_strict_mode_state = m_parser_state.m_strict_mode;
if (initial_strict_mode_state) {
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::None;
block->set_strict_mode();
} else {
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::Looking;
}
while (!done() && !match(TokenType::CurlyClose)) { while (!done() && !match(TokenType::CurlyClose)) {
if (match(TokenType::Semicolon)) { if (match(TokenType::Semicolon)) {
consume(); consume();
} else if (match_statement()) { } else if (match_statement()) {
block->append(parse_statement()); block->append(parse_statement());
if (first && !initial_strict_mode_state) {
if (m_parser_state.m_use_strict_directive == UseStrictDirectiveState::Found) {
block->set_strict_mode();
m_parser_state.m_strict_mode = true;
}
m_parser_state.m_use_strict_directive = UseStrictDirectiveState::None;
}
} else { } else {
expected("statement"); expected("statement");
consume(); consume();
} }
first = false;
} }
m_parser_state.m_strict_mode = initial_strict_mode_state;
consume(TokenType::CurlyClose); consume(TokenType::CurlyClose);
block->add_variables(m_parser_state.m_let_scopes.last()); block->add_variables(m_parser_state.m_let_scopes.last());
return block; return block;

View file

@ -26,10 +26,10 @@
#pragma once #pragma once
#include "AST.h"
#include "Lexer.h"
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibJS/AST.h>
#include <LibJS/Lexer.h>
#include <stdio.h> #include <stdio.h>
namespace JS { namespace JS {
@ -134,12 +134,20 @@ private:
void save_state(); void save_state();
void load_state(); void load_state();
enum class UseStrictDirectiveState {
None,
Looking,
Found,
};
struct ParserState { struct ParserState {
Lexer m_lexer; Lexer m_lexer;
Token m_current_token; Token m_current_token;
Vector<Error> m_errors; Vector<Error> m_errors;
Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes; Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes;
Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes; Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes;
UseStrictDirectiveState m_use_strict_directive { UseStrictDirectiveState::None };
bool m_strict_mode { false };
explicit ParserState(Lexer); explicit ParserState(Lexer);
}; };

View file

@ -60,6 +60,31 @@ try {
assert(foo === undefined); assert(foo === undefined);
assert(bar === undefined); assert(bar === undefined);
(() => {
"use strict";
assert(isStrictMode());
(() => {
assert(isStrictMode());
})();
})();
(() => {
'use strict';
assert(isStrictMode());
})();
(() => {
assert(!isStrictMode());
(() => {
"use strict";
assert(isStrictMode());
})();
assert(!isStrictMode());
})();
console.log("PASS"); console.log("PASS");
} catch { } catch {
console.log("FAIL"); console.log("FAIL");

View file

@ -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);
}

View file

@ -0,0 +1,30 @@
"use strict";
load("test-common.js");
try {
assert(isStrictMode());
(function() {
assert(isStrictMode());
})();
(function() {
"use strict";
assert(isStrictMode());
})();
(() => {
assert(isStrictMode());
})();
(() => {
"use strict";
assert(isStrictMode());
})();
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -55,6 +55,7 @@ public:
virtual ~ReplObject() override; virtual ~ReplObject() override;
static JS::Value load_file(JS::Interpreter&); static JS::Value load_file(JS::Interpreter&);
static JS::Value is_strict_mode(JS::Interpreter&);
private: private:
virtual const char* class_name() const override { return "ReplObject"; } virtual const char* class_name() const override { return "ReplObject"; }
@ -391,6 +392,11 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
return JS::Value(true); return JS::Value(true);
} }
JS::Value ReplObject::is_strict_mode(JS::Interpreter& interpreter)
{
return JS::Value(interpreter.in_strict_mode());
}
void repl(JS::Interpreter& interpreter) void repl(JS::Interpreter& interpreter)
{ {
while (!s_fail_repl) { while (!s_fail_repl) {
@ -405,6 +411,7 @@ void repl(JS::Interpreter& interpreter)
void enable_test_mode(JS::Interpreter& interpreter) void enable_test_mode(JS::Interpreter& interpreter)
{ {
interpreter.global_object().define_native_function("load", ReplObject::load_file); interpreter.global_object().define_native_function("load", ReplObject::load_file);
interpreter.global_object().define_native_function("isStrictMode", ReplObject::is_strict_mode);
} }
static Function<void()> interrupt_interpreter; static Function<void()> interrupt_interpreter;