Skip to content

Commit fb05063

Browse files
committed
LibJS: Let bytecode instructions know whether they are in strict mode
This commits puts the strict mode flag in the header of every bytecode instruction. This allows us to check for strict mode without looking at the currently running execution context.
1 parent 3fb678b commit fb05063

File tree

16 files changed

+183
-227
lines changed

16 files changed

+183
-227
lines changed

Libraries/LibJS/Bytecode/Executable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Executable::Executable(
2525
size_t number_of_property_lookup_caches,
2626
size_t number_of_global_variable_caches,
2727
size_t number_of_registers,
28-
bool is_strict_mode)
28+
Strict strict)
2929
: bytecode(move(bytecode))
3030
, string_table(move(string_table))
3131
, identifier_table(move(identifier_table))
3232
, regex_table(move(regex_table))
3333
, constants(move(constants))
3434
, source_code(move(source_code))
3535
, number_of_registers(number_of_registers)
36-
, is_strict_mode(is_strict_mode)
36+
, is_strict_mode(strict == Strict::Yes)
3737
{
3838
property_lookup_caches.resize(number_of_property_lookup_caches);
3939
global_variable_caches.resize(number_of_global_variable_caches);

Libraries/LibJS/Bytecode/Executable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class JS_API Executable final : public Cell {
7575
size_t number_of_property_lookup_caches,
7676
size_t number_of_global_variable_caches,
7777
size_t number_of_registers,
78-
bool is_strict_mode);
78+
Strict);
7979

8080
virtual ~Executable() override;
8181

Libraries/LibJS/Bytecode/Generator.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ CodeGenerationErrorOr<void> Generator::emit_function_declaration_instantiation(E
218218
CodeGenerationErrorOr<GC::Ref<Executable>> Generator::compile(VM& vm, ASTNode const& node, FunctionKind enclosing_function_kind, GC::Ptr<ECMAScriptFunctionObject const> function, MustPropagateCompletion must_propagate_completion, Vector<LocalVariable> local_variable_names)
219219
{
220220
Generator generator(vm, function, must_propagate_completion);
221+
222+
if (is<Program>(node))
223+
generator.m_strict = static_cast<Program const&>(node).is_strict_mode() ? Strict::Yes : Strict::No;
224+
else if (is<FunctionBody>(node))
225+
generator.m_strict = static_cast<FunctionBody const&>(node).in_strict_mode() ? Strict::Yes : Strict::No;
226+
else if (is<FunctionDeclaration>(node))
227+
generator.m_strict = static_cast<FunctionDeclaration const&>(node).is_strict_mode() ? Strict::Yes : Strict::No;
228+
221229
generator.m_local_variables = local_variable_names;
222230

223231
generator.switch_to_basic_block(generator.make_block());
@@ -260,14 +268,6 @@ CodeGenerationErrorOr<GC::Ref<Executable>> Generator::compile(VM& vm, ASTNode co
260268
}
261269
}
262270

263-
bool is_strict_mode = false;
264-
if (is<Program>(node))
265-
is_strict_mode = static_cast<Program const&>(node).is_strict_mode();
266-
else if (is<FunctionBody>(node))
267-
is_strict_mode = static_cast<FunctionBody const&>(node).in_strict_mode();
268-
else if (is<FunctionDeclaration>(node))
269-
is_strict_mode = static_cast<FunctionDeclaration const&>(node).is_strict_mode();
270-
271271
size_t size_needed = 0;
272272
for (auto& block : generator.m_root_basic_blocks) {
273273
size_needed += block->size();
@@ -453,7 +453,7 @@ CodeGenerationErrorOr<GC::Ref<Executable>> Generator::compile(VM& vm, ASTNode co
453453
generator.m_next_property_lookup_cache,
454454
generator.m_next_global_variable_cache,
455455
generator.m_next_register,
456-
is_strict_mode);
456+
generator.m_strict);
457457

458458
Vector<Executable::ExceptionHandlers> linked_exception_handlers;
459459

Libraries/LibJS/Bytecode/Generator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Generator {
9393
grow(sizeof(OpType));
9494
void* slot = m_current_basic_block->data() + slot_offset;
9595
new (slot) OpType(forward<Args>(args)...);
96+
static_cast<OpType*>(slot)->set_strict(m_strict);
9697
if constexpr (OpType::IsTerminator)
9798
m_current_basic_block->terminate({});
9899
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
@@ -110,6 +111,7 @@ class Generator {
110111
grow(size_to_allocate);
111112
void* slot = m_current_basic_block->data() + slot_offset;
112113
new (slot) OpType(forward<Args>(args)...);
114+
static_cast<OpType*>(slot)->set_strict(m_strict);
113115
if constexpr (OpType::IsTerminator)
114116
m_current_basic_block->terminate({});
115117
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
@@ -383,6 +385,8 @@ class Generator {
383385
Vector<FlyString> language_label_set;
384386
};
385387

388+
Strict m_strict { Strict::No };
389+
386390
BasicBlock* m_current_basic_block { nullptr };
387391
ASTNode const* m_current_ast_node { nullptr };
388392
UnwindContext const* m_current_unwind_context { nullptr };

Libraries/LibJS/Bytecode/Instruction.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class alignas(void*) Instruction {
188188
constexpr static bool IsTerminator = false;
189189
static constexpr bool IsVariableLength = false;
190190

191-
enum class Type {
191+
enum class Type : u8 {
192192
#define __BYTECODE_OP(op) \
193193
op,
194194
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
@@ -202,6 +202,9 @@ class alignas(void*) Instruction {
202202
void visit_operands(Function<void(Operand&)> visitor);
203203
static void destroy(Instruction&);
204204

205+
Strict strict() const { return m_strict; }
206+
void set_strict(Strict strict) { m_strict = strict; }
207+
205208
protected:
206209
explicit Instruction(Type type)
207210
: m_type(type)
@@ -213,6 +216,7 @@ class alignas(void*) Instruction {
213216

214217
private:
215218
Type m_type {};
219+
Strict m_strict {};
216220
};
217221

218222
class InstructionStreamIterator {

0 commit comments

Comments
 (0)