/src/serenity/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/Debug.h> |
8 | | #include <AK/HashTable.h> |
9 | | #include <AK/TemporaryChange.h> |
10 | | #include <LibJS/AST.h> |
11 | | #include <LibJS/Bytecode/BasicBlock.h> |
12 | | #include <LibJS/Bytecode/Generator.h> |
13 | | #include <LibJS/Bytecode/Instruction.h> |
14 | | #include <LibJS/Bytecode/Interpreter.h> |
15 | | #include <LibJS/Bytecode/Label.h> |
16 | | #include <LibJS/Bytecode/Op.h> |
17 | | #include <LibJS/Runtime/AbstractOperations.h> |
18 | | #include <LibJS/Runtime/Accessor.h> |
19 | | #include <LibJS/Runtime/Array.h> |
20 | | #include <LibJS/Runtime/BigInt.h> |
21 | | #include <LibJS/Runtime/DeclarativeEnvironment.h> |
22 | | #include <LibJS/Runtime/ECMAScriptFunctionObject.h> |
23 | | #include <LibJS/Runtime/Environment.h> |
24 | | #include <LibJS/Runtime/FunctionEnvironment.h> |
25 | | #include <LibJS/Runtime/GlobalEnvironment.h> |
26 | | #include <LibJS/Runtime/GlobalObject.h> |
27 | | #include <LibJS/Runtime/Iterator.h> |
28 | | #include <LibJS/Runtime/MathObject.h> |
29 | | #include <LibJS/Runtime/NativeFunction.h> |
30 | | #include <LibJS/Runtime/ObjectEnvironment.h> |
31 | | #include <LibJS/Runtime/Realm.h> |
32 | | #include <LibJS/Runtime/Reference.h> |
33 | | #include <LibJS/Runtime/RegExpObject.h> |
34 | | #include <LibJS/Runtime/TypedArray.h> |
35 | | #include <LibJS/Runtime/Value.h> |
36 | | #include <LibJS/Runtime/ValueInlines.h> |
37 | | #include <LibJS/SourceTextModule.h> |
38 | | |
39 | | namespace JS::Bytecode { |
40 | | |
41 | | bool g_dump_bytecode = false; |
42 | | |
43 | | static ByteString format_operand(StringView name, Operand operand, Bytecode::Executable const& executable) |
44 | 0 | { |
45 | 0 | StringBuilder builder; |
46 | 0 | if (!name.is_empty()) |
47 | 0 | builder.appendff("\033[32m{}\033[0m:", name); |
48 | 0 | switch (operand.type()) { |
49 | 0 | case Operand::Type::Register: |
50 | 0 | if (operand.index() == Register::this_value().index()) { |
51 | 0 | builder.appendff("\033[33mthis\033[0m"); |
52 | 0 | } else { |
53 | 0 | builder.appendff("\033[33mreg{}\033[0m", operand.index()); |
54 | 0 | } |
55 | 0 | break; |
56 | 0 | case Operand::Type::Local: |
57 | 0 | builder.appendff("\033[34m{}~{}\033[0m", executable.local_variable_names[operand.index() - executable.local_index_base], operand.index() - executable.local_index_base); |
58 | 0 | break; |
59 | 0 | case Operand::Type::Constant: { |
60 | 0 | builder.append("\033[36m"sv); |
61 | 0 | auto value = executable.constants[operand.index() - executable.number_of_registers]; |
62 | 0 | if (value.is_empty()) |
63 | 0 | builder.append("<Empty>"sv); |
64 | 0 | else if (value.is_boolean()) |
65 | 0 | builder.appendff("Bool({})", value.as_bool() ? "true"sv : "false"sv); |
66 | 0 | else if (value.is_int32()) |
67 | 0 | builder.appendff("Int32({})", value.as_i32()); |
68 | 0 | else if (value.is_double()) |
69 | 0 | builder.appendff("Double({})", value.as_double()); |
70 | 0 | else if (value.is_bigint()) |
71 | 0 | builder.appendff("BigInt({})", value.as_bigint().to_byte_string()); |
72 | 0 | else if (value.is_string()) |
73 | 0 | builder.appendff("String(\"{}\")", value.as_string().utf8_string_view()); |
74 | 0 | else if (value.is_undefined()) |
75 | 0 | builder.append("Undefined"sv); |
76 | 0 | else if (value.is_null()) |
77 | 0 | builder.append("Null"sv); |
78 | 0 | else |
79 | 0 | builder.appendff("Value: {}", value); |
80 | 0 | builder.append("\033[0m"sv); |
81 | 0 | break; |
82 | 0 | } |
83 | 0 | default: |
84 | 0 | VERIFY_NOT_REACHED(); |
85 | 0 | } |
86 | 0 | return builder.to_byte_string(); |
87 | 0 | } |
88 | | |
89 | | static ByteString format_operand_list(StringView name, ReadonlySpan<Operand> operands, Bytecode::Executable const& executable) |
90 | 0 | { |
91 | 0 | StringBuilder builder; |
92 | 0 | if (!name.is_empty()) |
93 | 0 | builder.appendff("\033[32m{}\033[0m:[", name); |
94 | 0 | for (size_t i = 0; i < operands.size(); ++i) { |
95 | 0 | if (i != 0) |
96 | 0 | builder.append(", "sv); |
97 | 0 | builder.appendff("{}", format_operand(""sv, operands[i], executable)); |
98 | 0 | } |
99 | 0 | builder.append("]"sv); |
100 | 0 | return builder.to_byte_string(); |
101 | 0 | } |
102 | | |
103 | | static ByteString format_value_list(StringView name, ReadonlySpan<Value> values) |
104 | 0 | { |
105 | 0 | StringBuilder builder; |
106 | 0 | if (!name.is_empty()) |
107 | 0 | builder.appendff("\033[32m{}\033[0m:[", name); |
108 | 0 | builder.join(", "sv, values); |
109 | 0 | builder.append("]"sv); |
110 | 0 | return builder.to_byte_string(); |
111 | 0 | } |
112 | | |
113 | | ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_inequals(VM& vm, Value src1, Value src2) |
114 | 0 | { |
115 | 0 | if (src1.tag() == src2.tag()) { |
116 | 0 | if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) |
117 | 0 | return Value(src1.encoded() != src2.encoded()); |
118 | 0 | } |
119 | 0 | return Value(!TRY(is_loosely_equal(vm, src1, src2))); |
120 | 0 | } |
121 | | |
122 | | ALWAYS_INLINE static ThrowCompletionOr<Value> loosely_equals(VM& vm, Value src1, Value src2) |
123 | 0 | { |
124 | 0 | if (src1.tag() == src2.tag()) { |
125 | 0 | if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) |
126 | 0 | return Value(src1.encoded() == src2.encoded()); |
127 | 0 | } |
128 | 0 | return Value(TRY(is_loosely_equal(vm, src1, src2))); |
129 | 0 | } |
130 | | |
131 | | ALWAYS_INLINE static ThrowCompletionOr<Value> strict_inequals(VM&, Value src1, Value src2) |
132 | 0 | { |
133 | 0 | if (src1.tag() == src2.tag()) { |
134 | 0 | if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) |
135 | 0 | return Value(src1.encoded() != src2.encoded()); |
136 | 0 | } |
137 | 0 | return Value(!is_strictly_equal(src1, src2)); |
138 | 0 | } |
139 | | |
140 | | ALWAYS_INLINE static ThrowCompletionOr<Value> strict_equals(VM&, Value src1, Value src2) |
141 | 0 | { |
142 | 0 | if (src1.tag() == src2.tag()) { |
143 | 0 | if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish()) |
144 | 0 | return Value(src1.encoded() == src2.encoded()); |
145 | 0 | } |
146 | 0 | return Value(is_strictly_equal(src1, src2)); |
147 | 0 | } |
148 | | |
149 | | Interpreter::Interpreter(VM& vm) |
150 | 55 | : m_vm(vm) |
151 | 55 | { |
152 | 55 | } |
153 | | |
154 | | Interpreter::~Interpreter() |
155 | 55 | { |
156 | 55 | } |
157 | | |
158 | | ALWAYS_INLINE Value Interpreter::get(Operand op) const |
159 | 400k | { |
160 | 400k | return m_registers_and_constants_and_locals.data()[op.index()]; |
161 | 400k | } |
162 | | |
163 | | ALWAYS_INLINE void Interpreter::set(Operand op, Value value) |
164 | 4 | { |
165 | 4 | m_registers_and_constants_and_locals.data()[op.index()] = value; |
166 | 4 | } |
167 | | |
168 | | ALWAYS_INLINE Value Interpreter::do_yield(Value value, Optional<Label> continuation) |
169 | 0 | { |
170 | 0 | auto object = Object::create(realm(), nullptr); |
171 | 0 | object->define_direct_property("result", value, JS::default_attributes); |
172 | |
|
173 | 0 | if (continuation.has_value()) |
174 | | // FIXME: If we get a pointer, which is not accurately representable as a double |
175 | | // will cause this to explode |
176 | 0 | object->define_direct_property("continuation", Value(continuation->address()), JS::default_attributes); |
177 | 0 | else |
178 | 0 | object->define_direct_property("continuation", js_null(), JS::default_attributes); |
179 | |
|
180 | 0 | object->define_direct_property("isAwait", Value(false), JS::default_attributes); |
181 | 0 | return object; |
182 | 0 | } |
183 | | |
184 | | // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation |
185 | | ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override) |
186 | 22 | { |
187 | 22 | auto& vm = this->vm(); |
188 | | |
189 | | // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]]. |
190 | 22 | auto& global_environment = script_record.realm().global_environment(); |
191 | | |
192 | | // 2. Let scriptContext be a new ECMAScript code execution context. |
193 | 22 | auto script_context = ExecutionContext::create(); |
194 | | |
195 | | // 3. Set the Function of scriptContext to null. |
196 | | // NOTE: This was done during execution context construction. |
197 | | |
198 | | // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]]. |
199 | 22 | script_context->realm = &script_record.realm(); |
200 | | |
201 | | // 5. Set the ScriptOrModule of scriptContext to scriptRecord. |
202 | 22 | script_context->script_or_module = NonnullGCPtr<Script>(script_record); |
203 | | |
204 | | // 6. Set the VariableEnvironment of scriptContext to globalEnv. |
205 | 22 | script_context->variable_environment = &global_environment; |
206 | | |
207 | | // 7. Set the LexicalEnvironment of scriptContext to globalEnv. |
208 | 22 | script_context->lexical_environment = &global_environment; |
209 | | |
210 | | // Non-standard: Override the lexical environment if requested. |
211 | 22 | if (lexical_environment_override) |
212 | 0 | script_context->lexical_environment = lexical_environment_override; |
213 | | |
214 | | // 8. Set the PrivateEnvironment of scriptContext to null. |
215 | | |
216 | | // NOTE: This isn't in the spec, but we require it. |
217 | 22 | script_context->is_strict_mode = script_record.parse_node().is_strict_mode(); |
218 | | |
219 | | // FIXME: 9. Suspend the currently running execution context. |
220 | | |
221 | | // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context. |
222 | 22 | TRY(vm.push_execution_context(*script_context, {})); |
223 | | |
224 | | // 11. Let script be scriptRecord.[[ECMAScriptCode]]. |
225 | 22 | auto& script = script_record.parse_node(); |
226 | | |
227 | | // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)). |
228 | 22 | auto instantiation_result = script.global_declaration_instantiation(vm, global_environment); |
229 | 22 | Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({}); |
230 | | |
231 | | // 13. If result.[[Type]] is normal, then |
232 | 22 | if (result.type() == Completion::Type::Normal) { |
233 | 22 | auto executable_result = JS::Bytecode::Generator::generate_from_ast_node(vm, script, {}); |
234 | | |
235 | 22 | if (executable_result.is_error()) { |
236 | 0 | if (auto error_string = executable_result.error().to_string(); error_string.is_error()) |
237 | 0 | result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory)); |
238 | 0 | else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error()) |
239 | 0 | result = vm.template throw_completion<JS::InternalError>(vm.error_message(JS::VM::ErrorMessage::OutOfMemory)); |
240 | 0 | else |
241 | 0 | result = JS::throw_completion(JS::InternalError::create(realm(), error_string.release_value())); |
242 | 22 | } else { |
243 | 22 | auto executable = executable_result.release_value(); |
244 | | |
245 | 22 | if (g_dump_bytecode) |
246 | 0 | executable->dump(); |
247 | | |
248 | | // a. Set result to the result of evaluating script. |
249 | 22 | auto result_or_error = run_executable(*executable, {}, {}); |
250 | 22 | if (result_or_error.value.is_error()) |
251 | 11 | result = result_or_error.value.release_error(); |
252 | 11 | else |
253 | 11 | result = result_or_error.return_register_value; |
254 | 22 | } |
255 | 22 | } |
256 | | |
257 | | // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then |
258 | 22 | if (result.type() == Completion::Type::Normal && !result.value().has_value()) { |
259 | | // a. Set result to NormalCompletion(undefined). |
260 | 0 | result = normal_completion(js_undefined()); |
261 | 0 | } |
262 | | |
263 | | // FIXME: 15. Suspend scriptContext and remove it from the execution context stack. |
264 | 22 | vm.pop_execution_context(); |
265 | | |
266 | | // 16. Assert: The execution context stack is not empty. |
267 | 22 | VERIFY(!vm.execution_context_stack().is_empty()); |
268 | | |
269 | | // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context. |
270 | | |
271 | | // At this point we may have already run any queued promise jobs via on_call_stack_emptied, |
272 | | // in which case this is a no-op. |
273 | | // FIXME: These three should be moved out of Interpreter::run and give the host an option to run these, as it's up to the host when these get run. |
274 | | // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects |
275 | | // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint. |
276 | | // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues. |
277 | 22 | vm.run_queued_promise_jobs(); |
278 | | |
279 | 22 | vm.run_queued_finalization_registry_cleanup_jobs(); |
280 | | |
281 | 22 | vm.finish_execution_generation(); |
282 | | |
283 | | // 18. Return ? result. |
284 | 22 | if (result.is_abrupt()) { |
285 | 11 | VERIFY(result.type() == Completion::Type::Throw); |
286 | 11 | return result.release_error(); |
287 | 11 | } |
288 | | |
289 | 11 | VERIFY(result.value().has_value()); |
290 | 11 | return *result.value(); |
291 | 11 | } |
292 | | |
293 | | ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module) |
294 | 0 | { |
295 | | // FIXME: This is not a entry point as defined in the spec, but is convenient. |
296 | | // To avoid work we use link_and_eval_module however that can already be |
297 | | // dangerous if the vm loaded other modules. |
298 | 0 | auto& vm = this->vm(); |
299 | |
|
300 | 0 | TRY(vm.link_and_eval_module(Badge<Bytecode::Interpreter> {}, module)); |
301 | |
|
302 | 0 | vm.run_queued_promise_jobs(); |
303 | |
|
304 | 0 | vm.run_queued_finalization_registry_cleanup_jobs(); |
305 | |
|
306 | 0 | return js_undefined(); |
307 | 0 | } |
308 | | |
309 | | Interpreter::HandleExceptionResponse Interpreter::handle_exception(size_t& program_counter, Value exception) |
310 | 11 | { |
311 | 11 | reg(Register::exception()) = exception; |
312 | 11 | m_scheduled_jump = {}; |
313 | 11 | auto handlers = current_executable().exception_handlers_for_offset(program_counter); |
314 | 11 | if (!handlers.has_value()) { |
315 | 11 | return HandleExceptionResponse::ExitFromExecutable; |
316 | 11 | } |
317 | 0 | auto& handler = handlers->handler_offset; |
318 | 0 | auto& finalizer = handlers->finalizer_offset; |
319 | |
|
320 | 0 | VERIFY(!running_execution_context().unwind_contexts.is_empty()); |
321 | 0 | auto& unwind_context = running_execution_context().unwind_contexts.last(); |
322 | 0 | VERIFY(unwind_context.executable == m_current_executable); |
323 | | |
324 | 0 | if (handler.has_value()) { |
325 | 0 | program_counter = handler.value(); |
326 | 0 | return HandleExceptionResponse::ContinueInThisExecutable; |
327 | 0 | } |
328 | 0 | if (finalizer.has_value()) { |
329 | 0 | program_counter = finalizer.value(); |
330 | 0 | return HandleExceptionResponse::ContinueInThisExecutable; |
331 | 0 | } |
332 | 0 | VERIFY_NOT_REACHED(); |
333 | 0 | } |
334 | | |
335 | | // FIXME: GCC takes a *long* time to compile with flattening, and it will time out our CI. :| |
336 | | #if defined(AK_COMPILER_CLANG) |
337 | | # define FLATTEN_ON_CLANG FLATTEN |
338 | | #else |
339 | | # define FLATTEN_ON_CLANG |
340 | | #endif |
341 | | |
342 | | FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point) |
343 | 22 | { |
344 | 22 | if (vm().did_reach_stack_space_limit()) { |
345 | 0 | reg(Register::exception()) = vm().throw_completion<InternalError>(ErrorType::CallStackSizeExceeded).release_value().value(); |
346 | 0 | return; |
347 | 0 | } |
348 | | |
349 | 22 | auto& running_execution_context = this->running_execution_context(); |
350 | 22 | auto* arguments = running_execution_context.arguments.data(); |
351 | 22 | auto& accumulator = this->accumulator(); |
352 | 22 | auto& executable = current_executable(); |
353 | 22 | auto const* bytecode = executable.bytecode.data(); |
354 | | |
355 | 22 | size_t program_counter = entry_point; |
356 | | |
357 | 22 | TemporaryChange change(m_program_counter, Optional<size_t&>(program_counter)); |
358 | | |
359 | | // Declare a lookup table for computed goto with each of the `handle_*` labels |
360 | | // to avoid the overhead of a switch statement. |
361 | | // This is a GCC extension, but it's also supported by Clang. |
362 | | |
363 | 22 | static void* const bytecode_dispatch_table[] = { |
364 | 2.83k | #define SET_UP_LABEL(name) &&handle_##name, |
365 | 2.83k | ENUMERATE_BYTECODE_OPS(SET_UP_LABEL) |
366 | 22 | }; |
367 | 22 | #undef SET_UP_LABEL |
368 | | |
369 | 22 | #define DISPATCH_NEXT(name) \ |
370 | 22 | do { \ |
371 | 4 | if constexpr (Op::name::IsVariableLength) \ |
372 | 4 | program_counter += instruction.length(); \ |
373 | 4 | else \ |
374 | 4 | program_counter += sizeof(Op::name); \ |
375 | 4 | auto& next_instruction = *reinterpret_cast<Instruction const*>(&bytecode[program_counter]); \ |
376 | 4 | goto* bytecode_dispatch_table[static_cast<size_t>(next_instruction.type())]; \ |
377 | 4 | } while (0) |
378 | | |
379 | 22 | for (;;) { |
380 | 22 | start: |
381 | 22 | for (;;) { |
382 | 22 | goto* bytecode_dispatch_table[static_cast<size_t>((*reinterpret_cast<Instruction const*>(&bytecode[program_counter])).type())]; |
383 | | |
384 | 22 | handle_GetArgument: { |
385 | 0 | auto const& instruction = *reinterpret_cast<Op::GetArgument const*>(&bytecode[program_counter]); |
386 | 0 | set(instruction.dst(), arguments[instruction.index()]); |
387 | 0 | DISPATCH_NEXT(GetArgument); |
388 | 0 | } |
389 | |
|
390 | 0 | handle_SetArgument: { |
391 | 0 | auto const& instruction = *reinterpret_cast<Op::SetArgument const*>(&bytecode[program_counter]); |
392 | 0 | arguments[instruction.index()] = get(instruction.src()); |
393 | 0 | DISPATCH_NEXT(SetArgument); |
394 | 0 | } |
395 | |
|
396 | 0 | handle_Mov: { |
397 | 0 | auto& instruction = *reinterpret_cast<Op::Mov const*>(&bytecode[program_counter]); |
398 | 0 | set(instruction.dst(), get(instruction.src())); |
399 | 0 | DISPATCH_NEXT(Mov); |
400 | 0 | } |
401 | |
|
402 | 11 | handle_End: { |
403 | 11 | auto& instruction = *reinterpret_cast<Op::End const*>(&bytecode[program_counter]); |
404 | 11 | accumulator = get(instruction.value()); |
405 | 11 | return; |
406 | 0 | } |
407 | | |
408 | 0 | handle_Jump: { |
409 | 0 | auto& instruction = *reinterpret_cast<Op::Jump const*>(&bytecode[program_counter]); |
410 | 0 | program_counter = instruction.target().address(); |
411 | 0 | goto start; |
412 | 0 | } |
413 | | |
414 | 0 | handle_JumpIf: { |
415 | 0 | auto& instruction = *reinterpret_cast<Op::JumpIf const*>(&bytecode[program_counter]); |
416 | 0 | if (get(instruction.condition()).to_boolean()) |
417 | 0 | program_counter = instruction.true_target().address(); |
418 | 0 | else |
419 | 0 | program_counter = instruction.false_target().address(); |
420 | 0 | goto start; |
421 | 0 | } |
422 | | |
423 | 0 | handle_JumpTrue: { |
424 | 0 | auto& instruction = *reinterpret_cast<Op::JumpTrue const*>(&bytecode[program_counter]); |
425 | 0 | if (get(instruction.condition()).to_boolean()) { |
426 | 0 | program_counter = instruction.target().address(); |
427 | 0 | goto start; |
428 | 0 | } |
429 | 0 | DISPATCH_NEXT(JumpTrue); |
430 | 0 | } |
431 | | |
432 | 0 | handle_JumpFalse: { |
433 | 0 | auto& instruction = *reinterpret_cast<Op::JumpFalse const*>(&bytecode[program_counter]); |
434 | 0 | if (!get(instruction.condition()).to_boolean()) { |
435 | 0 | program_counter = instruction.target().address(); |
436 | 0 | goto start; |
437 | 0 | } |
438 | 0 | DISPATCH_NEXT(JumpFalse); |
439 | 0 | } |
440 | | |
441 | 0 | handle_JumpNullish: { |
442 | 0 | auto& instruction = *reinterpret_cast<Op::JumpNullish const*>(&bytecode[program_counter]); |
443 | 0 | if (get(instruction.condition()).is_nullish()) |
444 | 0 | program_counter = instruction.true_target().address(); |
445 | 0 | else |
446 | 0 | program_counter = instruction.false_target().address(); |
447 | 0 | goto start; |
448 | 0 | } |
449 | | |
450 | 0 | #define HANDLE_COMPARISON_OP(op_TitleCase, op_snake_case, numeric_operator) \ |
451 | 0 | handle_Jump##op_TitleCase: \ |
452 | 0 | { \ |
453 | 0 | auto& instruction = *reinterpret_cast<Op::Jump##op_TitleCase const*>(&bytecode[program_counter]); \ |
454 | 0 | auto lhs = get(instruction.lhs()); \ |
455 | 0 | auto rhs = get(instruction.rhs()); \ |
456 | 0 | if (lhs.is_number() && rhs.is_number()) { \ |
457 | 0 | bool result; \ |
458 | 0 | if (lhs.is_int32() && rhs.is_int32()) { \ |
459 | 0 | result = lhs.as_i32() numeric_operator rhs.as_i32(); \ |
460 | 0 | } else { \ |
461 | 0 | result = lhs.as_double() numeric_operator rhs.as_double(); \ |
462 | 0 | } \ |
463 | 0 | program_counter = result ? instruction.true_target().address() : instruction.false_target().address(); \ |
464 | 0 | goto start; \ |
465 | 0 | } \ |
466 | 0 | auto result = op_snake_case(vm(), get(instruction.lhs()), get(instruction.rhs())); \ |
467 | 0 | if (result.is_error()) { \ |
468 | 0 | if (handle_exception(program_counter, result.error_value()) == HandleExceptionResponse::ExitFromExecutable) \ |
469 | 0 | return; \ |
470 | 0 | goto start; \ |
471 | 0 | } \ |
472 | 0 | if (result.value().to_boolean()) \ |
473 | 0 | program_counter = instruction.true_target().address(); \ |
474 | 0 | else \ |
475 | 0 | program_counter = instruction.false_target().address(); \ |
476 | 0 | goto start; \ |
477 | 0 | } |
478 | | |
479 | 0 | JS_ENUMERATE_COMPARISON_OPS(HANDLE_COMPARISON_OP) |
480 | 0 | #undef HANDLE_COMPARISON_OP |
481 | | |
482 | 0 | handle_JumpUndefined: { |
483 | 0 | auto& instruction = *reinterpret_cast<Op::JumpUndefined const*>(&bytecode[program_counter]); |
484 | 0 | if (get(instruction.condition()).is_undefined()) |
485 | 0 | program_counter = instruction.true_target().address(); |
486 | 0 | else |
487 | 0 | program_counter = instruction.false_target().address(); |
488 | 0 | goto start; |
489 | 0 | } |
490 | | |
491 | 0 | handle_EnterUnwindContext: { |
492 | 0 | auto& instruction = *reinterpret_cast<Op::EnterUnwindContext const*>(&bytecode[program_counter]); |
493 | 0 | enter_unwind_context(); |
494 | 0 | program_counter = instruction.entry_point().address(); |
495 | 0 | goto start; |
496 | 0 | } |
497 | | |
498 | 0 | handle_ContinuePendingUnwind: { |
499 | 0 | auto& instruction = *reinterpret_cast<Op::ContinuePendingUnwind const*>(&bytecode[program_counter]); |
500 | 0 | if (auto exception = reg(Register::exception()); !exception.is_empty()) { |
501 | 0 | if (handle_exception(program_counter, exception) == HandleExceptionResponse::ExitFromExecutable) |
502 | 0 | return; |
503 | 0 | goto start; |
504 | 0 | } |
505 | 0 | if (!saved_return_value().is_empty()) { |
506 | 0 | do_return(saved_return_value()); |
507 | 0 | if (auto handlers = executable.exception_handlers_for_offset(program_counter); handlers.has_value()) { |
508 | 0 | if (auto finalizer = handlers.value().finalizer_offset; finalizer.has_value()) { |
509 | 0 | VERIFY(!running_execution_context.unwind_contexts.is_empty()); |
510 | 0 | auto& unwind_context = running_execution_context.unwind_contexts.last(); |
511 | 0 | VERIFY(unwind_context.executable == m_current_executable); |
512 | 0 | reg(Register::saved_return_value()) = reg(Register::return_value()); |
513 | 0 | reg(Register::return_value()) = {}; |
514 | 0 | program_counter = finalizer.value(); |
515 | | // the unwind_context will be pop'ed when entering the finally block |
516 | 0 | goto start; |
517 | 0 | } |
518 | 0 | } |
519 | 0 | return; |
520 | 0 | } |
521 | 0 | auto const old_scheduled_jump = running_execution_context.previously_scheduled_jumps.take_last(); |
522 | 0 | if (m_scheduled_jump.has_value()) { |
523 | 0 | program_counter = m_scheduled_jump.value(); |
524 | 0 | m_scheduled_jump = {}; |
525 | 0 | } else { |
526 | 0 | program_counter = instruction.resume_target().address(); |
527 | | // set the scheduled jump to the old value if we continue |
528 | | // where we left it |
529 | 0 | m_scheduled_jump = old_scheduled_jump; |
530 | 0 | } |
531 | 0 | goto start; |
532 | 0 | } |
533 | | |
534 | 0 | handle_ScheduleJump: { |
535 | 0 | auto& instruction = *reinterpret_cast<Op::ScheduleJump const*>(&bytecode[program_counter]); |
536 | 0 | m_scheduled_jump = instruction.target().address(); |
537 | 0 | auto finalizer = executable.exception_handlers_for_offset(program_counter).value().finalizer_offset; |
538 | 0 | VERIFY(finalizer.has_value()); |
539 | 0 | program_counter = finalizer.value(); |
540 | 0 | goto start; |
541 | 0 | } |
542 | | |
543 | 0 | #define HANDLE_INSTRUCTION(name) \ |
544 | 11 | handle_##name: \ |
545 | 11 | { \ |
546 | 11 | auto& instruction = *reinterpret_cast<Op::name const*>(&bytecode[program_counter]); \ |
547 | 11 | { \ |
548 | 11 | auto result = instruction.execute_impl(*this); \ |
549 | 11 | if (result.is_error()) { \ |
550 | 11 | if (handle_exception(program_counter, result.error_value()) == HandleExceptionResponse::ExitFromExecutable) \ |
551 | 11 | return; \ |
552 | 11 | goto start; \ |
553 | 11 | } \ |
554 | 11 | } \ |
555 | 11 | DISPATCH_NEXT(name); \ |
556 | 0 | } |
557 | | |
558 | 0 | #define HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(name) \ |
559 | 4 | handle_##name: \ |
560 | 4 | { \ |
561 | 4 | auto& instruction = *reinterpret_cast<Op::name const*>(&bytecode[program_counter]); \ |
562 | 4 | instruction.execute_impl(*this); \ |
563 | 4 | DISPATCH_NEXT(name); \ |
564 | 4 | } |
565 | | |
566 | 0 | HANDLE_INSTRUCTION(Add); |
567 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(AddPrivateName); |
568 | 0 | HANDLE_INSTRUCTION(ArrayAppend); |
569 | 0 | HANDLE_INSTRUCTION(AsyncIteratorClose); |
570 | 0 | HANDLE_INSTRUCTION(BitwiseAnd); |
571 | 0 | HANDLE_INSTRUCTION(BitwiseNot); |
572 | 0 | HANDLE_INSTRUCTION(BitwiseOr); |
573 | 0 | HANDLE_INSTRUCTION(BitwiseXor); |
574 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(BlockDeclarationInstantiation); |
575 | 0 | HANDLE_INSTRUCTION(Call); |
576 | 0 | HANDLE_INSTRUCTION(CallWithArgumentArray); |
577 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(Catch); |
578 | 0 | HANDLE_INSTRUCTION(ConcatString); |
579 | 0 | HANDLE_INSTRUCTION(CopyObjectExcludingProperties); |
580 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreateLexicalEnvironment); |
581 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreateVariableEnvironment); |
582 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(CreatePrivateEnvironment); |
583 | 0 | HANDLE_INSTRUCTION(CreateVariable); |
584 | 0 | HANDLE_INSTRUCTION(CreateRestParams); |
585 | 0 | HANDLE_INSTRUCTION(CreateArguments); |
586 | 0 | HANDLE_INSTRUCTION(Decrement); |
587 | 0 | HANDLE_INSTRUCTION(DeleteById); |
588 | 0 | HANDLE_INSTRUCTION(DeleteByIdWithThis); |
589 | 0 | HANDLE_INSTRUCTION(DeleteByValue); |
590 | 0 | HANDLE_INSTRUCTION(DeleteByValueWithThis); |
591 | 0 | HANDLE_INSTRUCTION(DeleteVariable); |
592 | 0 | HANDLE_INSTRUCTION(Div); |
593 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(Dump); |
594 | 0 | HANDLE_INSTRUCTION(EnterObjectEnvironment); |
595 | 0 | HANDLE_INSTRUCTION(Exp); |
596 | 0 | HANDLE_INSTRUCTION(GetById); |
597 | 0 | HANDLE_INSTRUCTION(GetByIdWithThis); |
598 | 0 | HANDLE_INSTRUCTION(GetByValue); |
599 | 0 | HANDLE_INSTRUCTION(GetByValueWithThis); |
600 | 0 | HANDLE_INSTRUCTION(GetCalleeAndThisFromEnvironment); |
601 | 11 | HANDLE_INSTRUCTION(GetGlobal); |
602 | 11 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetImportMeta); |
603 | 0 | HANDLE_INSTRUCTION(GetIterator); |
604 | 0 | HANDLE_INSTRUCTION(GetLength); |
605 | 0 | HANDLE_INSTRUCTION(GetLengthWithThis); |
606 | 0 | HANDLE_INSTRUCTION(GetMethod); |
607 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetNewTarget); |
608 | 0 | HANDLE_INSTRUCTION(GetNextMethodFromIteratorRecord); |
609 | 0 | HANDLE_INSTRUCTION(GetObjectFromIteratorRecord); |
610 | 0 | HANDLE_INSTRUCTION(GetObjectPropertyIterator); |
611 | 0 | HANDLE_INSTRUCTION(GetPrivateById); |
612 | 0 | HANDLE_INSTRUCTION(GetBinding); |
613 | 0 | HANDLE_INSTRUCTION(GreaterThan); |
614 | 0 | HANDLE_INSTRUCTION(GreaterThanEquals); |
615 | 0 | HANDLE_INSTRUCTION(HasPrivateId); |
616 | 0 | HANDLE_INSTRUCTION(ImportCall); |
617 | 0 | HANDLE_INSTRUCTION(In); |
618 | 0 | HANDLE_INSTRUCTION(Increment); |
619 | 0 | HANDLE_INSTRUCTION(InitializeLexicalBinding); |
620 | 0 | HANDLE_INSTRUCTION(InitializeVariableBinding); |
621 | 0 | HANDLE_INSTRUCTION(InstanceOf); |
622 | 0 | HANDLE_INSTRUCTION(IteratorClose); |
623 | 0 | HANDLE_INSTRUCTION(IteratorNext); |
624 | 0 | HANDLE_INSTRUCTION(IteratorToArray); |
625 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveFinally); |
626 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveLexicalEnvironment); |
627 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeavePrivateEnvironment); |
628 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(LeaveUnwindContext); |
629 | 0 | HANDLE_INSTRUCTION(LeftShift); |
630 | 0 | HANDLE_INSTRUCTION(LessThan); |
631 | 0 | HANDLE_INSTRUCTION(LessThanEquals); |
632 | 0 | HANDLE_INSTRUCTION(LooselyEquals); |
633 | 0 | HANDLE_INSTRUCTION(LooselyInequals); |
634 | 0 | HANDLE_INSTRUCTION(Mod); |
635 | 0 | HANDLE_INSTRUCTION(Mul); |
636 | 2 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewArray); |
637 | 2 | HANDLE_INSTRUCTION(NewClass); |
638 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewFunction); |
639 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewObject); |
640 | 2 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewPrimitiveArray); |
641 | 2 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewRegExp); |
642 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(NewTypeError); |
643 | 0 | HANDLE_INSTRUCTION(Not); |
644 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(PrepareYield); |
645 | 0 | HANDLE_INSTRUCTION(PostfixDecrement); |
646 | 0 | HANDLE_INSTRUCTION(PostfixIncrement); |
647 | 0 | HANDLE_INSTRUCTION(PutById); |
648 | 0 | HANDLE_INSTRUCTION(PutByIdWithThis); |
649 | 0 | HANDLE_INSTRUCTION(PutByValue); |
650 | 0 | HANDLE_INSTRUCTION(PutByValueWithThis); |
651 | 0 | HANDLE_INSTRUCTION(PutPrivateById); |
652 | 0 | HANDLE_INSTRUCTION(ResolveSuperBase); |
653 | 0 | HANDLE_INSTRUCTION(ResolveThisBinding); |
654 | 0 | HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(RestoreScheduledJump); |
655 | 0 | HANDLE_INSTRUCTION(RightShift); |
656 | 0 | HANDLE_INSTRUCTION(SetLexicalBinding); |
657 | 0 | HANDLE_INSTRUCTION(SetVariableBinding); |
658 | 0 | HANDLE_INSTRUCTION(StrictlyEquals); |
659 | 0 | HANDLE_INSTRUCTION(StrictlyInequals); |
660 | 0 | HANDLE_INSTRUCTION(Sub); |
661 | 0 | HANDLE_INSTRUCTION(SuperCallWithArgumentArray); |
662 | 0 | HANDLE_INSTRUCTION(Throw); |
663 | 0 | HANDLE_INSTRUCTION(ThrowIfNotObject); |
664 | 0 | HANDLE_INSTRUCTION(ThrowIfNullish); |
665 | 0 | HANDLE_INSTRUCTION(ThrowIfTDZ); |
666 | 0 | HANDLE_INSTRUCTION(Typeof); |
667 | 0 | HANDLE_INSTRUCTION(TypeofBinding); |
668 | 0 | HANDLE_INSTRUCTION(UnaryMinus); |
669 | 0 | HANDLE_INSTRUCTION(UnaryPlus); |
670 | 0 | HANDLE_INSTRUCTION(UnsignedRightShift); |
671 | |
|
672 | 0 | handle_Await: { |
673 | 0 | auto& instruction = *reinterpret_cast<Op::Await const*>(&bytecode[program_counter]); |
674 | 0 | instruction.execute_impl(*this); |
675 | 0 | return; |
676 | 0 | } |
677 | | |
678 | 0 | handle_Return: { |
679 | 0 | auto& instruction = *reinterpret_cast<Op::Return const*>(&bytecode[program_counter]); |
680 | 0 | instruction.execute_impl(*this); |
681 | 0 | return; |
682 | 0 | } |
683 | | |
684 | 0 | handle_Yield: { |
685 | 0 | auto& instruction = *reinterpret_cast<Op::Yield const*>(&bytecode[program_counter]); |
686 | 0 | instruction.execute_impl(*this); |
687 | | // Note: A `yield` statement will not go through a finally statement, |
688 | | // hence we need to set a flag to not do so, |
689 | | // but we generate a Yield Operation in the case of returns in |
690 | | // generators as well, so we need to check if it will actually |
691 | | // continue or is a `return` in disguise |
692 | 0 | return; |
693 | 0 | } |
694 | 0 | } |
695 | 22 | } |
696 | 22 | } |
697 | | |
698 | | Interpreter::ResultAndReturnRegister Interpreter::run_executable(Executable& executable, Optional<size_t> entry_point, Value initial_accumulator_value) |
699 | 22 | { |
700 | 22 | dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable); |
701 | | |
702 | 22 | TemporaryChange restore_executable { m_current_executable, GCPtr { executable } }; |
703 | 22 | TemporaryChange restore_saved_jump { m_scheduled_jump, Optional<size_t> {} }; |
704 | 22 | TemporaryChange restore_realm { m_realm, GCPtr { vm().current_realm() } }; |
705 | 22 | TemporaryChange restore_global_object { m_global_object, GCPtr { m_realm->global_object() } }; |
706 | 22 | TemporaryChange restore_global_declarative_environment { m_global_declarative_environment, GCPtr { m_realm->global_environment().declarative_record() } }; |
707 | | |
708 | 22 | VERIFY(!vm().execution_context_stack().is_empty()); |
709 | | |
710 | 22 | auto& running_execution_context = vm().running_execution_context(); |
711 | 22 | u32 registers_and_constants_and_locals_count = executable.number_of_registers + executable.constants.size() + executable.local_variable_names.size(); |
712 | 22 | if (running_execution_context.registers_and_constants_and_locals.size() < registers_and_constants_and_locals_count) |
713 | 22 | running_execution_context.registers_and_constants_and_locals.resize(registers_and_constants_and_locals_count); |
714 | | |
715 | 22 | TemporaryChange restore_running_execution_context { m_running_execution_context, &running_execution_context }; |
716 | 22 | TemporaryChange restore_arguments { m_arguments, running_execution_context.arguments.span() }; |
717 | 22 | TemporaryChange restore_registers_and_constants_and_locals { m_registers_and_constants_and_locals, running_execution_context.registers_and_constants_and_locals.span() }; |
718 | | |
719 | 22 | reg(Register::accumulator()) = initial_accumulator_value; |
720 | 22 | reg(Register::return_value()) = {}; |
721 | | |
722 | | // NOTE: We only copy the `this` value from ExecutionContext if it's not already set. |
723 | | // If we are re-entering an async/generator context, the `this` value |
724 | | // may have already been cached by a ResolveThisBinding instruction, |
725 | | // and subsequent instructions expect this value to be set. |
726 | 22 | if (reg(Register::this_value()).is_empty()) |
727 | 22 | reg(Register::this_value()) = running_execution_context.this_value; |
728 | | |
729 | 22 | running_execution_context.executable = &executable; |
730 | | |
731 | 135k | for (size_t i = 0; i < executable.constants.size(); ++i) { |
732 | 135k | running_execution_context.registers_and_constants_and_locals[executable.number_of_registers + i] = executable.constants[i]; |
733 | 135k | } |
734 | | |
735 | 22 | run_bytecode(entry_point.value_or(0)); |
736 | | |
737 | 22 | dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable); |
738 | | |
739 | | if constexpr (JS_BYTECODE_DEBUG) { |
740 | | auto const& registers_and_constants_and_locals = running_execution_context.registers_and_constants_and_locals; |
741 | | for (size_t i = 0; i < executable.number_of_registers; ++i) { |
742 | | String value_string; |
743 | | if (registers_and_constants_and_locals[i].is_empty()) |
744 | | value_string = "(empty)"_string; |
745 | | else |
746 | | value_string = registers_and_constants_and_locals[i].to_string_without_side_effects(); |
747 | | dbgln("[{:3}] {}", i, value_string); |
748 | | } |
749 | | } |
750 | | |
751 | 22 | auto return_value = js_undefined(); |
752 | 22 | if (!reg(Register::return_value()).is_empty()) |
753 | 0 | return_value = reg(Register::return_value()); |
754 | 22 | else if (!reg(Register::saved_return_value()).is_empty()) |
755 | 0 | return_value = reg(Register::saved_return_value()); |
756 | 22 | auto exception = reg(Register::exception()); |
757 | | |
758 | | // At this point we may have already run any queued promise jobs via on_call_stack_emptied, |
759 | | // in which case this is a no-op. |
760 | 22 | vm().run_queued_promise_jobs(); |
761 | | |
762 | 22 | vm().finish_execution_generation(); |
763 | | |
764 | 22 | if (!exception.is_empty()) |
765 | 11 | return { throw_completion(exception), running_execution_context.registers_and_constants_and_locals[0] }; |
766 | 11 | return { return_value, running_execution_context.registers_and_constants_and_locals[0] }; |
767 | 22 | } |
768 | | |
769 | | void Interpreter::enter_unwind_context() |
770 | 0 | { |
771 | 0 | running_execution_context().unwind_contexts.empend( |
772 | 0 | m_current_executable, |
773 | 0 | running_execution_context().lexical_environment); |
774 | 0 | running_execution_context().previously_scheduled_jumps.append(m_scheduled_jump); |
775 | 0 | m_scheduled_jump = {}; |
776 | 0 | } |
777 | | |
778 | | void Interpreter::leave_unwind_context() |
779 | 0 | { |
780 | 0 | running_execution_context().unwind_contexts.take_last(); |
781 | 0 | } |
782 | | |
783 | | void Interpreter::catch_exception(Operand dst) |
784 | 0 | { |
785 | 0 | set(dst, reg(Register::exception())); |
786 | 0 | reg(Register::exception()) = {}; |
787 | 0 | auto& context = running_execution_context().unwind_contexts.last(); |
788 | 0 | VERIFY(!context.handler_called); |
789 | 0 | VERIFY(context.executable == ¤t_executable()); |
790 | 0 | context.handler_called = true; |
791 | 0 | running_execution_context().lexical_environment = context.lexical_environment; |
792 | 0 | } |
793 | | |
794 | | void Interpreter::restore_scheduled_jump() |
795 | 0 | { |
796 | 0 | m_scheduled_jump = running_execution_context().previously_scheduled_jumps.take_last(); |
797 | 0 | } |
798 | | |
799 | | void Interpreter::leave_finally() |
800 | 0 | { |
801 | 0 | reg(Register::exception()) = {}; |
802 | 0 | m_scheduled_jump = running_execution_context().previously_scheduled_jumps.take_last(); |
803 | 0 | } |
804 | | |
805 | | void Interpreter::enter_object_environment(Object& object) |
806 | 0 | { |
807 | 0 | auto& old_environment = running_execution_context().lexical_environment; |
808 | 0 | running_execution_context().saved_lexical_environments.append(old_environment); |
809 | 0 | running_execution_context().lexical_environment = new_object_environment(object, true, old_environment); |
810 | 0 | } |
811 | | |
812 | | ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, FunctionKind kind, DeprecatedFlyString const& name) |
813 | 0 | { |
814 | 0 | auto executable_result = Bytecode::Generator::generate_from_ast_node(vm, node, kind); |
815 | 0 | if (executable_result.is_error()) |
816 | 0 | return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string())); |
817 | | |
818 | 0 | auto bytecode_executable = executable_result.release_value(); |
819 | 0 | bytecode_executable->name = name; |
820 | |
|
821 | 0 | if (Bytecode::g_dump_bytecode) |
822 | 0 | bytecode_executable->dump(); |
823 | |
|
824 | 0 | return bytecode_executable; |
825 | 0 | } |
826 | | |
827 | | ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ECMAScriptFunctionObject const& function) |
828 | 0 | { |
829 | 0 | auto const& name = function.name(); |
830 | |
|
831 | 0 | auto executable_result = Bytecode::Generator::generate_from_function(vm, function); |
832 | 0 | if (executable_result.is_error()) |
833 | 0 | return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string())); |
834 | | |
835 | 0 | auto bytecode_executable = executable_result.release_value(); |
836 | 0 | bytecode_executable->name = name; |
837 | |
|
838 | 0 | if (Bytecode::g_dump_bytecode) |
839 | 0 | bytecode_executable->dump(); |
840 | |
|
841 | 0 | return bytecode_executable; |
842 | 0 | } |
843 | | |
844 | | // NOTE: This function assumes that the index is valid within the TypedArray, |
845 | | // and that the TypedArray is not detached. |
846 | | template<typename T> |
847 | | inline Value fast_typed_array_get_element(TypedArrayBase& typed_array, u32 index) |
848 | 0 | { |
849 | 0 | Checked<u32> offset_into_array_buffer = index; |
850 | 0 | offset_into_array_buffer *= sizeof(T); |
851 | 0 | offset_into_array_buffer += typed_array.byte_offset(); |
852 | |
|
853 | 0 | if (offset_into_array_buffer.has_overflow()) [[unlikely]] { |
854 | 0 | return js_undefined(); |
855 | 0 | } |
856 | | |
857 | 0 | auto const& array_buffer = *typed_array.viewed_array_buffer(); |
858 | 0 | auto const* slot = reinterpret_cast<T const*>(array_buffer.buffer().offset_pointer(offset_into_array_buffer.value())); |
859 | 0 | return Value { *slot }; |
860 | 0 | } Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<unsigned char>(JS::TypedArrayBase&, unsigned int) Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<unsigned short>(JS::TypedArrayBase&, unsigned int) Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<unsigned int>(JS::TypedArrayBase&, unsigned int) Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<signed char>(JS::TypedArrayBase&, unsigned int) Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<short>(JS::TypedArrayBase&, unsigned int) Unexecuted instantiation: JS::Value JS::Bytecode::fast_typed_array_get_element<int>(JS::TypedArrayBase&, unsigned int) |
861 | | |
862 | | // NOTE: This function assumes that the index is valid within the TypedArray, |
863 | | // and that the TypedArray is not detached. |
864 | | template<typename T> |
865 | | inline void fast_typed_array_set_element(TypedArrayBase& typed_array, u32 index, T value) |
866 | 0 | { |
867 | 0 | Checked<u32> offset_into_array_buffer = index; |
868 | 0 | offset_into_array_buffer *= sizeof(T); |
869 | 0 | offset_into_array_buffer += typed_array.byte_offset(); |
870 | |
|
871 | 0 | if (offset_into_array_buffer.has_overflow()) [[unlikely]] { |
872 | 0 | return; |
873 | 0 | } |
874 | | |
875 | 0 | auto& array_buffer = *typed_array.viewed_array_buffer(); |
876 | 0 | auto* slot = reinterpret_cast<T*>(array_buffer.buffer().offset_pointer(offset_into_array_buffer.value())); |
877 | 0 | *slot = value; |
878 | 0 | } Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<unsigned char>(JS::TypedArrayBase&, unsigned int, unsigned char) Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<unsigned short>(JS::TypedArrayBase&, unsigned int, unsigned short) Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<unsigned int>(JS::TypedArrayBase&, unsigned int, unsigned int) Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<signed char>(JS::TypedArrayBase&, unsigned int, signed char) Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<short>(JS::TypedArrayBase&, unsigned int, short) Unexecuted instantiation: void JS::Bytecode::fast_typed_array_set_element<int>(JS::TypedArrayBase&, unsigned int, int) |
879 | | |
880 | | static Completion throw_null_or_undefined_property_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, IdentifierTableIndex property_identifier, Executable const& executable) |
881 | 0 | { |
882 | 0 | VERIFY(base_value.is_nullish()); |
883 | | |
884 | 0 | if (base_identifier.has_value()) |
885 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithPropertyAndName, executable.get_identifier(property_identifier), base_value, executable.get_identifier(base_identifier.value())); |
886 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithProperty, executable.get_identifier(property_identifier), base_value); |
887 | 0 | } |
888 | | |
889 | | static Completion throw_null_or_undefined_property_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, Value property, Executable const& executable) |
890 | 0 | { |
891 | 0 | VERIFY(base_value.is_nullish()); |
892 | | |
893 | 0 | if (base_identifier.has_value()) |
894 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithPropertyAndName, property, base_value, executable.get_identifier(base_identifier.value())); |
895 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithProperty, property, base_value); |
896 | 0 | } |
897 | | |
898 | | template<typename BaseType, typename PropertyType> |
899 | | ALWAYS_INLINE Completion throw_null_or_undefined_property_access(VM& vm, Value base_value, BaseType const& base_identifier, PropertyType const& property_identifier) |
900 | 0 | { |
901 | 0 | VERIFY(base_value.is_nullish()); |
902 | | |
903 | 0 | bool has_base_identifier = true; |
904 | 0 | bool has_property_identifier = true; |
905 | |
|
906 | | if constexpr (requires { base_identifier.has_value(); }) |
907 | 0 | has_base_identifier = base_identifier.has_value(); |
908 | | if constexpr (requires { property_identifier.has_value(); }) |
909 | | has_property_identifier = property_identifier.has_value(); |
910 | |
|
911 | 0 | if (has_base_identifier && has_property_identifier) |
912 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithPropertyAndName, property_identifier, base_value, base_identifier); |
913 | 0 | if (has_property_identifier) |
914 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithProperty, property_identifier, base_value); |
915 | 0 | if (has_base_identifier) |
916 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithName, base_identifier, base_value); |
917 | 0 | return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefined); |
918 | 0 | } |
919 | | |
920 | | ALWAYS_INLINE GCPtr<Object> base_object_for_get_impl(VM& vm, Value base_value) |
921 | 0 | { |
922 | 0 | if (base_value.is_object()) [[likely]] |
923 | 0 | return base_value.as_object(); |
924 | | |
925 | | // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them. |
926 | 0 | auto& realm = *vm.current_realm(); |
927 | 0 | if (base_value.is_string()) |
928 | 0 | return realm.intrinsics().string_prototype(); |
929 | 0 | if (base_value.is_number()) |
930 | 0 | return realm.intrinsics().number_prototype(); |
931 | 0 | if (base_value.is_boolean()) |
932 | 0 | return realm.intrinsics().boolean_prototype(); |
933 | 0 | if (base_value.is_bigint()) |
934 | 0 | return realm.intrinsics().bigint_prototype(); |
935 | 0 | if (base_value.is_symbol()) |
936 | 0 | return realm.intrinsics().symbol_prototype(); |
937 | | |
938 | 0 | return nullptr; |
939 | 0 | } |
940 | | |
941 | | ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> base_object_for_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, IdentifierTableIndex property_identifier, Executable const& executable) |
942 | 0 | { |
943 | 0 | if (auto base_object = base_object_for_get_impl(vm, base_value)) |
944 | 0 | return NonnullGCPtr { *base_object }; |
945 | | |
946 | | // NOTE: At this point this is guaranteed to throw (null or undefined). |
947 | 0 | return throw_null_or_undefined_property_get(vm, base_value, base_identifier, property_identifier, executable); |
948 | 0 | } |
949 | | |
950 | | ALWAYS_INLINE ThrowCompletionOr<NonnullGCPtr<Object>> base_object_for_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, Value property, Executable const& executable) |
951 | 0 | { |
952 | 0 | if (auto base_object = base_object_for_get_impl(vm, base_value)) |
953 | 0 | return NonnullGCPtr { *base_object }; |
954 | | |
955 | | // NOTE: At this point this is guaranteed to throw (null or undefined). |
956 | 0 | return throw_null_or_undefined_property_get(vm, base_value, base_identifier, property, executable); |
957 | 0 | } |
958 | | |
959 | | enum class GetByIdMode { |
960 | | Normal, |
961 | | Length, |
962 | | }; |
963 | | |
964 | | template<GetByIdMode mode = GetByIdMode::Normal> |
965 | | inline ThrowCompletionOr<Value> get_by_id(VM& vm, Optional<IdentifierTableIndex> base_identifier, IdentifierTableIndex property, Value base_value, Value this_value, PropertyLookupCache& cache, Executable const& executable) |
966 | 0 | { |
967 | 0 | if constexpr (mode == GetByIdMode::Length) { |
968 | 0 | if (base_value.is_string()) { |
969 | 0 | return Value(base_value.as_string().utf16_string().length_in_code_units()); |
970 | 0 | } |
971 | 0 | } |
972 | | |
973 | 0 | auto base_obj = TRY(base_object_for_get(vm, base_value, base_identifier, property, executable)); |
974 | |
|
975 | 0 | if constexpr (mode == GetByIdMode::Length) { |
976 | | // OPTIMIZATION: Fast path for the magical "length" property on Array objects. |
977 | 0 | if (base_obj->has_magical_length_property()) { |
978 | 0 | return Value { base_obj->indexed_properties().array_like_size() }; |
979 | 0 | } |
980 | 0 | } |
981 | | |
982 | 0 | auto& shape = base_obj->shape(); |
983 | |
|
984 | 0 | if (cache.prototype) { |
985 | | // OPTIMIZATION: If the prototype chain hasn't been mutated in a way that would invalidate the cache, we can use it. |
986 | 0 | bool can_use_cache = [&]() -> bool { |
987 | 0 | if (&shape != cache.shape) |
988 | 0 | return false; |
989 | 0 | if (!cache.prototype_chain_validity) |
990 | 0 | return false; |
991 | 0 | if (!cache.prototype_chain_validity->is_valid()) |
992 | 0 | return false; |
993 | 0 | return true; |
994 | 0 | }(); Unexecuted instantiation: JS::Bytecode::get_by_id<(JS::Bytecode::GetByIdMode)0>(JS::VM&, AK::Optional<JS::Bytecode::IdentifierTableIndex>, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::Value, JS::Bytecode::PropertyLookupCache&, JS::Bytecode::Executable const&)::{lambda()#1}::operator()() constUnexecuted instantiation: JS::Bytecode::get_by_id<(JS::Bytecode::GetByIdMode)1>(JS::VM&, AK::Optional<JS::Bytecode::IdentifierTableIndex>, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::Value, JS::Bytecode::PropertyLookupCache&, JS::Bytecode::Executable const&)::{lambda()#1}::operator()() const |
995 | 0 | if (can_use_cache) { |
996 | 0 | auto value = cache.prototype->get_direct(cache.property_offset.value()); |
997 | 0 | if (value.is_accessor()) |
998 | 0 | return TRY(call(vm, value.as_accessor().getter(), this_value)); |
999 | 0 | return value; |
1000 | 0 | } |
1001 | 0 | } else if (&shape == cache.shape) { |
1002 | | // OPTIMIZATION: If the shape of the object hasn't changed, we can use the cached property offset. |
1003 | 0 | auto value = base_obj->get_direct(cache.property_offset.value()); |
1004 | 0 | if (value.is_accessor()) |
1005 | 0 | return TRY(call(vm, value.as_accessor().getter(), this_value)); |
1006 | 0 | return value; |
1007 | 0 | } |
1008 | | |
1009 | 0 | CacheablePropertyMetadata cacheable_metadata; |
1010 | 0 | auto value = TRY(base_obj->internal_get(executable.get_identifier(property), this_value, &cacheable_metadata)); |
1011 | |
|
1012 | 0 | if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) { |
1013 | 0 | cache = {}; |
1014 | 0 | cache.shape = shape; |
1015 | 0 | cache.property_offset = cacheable_metadata.property_offset.value(); |
1016 | 0 | } else if (cacheable_metadata.type == CacheablePropertyMetadata::Type::InPrototypeChain) { |
1017 | 0 | cache = {}; |
1018 | 0 | cache.shape = &base_obj->shape(); |
1019 | 0 | cache.property_offset = cacheable_metadata.property_offset.value(); |
1020 | 0 | cache.prototype = *cacheable_metadata.prototype; |
1021 | 0 | cache.prototype_chain_validity = *cacheable_metadata.prototype->shape().prototype_chain_validity(); |
1022 | 0 | } |
1023 | |
|
1024 | 0 | return value; |
1025 | 0 | } Unexecuted instantiation: JS::ThrowCompletionOr<JS::Value> JS::Bytecode::get_by_id<(JS::Bytecode::GetByIdMode)0>(JS::VM&, AK::Optional<JS::Bytecode::IdentifierTableIndex>, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::Value, JS::Bytecode::PropertyLookupCache&, JS::Bytecode::Executable const&) Unexecuted instantiation: JS::ThrowCompletionOr<JS::Value> JS::Bytecode::get_by_id<(JS::Bytecode::GetByIdMode)1>(JS::VM&, AK::Optional<JS::Bytecode::IdentifierTableIndex>, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::Value, JS::Bytecode::PropertyLookupCache&, JS::Bytecode::Executable const&) |
1026 | | |
1027 | | inline ThrowCompletionOr<Value> get_by_value(VM& vm, Optional<IdentifierTableIndex> base_identifier, Value base_value, Value property_key_value, Executable const& executable) |
1028 | 0 | { |
1029 | | // OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects. |
1030 | 0 | if (base_value.is_object() && property_key_value.is_int32() && property_key_value.as_i32() >= 0) { |
1031 | 0 | auto& object = base_value.as_object(); |
1032 | 0 | auto index = static_cast<u32>(property_key_value.as_i32()); |
1033 | |
|
1034 | 0 | auto const* object_storage = object.indexed_properties().storage(); |
1035 | | |
1036 | | // For "non-typed arrays": |
1037 | 0 | if (!object.may_interfere_with_indexed_property_access() |
1038 | 0 | && object_storage) { |
1039 | 0 | auto maybe_value = [&] { |
1040 | 0 | if (object_storage->is_simple_storage()) |
1041 | 0 | return static_cast<SimpleIndexedPropertyStorage const*>(object_storage)->inline_get(index); |
1042 | 0 | else |
1043 | 0 | return static_cast<GenericIndexedPropertyStorage const*>(object_storage)->get(index); |
1044 | 0 | }(); |
1045 | 0 | if (maybe_value.has_value()) { |
1046 | 0 | auto value = maybe_value->value; |
1047 | 0 | if (!value.is_accessor()) |
1048 | 0 | return value; |
1049 | 0 | } |
1050 | 0 | } |
1051 | | |
1052 | | // For typed arrays: |
1053 | 0 | if (object.is_typed_array()) { |
1054 | 0 | auto& typed_array = static_cast<TypedArrayBase&>(object); |
1055 | 0 | auto canonical_index = CanonicalIndex { CanonicalIndex::Type::Index, index }; |
1056 | |
|
1057 | 0 | if (is_valid_integer_index(typed_array, canonical_index)) { |
1058 | 0 | switch (typed_array.kind()) { |
1059 | 0 | case TypedArrayBase::Kind::Uint8Array: |
1060 | 0 | return fast_typed_array_get_element<u8>(typed_array, index); |
1061 | 0 | case TypedArrayBase::Kind::Uint16Array: |
1062 | 0 | return fast_typed_array_get_element<u16>(typed_array, index); |
1063 | 0 | case TypedArrayBase::Kind::Uint32Array: |
1064 | 0 | return fast_typed_array_get_element<u32>(typed_array, index); |
1065 | 0 | case TypedArrayBase::Kind::Int8Array: |
1066 | 0 | return fast_typed_array_get_element<i8>(typed_array, index); |
1067 | 0 | case TypedArrayBase::Kind::Int16Array: |
1068 | 0 | return fast_typed_array_get_element<i16>(typed_array, index); |
1069 | 0 | case TypedArrayBase::Kind::Int32Array: |
1070 | 0 | return fast_typed_array_get_element<i32>(typed_array, index); |
1071 | 0 | case TypedArrayBase::Kind::Uint8ClampedArray: |
1072 | 0 | return fast_typed_array_get_element<u8>(typed_array, index); |
1073 | 0 | default: |
1074 | | // FIXME: Support more TypedArray kinds. |
1075 | 0 | break; |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | 0 | switch (typed_array.kind()) { |
1080 | 0 | #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \ |
1081 | 0 | case TypedArrayBase::Kind::ClassName: \ |
1082 | 0 | return typed_array_get_element<Type>(typed_array, canonical_index); |
1083 | 0 | JS_ENUMERATE_TYPED_ARRAYS |
1084 | 0 | #undef __JS_ENUMERATE |
1085 | 0 | } |
1086 | 0 | } |
1087 | 0 | } |
1088 | | |
1089 | 0 | auto object = TRY(base_object_for_get(vm, base_value, base_identifier, property_key_value, executable)); |
1090 | |
|
1091 | 0 | auto property_key = TRY(property_key_value.to_property_key(vm)); |
1092 | |
|
1093 | 0 | if (base_value.is_string()) { |
1094 | 0 | auto string_value = TRY(base_value.as_string().get(vm, property_key)); |
1095 | 0 | if (string_value.has_value()) |
1096 | 0 | return *string_value; |
1097 | 0 | } |
1098 | | |
1099 | 0 | return TRY(object->internal_get(property_key, base_value)); |
1100 | 0 | } |
1101 | | |
1102 | | inline ThrowCompletionOr<Value> get_global(Interpreter& interpreter, IdentifierTableIndex identifier_index, GlobalVariableCache& cache) |
1103 | 11 | { |
1104 | 11 | auto& vm = interpreter.vm(); |
1105 | 11 | auto& binding_object = interpreter.global_object(); |
1106 | 11 | auto& declarative_record = interpreter.global_declarative_environment(); |
1107 | | |
1108 | 11 | auto& shape = binding_object.shape(); |
1109 | 11 | if (cache.environment_serial_number == declarative_record.environment_serial_number()) { |
1110 | | |
1111 | | // OPTIMIZATION: For global var bindings, if the shape of the global object hasn't changed, |
1112 | | // we can use the cached property offset. |
1113 | 11 | if (&shape == cache.shape) { |
1114 | 0 | auto value = binding_object.get_direct(cache.property_offset.value()); |
1115 | 0 | if (value.is_accessor()) |
1116 | 0 | return TRY(call(vm, value.as_accessor().getter(), js_undefined())); |
1117 | 0 | return value; |
1118 | 0 | } |
1119 | | |
1120 | | // OPTIMIZATION: For global lexical bindings, if the global declarative environment hasn't changed, |
1121 | | // we can use the cached environment binding index. |
1122 | 11 | if (cache.environment_binding_index.has_value()) |
1123 | 0 | return declarative_record.get_binding_value_direct(vm, cache.environment_binding_index.value()); |
1124 | 11 | } |
1125 | | |
1126 | 11 | cache.environment_serial_number = declarative_record.environment_serial_number(); |
1127 | | |
1128 | 11 | auto& identifier = interpreter.current_executable().get_identifier(identifier_index); |
1129 | | |
1130 | 11 | if (vm.running_execution_context().script_or_module.has<NonnullGCPtr<Module>>()) { |
1131 | | // NOTE: GetGlobal is used to access variables stored in the module environment and global environment. |
1132 | | // The module environment is checked first since it precedes the global environment in the environment chain. |
1133 | 0 | auto& module_environment = *vm.running_execution_context().script_or_module.get<NonnullGCPtr<Module>>()->environment(); |
1134 | 0 | if (TRY(module_environment.has_binding(identifier))) { |
1135 | | // TODO: Cache offset of binding value |
1136 | 0 | return TRY(module_environment.get_binding_value(vm, identifier, vm.in_strict_mode())); |
1137 | 0 | } |
1138 | 0 | } |
1139 | | |
1140 | 11 | Optional<size_t> offset; |
1141 | 11 | if (TRY(declarative_record.has_binding(identifier, &offset))) { |
1142 | 0 | cache.environment_binding_index = static_cast<u32>(offset.value()); |
1143 | 0 | return TRY(declarative_record.get_binding_value(vm, identifier, vm.in_strict_mode())); |
1144 | 0 | } |
1145 | | |
1146 | 11 | if (TRY(binding_object.has_property(identifier))) { |
1147 | 0 | CacheablePropertyMetadata cacheable_metadata; |
1148 | 0 | auto value = TRY(binding_object.internal_get(identifier, js_undefined(), &cacheable_metadata)); |
1149 | 0 | if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) { |
1150 | 0 | cache.shape = shape; |
1151 | 0 | cache.property_offset = cacheable_metadata.property_offset.value(); |
1152 | 0 | } |
1153 | 0 | return value; |
1154 | 0 | } |
1155 | | |
1156 | 11 | return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, identifier); |
1157 | 11 | } |
1158 | | |
1159 | | inline ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value this_value, Value value, Optional<DeprecatedFlyString const&> const& base_identifier, PropertyKey name, Op::PropertyKind kind, PropertyLookupCache* cache = nullptr) |
1160 | 0 | { |
1161 | | // Better error message than to_object would give |
1162 | 0 | if (vm.in_strict_mode() && base.is_nullish()) |
1163 | 0 | return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, base.to_string_without_side_effects()); |
1164 | | |
1165 | | // a. Let baseObj be ? ToObject(V.[[Base]]). |
1166 | 0 | auto maybe_object = base.to_object(vm); |
1167 | 0 | if (maybe_object.is_error()) |
1168 | 0 | return throw_null_or_undefined_property_access(vm, base, base_identifier, name); |
1169 | 0 | auto object = maybe_object.release_value(); |
1170 | |
|
1171 | 0 | if (kind == Op::PropertyKind::Getter || kind == Op::PropertyKind::Setter) { |
1172 | | // The generator should only pass us functions for getters and setters. |
1173 | 0 | VERIFY(value.is_function()); |
1174 | 0 | } |
1175 | 0 | switch (kind) { |
1176 | 0 | case Op::PropertyKind::Getter: { |
1177 | 0 | auto& function = value.as_function(); |
1178 | 0 | if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function)) |
1179 | 0 | static_cast<ECMAScriptFunctionObject*>(&function)->set_name(ByteString::formatted("get {}", name)); |
1180 | 0 | object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable); |
1181 | 0 | break; |
1182 | 0 | } |
1183 | 0 | case Op::PropertyKind::Setter: { |
1184 | 0 | auto& function = value.as_function(); |
1185 | 0 | if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function)) |
1186 | 0 | static_cast<ECMAScriptFunctionObject*>(&function)->set_name(ByteString::formatted("set {}", name)); |
1187 | 0 | object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable); |
1188 | 0 | break; |
1189 | 0 | } |
1190 | 0 | case Op::PropertyKind::KeyValue: { |
1191 | 0 | if (cache && cache->shape == &object->shape()) { |
1192 | 0 | object->put_direct(*cache->property_offset, value); |
1193 | 0 | return {}; |
1194 | 0 | } |
1195 | | |
1196 | 0 | CacheablePropertyMetadata cacheable_metadata; |
1197 | 0 | bool succeeded = TRY(object->internal_set(name, value, this_value, &cacheable_metadata)); |
1198 | |
|
1199 | 0 | if (succeeded && cache && cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) { |
1200 | 0 | cache->shape = object->shape(); |
1201 | 0 | cache->property_offset = cacheable_metadata.property_offset.value(); |
1202 | 0 | } |
1203 | |
|
1204 | 0 | if (!succeeded && vm.in_strict_mode()) { |
1205 | 0 | if (base.is_object()) |
1206 | 0 | return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, base.to_string_without_side_effects()); |
1207 | 0 | return vm.throw_completion<TypeError>(ErrorType::ReferencePrimitiveSetProperty, name, base.typeof_(vm)->utf8_string(), base.to_string_without_side_effects()); |
1208 | 0 | } |
1209 | 0 | break; |
1210 | 0 | } |
1211 | 0 | case Op::PropertyKind::DirectKeyValue: |
1212 | 0 | object->define_direct_property(name, value, Attribute::Enumerable | Attribute::Writable | Attribute::Configurable); |
1213 | 0 | break; |
1214 | 0 | case Op::PropertyKind::Spread: |
1215 | 0 | TRY(object->copy_data_properties(vm, value, {})); |
1216 | 0 | break; |
1217 | 0 | case Op::PropertyKind::ProtoSetter: |
1218 | 0 | if (value.is_object() || value.is_null()) |
1219 | 0 | MUST(object->internal_set_prototype_of(value.is_object() ? &value.as_object() : nullptr)); |
1220 | 0 | break; |
1221 | 0 | } |
1222 | | |
1223 | 0 | return {}; |
1224 | 0 | } |
1225 | | |
1226 | | inline ThrowCompletionOr<Value> perform_call(Interpreter& interpreter, Value this_value, Op::CallType call_type, Value callee, ReadonlySpan<Value> argument_values) |
1227 | 0 | { |
1228 | 0 | auto& vm = interpreter.vm(); |
1229 | 0 | auto& function = callee.as_function(); |
1230 | 0 | Value return_value; |
1231 | 0 | if (call_type == Op::CallType::DirectEval) { |
1232 | 0 | if (callee == interpreter.realm().intrinsics().eval_function()) |
1233 | 0 | return_value = TRY(perform_eval(vm, !argument_values.is_empty() ? argument_values[0].value_or(JS::js_undefined()) : js_undefined(), vm.in_strict_mode() ? CallerMode::Strict : CallerMode::NonStrict, EvalMode::Direct)); |
1234 | 0 | else |
1235 | 0 | return_value = TRY(JS::call(vm, function, this_value, argument_values)); |
1236 | 0 | } else if (call_type == Op::CallType::Call) |
1237 | 0 | return_value = TRY(JS::call(vm, function, this_value, argument_values)); |
1238 | 0 | else |
1239 | 0 | return_value = TRY(construct(vm, function, argument_values)); |
1240 | |
|
1241 | 0 | return return_value; |
1242 | 0 | } |
1243 | | |
1244 | | static inline Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter, Value callee, StringView callee_type, Optional<StringTableIndex> const& expression_string) |
1245 | 0 | { |
1246 | 0 | auto& vm = interpreter.vm(); |
1247 | |
|
1248 | 0 | if (expression_string.has_value()) |
1249 | 0 | return vm.throw_completion<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), callee_type, interpreter.current_executable().get_string(expression_string->value())); |
1250 | | |
1251 | 0 | return vm.throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), callee_type); |
1252 | 0 | } |
1253 | | |
1254 | | inline ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, Value callee, Op::CallType call_type, Optional<StringTableIndex> const& expression_string) |
1255 | 0 | { |
1256 | 0 | if ((call_type == Op::CallType::Call || call_type == Op::CallType::DirectEval) |
1257 | 0 | && !callee.is_function()) |
1258 | 0 | return throw_type_error_for_callee(interpreter, callee, "function"sv, expression_string); |
1259 | 0 | if (call_type == Op::CallType::Construct && !callee.is_constructor()) |
1260 | 0 | return throw_type_error_for_callee(interpreter, callee, "constructor"sv, expression_string); |
1261 | 0 | return {}; |
1262 | 0 | } |
1263 | | |
1264 | | inline Value new_function(VM& vm, FunctionNode const& function_node, Optional<IdentifierTableIndex> const& lhs_name, Optional<Operand> const& home_object) |
1265 | 0 | { |
1266 | 0 | Value value; |
1267 | |
|
1268 | 0 | if (!function_node.has_name()) { |
1269 | 0 | DeprecatedFlyString name = {}; |
1270 | 0 | if (lhs_name.has_value()) |
1271 | 0 | name = vm.bytecode_interpreter().current_executable().get_identifier(lhs_name.value()); |
1272 | 0 | value = function_node.instantiate_ordinary_function_expression(vm, name); |
1273 | 0 | } else { |
1274 | 0 | value = ECMAScriptFunctionObject::create(*vm.current_realm(), function_node.name(), function_node.source_text(), function_node.body(), function_node.parameters(), function_node.function_length(), function_node.local_variables_names(), vm.lexical_environment(), vm.running_execution_context().private_environment, function_node.kind(), function_node.is_strict_mode(), |
1275 | 0 | function_node.parsing_insights(), function_node.is_arrow_function()); |
1276 | 0 | } |
1277 | |
|
1278 | 0 | if (home_object.has_value()) { |
1279 | 0 | auto home_object_value = vm.bytecode_interpreter().get(home_object.value()); |
1280 | 0 | static_cast<ECMAScriptFunctionObject&>(value.as_function()).set_home_object(&home_object_value.as_object()); |
1281 | 0 | } |
1282 | |
|
1283 | 0 | return value; |
1284 | 0 | } |
1285 | | |
1286 | | inline ThrowCompletionOr<void> put_by_value(VM& vm, Value base, Optional<DeprecatedFlyString const&> const& base_identifier, Value property_key_value, Value value, Op::PropertyKind kind) |
1287 | 0 | { |
1288 | | // OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects. |
1289 | 0 | if ((kind == Op::PropertyKind::KeyValue || kind == Op::PropertyKind::DirectKeyValue) |
1290 | 0 | && base.is_object() && property_key_value.is_int32() && property_key_value.as_i32() >= 0) { |
1291 | 0 | auto& object = base.as_object(); |
1292 | 0 | auto* storage = object.indexed_properties().storage(); |
1293 | 0 | auto index = static_cast<u32>(property_key_value.as_i32()); |
1294 | | |
1295 | | // For "non-typed arrays": |
1296 | 0 | if (storage |
1297 | 0 | && storage->is_simple_storage() |
1298 | 0 | && !object.may_interfere_with_indexed_property_access()) { |
1299 | 0 | auto maybe_value = storage->get(index); |
1300 | 0 | if (maybe_value.has_value()) { |
1301 | 0 | auto existing_value = maybe_value->value; |
1302 | 0 | if (!existing_value.is_accessor()) { |
1303 | 0 | storage->put(index, value); |
1304 | 0 | return {}; |
1305 | 0 | } |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | | // For typed arrays: |
1310 | 0 | if (object.is_typed_array()) { |
1311 | 0 | auto& typed_array = static_cast<TypedArrayBase&>(object); |
1312 | 0 | auto canonical_index = CanonicalIndex { CanonicalIndex::Type::Index, index }; |
1313 | |
|
1314 | 0 | if (value.is_int32() && is_valid_integer_index(typed_array, canonical_index)) { |
1315 | 0 | switch (typed_array.kind()) { |
1316 | 0 | case TypedArrayBase::Kind::Uint8Array: |
1317 | 0 | fast_typed_array_set_element<u8>(typed_array, index, static_cast<u8>(value.as_i32())); |
1318 | 0 | return {}; |
1319 | 0 | case TypedArrayBase::Kind::Uint16Array: |
1320 | 0 | fast_typed_array_set_element<u16>(typed_array, index, static_cast<u16>(value.as_i32())); |
1321 | 0 | return {}; |
1322 | 0 | case TypedArrayBase::Kind::Uint32Array: |
1323 | 0 | fast_typed_array_set_element<u32>(typed_array, index, static_cast<u32>(value.as_i32())); |
1324 | 0 | return {}; |
1325 | 0 | case TypedArrayBase::Kind::Int8Array: |
1326 | 0 | fast_typed_array_set_element<i8>(typed_array, index, static_cast<i8>(value.as_i32())); |
1327 | 0 | return {}; |
1328 | 0 | case TypedArrayBase::Kind::Int16Array: |
1329 | 0 | fast_typed_array_set_element<i16>(typed_array, index, static_cast<i16>(value.as_i32())); |
1330 | 0 | return {}; |
1331 | 0 | case TypedArrayBase::Kind::Int32Array: |
1332 | 0 | fast_typed_array_set_element<i32>(typed_array, index, value.as_i32()); |
1333 | 0 | return {}; |
1334 | 0 | case TypedArrayBase::Kind::Uint8ClampedArray: |
1335 | 0 | fast_typed_array_set_element<u8>(typed_array, index, clamp(value.as_i32(), 0, 255)); |
1336 | 0 | return {}; |
1337 | 0 | default: |
1338 | | // FIXME: Support more TypedArray kinds. |
1339 | 0 | break; |
1340 | 0 | } |
1341 | 0 | } |
1342 | | |
1343 | 0 | if (typed_array.kind() == TypedArrayBase::Kind::Uint32Array && value.is_integral_number()) { |
1344 | 0 | auto integer = value.as_double(); |
1345 | |
|
1346 | 0 | if (AK::is_within_range<u32>(integer) && is_valid_integer_index(typed_array, canonical_index)) { |
1347 | 0 | fast_typed_array_set_element<u32>(typed_array, index, static_cast<u32>(integer)); |
1348 | 0 | return {}; |
1349 | 0 | } |
1350 | 0 | } |
1351 | | |
1352 | 0 | switch (typed_array.kind()) { |
1353 | 0 | #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \ |
1354 | 0 | case TypedArrayBase::Kind::ClassName: \ |
1355 | 0 | return typed_array_set_element<Type>(typed_array, canonical_index, value); |
1356 | 0 | JS_ENUMERATE_TYPED_ARRAYS |
1357 | 0 | #undef __JS_ENUMERATE |
1358 | 0 | } |
1359 | 0 | return {}; |
1360 | 0 | } |
1361 | 0 | } |
1362 | | |
1363 | 0 | auto property_key = kind != Op::PropertyKind::Spread ? TRY(property_key_value.to_property_key(vm)) : PropertyKey {}; |
1364 | 0 | TRY(put_by_property_key(vm, base, base, value, base_identifier, property_key, kind)); |
1365 | 0 | return {}; |
1366 | 0 | } |
1367 | | |
1368 | | struct CalleeAndThis { |
1369 | | Value callee; |
1370 | | Value this_value; |
1371 | | }; |
1372 | | |
1373 | | inline ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(Bytecode::Interpreter& interpreter, DeprecatedFlyString const& name, EnvironmentCoordinate& cache) |
1374 | 0 | { |
1375 | 0 | auto& vm = interpreter.vm(); |
1376 | |
|
1377 | 0 | Value callee = js_undefined(); |
1378 | 0 | Value this_value = js_undefined(); |
1379 | |
|
1380 | 0 | if (cache.is_valid()) { |
1381 | 0 | auto const* environment = interpreter.running_execution_context().lexical_environment.ptr(); |
1382 | 0 | for (size_t i = 0; i < cache.hops; ++i) |
1383 | 0 | environment = environment->outer_environment(); |
1384 | 0 | if (!environment->is_permanently_screwed_by_eval()) { |
1385 | 0 | callee = TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, cache.index)); |
1386 | 0 | this_value = js_undefined(); |
1387 | 0 | if (auto base_object = environment->with_base_object()) |
1388 | 0 | this_value = base_object; |
1389 | 0 | return CalleeAndThis { |
1390 | 0 | .callee = callee, |
1391 | 0 | .this_value = this_value, |
1392 | 0 | }; |
1393 | 0 | } |
1394 | 0 | cache = {}; |
1395 | 0 | } |
1396 | | |
1397 | 0 | auto reference = TRY(vm.resolve_binding(name)); |
1398 | 0 | if (reference.environment_coordinate().has_value()) |
1399 | 0 | cache = reference.environment_coordinate().value(); |
1400 | |
|
1401 | 0 | callee = TRY(reference.get_value(vm)); |
1402 | |
|
1403 | 0 | if (reference.is_property_reference()) { |
1404 | 0 | this_value = reference.get_this_value(); |
1405 | 0 | } else { |
1406 | 0 | if (reference.is_environment_reference()) { |
1407 | 0 | if (auto base_object = reference.base_environment().with_base_object(); base_object != nullptr) |
1408 | 0 | this_value = base_object; |
1409 | 0 | } |
1410 | 0 | } |
1411 | |
|
1412 | 0 | return CalleeAndThis { |
1413 | 0 | .callee = callee, |
1414 | 0 | .this_value = this_value, |
1415 | 0 | }; |
1416 | 0 | } |
1417 | | |
1418 | | // 13.2.7.3 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-regular-expression-literals-runtime-semantics-evaluation |
1419 | | inline Value new_regexp(VM& vm, ParsedRegex const& parsed_regex, ByteString const& pattern, ByteString const& flags) |
1420 | 0 | { |
1421 | | // 1. Let pattern be CodePointsToString(BodyText of RegularExpressionLiteral). |
1422 | | // 2. Let flags be CodePointsToString(FlagText of RegularExpressionLiteral). |
1423 | | |
1424 | | // 3. Return ! RegExpCreate(pattern, flags). |
1425 | 0 | auto& realm = *vm.current_realm(); |
1426 | 0 | Regex<ECMA262> regex(parsed_regex.regex, parsed_regex.pattern, parsed_regex.flags); |
1427 | | // NOTE: We bypass RegExpCreate and subsequently RegExpAlloc as an optimization to use the already parsed values. |
1428 | 0 | auto regexp_object = RegExpObject::create(realm, move(regex), pattern, flags); |
1429 | | // RegExpAlloc has these two steps from the 'Legacy RegExp features' proposal. |
1430 | 0 | regexp_object->set_realm(realm); |
1431 | | // We don't need to check 'If SameValue(newTarget, thisRealm.[[Intrinsics]].[[%RegExp%]]) is true' |
1432 | | // here as we know RegExpCreate calls RegExpAlloc with %RegExp% for newTarget. |
1433 | 0 | regexp_object->set_legacy_features_enabled(true); |
1434 | 0 | return regexp_object; |
1435 | 0 | } |
1436 | | |
1437 | | // 13.3.8.1 https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation |
1438 | | inline MarkedVector<Value> argument_list_evaluation(VM& vm, Value arguments) |
1439 | 0 | { |
1440 | | // Note: Any spreading and actual evaluation is handled in preceding opcodes |
1441 | | // Note: The spec uses the concept of a list, while we create a temporary array |
1442 | | // in the preceding opcodes, so we have to convert in a manner that is not |
1443 | | // visible to the user |
1444 | 0 | MarkedVector<Value> argument_values { vm.heap() }; |
1445 | |
|
1446 | 0 | auto& argument_array = arguments.as_array(); |
1447 | 0 | auto array_length = argument_array.indexed_properties().array_like_size(); |
1448 | |
|
1449 | 0 | argument_values.ensure_capacity(array_length); |
1450 | |
|
1451 | 0 | for (size_t i = 0; i < array_length; ++i) { |
1452 | 0 | if (auto maybe_value = argument_array.indexed_properties().get(i); maybe_value.has_value()) |
1453 | 0 | argument_values.append(maybe_value.release_value().value); |
1454 | 0 | else |
1455 | 0 | argument_values.append(js_undefined()); |
1456 | 0 | } |
1457 | |
|
1458 | 0 | return argument_values; |
1459 | 0 | } |
1460 | | |
1461 | | inline ThrowCompletionOr<void> create_variable(VM& vm, DeprecatedFlyString const& name, Op::EnvironmentMode mode, bool is_global, bool is_immutable, bool is_strict) |
1462 | 0 | { |
1463 | 0 | if (mode == Op::EnvironmentMode::Lexical) { |
1464 | 0 | VERIFY(!is_global); |
1465 | | |
1466 | | // Note: This is papering over an issue where "FunctionDeclarationInstantiation" creates these bindings for us. |
1467 | | // Instead of crashing in there, we'll just raise an exception here. |
1468 | 0 | if (TRY(vm.lexical_environment()->has_binding(name))) |
1469 | 0 | return vm.throw_completion<InternalError>(TRY_OR_THROW_OOM(vm, String::formatted("Lexical environment already has binding '{}'", name))); |
1470 | | |
1471 | 0 | if (is_immutable) |
1472 | 0 | return vm.lexical_environment()->create_immutable_binding(vm, name, is_strict); |
1473 | 0 | return vm.lexical_environment()->create_mutable_binding(vm, name, is_strict); |
1474 | 0 | } |
1475 | | |
1476 | 0 | if (!is_global) { |
1477 | 0 | if (is_immutable) |
1478 | 0 | return vm.variable_environment()->create_immutable_binding(vm, name, is_strict); |
1479 | 0 | return vm.variable_environment()->create_mutable_binding(vm, name, is_strict); |
1480 | 0 | } |
1481 | | |
1482 | | // NOTE: CreateVariable with m_is_global set to true is expected to only be used in GlobalDeclarationInstantiation currently, which only uses "false" for "can_be_deleted". |
1483 | | // The only area that sets "can_be_deleted" to true is EvalDeclarationInstantiation, which is currently fully implemented in C++ and not in Bytecode. |
1484 | 0 | return verify_cast<GlobalEnvironment>(vm.variable_environment())->create_global_var_binding(name, false); |
1485 | 0 | } |
1486 | | |
1487 | | inline ThrowCompletionOr<ECMAScriptFunctionObject*> new_class(VM& vm, Value super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> const& lhs_name, ReadonlySpan<Value> element_keys) |
1488 | 0 | { |
1489 | 0 | auto& interpreter = vm.bytecode_interpreter(); |
1490 | 0 | auto name = class_expression.name(); |
1491 | | |
1492 | | // NOTE: NewClass expects classEnv to be active lexical environment |
1493 | 0 | auto* class_environment = vm.lexical_environment(); |
1494 | 0 | vm.running_execution_context().lexical_environment = vm.running_execution_context().saved_lexical_environments.take_last(); |
1495 | |
|
1496 | 0 | Optional<DeprecatedFlyString> binding_name; |
1497 | 0 | DeprecatedFlyString class_name; |
1498 | 0 | if (!class_expression.has_name() && lhs_name.has_value()) { |
1499 | 0 | class_name = interpreter.current_executable().get_identifier(lhs_name.value()); |
1500 | 0 | } else { |
1501 | 0 | binding_name = name; |
1502 | 0 | class_name = name.is_null() ? ""sv : name; |
1503 | 0 | } |
1504 | |
|
1505 | 0 | return TRY(class_expression.create_class_constructor(vm, class_environment, vm.lexical_environment(), super_class, element_keys, binding_name, class_name)); |
1506 | 0 | } |
1507 | | |
1508 | | // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation |
1509 | | inline ThrowCompletionOr<NonnullGCPtr<Object>> super_call_with_argument_array(VM& vm, Value argument_array, bool is_synthetic) |
1510 | 0 | { |
1511 | | // 1. Let newTarget be GetNewTarget(). |
1512 | 0 | auto new_target = vm.get_new_target(); |
1513 | | |
1514 | | // 2. Assert: Type(newTarget) is Object. |
1515 | 0 | VERIFY(new_target.is_object()); |
1516 | | |
1517 | | // 3. Let func be GetSuperConstructor(). |
1518 | 0 | auto* func = get_super_constructor(vm); |
1519 | | |
1520 | | // 4. Let argList be ? ArgumentListEvaluation of Arguments. |
1521 | 0 | MarkedVector<Value> arg_list { vm.heap() }; |
1522 | 0 | if (is_synthetic) { |
1523 | 0 | VERIFY(argument_array.is_object() && is<Array>(argument_array.as_object())); |
1524 | 0 | auto const& array_value = static_cast<Array const&>(argument_array.as_object()); |
1525 | 0 | auto length = MUST(length_of_array_like(vm, array_value)); |
1526 | 0 | for (size_t i = 0; i < length; ++i) |
1527 | 0 | arg_list.append(array_value.get_without_side_effects(PropertyKey { i })); |
1528 | 0 | } else { |
1529 | 0 | arg_list = argument_list_evaluation(vm, argument_array); |
1530 | 0 | } |
1531 | | |
1532 | | // 5. If IsConstructor(func) is false, throw a TypeError exception. |
1533 | 0 | if (!Value(func).is_constructor()) |
1534 | 0 | return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, "Super constructor"); |
1535 | | |
1536 | | // 6. Let result be ? Construct(func, argList, newTarget). |
1537 | 0 | auto result = TRY(construct(vm, static_cast<FunctionObject&>(*func), arg_list.span(), &new_target.as_function())); |
1538 | | |
1539 | | // 7. Let thisER be GetThisEnvironment(). |
1540 | 0 | auto& this_environment = verify_cast<FunctionEnvironment>(*get_this_environment(vm)); |
1541 | | |
1542 | | // 8. Perform ? thisER.BindThisValue(result). |
1543 | 0 | TRY(this_environment.bind_this_value(vm, result)); |
1544 | | |
1545 | | // 9. Let F be thisER.[[FunctionObject]]. |
1546 | 0 | auto& f = this_environment.function_object(); |
1547 | | |
1548 | | // 10. Assert: F is an ECMAScript function object. |
1549 | | // NOTE: This is implied by the strong C++ type. |
1550 | | |
1551 | | // 11. Perform ? InitializeInstanceElements(result, F). |
1552 | 0 | TRY(result->initialize_instance_elements(f)); |
1553 | | |
1554 | | // 12. Return result. |
1555 | 0 | return result; |
1556 | 0 | } |
1557 | | |
1558 | | inline ThrowCompletionOr<NonnullGCPtr<Array>> iterator_to_array(VM& vm, Value iterator) |
1559 | 0 | { |
1560 | 0 | auto& iterator_record = verify_cast<IteratorRecord>(iterator.as_object()); |
1561 | |
|
1562 | 0 | auto array = MUST(Array::create(*vm.current_realm(), 0)); |
1563 | 0 | size_t index = 0; |
1564 | |
|
1565 | 0 | while (true) { |
1566 | 0 | auto value = TRY(iterator_step_value(vm, iterator_record)); |
1567 | 0 | if (!value.has_value()) |
1568 | 0 | return array; |
1569 | | |
1570 | 0 | MUST(array->create_data_property_or_throw(index, value.release_value())); |
1571 | 0 | index++; |
1572 | 0 | } |
1573 | 0 | } |
1574 | | |
1575 | | inline ThrowCompletionOr<void> append(VM& vm, Value lhs, Value rhs, bool is_spread) |
1576 | 0 | { |
1577 | | // Note: This OpCode is used to construct array literals and argument arrays for calls, |
1578 | | // containing at least one spread element, |
1579 | | // Iterating over such a spread element to unpack it has to be visible by |
1580 | | // the user courtesy of |
1581 | | // (1) https://tc39.es/ecma262/#sec-runtime-semantics-arrayaccumulation |
1582 | | // SpreadElement : ... AssignmentExpression |
1583 | | // 1. Let spreadRef be ? Evaluation of AssignmentExpression. |
1584 | | // 2. Let spreadObj be ? GetValue(spreadRef). |
1585 | | // 3. Let iteratorRecord be ? GetIterator(spreadObj). |
1586 | | // 4. Repeat, |
1587 | | // a. Let next be ? IteratorStep(iteratorRecord). |
1588 | | // b. If next is false, return nextIndex. |
1589 | | // c. Let nextValue be ? IteratorValue(next). |
1590 | | // d. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), nextValue). |
1591 | | // e. Set nextIndex to nextIndex + 1. |
1592 | | // (2) https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation |
1593 | | // ArgumentList : ... AssignmentExpression |
1594 | | // 1. Let list be a new empty List. |
1595 | | // 2. Let spreadRef be ? Evaluation of AssignmentExpression. |
1596 | | // 3. Let spreadObj be ? GetValue(spreadRef). |
1597 | | // 4. Let iteratorRecord be ? GetIterator(spreadObj). |
1598 | | // 5. Repeat, |
1599 | | // a. Let next be ? IteratorStep(iteratorRecord). |
1600 | | // b. If next is false, return list. |
1601 | | // c. Let nextArg be ? IteratorValue(next). |
1602 | | // d. Append nextArg to list. |
1603 | | // ArgumentList : ArgumentList , ... AssignmentExpression |
1604 | | // 1. Let precedingArgs be ? ArgumentListEvaluation of ArgumentList. |
1605 | | // 2. Let spreadRef be ? Evaluation of AssignmentExpression. |
1606 | | // 3. Let iteratorRecord be ? GetIterator(? GetValue(spreadRef)). |
1607 | | // 4. Repeat, |
1608 | | // a. Let next be ? IteratorStep(iteratorRecord). |
1609 | | // b. If next is false, return precedingArgs. |
1610 | | // c. Let nextArg be ? IteratorValue(next). |
1611 | | // d. Append nextArg to precedingArgs. |
1612 | | |
1613 | | // Note: We know from codegen, that lhs is a plain array with only indexed properties |
1614 | 0 | auto& lhs_array = lhs.as_array(); |
1615 | 0 | auto lhs_size = lhs_array.indexed_properties().array_like_size(); |
1616 | |
|
1617 | 0 | if (is_spread) { |
1618 | | // ...rhs |
1619 | 0 | size_t i = lhs_size; |
1620 | 0 | TRY(get_iterator_values(vm, rhs, [&i, &lhs_array](Value iterator_value) -> Optional<Completion> { |
1621 | 0 | lhs_array.indexed_properties().put(i, iterator_value, default_attributes); |
1622 | 0 | ++i; |
1623 | 0 | return {}; |
1624 | 0 | })); |
1625 | 0 | } else { |
1626 | 0 | lhs_array.indexed_properties().put(lhs_size, rhs, default_attributes); |
1627 | 0 | } |
1628 | |
|
1629 | 0 | return {}; |
1630 | 0 | } |
1631 | | |
1632 | | inline ThrowCompletionOr<Value> delete_by_id(Bytecode::Interpreter& interpreter, Value base, IdentifierTableIndex property) |
1633 | 0 | { |
1634 | 0 | auto& vm = interpreter.vm(); |
1635 | |
|
1636 | 0 | auto const& identifier = interpreter.current_executable().get_identifier(property); |
1637 | 0 | bool strict = vm.in_strict_mode(); |
1638 | 0 | auto reference = Reference { base, identifier, {}, strict }; |
1639 | |
|
1640 | 0 | return TRY(reference.delete_(vm)); |
1641 | 0 | } |
1642 | | |
1643 | | inline ThrowCompletionOr<Value> delete_by_value(Bytecode::Interpreter& interpreter, Value base, Value property_key_value) |
1644 | 0 | { |
1645 | 0 | auto& vm = interpreter.vm(); |
1646 | |
|
1647 | 0 | auto property_key = TRY(property_key_value.to_property_key(vm)); |
1648 | 0 | bool strict = vm.in_strict_mode(); |
1649 | 0 | auto reference = Reference { base, property_key, {}, strict }; |
1650 | |
|
1651 | 0 | return Value(TRY(reference.delete_(vm))); |
1652 | 0 | } |
1653 | | |
1654 | | inline ThrowCompletionOr<Value> delete_by_value_with_this(Bytecode::Interpreter& interpreter, Value base, Value property_key_value, Value this_value) |
1655 | 0 | { |
1656 | 0 | auto& vm = interpreter.vm(); |
1657 | |
|
1658 | 0 | auto property_key = TRY(property_key_value.to_property_key(vm)); |
1659 | 0 | bool strict = vm.in_strict_mode(); |
1660 | 0 | auto reference = Reference { base, property_key, this_value, strict }; |
1661 | |
|
1662 | 0 | return Value(TRY(reference.delete_(vm))); |
1663 | 0 | } |
1664 | | |
1665 | | // 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties |
1666 | | inline ThrowCompletionOr<Object*> get_object_property_iterator(VM& vm, Value value) |
1667 | 0 | { |
1668 | | // While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants: |
1669 | | // 1- Returned property keys do not include keys that are Symbols |
1670 | | // 2- Properties of the target object may be deleted during enumeration. A property that is deleted before it is processed by the iterator's next method is ignored |
1671 | | // 3- If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration |
1672 | | // 4- A property name will be returned by the iterator's next method at most once in any enumeration. |
1673 | | // 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively; |
1674 | | // but a property of a prototype is not processed if it has the same name as a property that has already been processed by the iterator's next method. |
1675 | | // 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed. |
1676 | | // 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument. |
1677 | | // 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method. |
1678 | | // 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method |
1679 | | |
1680 | | // Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations). |
1681 | 0 | auto object = TRY(value.to_object(vm)); |
1682 | | // Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys, |
1683 | | // so we just keep the order consistent anyway. |
1684 | 0 | OrderedHashTable<PropertyKey> properties; |
1685 | 0 | OrderedHashTable<PropertyKey> non_enumerable_properties; |
1686 | 0 | HashTable<NonnullGCPtr<Object>> seen_objects; |
1687 | | // Collect all keys immediately (invariant no. 5) |
1688 | 0 | for (auto object_to_check = GCPtr { object.ptr() }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) { |
1689 | 0 | seen_objects.set(*object_to_check); |
1690 | 0 | for (auto& key : TRY(object_to_check->internal_own_property_keys())) { |
1691 | 0 | if (key.is_symbol()) |
1692 | 0 | continue; |
1693 | 0 | auto property_key = TRY(PropertyKey::from_value(vm, key)); |
1694 | | |
1695 | | // If there is a non-enumerable property higher up the prototype chain with the same key, |
1696 | | // we mustn't include this property even if it's enumerable (invariant no. 5 and 6) |
1697 | 0 | if (non_enumerable_properties.contains(property_key)) |
1698 | 0 | continue; |
1699 | 0 | if (properties.contains(property_key)) |
1700 | 0 | continue; |
1701 | | |
1702 | 0 | auto descriptor = TRY(object_to_check->internal_get_own_property(property_key)); |
1703 | 0 | if (!*descriptor->enumerable) |
1704 | 0 | non_enumerable_properties.set(move(property_key)); |
1705 | 0 | else |
1706 | 0 | properties.set(move(property_key)); |
1707 | 0 | } |
1708 | 0 | } |
1709 | 0 | auto& realm = *vm.current_realm(); |
1710 | 0 | auto callback = NativeFunction::create( |
1711 | 0 | *vm.current_realm(), [items = move(properties)](VM& vm) mutable -> ThrowCompletionOr<Value> { |
1712 | 0 | auto& realm = *vm.current_realm(); |
1713 | 0 | auto iterated_object_value = vm.this_value(); |
1714 | 0 | if (!iterated_object_value.is_object()) |
1715 | 0 | return vm.throw_completion<InternalError>("Invalid state for GetObjectPropertyIterator.next"sv); |
1716 | | |
1717 | 0 | auto& iterated_object = iterated_object_value.as_object(); |
1718 | 0 | auto result_object = Object::create(realm, nullptr); |
1719 | 0 | while (true) { |
1720 | 0 | if (items.is_empty()) { |
1721 | 0 | result_object->define_direct_property(vm.names.done, JS::Value(true), default_attributes); |
1722 | 0 | return result_object; |
1723 | 0 | } |
1724 | | |
1725 | 0 | auto key = items.take_first(); |
1726 | | |
1727 | | // If the property is deleted, don't include it (invariant no. 2) |
1728 | 0 | if (!TRY(iterated_object.has_property(key))) |
1729 | 0 | continue; |
1730 | | |
1731 | 0 | result_object->define_direct_property(vm.names.done, JS::Value(false), default_attributes); |
1732 | |
|
1733 | 0 | if (key.is_number()) |
1734 | 0 | result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, String::number(key.as_number())), default_attributes); |
1735 | 0 | else if (key.is_string()) |
1736 | 0 | result_object->define_direct_property(vm.names.value, PrimitiveString::create(vm, key.as_string()), default_attributes); |
1737 | 0 | else |
1738 | 0 | VERIFY_NOT_REACHED(); // We should not have non-string/number keys. |
1739 | | |
1740 | 0 | return result_object; |
1741 | 0 | } |
1742 | 0 | }, |
1743 | 0 | 1, vm.names.next); |
1744 | 0 | return vm.heap().allocate<IteratorRecord>(realm, realm, object, callback, false).ptr(); |
1745 | 0 | } |
1746 | | |
1747 | | ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const |
1748 | 0 | { |
1749 | 0 | #define __BYTECODE_OP(op) \ |
1750 | 0 | case Instruction::Type::op: \ |
1751 | 0 | return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable); |
1752 | |
|
1753 | 0 | switch (type()) { |
1754 | 0 | ENUMERATE_BYTECODE_OPS(__BYTECODE_OP) |
1755 | 0 | default: |
1756 | 0 | VERIFY_NOT_REACHED(); |
1757 | 0 | } |
1758 | |
|
1759 | 0 | #undef __BYTECODE_OP |
1760 | 0 | } |
1761 | | |
1762 | | } |
1763 | | |
1764 | | namespace JS::Bytecode::Op { |
1765 | | |
1766 | | static void dump_object(Object& o, HashTable<Object const*>& seen, int indent = 0) |
1767 | 0 | { |
1768 | 0 | if (seen.contains(&o)) |
1769 | 0 | return; |
1770 | 0 | seen.set(&o); |
1771 | 0 | for (auto& it : o.shape().property_table()) { |
1772 | 0 | auto value = o.get_direct(it.value.offset); |
1773 | 0 | dbgln("{} {} -> {}", String::repeated(' ', indent).release_value(), it.key.to_display_string(), value); |
1774 | 0 | if (value.is_object()) { |
1775 | 0 | dump_object(value.as_object(), seen, indent + 2); |
1776 | 0 | } |
1777 | 0 | } |
1778 | 0 | } |
1779 | | |
1780 | | void Dump::execute_impl(Bytecode::Interpreter& interpreter) const |
1781 | 0 | { |
1782 | 0 | auto value = interpreter.get(m_value); |
1783 | 0 | dbgln("(DUMP) {}: {}", m_text, value); |
1784 | 0 | if (value.is_object()) { |
1785 | 0 | HashTable<Object const*> seen; |
1786 | 0 | dump_object(value.as_object(), seen); |
1787 | 0 | } |
1788 | 0 | } |
1789 | | |
1790 | | #define JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \ |
1791 | | ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \ |
1792 | 0 | { \ |
1793 | 0 | auto& vm = interpreter.vm(); \ |
1794 | 0 | auto lhs = interpreter.get(m_lhs); \ |
1795 | 0 | auto rhs = interpreter.get(m_rhs); \ |
1796 | 0 | interpreter.set(m_dst, TRY(op_snake_case(vm, lhs, rhs))); \ |
1797 | 0 | return {}; \ |
1798 | 0 | } Unexecuted instantiation: JS::Bytecode::Op::Div::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::Exp::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::Mod::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::In::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::InstanceOf::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::LooselyInequals::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::LooselyEquals::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::StrictlyInequals::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::StrictlyEquals::execute_impl(JS::Bytecode::Interpreter&) const |
1799 | | |
1800 | | #define JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \ |
1801 | | ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \ |
1802 | 0 | { \ |
1803 | 0 | return ByteString::formatted(#OpTitleCase " {}, {}, {}", \ |
1804 | 0 | format_operand("dst"sv, m_dst, executable), \ |
1805 | 0 | format_operand("lhs"sv, m_lhs, executable), \ |
1806 | 0 | format_operand("rhs"sv, m_rhs, executable)); \ |
1807 | 0 | } Unexecuted instantiation: JS::Bytecode::Op::Div::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Exp::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Mod::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::In::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::InstanceOf::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::LooselyInequals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::LooselyEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::StrictlyInequals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::StrictlyEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Add::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::BitwiseAnd::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::BitwiseOr::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::BitwiseXor::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::GreaterThan::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::GreaterThanEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::LeftShift::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::LessThan::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::LessThanEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Mul::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::RightShift::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Sub::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::UnsignedRightShift::to_byte_string_impl(JS::Bytecode::Executable const&) const |
1808 | | |
1809 | | JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP) |
1810 | | JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP) |
1811 | | JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DEFINE_TO_BYTE_STRING_FOR_COMMON_BINARY_OP) |
1812 | | |
1813 | | ThrowCompletionOr<void> Add::execute_impl(Bytecode::Interpreter& interpreter) const |
1814 | 0 | { |
1815 | 0 | auto& vm = interpreter.vm(); |
1816 | 0 | auto const lhs = interpreter.get(m_lhs); |
1817 | 0 | auto const rhs = interpreter.get(m_rhs); |
1818 | |
|
1819 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1820 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1821 | 0 | if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), rhs.as_i32())) { |
1822 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() + rhs.as_i32())); |
1823 | 0 | return {}; |
1824 | 0 | } |
1825 | 0 | } |
1826 | 0 | interpreter.set(m_dst, Value(lhs.as_double() + rhs.as_double())); |
1827 | 0 | return {}; |
1828 | 0 | } |
1829 | | |
1830 | 0 | interpreter.set(m_dst, TRY(add(vm, lhs, rhs))); |
1831 | 0 | return {}; |
1832 | 0 | } |
1833 | | |
1834 | | ThrowCompletionOr<void> Mul::execute_impl(Bytecode::Interpreter& interpreter) const |
1835 | 0 | { |
1836 | 0 | auto& vm = interpreter.vm(); |
1837 | 0 | auto const lhs = interpreter.get(m_lhs); |
1838 | 0 | auto const rhs = interpreter.get(m_rhs); |
1839 | |
|
1840 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1841 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1842 | 0 | if (!Checked<i32>::multiplication_would_overflow(lhs.as_i32(), rhs.as_i32())) { |
1843 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() * rhs.as_i32())); |
1844 | 0 | return {}; |
1845 | 0 | } |
1846 | 0 | } |
1847 | 0 | interpreter.set(m_dst, Value(lhs.as_double() * rhs.as_double())); |
1848 | 0 | return {}; |
1849 | 0 | } |
1850 | | |
1851 | 0 | interpreter.set(m_dst, TRY(mul(vm, lhs, rhs))); |
1852 | 0 | return {}; |
1853 | 0 | } |
1854 | | |
1855 | | ThrowCompletionOr<void> Sub::execute_impl(Bytecode::Interpreter& interpreter) const |
1856 | 0 | { |
1857 | 0 | auto& vm = interpreter.vm(); |
1858 | 0 | auto const lhs = interpreter.get(m_lhs); |
1859 | 0 | auto const rhs = interpreter.get(m_rhs); |
1860 | |
|
1861 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1862 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1863 | 0 | if (!Checked<i32>::subtraction_would_overflow(lhs.as_i32(), rhs.as_i32())) { |
1864 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() - rhs.as_i32())); |
1865 | 0 | return {}; |
1866 | 0 | } |
1867 | 0 | } |
1868 | 0 | interpreter.set(m_dst, Value(lhs.as_double() - rhs.as_double())); |
1869 | 0 | return {}; |
1870 | 0 | } |
1871 | | |
1872 | 0 | interpreter.set(m_dst, TRY(sub(vm, lhs, rhs))); |
1873 | 0 | return {}; |
1874 | 0 | } |
1875 | | |
1876 | | ThrowCompletionOr<void> BitwiseXor::execute_impl(Bytecode::Interpreter& interpreter) const |
1877 | 0 | { |
1878 | 0 | auto& vm = interpreter.vm(); |
1879 | 0 | auto const lhs = interpreter.get(m_lhs); |
1880 | 0 | auto const rhs = interpreter.get(m_rhs); |
1881 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1882 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() ^ rhs.as_i32())); |
1883 | 0 | return {}; |
1884 | 0 | } |
1885 | 0 | interpreter.set(m_dst, TRY(bitwise_xor(vm, lhs, rhs))); |
1886 | 0 | return {}; |
1887 | 0 | } |
1888 | | |
1889 | | ThrowCompletionOr<void> BitwiseAnd::execute_impl(Bytecode::Interpreter& interpreter) const |
1890 | 0 | { |
1891 | 0 | auto& vm = interpreter.vm(); |
1892 | 0 | auto const lhs = interpreter.get(m_lhs); |
1893 | 0 | auto const rhs = interpreter.get(m_rhs); |
1894 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1895 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() & rhs.as_i32())); |
1896 | 0 | return {}; |
1897 | 0 | } |
1898 | 0 | interpreter.set(m_dst, TRY(bitwise_and(vm, lhs, rhs))); |
1899 | 0 | return {}; |
1900 | 0 | } |
1901 | | |
1902 | | ThrowCompletionOr<void> BitwiseOr::execute_impl(Bytecode::Interpreter& interpreter) const |
1903 | 0 | { |
1904 | 0 | auto& vm = interpreter.vm(); |
1905 | 0 | auto const lhs = interpreter.get(m_lhs); |
1906 | 0 | auto const rhs = interpreter.get(m_rhs); |
1907 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1908 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() | rhs.as_i32())); |
1909 | 0 | return {}; |
1910 | 0 | } |
1911 | 0 | interpreter.set(m_dst, TRY(bitwise_or(vm, lhs, rhs))); |
1912 | 0 | return {}; |
1913 | 0 | } |
1914 | | |
1915 | | ThrowCompletionOr<void> UnsignedRightShift::execute_impl(Bytecode::Interpreter& interpreter) const |
1916 | 0 | { |
1917 | 0 | auto& vm = interpreter.vm(); |
1918 | 0 | auto const lhs = interpreter.get(m_lhs); |
1919 | 0 | auto const rhs = interpreter.get(m_rhs); |
1920 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1921 | 0 | auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32; |
1922 | 0 | interpreter.set(m_dst, Value(static_cast<u32>(lhs.as_i32()) >> shift_count)); |
1923 | 0 | return {}; |
1924 | 0 | } |
1925 | 0 | interpreter.set(m_dst, TRY(unsigned_right_shift(vm, lhs, rhs))); |
1926 | 0 | return {}; |
1927 | 0 | } |
1928 | | |
1929 | | ThrowCompletionOr<void> RightShift::execute_impl(Bytecode::Interpreter& interpreter) const |
1930 | 0 | { |
1931 | 0 | auto& vm = interpreter.vm(); |
1932 | 0 | auto const lhs = interpreter.get(m_lhs); |
1933 | 0 | auto const rhs = interpreter.get(m_rhs); |
1934 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1935 | 0 | auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32; |
1936 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() >> shift_count)); |
1937 | 0 | return {}; |
1938 | 0 | } |
1939 | 0 | interpreter.set(m_dst, TRY(right_shift(vm, lhs, rhs))); |
1940 | 0 | return {}; |
1941 | 0 | } |
1942 | | |
1943 | | ThrowCompletionOr<void> LeftShift::execute_impl(Bytecode::Interpreter& interpreter) const |
1944 | 0 | { |
1945 | 0 | auto& vm = interpreter.vm(); |
1946 | 0 | auto const lhs = interpreter.get(m_lhs); |
1947 | 0 | auto const rhs = interpreter.get(m_rhs); |
1948 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1949 | 0 | auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32; |
1950 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() << shift_count)); |
1951 | 0 | return {}; |
1952 | 0 | } |
1953 | 0 | interpreter.set(m_dst, TRY(left_shift(vm, lhs, rhs))); |
1954 | 0 | return {}; |
1955 | 0 | } |
1956 | | |
1957 | | ThrowCompletionOr<void> LessThan::execute_impl(Bytecode::Interpreter& interpreter) const |
1958 | 0 | { |
1959 | 0 | auto& vm = interpreter.vm(); |
1960 | 0 | auto const lhs = interpreter.get(m_lhs); |
1961 | 0 | auto const rhs = interpreter.get(m_rhs); |
1962 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1963 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1964 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() < rhs.as_i32())); |
1965 | 0 | return {}; |
1966 | 0 | } |
1967 | 0 | interpreter.set(m_dst, Value(lhs.as_double() < rhs.as_double())); |
1968 | 0 | return {}; |
1969 | 0 | } |
1970 | 0 | interpreter.set(m_dst, TRY(less_than(vm, lhs, rhs))); |
1971 | 0 | return {}; |
1972 | 0 | } |
1973 | | |
1974 | | ThrowCompletionOr<void> LessThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const |
1975 | 0 | { |
1976 | 0 | auto& vm = interpreter.vm(); |
1977 | 0 | auto const lhs = interpreter.get(m_lhs); |
1978 | 0 | auto const rhs = interpreter.get(m_rhs); |
1979 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1980 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1981 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() <= rhs.as_i32())); |
1982 | 0 | return {}; |
1983 | 0 | } |
1984 | 0 | interpreter.set(m_dst, Value(lhs.as_double() <= rhs.as_double())); |
1985 | 0 | return {}; |
1986 | 0 | } |
1987 | 0 | interpreter.set(m_dst, TRY(less_than_equals(vm, lhs, rhs))); |
1988 | 0 | return {}; |
1989 | 0 | } |
1990 | | |
1991 | | ThrowCompletionOr<void> GreaterThan::execute_impl(Bytecode::Interpreter& interpreter) const |
1992 | 0 | { |
1993 | 0 | auto& vm = interpreter.vm(); |
1994 | 0 | auto const lhs = interpreter.get(m_lhs); |
1995 | 0 | auto const rhs = interpreter.get(m_rhs); |
1996 | 0 | if (lhs.is_number() && rhs.is_number()) { |
1997 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
1998 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() > rhs.as_i32())); |
1999 | 0 | return {}; |
2000 | 0 | } |
2001 | 0 | interpreter.set(m_dst, Value(lhs.as_double() > rhs.as_double())); |
2002 | 0 | return {}; |
2003 | 0 | } |
2004 | 0 | interpreter.set(m_dst, TRY(greater_than(vm, lhs, rhs))); |
2005 | 0 | return {}; |
2006 | 0 | } |
2007 | | |
2008 | | ThrowCompletionOr<void> GreaterThanEquals::execute_impl(Bytecode::Interpreter& interpreter) const |
2009 | 0 | { |
2010 | 0 | auto& vm = interpreter.vm(); |
2011 | 0 | auto const lhs = interpreter.get(m_lhs); |
2012 | 0 | auto const rhs = interpreter.get(m_rhs); |
2013 | 0 | if (lhs.is_number() && rhs.is_number()) { |
2014 | 0 | if (lhs.is_int32() && rhs.is_int32()) { |
2015 | 0 | interpreter.set(m_dst, Value(lhs.as_i32() >= rhs.as_i32())); |
2016 | 0 | return {}; |
2017 | 0 | } |
2018 | 0 | interpreter.set(m_dst, Value(lhs.as_double() >= rhs.as_double())); |
2019 | 0 | return {}; |
2020 | 0 | } |
2021 | 0 | interpreter.set(m_dst, TRY(greater_than_equals(vm, lhs, rhs))); |
2022 | 0 | return {}; |
2023 | 0 | } |
2024 | | |
2025 | | static ThrowCompletionOr<Value> not_(VM&, Value value) |
2026 | 0 | { |
2027 | 0 | return Value(!value.to_boolean()); |
2028 | 0 | } |
2029 | | |
2030 | | static ThrowCompletionOr<Value> typeof_(VM& vm, Value value) |
2031 | 0 | { |
2032 | 0 | return value.typeof_(vm); |
2033 | 0 | } |
2034 | | |
2035 | | #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \ |
2036 | | ThrowCompletionOr<void> OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \ |
2037 | 0 | { \ |
2038 | 0 | auto& vm = interpreter.vm(); \ |
2039 | 0 | interpreter.set(dst(), TRY(op_snake_case(vm, interpreter.get(src())))); \ |
2040 | 0 | return {}; \ |
2041 | 0 | } \ Unexecuted instantiation: JS::Bytecode::Op::BitwiseNot::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::Not::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::UnaryPlus::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::UnaryMinus::execute_impl(JS::Bytecode::Interpreter&) const Unexecuted instantiation: JS::Bytecode::Op::Typeof::execute_impl(JS::Bytecode::Interpreter&) const |
2042 | | ByteString OpTitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \ |
2043 | 0 | { \ |
2044 | 0 | return ByteString::formatted(#OpTitleCase " {}, {}", \ |
2045 | 0 | format_operand("dst"sv, dst(), executable), \ |
2046 | 0 | format_operand("src"sv, src(), executable)); \ |
2047 | 0 | } Unexecuted instantiation: JS::Bytecode::Op::BitwiseNot::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Not::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::UnaryPlus::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::UnaryMinus::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::Typeof::to_byte_string_impl(JS::Bytecode::Executable const&) const |
2048 | | |
2049 | | JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP) |
2050 | | |
2051 | | void NewArray::execute_impl(Bytecode::Interpreter& interpreter) const |
2052 | 2 | { |
2053 | 2 | auto array = MUST(Array::create(interpreter.realm(), 0)); |
2054 | 400k | for (size_t i = 0; i < m_element_count; i++) { |
2055 | 400k | array->indexed_properties().put(i, interpreter.get(m_elements[i]), default_attributes); |
2056 | 400k | } |
2057 | 2 | interpreter.set(dst(), array); |
2058 | 2 | } |
2059 | | |
2060 | | void NewPrimitiveArray::execute_impl(Bytecode::Interpreter& interpreter) const |
2061 | 2 | { |
2062 | 2 | auto array = MUST(Array::create(interpreter.realm(), 0)); |
2063 | 262k | for (size_t i = 0; i < m_element_count; i++) |
2064 | 262k | array->indexed_properties().put(i, m_elements[i], default_attributes); |
2065 | 2 | interpreter.set(dst(), array); |
2066 | 2 | } |
2067 | | |
2068 | | void AddPrivateName::execute_impl(Bytecode::Interpreter& interpreter) const |
2069 | 0 | { |
2070 | 0 | auto const& name = interpreter.current_executable().get_identifier(m_name); |
2071 | 0 | interpreter.vm().running_execution_context().private_environment->add_private_name(name); |
2072 | 0 | } |
2073 | | |
2074 | | ThrowCompletionOr<void> ArrayAppend::execute_impl(Bytecode::Interpreter& interpreter) const |
2075 | 0 | { |
2076 | 0 | return append(interpreter.vm(), interpreter.get(dst()), interpreter.get(src()), m_is_spread); |
2077 | 0 | } |
2078 | | |
2079 | | ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpreter) const |
2080 | 0 | { |
2081 | 0 | auto& vm = interpreter.vm(); |
2082 | 0 | auto specifier = interpreter.get(m_specifier); |
2083 | 0 | auto options_value = interpreter.get(m_options); |
2084 | 0 | interpreter.set(dst(), TRY(perform_import_call(vm, specifier, options_value))); |
2085 | 0 | return {}; |
2086 | 0 | } |
2087 | | |
2088 | | ThrowCompletionOr<void> IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const |
2089 | 0 | { |
2090 | 0 | interpreter.set(dst(), TRY(iterator_to_array(interpreter.vm(), interpreter.get(iterator())))); |
2091 | 0 | return {}; |
2092 | 0 | } |
2093 | | |
2094 | | void NewObject::execute_impl(Bytecode::Interpreter& interpreter) const |
2095 | 0 | { |
2096 | 0 | auto& vm = interpreter.vm(); |
2097 | 0 | auto& realm = *vm.current_realm(); |
2098 | 0 | interpreter.set(dst(), Object::create(realm, realm.intrinsics().object_prototype())); |
2099 | 0 | } |
2100 | | |
2101 | | void NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const |
2102 | 0 | { |
2103 | 0 | interpreter.set(dst(), |
2104 | 0 | new_regexp( |
2105 | 0 | interpreter.vm(), |
2106 | 0 | interpreter.current_executable().regex_table->get(m_regex_index), |
2107 | 0 | interpreter.current_executable().get_string(m_source_index), |
2108 | 0 | interpreter.current_executable().get_string(m_flags_index))); |
2109 | 0 | } |
2110 | | |
2111 | | #define JS_DEFINE_NEW_BUILTIN_ERROR_OP(ErrorName) \ |
2112 | | void New##ErrorName::execute_impl(Bytecode::Interpreter& interpreter) const \ |
2113 | 0 | { \ |
2114 | 0 | auto& vm = interpreter.vm(); \ |
2115 | 0 | auto& realm = *vm.current_realm(); \ |
2116 | 0 | interpreter.set(dst(), ErrorName::create(realm, interpreter.current_executable().get_string(m_error_string))); \ |
2117 | 0 | } \ |
2118 | | ByteString New##ErrorName::to_byte_string_impl(Bytecode::Executable const& executable) const \ |
2119 | 0 | { \ |
2120 | 0 | return ByteString::formatted("New" #ErrorName " {}, {}", \ |
2121 | 0 | format_operand("dst"sv, m_dst, executable), \ |
2122 | 0 | executable.string_table->get(m_error_string)); \ |
2123 | 0 | } |
2124 | | |
2125 | | JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DEFINE_NEW_BUILTIN_ERROR_OP) |
2126 | | |
2127 | | ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const |
2128 | 0 | { |
2129 | 0 | auto& vm = interpreter.vm(); |
2130 | 0 | auto& realm = *vm.current_realm(); |
2131 | |
|
2132 | 0 | auto from_object = interpreter.get(m_from_object); |
2133 | |
|
2134 | 0 | auto to_object = Object::create(realm, realm.intrinsics().object_prototype()); |
2135 | |
|
2136 | 0 | HashTable<PropertyKey> excluded_names; |
2137 | 0 | for (size_t i = 0; i < m_excluded_names_count; ++i) { |
2138 | 0 | excluded_names.set(TRY(interpreter.get(m_excluded_names[i]).to_property_key(vm))); |
2139 | 0 | } |
2140 | |
|
2141 | 0 | TRY(to_object->copy_data_properties(vm, from_object, excluded_names)); |
2142 | |
|
2143 | 0 | interpreter.set(dst(), to_object); |
2144 | 0 | return {}; |
2145 | 0 | } |
2146 | | |
2147 | | ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const |
2148 | 0 | { |
2149 | 0 | auto& vm = interpreter.vm(); |
2150 | 0 | auto string = TRY(interpreter.get(src()).to_primitive_string(vm)); |
2151 | 0 | interpreter.set(dst(), PrimitiveString::create(vm, interpreter.get(dst()).as_string(), string)); |
2152 | 0 | return {}; |
2153 | 0 | } |
2154 | | |
2155 | | ThrowCompletionOr<void> GetBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2156 | 0 | { |
2157 | 0 | auto& vm = interpreter.vm(); |
2158 | 0 | auto& executable = interpreter.current_executable(); |
2159 | |
|
2160 | 0 | if (m_cache.is_valid()) { |
2161 | 0 | auto const* environment = interpreter.running_execution_context().lexical_environment.ptr(); |
2162 | 0 | for (size_t i = 0; i < m_cache.hops; ++i) |
2163 | 0 | environment = environment->outer_environment(); |
2164 | 0 | if (!environment->is_permanently_screwed_by_eval()) { |
2165 | 0 | interpreter.set(dst(), TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, m_cache.index))); |
2166 | 0 | return {}; |
2167 | 0 | } |
2168 | 0 | m_cache = {}; |
2169 | 0 | } |
2170 | | |
2171 | 0 | auto reference = TRY(vm.resolve_binding(executable.get_identifier(m_identifier))); |
2172 | 0 | if (reference.environment_coordinate().has_value()) |
2173 | 0 | m_cache = reference.environment_coordinate().value(); |
2174 | 0 | interpreter.set(dst(), TRY(reference.get_value(vm))); |
2175 | 0 | return {}; |
2176 | 0 | } |
2177 | | |
2178 | | ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2179 | 0 | { |
2180 | 0 | auto callee_and_this = TRY(get_callee_and_this_from_environment( |
2181 | 0 | interpreter, |
2182 | 0 | interpreter.current_executable().get_identifier(m_identifier), |
2183 | 0 | m_cache)); |
2184 | 0 | interpreter.set(m_callee, callee_and_this.callee); |
2185 | 0 | interpreter.set(m_this_value, callee_and_this.this_value); |
2186 | 0 | return {}; |
2187 | 0 | } |
2188 | | |
2189 | | ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const |
2190 | 11 | { |
2191 | 11 | interpreter.set(dst(), TRY(get_global(interpreter, m_identifier, interpreter.current_executable().global_variable_caches[m_cache_index]))); |
2192 | 0 | return {}; |
2193 | 11 | } |
2194 | | |
2195 | | ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const |
2196 | 0 | { |
2197 | 0 | auto& vm = interpreter.vm(); |
2198 | 0 | auto const& string = interpreter.current_executable().get_identifier(m_identifier); |
2199 | 0 | auto reference = TRY(vm.resolve_binding(string)); |
2200 | 0 | interpreter.set(dst(), Value(TRY(reference.delete_(vm)))); |
2201 | 0 | return {}; |
2202 | 0 | } |
2203 | | |
2204 | | void CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2205 | 0 | { |
2206 | 0 | auto make_and_swap_envs = [&](auto& old_environment) { |
2207 | 0 | auto declarative_environment = new_declarative_environment(*old_environment).ptr(); |
2208 | 0 | declarative_environment->ensure_capacity(m_capacity); |
2209 | 0 | GCPtr<Environment> environment = declarative_environment; |
2210 | 0 | swap(old_environment, environment); |
2211 | 0 | return environment; |
2212 | 0 | }; |
2213 | 0 | auto& running_execution_context = interpreter.running_execution_context(); |
2214 | 0 | running_execution_context.saved_lexical_environments.append(make_and_swap_envs(running_execution_context.lexical_environment)); |
2215 | 0 | } |
2216 | | |
2217 | | void CreatePrivateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2218 | 0 | { |
2219 | 0 | auto& running_execution_context = interpreter.vm().running_execution_context(); |
2220 | 0 | auto outer_private_environment = running_execution_context.private_environment; |
2221 | 0 | running_execution_context.private_environment = new_private_environment(interpreter.vm(), outer_private_environment); |
2222 | 0 | } |
2223 | | |
2224 | | void CreateVariableEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2225 | 0 | { |
2226 | 0 | auto& running_execution_context = interpreter.running_execution_context(); |
2227 | 0 | auto var_environment = new_declarative_environment(*running_execution_context.lexical_environment); |
2228 | 0 | var_environment->ensure_capacity(m_capacity); |
2229 | 0 | running_execution_context.variable_environment = var_environment; |
2230 | 0 | running_execution_context.lexical_environment = var_environment; |
2231 | 0 | } |
2232 | | |
2233 | | ThrowCompletionOr<void> EnterObjectEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2234 | 0 | { |
2235 | 0 | auto object = TRY(interpreter.get(m_object).to_object(interpreter.vm())); |
2236 | 0 | interpreter.enter_object_environment(*object); |
2237 | 0 | return {}; |
2238 | 0 | } |
2239 | | |
2240 | | void Catch::execute_impl(Bytecode::Interpreter& interpreter) const |
2241 | 0 | { |
2242 | 0 | interpreter.catch_exception(dst()); |
2243 | 0 | } |
2244 | | |
2245 | | void LeaveFinally::execute_impl(Bytecode::Interpreter& interpreter) const |
2246 | 0 | { |
2247 | 0 | interpreter.leave_finally(); |
2248 | 0 | } |
2249 | | |
2250 | | void RestoreScheduledJump::execute_impl(Bytecode::Interpreter& interpreter) const |
2251 | 0 | { |
2252 | 0 | interpreter.restore_scheduled_jump(); |
2253 | 0 | } |
2254 | | |
2255 | | ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& interpreter) const |
2256 | 0 | { |
2257 | 0 | auto const& name = interpreter.current_executable().get_identifier(m_identifier); |
2258 | 0 | return create_variable(interpreter.vm(), name, m_mode, m_is_global, m_is_immutable, m_is_strict); |
2259 | 0 | } |
2260 | | |
2261 | | ThrowCompletionOr<void> CreateRestParams::execute_impl(Bytecode::Interpreter& interpreter) const |
2262 | 0 | { |
2263 | 0 | auto const& arguments = interpreter.running_execution_context().arguments; |
2264 | 0 | auto arguments_count = interpreter.running_execution_context().passed_argument_count; |
2265 | 0 | auto array = MUST(Array::create(interpreter.realm(), 0)); |
2266 | 0 | for (size_t rest_index = m_rest_index; rest_index < arguments_count; ++rest_index) |
2267 | 0 | array->indexed_properties().append(arguments[rest_index]); |
2268 | 0 | interpreter.set(m_dst, array); |
2269 | 0 | return {}; |
2270 | 0 | } |
2271 | | |
2272 | | ThrowCompletionOr<void> CreateArguments::execute_impl(Bytecode::Interpreter& interpreter) const |
2273 | 0 | { |
2274 | 0 | auto const& function = interpreter.running_execution_context().function; |
2275 | 0 | auto const& arguments = interpreter.running_execution_context().arguments; |
2276 | 0 | auto const& environment = interpreter.running_execution_context().lexical_environment; |
2277 | |
|
2278 | 0 | auto passed_arguments = ReadonlySpan<Value> { arguments.data(), interpreter.running_execution_context().passed_argument_count }; |
2279 | 0 | Object* arguments_object; |
2280 | 0 | if (m_kind == Kind::Mapped) { |
2281 | 0 | arguments_object = create_mapped_arguments_object(interpreter.vm(), *function, function->formal_parameters(), passed_arguments, *environment); |
2282 | 0 | } else { |
2283 | 0 | arguments_object = create_unmapped_arguments_object(interpreter.vm(), passed_arguments); |
2284 | 0 | } |
2285 | |
|
2286 | 0 | if (m_dst.has_value()) { |
2287 | 0 | interpreter.set(*m_dst, arguments_object); |
2288 | 0 | return {}; |
2289 | 0 | } |
2290 | | |
2291 | 0 | if (m_is_immutable) { |
2292 | 0 | MUST(environment->create_immutable_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), false)); |
2293 | 0 | } else { |
2294 | 0 | MUST(environment->create_mutable_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), false)); |
2295 | 0 | } |
2296 | 0 | MUST(environment->initialize_binding(interpreter.vm(), interpreter.vm().names.arguments.as_string(), arguments_object, Environment::InitializeBindingHint::Normal)); |
2297 | |
|
2298 | 0 | return {}; |
2299 | 0 | } |
2300 | | |
2301 | | template<EnvironmentMode environment_mode, BindingInitializationMode initialization_mode> |
2302 | | static ThrowCompletionOr<void> initialize_or_set_binding(Interpreter& interpreter, IdentifierTableIndex identifier_index, Value value, EnvironmentCoordinate& cache) |
2303 | 0 | { |
2304 | 0 | auto& vm = interpreter.vm(); |
2305 | |
|
2306 | 0 | auto* environment = environment_mode == EnvironmentMode::Lexical |
2307 | 0 | ? interpreter.running_execution_context().lexical_environment.ptr() |
2308 | 0 | : interpreter.running_execution_context().variable_environment.ptr(); |
2309 | |
|
2310 | 0 | if (cache.is_valid()) { |
2311 | 0 | for (size_t i = 0; i < cache.hops; ++i) |
2312 | 0 | environment = environment->outer_environment(); |
2313 | 0 | if (!environment->is_permanently_screwed_by_eval()) { |
2314 | 0 | if constexpr (initialization_mode == BindingInitializationMode::Initialize) { |
2315 | 0 | TRY(static_cast<DeclarativeEnvironment&>(*environment).initialize_binding_direct(vm, cache.index, value, Environment::InitializeBindingHint::Normal)); |
2316 | 0 | } else { |
2317 | 0 | TRY(static_cast<DeclarativeEnvironment&>(*environment).set_mutable_binding_direct(vm, cache.index, value, vm.in_strict_mode())); |
2318 | 0 | } |
2319 | 0 | return {}; |
2320 | 0 | } |
2321 | 0 | cache = {}; |
2322 | 0 | } |
2323 | | |
2324 | 0 | auto reference = TRY(vm.resolve_binding(interpreter.current_executable().get_identifier(identifier_index), environment)); |
2325 | 0 | if (reference.environment_coordinate().has_value()) |
2326 | 0 | cache = reference.environment_coordinate().value(); |
2327 | 0 | if constexpr (initialization_mode == BindingInitializationMode::Initialize) { |
2328 | 0 | TRY(reference.initialize_referenced_binding(vm, value)); |
2329 | 0 | } else if (initialization_mode == BindingInitializationMode::Set) { |
2330 | 0 | TRY(reference.put_value(vm, value)); |
2331 | 0 | } |
2332 | 0 | return {}; |
2333 | 0 | } Unexecuted instantiation: Interpreter.cpp:JS::ThrowCompletionOr<void> JS::Bytecode::Op::initialize_or_set_binding<(JS::Bytecode::Op::EnvironmentMode)0, (JS::Bytecode::Op::BindingInitializationMode)0>(JS::Bytecode::Interpreter&, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::EnvironmentCoordinate&) Unexecuted instantiation: Interpreter.cpp:JS::ThrowCompletionOr<void> JS::Bytecode::Op::initialize_or_set_binding<(JS::Bytecode::Op::EnvironmentMode)1, (JS::Bytecode::Op::BindingInitializationMode)0>(JS::Bytecode::Interpreter&, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::EnvironmentCoordinate&) Unexecuted instantiation: Interpreter.cpp:JS::ThrowCompletionOr<void> JS::Bytecode::Op::initialize_or_set_binding<(JS::Bytecode::Op::EnvironmentMode)0, (JS::Bytecode::Op::BindingInitializationMode)1>(JS::Bytecode::Interpreter&, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::EnvironmentCoordinate&) Unexecuted instantiation: Interpreter.cpp:JS::ThrowCompletionOr<void> JS::Bytecode::Op::initialize_or_set_binding<(JS::Bytecode::Op::EnvironmentMode)1, (JS::Bytecode::Op::BindingInitializationMode)1>(JS::Bytecode::Interpreter&, JS::Bytecode::IdentifierTableIndex, JS::Value, JS::EnvironmentCoordinate&) |
2334 | | |
2335 | | ThrowCompletionOr<void> InitializeLexicalBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2336 | 0 | { |
2337 | 0 | return initialize_or_set_binding<EnvironmentMode::Lexical, BindingInitializationMode::Initialize>(interpreter, m_identifier, interpreter.get(m_src), m_cache); |
2338 | 0 | } |
2339 | | |
2340 | | ThrowCompletionOr<void> InitializeVariableBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2341 | 0 | { |
2342 | 0 | return initialize_or_set_binding<EnvironmentMode::Var, BindingInitializationMode::Initialize>(interpreter, m_identifier, interpreter.get(m_src), m_cache); |
2343 | 0 | } |
2344 | | |
2345 | | ThrowCompletionOr<void> SetLexicalBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2346 | 0 | { |
2347 | 0 | return initialize_or_set_binding<EnvironmentMode::Lexical, BindingInitializationMode::Set>(interpreter, m_identifier, interpreter.get(m_src), m_cache); |
2348 | 0 | } |
2349 | | |
2350 | | ThrowCompletionOr<void> SetVariableBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2351 | 0 | { |
2352 | 0 | return initialize_or_set_binding<EnvironmentMode::Var, BindingInitializationMode::Set>(interpreter, m_identifier, interpreter.get(m_src), m_cache); |
2353 | 0 | } |
2354 | | |
2355 | | ThrowCompletionOr<void> GetById::execute_impl(Bytecode::Interpreter& interpreter) const |
2356 | 0 | { |
2357 | 0 | auto base_value = interpreter.get(base()); |
2358 | 0 | auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index]; |
2359 | |
|
2360 | 0 | interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), m_base_identifier, m_property, base_value, base_value, cache, interpreter.current_executable()))); |
2361 | 0 | return {}; |
2362 | 0 | } |
2363 | | |
2364 | | ThrowCompletionOr<void> GetByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2365 | 0 | { |
2366 | 0 | auto base_value = interpreter.get(m_base); |
2367 | 0 | auto this_value = interpreter.get(m_this_value); |
2368 | 0 | auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index]; |
2369 | 0 | interpreter.set(dst(), TRY(get_by_id(interpreter.vm(), {}, m_property, base_value, this_value, cache, interpreter.current_executable()))); |
2370 | 0 | return {}; |
2371 | 0 | } |
2372 | | |
2373 | | ThrowCompletionOr<void> GetLength::execute_impl(Bytecode::Interpreter& interpreter) const |
2374 | 0 | { |
2375 | 0 | auto base_value = interpreter.get(base()); |
2376 | 0 | auto& executable = interpreter.current_executable(); |
2377 | 0 | auto& cache = executable.property_lookup_caches[m_cache_index]; |
2378 | |
|
2379 | 0 | interpreter.set(dst(), TRY(get_by_id<GetByIdMode::Length>(interpreter.vm(), m_base_identifier, *executable.length_identifier, base_value, base_value, cache, executable))); |
2380 | 0 | return {}; |
2381 | 0 | } |
2382 | | |
2383 | | ThrowCompletionOr<void> GetLengthWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2384 | 0 | { |
2385 | 0 | auto base_value = interpreter.get(m_base); |
2386 | 0 | auto this_value = interpreter.get(m_this_value); |
2387 | 0 | auto& executable = interpreter.current_executable(); |
2388 | 0 | auto& cache = executable.property_lookup_caches[m_cache_index]; |
2389 | 0 | interpreter.set(dst(), TRY(get_by_id<GetByIdMode::Length>(interpreter.vm(), {}, *executable.length_identifier, base_value, this_value, cache, executable))); |
2390 | 0 | return {}; |
2391 | 0 | } |
2392 | | |
2393 | | ThrowCompletionOr<void> GetPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const |
2394 | 0 | { |
2395 | 0 | auto& vm = interpreter.vm(); |
2396 | 0 | auto const& name = interpreter.current_executable().get_identifier(m_property); |
2397 | 0 | auto base_value = interpreter.get(m_base); |
2398 | 0 | auto private_reference = make_private_reference(vm, base_value, name); |
2399 | 0 | interpreter.set(dst(), TRY(private_reference.get_value(vm))); |
2400 | 0 | return {}; |
2401 | 0 | } |
2402 | | |
2403 | | ThrowCompletionOr<void> HasPrivateId::execute_impl(Bytecode::Interpreter& interpreter) const |
2404 | 0 | { |
2405 | 0 | auto& vm = interpreter.vm(); |
2406 | |
|
2407 | 0 | auto base = interpreter.get(m_base); |
2408 | 0 | if (!base.is_object()) |
2409 | 0 | return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject); |
2410 | | |
2411 | 0 | auto private_environment = interpreter.running_execution_context().private_environment; |
2412 | 0 | VERIFY(private_environment); |
2413 | 0 | auto private_name = private_environment->resolve_private_identifier(interpreter.current_executable().get_identifier(m_property)); |
2414 | 0 | interpreter.set(dst(), Value(base.as_object().private_element_find(private_name) != nullptr)); |
2415 | 0 | return {}; |
2416 | 0 | } |
2417 | | |
2418 | | ThrowCompletionOr<void> PutById::execute_impl(Bytecode::Interpreter& interpreter) const |
2419 | 0 | { |
2420 | 0 | auto& vm = interpreter.vm(); |
2421 | 0 | auto value = interpreter.get(m_src); |
2422 | 0 | auto base = interpreter.get(m_base); |
2423 | 0 | auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier); |
2424 | 0 | PropertyKey name = interpreter.current_executable().get_identifier(m_property); |
2425 | 0 | auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index]; |
2426 | 0 | TRY(put_by_property_key(vm, base, base, value, base_identifier, name, m_kind, &cache)); |
2427 | 0 | return {}; |
2428 | 0 | } |
2429 | | |
2430 | | ThrowCompletionOr<void> PutByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2431 | 0 | { |
2432 | 0 | auto& vm = interpreter.vm(); |
2433 | 0 | auto value = interpreter.get(m_src); |
2434 | 0 | auto base = interpreter.get(m_base); |
2435 | 0 | PropertyKey name = interpreter.current_executable().get_identifier(m_property); |
2436 | 0 | auto& cache = interpreter.current_executable().property_lookup_caches[m_cache_index]; |
2437 | 0 | TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, {}, name, m_kind, &cache)); |
2438 | 0 | return {}; |
2439 | 0 | } |
2440 | | |
2441 | | ThrowCompletionOr<void> PutPrivateById::execute_impl(Bytecode::Interpreter& interpreter) const |
2442 | 0 | { |
2443 | 0 | auto& vm = interpreter.vm(); |
2444 | 0 | auto value = interpreter.get(m_src); |
2445 | 0 | auto object = TRY(interpreter.get(m_base).to_object(vm)); |
2446 | 0 | auto name = interpreter.current_executable().get_identifier(m_property); |
2447 | 0 | auto private_reference = make_private_reference(vm, object, name); |
2448 | 0 | TRY(private_reference.put_value(vm, value)); |
2449 | 0 | return {}; |
2450 | 0 | } |
2451 | | |
2452 | | ThrowCompletionOr<void> DeleteById::execute_impl(Bytecode::Interpreter& interpreter) const |
2453 | 0 | { |
2454 | 0 | auto base_value = interpreter.get(m_base); |
2455 | 0 | interpreter.set(dst(), TRY(Bytecode::delete_by_id(interpreter, base_value, m_property))); |
2456 | 0 | return {}; |
2457 | 0 | } |
2458 | | |
2459 | | ThrowCompletionOr<void> DeleteByIdWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2460 | 0 | { |
2461 | 0 | auto& vm = interpreter.vm(); |
2462 | 0 | auto base_value = interpreter.get(m_base); |
2463 | 0 | auto const& identifier = interpreter.current_executable().get_identifier(m_property); |
2464 | 0 | bool strict = vm.in_strict_mode(); |
2465 | 0 | auto reference = Reference { base_value, identifier, interpreter.get(m_this_value), strict }; |
2466 | 0 | interpreter.set(dst(), Value(TRY(reference.delete_(vm)))); |
2467 | 0 | return {}; |
2468 | 0 | } |
2469 | | |
2470 | | ThrowCompletionOr<void> ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2471 | 0 | { |
2472 | 0 | auto& cached_this_value = interpreter.reg(Register::this_value()); |
2473 | 0 | if (!cached_this_value.is_empty()) |
2474 | 0 | return {}; |
2475 | | // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's |
2476 | | // resolved once and then saved for subsequent use. |
2477 | 0 | auto& running_execution_context = interpreter.running_execution_context(); |
2478 | 0 | if (auto function = running_execution_context.function; function && is<ECMAScriptFunctionObject>(*function) && !static_cast<ECMAScriptFunctionObject&>(*function).allocates_function_environment()) { |
2479 | 0 | cached_this_value = running_execution_context.this_value; |
2480 | 0 | } else { |
2481 | 0 | auto& vm = interpreter.vm(); |
2482 | 0 | cached_this_value = TRY(vm.resolve_this_binding()); |
2483 | 0 | } |
2484 | 0 | return {}; |
2485 | 0 | } |
2486 | | |
2487 | | // https://tc39.es/ecma262/#sec-makesuperpropertyreference |
2488 | | ThrowCompletionOr<void> ResolveSuperBase::execute_impl(Bytecode::Interpreter& interpreter) const |
2489 | 0 | { |
2490 | 0 | auto& vm = interpreter.vm(); |
2491 | | |
2492 | | // 1. Let env be GetThisEnvironment(). |
2493 | 0 | auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm)); |
2494 | | |
2495 | | // 2. Assert: env.HasSuperBinding() is true. |
2496 | 0 | VERIFY(env.has_super_binding()); |
2497 | | |
2498 | | // 3. Let baseValue be ? env.GetSuperBase(). |
2499 | 0 | interpreter.set(dst(), TRY(env.get_super_base())); |
2500 | |
|
2501 | 0 | return {}; |
2502 | 0 | } |
2503 | | |
2504 | | void GetNewTarget::execute_impl(Bytecode::Interpreter& interpreter) const |
2505 | 0 | { |
2506 | 0 | interpreter.set(dst(), interpreter.vm().get_new_target()); |
2507 | 0 | } |
2508 | | |
2509 | | void GetImportMeta::execute_impl(Bytecode::Interpreter& interpreter) const |
2510 | 0 | { |
2511 | 0 | interpreter.set(dst(), interpreter.vm().get_import_meta()); |
2512 | 0 | } |
2513 | | |
2514 | | static ThrowCompletionOr<Value> dispatch_builtin_call(Bytecode::Interpreter& interpreter, Bytecode::Builtin builtin, ReadonlySpan<Operand> arguments) |
2515 | 0 | { |
2516 | 0 | switch (builtin) { |
2517 | 0 | case Builtin::MathAbs: |
2518 | 0 | return TRY(MathObject::abs_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2519 | 0 | case Builtin::MathLog: |
2520 | 0 | return TRY(MathObject::log_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2521 | 0 | case Builtin::MathPow: |
2522 | 0 | return TRY(MathObject::pow_impl(interpreter.vm(), interpreter.get(arguments[0]), interpreter.get(arguments[1]))); |
2523 | 0 | case Builtin::MathExp: |
2524 | 0 | return TRY(MathObject::exp_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2525 | 0 | case Builtin::MathCeil: |
2526 | 0 | return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2527 | 0 | case Builtin::MathFloor: |
2528 | 0 | return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2529 | 0 | case Builtin::MathRound: |
2530 | 0 | return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2531 | 0 | case Builtin::MathSqrt: |
2532 | 0 | return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.get(arguments[0]))); |
2533 | 0 | case Bytecode::Builtin::__Count: |
2534 | 0 | VERIFY_NOT_REACHED(); |
2535 | 0 | } |
2536 | 0 | VERIFY_NOT_REACHED(); |
2537 | 0 | } |
2538 | | |
2539 | | ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) const |
2540 | 0 | { |
2541 | 0 | auto callee = interpreter.get(m_callee); |
2542 | |
|
2543 | 0 | TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string())); |
2544 | |
|
2545 | 0 | if (m_builtin.has_value() |
2546 | 0 | && m_argument_count == Bytecode::builtin_argument_count(m_builtin.value()) |
2547 | 0 | && callee.is_object() |
2548 | 0 | && interpreter.realm().get_builtin_value(m_builtin.value()) == &callee.as_object()) { |
2549 | 0 | interpreter.set(dst(), TRY(dispatch_builtin_call(interpreter, m_builtin.value(), { m_arguments, m_argument_count }))); |
2550 | 0 | return {}; |
2551 | 0 | } |
2552 | | |
2553 | 0 | Vector<Value> argument_values; |
2554 | 0 | argument_values.ensure_capacity(m_argument_count); |
2555 | 0 | for (size_t i = 0; i < m_argument_count; ++i) |
2556 | 0 | argument_values.unchecked_append(interpreter.get(m_arguments[i])); |
2557 | 0 | interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, argument_values))); |
2558 | 0 | return {}; |
2559 | 0 | } |
2560 | | |
2561 | | ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const |
2562 | 0 | { |
2563 | 0 | auto callee = interpreter.get(m_callee); |
2564 | 0 | TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string())); |
2565 | 0 | auto argument_values = argument_list_evaluation(interpreter.vm(), interpreter.get(arguments())); |
2566 | 0 | interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, move(argument_values)))); |
2567 | 0 | return {}; |
2568 | 0 | } |
2569 | | |
2570 | | // 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation |
2571 | | ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const |
2572 | 0 | { |
2573 | 0 | interpreter.set(dst(), TRY(super_call_with_argument_array(interpreter.vm(), interpreter.get(arguments()), m_is_synthetic))); |
2574 | 0 | return {}; |
2575 | 0 | } |
2576 | | |
2577 | | void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const |
2578 | 0 | { |
2579 | 0 | auto& vm = interpreter.vm(); |
2580 | 0 | interpreter.set(dst(), new_function(vm, m_function_node, m_lhs_name, m_home_object)); |
2581 | 0 | } |
2582 | | |
2583 | | void Return::execute_impl(Bytecode::Interpreter& interpreter) const |
2584 | 0 | { |
2585 | 0 | if (m_value.has_value()) |
2586 | 0 | interpreter.do_return(interpreter.get(*m_value)); |
2587 | 0 | else |
2588 | 0 | interpreter.do_return(js_undefined()); |
2589 | 0 | } |
2590 | | |
2591 | | ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpreter) const |
2592 | 0 | { |
2593 | 0 | auto& vm = interpreter.vm(); |
2594 | 0 | auto old_value = interpreter.get(dst()); |
2595 | | |
2596 | | // OPTIMIZATION: Fast path for Int32 values. |
2597 | 0 | if (old_value.is_int32()) { |
2598 | 0 | auto integer_value = old_value.as_i32(); |
2599 | 0 | if (integer_value != NumericLimits<i32>::max()) [[likely]] { |
2600 | 0 | interpreter.set(dst(), Value { integer_value + 1 }); |
2601 | 0 | return {}; |
2602 | 0 | } |
2603 | 0 | } |
2604 | | |
2605 | 0 | old_value = TRY(old_value.to_numeric(vm)); |
2606 | |
|
2607 | 0 | if (old_value.is_number()) |
2608 | 0 | interpreter.set(dst(), Value(old_value.as_double() + 1)); |
2609 | 0 | else |
2610 | 0 | interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }))); |
2611 | 0 | return {}; |
2612 | 0 | } |
2613 | | |
2614 | | ThrowCompletionOr<void> PostfixIncrement::execute_impl(Bytecode::Interpreter& interpreter) const |
2615 | 0 | { |
2616 | 0 | auto& vm = interpreter.vm(); |
2617 | 0 | auto old_value = interpreter.get(m_src); |
2618 | | |
2619 | | // OPTIMIZATION: Fast path for Int32 values. |
2620 | 0 | if (old_value.is_int32()) { |
2621 | 0 | auto integer_value = old_value.as_i32(); |
2622 | 0 | if (integer_value != NumericLimits<i32>::max()) [[likely]] { |
2623 | 0 | interpreter.set(m_dst, old_value); |
2624 | 0 | interpreter.set(m_src, Value { integer_value + 1 }); |
2625 | 0 | return {}; |
2626 | 0 | } |
2627 | 0 | } |
2628 | | |
2629 | 0 | old_value = TRY(old_value.to_numeric(vm)); |
2630 | 0 | interpreter.set(m_dst, old_value); |
2631 | |
|
2632 | 0 | if (old_value.is_number()) |
2633 | 0 | interpreter.set(m_src, Value(old_value.as_double() + 1)); |
2634 | 0 | else |
2635 | 0 | interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }))); |
2636 | 0 | return {}; |
2637 | 0 | } |
2638 | | |
2639 | | ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpreter) const |
2640 | 0 | { |
2641 | 0 | auto& vm = interpreter.vm(); |
2642 | 0 | auto old_value = interpreter.get(dst()); |
2643 | |
|
2644 | 0 | old_value = TRY(old_value.to_numeric(vm)); |
2645 | |
|
2646 | 0 | if (old_value.is_number()) |
2647 | 0 | interpreter.set(dst(), Value(old_value.as_double() - 1)); |
2648 | 0 | else |
2649 | 0 | interpreter.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }))); |
2650 | 0 | return {}; |
2651 | 0 | } |
2652 | | |
2653 | | ThrowCompletionOr<void> PostfixDecrement::execute_impl(Bytecode::Interpreter& interpreter) const |
2654 | 0 | { |
2655 | 0 | auto& vm = interpreter.vm(); |
2656 | 0 | auto old_value = interpreter.get(m_src); |
2657 | |
|
2658 | 0 | old_value = TRY(old_value.to_numeric(vm)); |
2659 | 0 | interpreter.set(m_dst, old_value); |
2660 | |
|
2661 | 0 | if (old_value.is_number()) |
2662 | 0 | interpreter.set(m_src, Value(old_value.as_double() - 1)); |
2663 | 0 | else |
2664 | 0 | interpreter.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }))); |
2665 | 0 | return {}; |
2666 | 0 | } |
2667 | | |
2668 | | ThrowCompletionOr<void> Throw::execute_impl(Bytecode::Interpreter& interpreter) const |
2669 | 0 | { |
2670 | 0 | return throw_completion(interpreter.get(src())); |
2671 | 0 | } |
2672 | | |
2673 | | ThrowCompletionOr<void> ThrowIfNotObject::execute_impl(Bytecode::Interpreter& interpreter) const |
2674 | 0 | { |
2675 | 0 | auto& vm = interpreter.vm(); |
2676 | 0 | auto src = interpreter.get(m_src); |
2677 | 0 | if (!src.is_object()) |
2678 | 0 | return vm.throw_completion<TypeError>(ErrorType::NotAnObject, src.to_string_without_side_effects()); |
2679 | 0 | return {}; |
2680 | 0 | } |
2681 | | |
2682 | | ThrowCompletionOr<void> ThrowIfNullish::execute_impl(Bytecode::Interpreter& interpreter) const |
2683 | 0 | { |
2684 | 0 | auto& vm = interpreter.vm(); |
2685 | 0 | auto value = interpreter.get(m_src); |
2686 | 0 | if (value.is_nullish()) |
2687 | 0 | return vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects()); |
2688 | 0 | return {}; |
2689 | 0 | } |
2690 | | |
2691 | | ThrowCompletionOr<void> ThrowIfTDZ::execute_impl(Bytecode::Interpreter& interpreter) const |
2692 | 0 | { |
2693 | 0 | auto& vm = interpreter.vm(); |
2694 | 0 | auto value = interpreter.get(m_src); |
2695 | 0 | if (value.is_empty()) |
2696 | 0 | return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, value.to_string_without_side_effects()); |
2697 | 0 | return {}; |
2698 | 0 | } |
2699 | | |
2700 | | void LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2701 | 0 | { |
2702 | 0 | auto& running_execution_context = interpreter.running_execution_context(); |
2703 | 0 | running_execution_context.lexical_environment = running_execution_context.saved_lexical_environments.take_last(); |
2704 | 0 | } |
2705 | | |
2706 | | void LeavePrivateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const |
2707 | 0 | { |
2708 | 0 | auto& running_execution_context = interpreter.vm().running_execution_context(); |
2709 | 0 | running_execution_context.private_environment = running_execution_context.private_environment->outer_environment(); |
2710 | 0 | } |
2711 | | |
2712 | | void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const |
2713 | 0 | { |
2714 | 0 | interpreter.leave_unwind_context(); |
2715 | 0 | } |
2716 | | |
2717 | | void Yield::execute_impl(Bytecode::Interpreter& interpreter) const |
2718 | 0 | { |
2719 | 0 | auto yielded_value = interpreter.get(m_value).value_or(js_undefined()); |
2720 | 0 | interpreter.do_return( |
2721 | 0 | interpreter.do_yield(yielded_value, m_continuation_label)); |
2722 | 0 | } |
2723 | | |
2724 | | void PrepareYield::execute_impl(Bytecode::Interpreter& interpreter) const |
2725 | 0 | { |
2726 | 0 | auto value = interpreter.get(m_value).value_or(js_undefined()); |
2727 | 0 | interpreter.set(m_dest, interpreter.do_yield(value, {})); |
2728 | 0 | } |
2729 | | |
2730 | | void Await::execute_impl(Bytecode::Interpreter& interpreter) const |
2731 | 0 | { |
2732 | 0 | auto yielded_value = interpreter.get(m_argument).value_or(js_undefined()); |
2733 | 0 | auto object = Object::create(interpreter.realm(), nullptr); |
2734 | 0 | object->define_direct_property("result", yielded_value, JS::default_attributes); |
2735 | | // FIXME: If we get a pointer, which is not accurately representable as a double |
2736 | | // will cause this to explode |
2737 | 0 | object->define_direct_property("continuation", Value(m_continuation_label.address()), JS::default_attributes); |
2738 | 0 | object->define_direct_property("isAwait", Value(true), JS::default_attributes); |
2739 | 0 | interpreter.do_return(object); |
2740 | 0 | } |
2741 | | |
2742 | | ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const |
2743 | 0 | { |
2744 | 0 | interpreter.set(dst(), TRY(get_by_value(interpreter.vm(), m_base_identifier, interpreter.get(m_base), interpreter.get(m_property), interpreter.current_executable()))); |
2745 | 0 | return {}; |
2746 | 0 | } |
2747 | | |
2748 | | ThrowCompletionOr<void> GetByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2749 | 0 | { |
2750 | 0 | auto& vm = interpreter.vm(); |
2751 | 0 | auto property_key_value = interpreter.get(m_property); |
2752 | 0 | auto object = TRY(interpreter.get(m_base).to_object(vm)); |
2753 | 0 | auto property_key = TRY(property_key_value.to_property_key(vm)); |
2754 | 0 | interpreter.set(dst(), TRY(object->internal_get(property_key, interpreter.get(m_this_value)))); |
2755 | 0 | return {}; |
2756 | 0 | } |
2757 | | |
2758 | | ThrowCompletionOr<void> PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const |
2759 | 0 | { |
2760 | 0 | auto& vm = interpreter.vm(); |
2761 | 0 | auto value = interpreter.get(m_src); |
2762 | 0 | auto base_identifier = interpreter.current_executable().get_identifier(m_base_identifier); |
2763 | 0 | TRY(put_by_value(vm, interpreter.get(m_base), base_identifier, interpreter.get(m_property), value, m_kind)); |
2764 | 0 | return {}; |
2765 | 0 | } |
2766 | | |
2767 | | ThrowCompletionOr<void> PutByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2768 | 0 | { |
2769 | 0 | auto& vm = interpreter.vm(); |
2770 | 0 | auto value = interpreter.get(m_src); |
2771 | 0 | auto base = interpreter.get(m_base); |
2772 | 0 | auto property_key = m_kind != PropertyKind::Spread ? TRY(interpreter.get(m_property).to_property_key(vm)) : PropertyKey {}; |
2773 | 0 | TRY(put_by_property_key(vm, base, interpreter.get(m_this_value), value, {}, property_key, m_kind)); |
2774 | 0 | return {}; |
2775 | 0 | } |
2776 | | |
2777 | | ThrowCompletionOr<void> DeleteByValue::execute_impl(Bytecode::Interpreter& interpreter) const |
2778 | 0 | { |
2779 | 0 | auto base_value = interpreter.get(m_base); |
2780 | 0 | auto property_key_value = interpreter.get(m_property); |
2781 | 0 | interpreter.set(dst(), TRY(delete_by_value(interpreter, base_value, property_key_value))); |
2782 | |
|
2783 | 0 | return {}; |
2784 | 0 | } |
2785 | | |
2786 | | ThrowCompletionOr<void> DeleteByValueWithThis::execute_impl(Bytecode::Interpreter& interpreter) const |
2787 | 0 | { |
2788 | 0 | auto property_key_value = interpreter.get(m_property); |
2789 | 0 | auto base_value = interpreter.get(m_base); |
2790 | 0 | auto this_value = interpreter.get(m_this_value); |
2791 | 0 | interpreter.set(dst(), TRY(delete_by_value_with_this(interpreter, base_value, property_key_value, this_value))); |
2792 | |
|
2793 | 0 | return {}; |
2794 | 0 | } |
2795 | | |
2796 | | ThrowCompletionOr<void> GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const |
2797 | 0 | { |
2798 | 0 | auto& vm = interpreter.vm(); |
2799 | 0 | interpreter.set(dst(), TRY(get_iterator(vm, interpreter.get(iterable()), m_hint))); |
2800 | 0 | return {}; |
2801 | 0 | } |
2802 | | |
2803 | | ThrowCompletionOr<void> GetObjectFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const |
2804 | 0 | { |
2805 | 0 | auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object()); |
2806 | 0 | interpreter.set(m_object, iterator_record.iterator); |
2807 | 0 | return {}; |
2808 | 0 | } |
2809 | | |
2810 | | ThrowCompletionOr<void> GetNextMethodFromIteratorRecord::execute_impl(Bytecode::Interpreter& interpreter) const |
2811 | 0 | { |
2812 | 0 | auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object()); |
2813 | 0 | interpreter.set(m_next_method, iterator_record.next_method); |
2814 | 0 | return {}; |
2815 | 0 | } |
2816 | | |
2817 | | ThrowCompletionOr<void> GetMethod::execute_impl(Bytecode::Interpreter& interpreter) const |
2818 | 0 | { |
2819 | 0 | auto& vm = interpreter.vm(); |
2820 | 0 | auto identifier = interpreter.current_executable().get_identifier(m_property); |
2821 | 0 | auto method = TRY(interpreter.get(m_object).get_method(vm, identifier)); |
2822 | 0 | interpreter.set(dst(), method ?: js_undefined()); |
2823 | 0 | return {}; |
2824 | 0 | } |
2825 | | |
2826 | | ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(Bytecode::Interpreter& interpreter) const |
2827 | 0 | { |
2828 | 0 | interpreter.set(dst(), TRY(get_object_property_iterator(interpreter.vm(), interpreter.get(object())))); |
2829 | 0 | return {}; |
2830 | 0 | } |
2831 | | |
2832 | | ThrowCompletionOr<void> IteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const |
2833 | 0 | { |
2834 | 0 | auto& vm = interpreter.vm(); |
2835 | 0 | auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object()); |
2836 | | |
2837 | | // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!) |
2838 | 0 | TRY(iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value })); |
2839 | 0 | return {}; |
2840 | 0 | } |
2841 | | |
2842 | | ThrowCompletionOr<void> AsyncIteratorClose::execute_impl(Bytecode::Interpreter& interpreter) const |
2843 | 0 | { |
2844 | 0 | auto& vm = interpreter.vm(); |
2845 | 0 | auto& iterator = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object()); |
2846 | | |
2847 | | // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!) |
2848 | 0 | TRY(async_iterator_close(vm, iterator, Completion { m_completion_type, m_completion_value })); |
2849 | 0 | return {}; |
2850 | 0 | } |
2851 | | |
2852 | | ThrowCompletionOr<void> IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const |
2853 | 0 | { |
2854 | 0 | auto& vm = interpreter.vm(); |
2855 | 0 | auto& iterator_record = verify_cast<IteratorRecord>(interpreter.get(m_iterator_record).as_object()); |
2856 | 0 | interpreter.set(dst(), TRY(iterator_next(vm, iterator_record))); |
2857 | 0 | return {}; |
2858 | 0 | } |
2859 | | |
2860 | | ThrowCompletionOr<void> NewClass::execute_impl(Bytecode::Interpreter& interpreter) const |
2861 | 0 | { |
2862 | 0 | Value super_class; |
2863 | 0 | if (m_super_class.has_value()) |
2864 | 0 | super_class = interpreter.get(m_super_class.value()); |
2865 | 0 | Vector<Value> element_keys; |
2866 | 0 | for (size_t i = 0; i < m_element_keys_count; ++i) { |
2867 | 0 | Value element_key; |
2868 | 0 | if (m_element_keys[i].has_value()) |
2869 | 0 | element_key = interpreter.get(m_element_keys[i].value()); |
2870 | 0 | element_keys.append(element_key); |
2871 | 0 | } |
2872 | 0 | interpreter.set(dst(), TRY(new_class(interpreter.vm(), super_class, m_class_expression, m_lhs_name, element_keys))); |
2873 | 0 | return {}; |
2874 | 0 | } |
2875 | | |
2876 | | // 13.5.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-typeof-operator-runtime-semantics-evaluation |
2877 | | ThrowCompletionOr<void> TypeofBinding::execute_impl(Bytecode::Interpreter& interpreter) const |
2878 | 0 | { |
2879 | 0 | auto& vm = interpreter.vm(); |
2880 | |
|
2881 | 0 | if (m_cache.is_valid()) { |
2882 | 0 | auto const* environment = interpreter.running_execution_context().lexical_environment.ptr(); |
2883 | 0 | for (size_t i = 0; i < m_cache.hops; ++i) |
2884 | 0 | environment = environment->outer_environment(); |
2885 | 0 | if (!environment->is_permanently_screwed_by_eval()) { |
2886 | 0 | auto value = TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, m_cache.index)); |
2887 | 0 | interpreter.set(dst(), value.typeof_(vm)); |
2888 | 0 | return {}; |
2889 | 0 | } |
2890 | 0 | m_cache = {}; |
2891 | 0 | } |
2892 | | |
2893 | | // 1. Let val be the result of evaluating UnaryExpression. |
2894 | 0 | auto reference = TRY(vm.resolve_binding(interpreter.current_executable().get_identifier(m_identifier))); |
2895 | | |
2896 | | // 2. If val is a Reference Record, then |
2897 | | // a. If IsUnresolvableReference(val) is true, return "undefined". |
2898 | 0 | if (reference.is_unresolvable()) { |
2899 | 0 | interpreter.set(dst(), PrimitiveString::create(vm, "undefined"_string)); |
2900 | 0 | return {}; |
2901 | 0 | } |
2902 | | |
2903 | | // 3. Set val to ? GetValue(val). |
2904 | 0 | auto value = TRY(reference.get_value(vm)); |
2905 | |
|
2906 | 0 | if (reference.environment_coordinate().has_value()) |
2907 | 0 | m_cache = reference.environment_coordinate().value(); |
2908 | | |
2909 | | // 4. NOTE: This step is replaced in section B.3.6.3. |
2910 | | // 5. Return a String according to Table 41. |
2911 | 0 | interpreter.set(dst(), value.typeof_(vm)); |
2912 | 0 | return {}; |
2913 | 0 | } |
2914 | | |
2915 | | void BlockDeclarationInstantiation::execute_impl(Bytecode::Interpreter& interpreter) const |
2916 | 0 | { |
2917 | 0 | auto& vm = interpreter.vm(); |
2918 | 0 | auto old_environment = interpreter.running_execution_context().lexical_environment; |
2919 | 0 | auto& running_execution_context = interpreter.running_execution_context(); |
2920 | 0 | running_execution_context.saved_lexical_environments.append(old_environment); |
2921 | 0 | running_execution_context.lexical_environment = new_declarative_environment(*old_environment); |
2922 | 0 | m_scope_node.block_declaration_instantiation(vm, running_execution_context.lexical_environment); |
2923 | 0 | } |
2924 | | |
2925 | | ByteString Mov::to_byte_string_impl(Bytecode::Executable const& executable) const |
2926 | 0 | { |
2927 | 0 | return ByteString::formatted("Mov {}, {}", |
2928 | 0 | format_operand("dst"sv, m_dst, executable), |
2929 | 0 | format_operand("src"sv, m_src, executable)); |
2930 | 0 | } |
2931 | | |
2932 | | ByteString NewArray::to_byte_string_impl(Bytecode::Executable const& executable) const |
2933 | 0 | { |
2934 | 0 | StringBuilder builder; |
2935 | 0 | builder.appendff("NewArray {}", format_operand("dst"sv, dst(), executable)); |
2936 | 0 | if (m_element_count != 0) { |
2937 | 0 | builder.appendff(", {}", format_operand_list("args"sv, { m_elements, m_element_count }, executable)); |
2938 | 0 | } |
2939 | 0 | return builder.to_byte_string(); |
2940 | 0 | } |
2941 | | |
2942 | | ByteString NewPrimitiveArray::to_byte_string_impl(Bytecode::Executable const& executable) const |
2943 | 0 | { |
2944 | 0 | return ByteString::formatted("NewPrimitiveArray {}, {}"sv, |
2945 | 0 | format_operand("dst"sv, dst(), executable), |
2946 | 0 | format_value_list("elements"sv, elements())); |
2947 | 0 | } |
2948 | | |
2949 | | ByteString AddPrivateName::to_byte_string_impl(Bytecode::Executable const& executable) const |
2950 | 0 | { |
2951 | 0 | return ByteString::formatted("AddPrivateName {}"sv, executable.identifier_table->get(m_name)); |
2952 | 0 | } |
2953 | | |
2954 | | ByteString ArrayAppend::to_byte_string_impl(Bytecode::Executable const& executable) const |
2955 | 0 | { |
2956 | 0 | return ByteString::formatted("Append {}, {}{}", |
2957 | 0 | format_operand("dst"sv, dst(), executable), |
2958 | 0 | format_operand("src"sv, src(), executable), |
2959 | 0 | m_is_spread ? " **"sv : ""sv); |
2960 | 0 | } |
2961 | | |
2962 | | ByteString IteratorToArray::to_byte_string_impl(Bytecode::Executable const& executable) const |
2963 | 0 | { |
2964 | 0 | return ByteString::formatted("IteratorToArray {}, {}", |
2965 | 0 | format_operand("dst"sv, dst(), executable), |
2966 | 0 | format_operand("iterator"sv, iterator(), executable)); |
2967 | 0 | } |
2968 | | |
2969 | | ByteString NewObject::to_byte_string_impl(Bytecode::Executable const& executable) const |
2970 | 0 | { |
2971 | 0 | return ByteString::formatted("NewObject {}", format_operand("dst"sv, dst(), executable)); |
2972 | 0 | } |
2973 | | |
2974 | | ByteString NewRegExp::to_byte_string_impl(Bytecode::Executable const& executable) const |
2975 | 0 | { |
2976 | 0 | return ByteString::formatted("NewRegExp {}, source:{} (\"{}\") flags:{} (\"{}\")", |
2977 | 0 | format_operand("dst"sv, dst(), executable), |
2978 | 0 | m_source_index, executable.get_string(m_source_index), m_flags_index, executable.get_string(m_flags_index)); |
2979 | 0 | } |
2980 | | |
2981 | | ByteString CopyObjectExcludingProperties::to_byte_string_impl(Bytecode::Executable const& executable) const |
2982 | 0 | { |
2983 | 0 | StringBuilder builder; |
2984 | 0 | builder.appendff("CopyObjectExcludingProperties {}, {}", |
2985 | 0 | format_operand("dst"sv, dst(), executable), |
2986 | 0 | format_operand("from"sv, m_from_object, executable)); |
2987 | 0 | if (m_excluded_names_count != 0) { |
2988 | 0 | builder.append(" excluding:["sv); |
2989 | 0 | for (size_t i = 0; i < m_excluded_names_count; ++i) { |
2990 | 0 | if (i != 0) |
2991 | 0 | builder.append(", "sv); |
2992 | 0 | builder.append(format_operand("#"sv, m_excluded_names[i], executable)); |
2993 | 0 | } |
2994 | 0 | builder.append(']'); |
2995 | 0 | } |
2996 | 0 | return builder.to_byte_string(); |
2997 | 0 | } |
2998 | | |
2999 | | ByteString ConcatString::to_byte_string_impl(Bytecode::Executable const& executable) const |
3000 | 0 | { |
3001 | 0 | return ByteString::formatted("ConcatString {}, {}", |
3002 | 0 | format_operand("dst"sv, dst(), executable), |
3003 | 0 | format_operand("src"sv, src(), executable)); |
3004 | 0 | } |
3005 | | |
3006 | | ByteString GetCalleeAndThisFromEnvironment::to_byte_string_impl(Bytecode::Executable const& executable) const |
3007 | 0 | { |
3008 | 0 | return ByteString::formatted("GetCalleeAndThisFromEnvironment {}, {} <- {}", |
3009 | 0 | format_operand("callee"sv, m_callee, executable), |
3010 | 0 | format_operand("this"sv, m_this_value, executable), |
3011 | 0 | executable.identifier_table->get(m_identifier)); |
3012 | 0 | } |
3013 | | |
3014 | | ByteString GetBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3015 | 0 | { |
3016 | 0 | return ByteString::formatted("GetBinding {}, {}", |
3017 | 0 | format_operand("dst"sv, dst(), executable), |
3018 | 0 | executable.identifier_table->get(m_identifier)); |
3019 | 0 | } |
3020 | | |
3021 | | ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const |
3022 | 0 | { |
3023 | 0 | return ByteString::formatted("GetGlobal {}, {}", format_operand("dst"sv, dst(), executable), |
3024 | 0 | executable.identifier_table->get(m_identifier)); |
3025 | 0 | } |
3026 | | |
3027 | | ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const |
3028 | 0 | { |
3029 | 0 | return ByteString::formatted("DeleteVariable {}", executable.identifier_table->get(m_identifier)); |
3030 | 0 | } |
3031 | | |
3032 | | ByteString CreateLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const |
3033 | 0 | { |
3034 | 0 | return "CreateLexicalEnvironment"sv; |
3035 | 0 | } |
3036 | | |
3037 | | ByteString CreatePrivateEnvironment::to_byte_string_impl(Bytecode::Executable const&) const |
3038 | 0 | { |
3039 | 0 | return "CreatePrivateEnvironment"sv; |
3040 | 0 | } |
3041 | | |
3042 | | ByteString CreateVariableEnvironment::to_byte_string_impl(Bytecode::Executable const&) const |
3043 | 0 | { |
3044 | 0 | return "CreateVariableEnvironment"sv; |
3045 | 0 | } |
3046 | | |
3047 | | ByteString CreateVariable::to_byte_string_impl(Bytecode::Executable const& executable) const |
3048 | 0 | { |
3049 | 0 | auto mode_string = m_mode == EnvironmentMode::Lexical ? "Lexical" : "Variable"; |
3050 | 0 | return ByteString::formatted("CreateVariable env:{} immutable:{} global:{} {}", mode_string, m_is_immutable, m_is_global, executable.identifier_table->get(m_identifier)); |
3051 | 0 | } |
3052 | | |
3053 | | ByteString CreateRestParams::to_byte_string_impl(Bytecode::Executable const& executable) const |
3054 | 0 | { |
3055 | 0 | return ByteString::formatted("CreateRestParams {}, rest_index:{}", format_operand("dst"sv, m_dst, executable), m_rest_index); |
3056 | 0 | } |
3057 | | |
3058 | | ByteString CreateArguments::to_byte_string_impl(Bytecode::Executable const& executable) const |
3059 | 0 | { |
3060 | 0 | StringBuilder builder; |
3061 | 0 | builder.appendff("CreateArguments"); |
3062 | 0 | if (m_dst.has_value()) |
3063 | 0 | builder.appendff(" {}", format_operand("dst"sv, *m_dst, executable)); |
3064 | 0 | builder.appendff(" {} immutable:{}", m_kind == Kind::Mapped ? "mapped"sv : "unmapped"sv, m_is_immutable); |
3065 | 0 | return builder.to_byte_string(); |
3066 | 0 | } |
3067 | | |
3068 | | ByteString EnterObjectEnvironment::to_byte_string_impl(Executable const& executable) const |
3069 | 0 | { |
3070 | 0 | return ByteString::formatted("EnterObjectEnvironment {}", |
3071 | 0 | format_operand("object"sv, m_object, executable)); |
3072 | 0 | } |
3073 | | |
3074 | | ByteString InitializeLexicalBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3075 | 0 | { |
3076 | 0 | return ByteString::formatted("InitializeLexicalBinding {}, {}", |
3077 | 0 | executable.identifier_table->get(m_identifier), |
3078 | 0 | format_operand("src"sv, src(), executable)); |
3079 | 0 | } |
3080 | | |
3081 | | ByteString InitializeVariableBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3082 | 0 | { |
3083 | 0 | return ByteString::formatted("InitializeVariableBinding {}, {}", |
3084 | 0 | executable.identifier_table->get(m_identifier), |
3085 | 0 | format_operand("src"sv, src(), executable)); |
3086 | 0 | } |
3087 | | |
3088 | | ByteString SetLexicalBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3089 | 0 | { |
3090 | 0 | return ByteString::formatted("SetLexicalBinding {}, {}", |
3091 | 0 | executable.identifier_table->get(m_identifier), |
3092 | 0 | format_operand("src"sv, src(), executable)); |
3093 | 0 | } |
3094 | | |
3095 | | ByteString SetVariableBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3096 | 0 | { |
3097 | 0 | return ByteString::formatted("SetVariableBinding {}, {}", |
3098 | 0 | executable.identifier_table->get(m_identifier), |
3099 | 0 | format_operand("src"sv, src(), executable)); |
3100 | 0 | } |
3101 | | |
3102 | | ByteString GetArgument::to_byte_string_impl(Bytecode::Executable const& executable) const |
3103 | 0 | { |
3104 | 0 | return ByteString::formatted("GetArgument {}, {}", index(), format_operand("dst"sv, dst(), executable)); |
3105 | 0 | } |
3106 | | |
3107 | | ByteString SetArgument::to_byte_string_impl(Bytecode::Executable const& executable) const |
3108 | 0 | { |
3109 | 0 | return ByteString::formatted("SetArgument {}, {}", index(), format_operand("src"sv, src(), executable)); |
3110 | 0 | } |
3111 | | |
3112 | | static StringView property_kind_to_string(PropertyKind kind) |
3113 | 0 | { |
3114 | 0 | switch (kind) { |
3115 | 0 | case PropertyKind::Getter: |
3116 | 0 | return "getter"sv; |
3117 | 0 | case PropertyKind::Setter: |
3118 | 0 | return "setter"sv; |
3119 | 0 | case PropertyKind::KeyValue: |
3120 | 0 | return "key-value"sv; |
3121 | 0 | case PropertyKind::DirectKeyValue: |
3122 | 0 | return "direct-key-value"sv; |
3123 | 0 | case PropertyKind::Spread: |
3124 | 0 | return "spread"sv; |
3125 | 0 | case PropertyKind::ProtoSetter: |
3126 | 0 | return "proto-setter"sv; |
3127 | 0 | } |
3128 | 0 | VERIFY_NOT_REACHED(); |
3129 | 0 | } |
3130 | | |
3131 | | ByteString PutById::to_byte_string_impl(Bytecode::Executable const& executable) const |
3132 | 0 | { |
3133 | 0 | auto kind = property_kind_to_string(m_kind); |
3134 | 0 | return ByteString::formatted("PutById {}, {}, {}, kind:{}", |
3135 | 0 | format_operand("base"sv, m_base, executable), |
3136 | 0 | executable.identifier_table->get(m_property), |
3137 | 0 | format_operand("src"sv, m_src, executable), |
3138 | 0 | kind); |
3139 | 0 | } |
3140 | | |
3141 | | ByteString PutByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3142 | 0 | { |
3143 | 0 | auto kind = property_kind_to_string(m_kind); |
3144 | 0 | return ByteString::formatted("PutByIdWithThis {}, {}, {}, {}, kind:{}", |
3145 | 0 | format_operand("base"sv, m_base, executable), |
3146 | 0 | executable.identifier_table->get(m_property), |
3147 | 0 | format_operand("src"sv, m_src, executable), |
3148 | 0 | format_operand("this"sv, m_this_value, executable), |
3149 | 0 | kind); |
3150 | 0 | } |
3151 | | |
3152 | | ByteString PutPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const |
3153 | 0 | { |
3154 | 0 | auto kind = property_kind_to_string(m_kind); |
3155 | 0 | return ByteString::formatted( |
3156 | 0 | "PutPrivateById {}, {}, {}, kind:{} ", |
3157 | 0 | format_operand("base"sv, m_base, executable), |
3158 | 0 | executable.identifier_table->get(m_property), |
3159 | 0 | format_operand("src"sv, m_src, executable), |
3160 | 0 | kind); |
3161 | 0 | } |
3162 | | |
3163 | | ByteString GetById::to_byte_string_impl(Bytecode::Executable const& executable) const |
3164 | 0 | { |
3165 | 0 | return ByteString::formatted("GetById {}, {}, {}", |
3166 | 0 | format_operand("dst"sv, m_dst, executable), |
3167 | 0 | format_operand("base"sv, m_base, executable), |
3168 | 0 | executable.identifier_table->get(m_property)); |
3169 | 0 | } |
3170 | | |
3171 | | ByteString GetByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3172 | 0 | { |
3173 | 0 | return ByteString::formatted("GetByIdWithThis {}, {}, {}, {}", |
3174 | 0 | format_operand("dst"sv, m_dst, executable), |
3175 | 0 | format_operand("base"sv, m_base, executable), |
3176 | 0 | executable.identifier_table->get(m_property), |
3177 | 0 | format_operand("this"sv, m_this_value, executable)); |
3178 | 0 | } |
3179 | | |
3180 | | ByteString GetLength::to_byte_string_impl(Bytecode::Executable const& executable) const |
3181 | 0 | { |
3182 | 0 | return ByteString::formatted("GetLength {}, {}", |
3183 | 0 | format_operand("dst"sv, m_dst, executable), |
3184 | 0 | format_operand("base"sv, m_base, executable)); |
3185 | 0 | } |
3186 | | |
3187 | | ByteString GetLengthWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3188 | 0 | { |
3189 | 0 | return ByteString::formatted("GetLengthWithThis {}, {}, {}", |
3190 | 0 | format_operand("dst"sv, m_dst, executable), |
3191 | 0 | format_operand("base"sv, m_base, executable), |
3192 | 0 | format_operand("this"sv, m_this_value, executable)); |
3193 | 0 | } |
3194 | | |
3195 | | ByteString GetPrivateById::to_byte_string_impl(Bytecode::Executable const& executable) const |
3196 | 0 | { |
3197 | 0 | return ByteString::formatted("GetPrivateById {}, {}, {}", |
3198 | 0 | format_operand("dst"sv, m_dst, executable), |
3199 | 0 | format_operand("base"sv, m_base, executable), |
3200 | 0 | executable.identifier_table->get(m_property)); |
3201 | 0 | } |
3202 | | |
3203 | | ByteString HasPrivateId::to_byte_string_impl(Bytecode::Executable const& executable) const |
3204 | 0 | { |
3205 | 0 | return ByteString::formatted("HasPrivateId {}, {}, {}", |
3206 | 0 | format_operand("dst"sv, m_dst, executable), |
3207 | 0 | format_operand("base"sv, m_base, executable), |
3208 | 0 | executable.identifier_table->get(m_property)); |
3209 | 0 | } |
3210 | | |
3211 | | ByteString DeleteById::to_byte_string_impl(Bytecode::Executable const& executable) const |
3212 | 0 | { |
3213 | 0 | return ByteString::formatted("DeleteById {}, {}, {}", |
3214 | 0 | format_operand("dst"sv, m_dst, executable), |
3215 | 0 | format_operand("base"sv, m_base, executable), |
3216 | 0 | executable.identifier_table->get(m_property)); |
3217 | 0 | } |
3218 | | |
3219 | | ByteString DeleteByIdWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3220 | 0 | { |
3221 | 0 | return ByteString::formatted("DeleteByIdWithThis {}, {}, {}, {}", |
3222 | 0 | format_operand("dst"sv, m_dst, executable), |
3223 | 0 | format_operand("base"sv, m_base, executable), |
3224 | 0 | executable.identifier_table->get(m_property), |
3225 | 0 | format_operand("this"sv, m_this_value, executable)); |
3226 | 0 | } |
3227 | | |
3228 | | ByteString Jump::to_byte_string_impl(Bytecode::Executable const&) const |
3229 | 0 | { |
3230 | 0 | return ByteString::formatted("Jump {}", m_target); |
3231 | 0 | } |
3232 | | |
3233 | | ByteString JumpIf::to_byte_string_impl(Bytecode::Executable const& executable) const |
3234 | 0 | { |
3235 | 0 | return ByteString::formatted("JumpIf {}, \033[32mtrue\033[0m:{} \033[32mfalse\033[0m:{}", |
3236 | 0 | format_operand("condition"sv, m_condition, executable), |
3237 | 0 | m_true_target, |
3238 | 0 | m_false_target); |
3239 | 0 | } |
3240 | | |
3241 | | ByteString JumpTrue::to_byte_string_impl(Bytecode::Executable const& executable) const |
3242 | 0 | { |
3243 | 0 | return ByteString::formatted("JumpTrue {}, {}", |
3244 | 0 | format_operand("condition"sv, m_condition, executable), |
3245 | 0 | m_target); |
3246 | 0 | } |
3247 | | |
3248 | | ByteString JumpFalse::to_byte_string_impl(Bytecode::Executable const& executable) const |
3249 | 0 | { |
3250 | 0 | return ByteString::formatted("JumpFalse {}, {}", |
3251 | 0 | format_operand("condition"sv, m_condition, executable), |
3252 | 0 | m_target); |
3253 | 0 | } |
3254 | | |
3255 | | ByteString JumpNullish::to_byte_string_impl(Bytecode::Executable const& executable) const |
3256 | 0 | { |
3257 | 0 | return ByteString::formatted("JumpNullish {}, null:{} nonnull:{}", |
3258 | 0 | format_operand("condition"sv, m_condition, executable), |
3259 | 0 | m_true_target, |
3260 | 0 | m_false_target); |
3261 | 0 | } |
3262 | | |
3263 | | #define HANDLE_COMPARISON_OP(op_TitleCase, op_snake_case, numeric_operator) \ |
3264 | | ByteString Jump##op_TitleCase::to_byte_string_impl(Bytecode::Executable const& executable) const \ |
3265 | 0 | { \ |
3266 | 0 | return ByteString::formatted("Jump" #op_TitleCase " {}, {}, true:{}, false:{}", \ |
3267 | 0 | format_operand("lhs"sv, m_lhs, executable), \ |
3268 | 0 | format_operand("rhs"sv, m_rhs, executable), \ |
3269 | 0 | m_true_target, \ |
3270 | 0 | m_false_target); \ |
3271 | 0 | } Unexecuted instantiation: JS::Bytecode::Op::JumpLessThan::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpLessThanEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpGreaterThan::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpGreaterThanEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpLooselyEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpLooselyInequals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpStrictlyEquals::to_byte_string_impl(JS::Bytecode::Executable const&) const Unexecuted instantiation: JS::Bytecode::Op::JumpStrictlyInequals::to_byte_string_impl(JS::Bytecode::Executable const&) const |
3272 | | |
3273 | | JS_ENUMERATE_COMPARISON_OPS(HANDLE_COMPARISON_OP) |
3274 | | |
3275 | | ByteString JumpUndefined::to_byte_string_impl(Bytecode::Executable const& executable) const |
3276 | 0 | { |
3277 | 0 | return ByteString::formatted("JumpUndefined {}, undefined:{} defined:{}", |
3278 | 0 | format_operand("condition"sv, m_condition, executable), |
3279 | 0 | m_true_target, |
3280 | 0 | m_false_target); |
3281 | 0 | } |
3282 | | |
3283 | | static StringView call_type_to_string(CallType type) |
3284 | 0 | { |
3285 | 0 | switch (type) { |
3286 | 0 | case CallType::Call: |
3287 | 0 | return ""sv; |
3288 | 0 | case CallType::Construct: |
3289 | 0 | return " (Construct)"sv; |
3290 | 0 | case CallType::DirectEval: |
3291 | 0 | return " (DirectEval)"sv; |
3292 | 0 | } |
3293 | 0 | VERIFY_NOT_REACHED(); |
3294 | 0 | } |
3295 | | |
3296 | | ByteString Call::to_byte_string_impl(Bytecode::Executable const& executable) const |
3297 | 0 | { |
3298 | 0 | auto type = call_type_to_string(m_type); |
3299 | |
|
3300 | 0 | StringBuilder builder; |
3301 | 0 | builder.appendff("Call{} {}, {}, {}, "sv, |
3302 | 0 | type, |
3303 | 0 | format_operand("dst"sv, m_dst, executable), |
3304 | 0 | format_operand("callee"sv, m_callee, executable), |
3305 | 0 | format_operand("this"sv, m_this_value, executable)); |
3306 | |
|
3307 | 0 | builder.append(format_operand_list("args"sv, { m_arguments, m_argument_count }, executable)); |
3308 | |
|
3309 | 0 | if (m_builtin.has_value()) { |
3310 | 0 | builder.appendff(", (builtin:{})", m_builtin.value()); |
3311 | 0 | } |
3312 | |
|
3313 | 0 | if (m_expression_string.has_value()) { |
3314 | 0 | builder.appendff(", `{}`", executable.get_string(m_expression_string.value())); |
3315 | 0 | } |
3316 | |
|
3317 | 0 | return builder.to_byte_string(); |
3318 | 0 | } |
3319 | | |
3320 | | ByteString CallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const |
3321 | 0 | { |
3322 | 0 | auto type = call_type_to_string(m_type); |
3323 | 0 | StringBuilder builder; |
3324 | 0 | builder.appendff("CallWithArgumentArray{} {}, {}, {}, {}", |
3325 | 0 | type, |
3326 | 0 | format_operand("dst"sv, m_dst, executable), |
3327 | 0 | format_operand("callee"sv, m_callee, executable), |
3328 | 0 | format_operand("this"sv, m_this_value, executable), |
3329 | 0 | format_operand("arguments"sv, m_arguments, executable)); |
3330 | |
|
3331 | 0 | if (m_expression_string.has_value()) |
3332 | 0 | builder.appendff(" ({})", executable.get_string(m_expression_string.value())); |
3333 | 0 | return builder.to_byte_string(); |
3334 | 0 | } |
3335 | | |
3336 | | ByteString SuperCallWithArgumentArray::to_byte_string_impl(Bytecode::Executable const& executable) const |
3337 | 0 | { |
3338 | 0 | return ByteString::formatted("SuperCallWithArgumentArray {}, {}", |
3339 | 0 | format_operand("dst"sv, m_dst, executable), |
3340 | 0 | format_operand("arguments"sv, m_arguments, executable)); |
3341 | 0 | } |
3342 | | |
3343 | | ByteString NewFunction::to_byte_string_impl(Bytecode::Executable const& executable) const |
3344 | 0 | { |
3345 | 0 | StringBuilder builder; |
3346 | 0 | builder.appendff("NewFunction {}", |
3347 | 0 | format_operand("dst"sv, m_dst, executable)); |
3348 | 0 | if (m_function_node.has_name()) |
3349 | 0 | builder.appendff(" name:{}"sv, m_function_node.name()); |
3350 | 0 | if (m_lhs_name.has_value()) |
3351 | 0 | builder.appendff(" lhs_name:{}"sv, executable.get_identifier(m_lhs_name.value())); |
3352 | 0 | if (m_home_object.has_value()) |
3353 | 0 | builder.appendff(", {}"sv, format_operand("home_object"sv, m_home_object.value(), executable)); |
3354 | 0 | return builder.to_byte_string(); |
3355 | 0 | } |
3356 | | |
3357 | | ByteString NewClass::to_byte_string_impl(Bytecode::Executable const& executable) const |
3358 | 0 | { |
3359 | 0 | StringBuilder builder; |
3360 | 0 | auto name = m_class_expression.name(); |
3361 | 0 | builder.appendff("NewClass {}", |
3362 | 0 | format_operand("dst"sv, m_dst, executable)); |
3363 | 0 | if (m_super_class.has_value()) |
3364 | 0 | builder.appendff(", {}", format_operand("super_class"sv, *m_super_class, executable)); |
3365 | 0 | if (!name.is_empty()) |
3366 | 0 | builder.appendff(", {}", name); |
3367 | 0 | if (m_lhs_name.has_value()) |
3368 | 0 | builder.appendff(", lhs_name:{}"sv, executable.get_identifier(m_lhs_name.value())); |
3369 | 0 | return builder.to_byte_string(); |
3370 | 0 | } |
3371 | | |
3372 | | ByteString Return::to_byte_string_impl(Bytecode::Executable const& executable) const |
3373 | 0 | { |
3374 | 0 | if (m_value.has_value()) |
3375 | 0 | return ByteString::formatted("Return {}", format_operand("value"sv, m_value.value(), executable)); |
3376 | 0 | return "Return"; |
3377 | 0 | } |
3378 | | |
3379 | | ByteString Increment::to_byte_string_impl(Bytecode::Executable const& executable) const |
3380 | 0 | { |
3381 | 0 | return ByteString::formatted("Increment {}", format_operand("dst"sv, m_dst, executable)); |
3382 | 0 | } |
3383 | | |
3384 | | ByteString PostfixIncrement::to_byte_string_impl(Bytecode::Executable const& executable) const |
3385 | 0 | { |
3386 | 0 | return ByteString::formatted("PostfixIncrement {}, {}", |
3387 | 0 | format_operand("dst"sv, m_dst, executable), |
3388 | 0 | format_operand("src"sv, m_src, executable)); |
3389 | 0 | } |
3390 | | |
3391 | | ByteString Decrement::to_byte_string_impl(Bytecode::Executable const& executable) const |
3392 | 0 | { |
3393 | 0 | return ByteString::formatted("Decrement {}", format_operand("dst"sv, m_dst, executable)); |
3394 | 0 | } |
3395 | | |
3396 | | ByteString PostfixDecrement::to_byte_string_impl(Bytecode::Executable const& executable) const |
3397 | 0 | { |
3398 | 0 | return ByteString::formatted("PostfixDecrement {}, {}", |
3399 | 0 | format_operand("dst"sv, m_dst, executable), |
3400 | 0 | format_operand("src"sv, m_src, executable)); |
3401 | 0 | } |
3402 | | |
3403 | | ByteString Throw::to_byte_string_impl(Bytecode::Executable const& executable) const |
3404 | 0 | { |
3405 | 0 | return ByteString::formatted("Throw {}", |
3406 | 0 | format_operand("src"sv, m_src, executable)); |
3407 | 0 | } |
3408 | | |
3409 | | ByteString ThrowIfNotObject::to_byte_string_impl(Bytecode::Executable const& executable) const |
3410 | 0 | { |
3411 | 0 | return ByteString::formatted("ThrowIfNotObject {}", |
3412 | 0 | format_operand("src"sv, m_src, executable)); |
3413 | 0 | } |
3414 | | |
3415 | | ByteString ThrowIfNullish::to_byte_string_impl(Bytecode::Executable const& executable) const |
3416 | 0 | { |
3417 | 0 | return ByteString::formatted("ThrowIfNullish {}", |
3418 | 0 | format_operand("src"sv, m_src, executable)); |
3419 | 0 | } |
3420 | | |
3421 | | ByteString ThrowIfTDZ::to_byte_string_impl(Bytecode::Executable const& executable) const |
3422 | 0 | { |
3423 | 0 | return ByteString::formatted("ThrowIfTDZ {}", |
3424 | 0 | format_operand("src"sv, m_src, executable)); |
3425 | 0 | } |
3426 | | |
3427 | | ByteString EnterUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const |
3428 | 0 | { |
3429 | 0 | return ByteString::formatted("EnterUnwindContext entry:{}", m_entry_point); |
3430 | 0 | } |
3431 | | |
3432 | | ByteString ScheduleJump::to_byte_string_impl(Bytecode::Executable const&) const |
3433 | 0 | { |
3434 | 0 | return ByteString::formatted("ScheduleJump {}", m_target); |
3435 | 0 | } |
3436 | | |
3437 | | ByteString LeaveLexicalEnvironment::to_byte_string_impl(Bytecode::Executable const&) const |
3438 | 0 | { |
3439 | 0 | return "LeaveLexicalEnvironment"sv; |
3440 | 0 | } |
3441 | | |
3442 | | ByteString LeavePrivateEnvironment::to_byte_string_impl(Bytecode::Executable const&) const |
3443 | 0 | { |
3444 | 0 | return "LeavePrivateEnvironment"sv; |
3445 | 0 | } |
3446 | | |
3447 | | ByteString LeaveUnwindContext::to_byte_string_impl(Bytecode::Executable const&) const |
3448 | 0 | { |
3449 | 0 | return "LeaveUnwindContext"; |
3450 | 0 | } |
3451 | | |
3452 | | ByteString ContinuePendingUnwind::to_byte_string_impl(Bytecode::Executable const&) const |
3453 | 0 | { |
3454 | 0 | return ByteString::formatted("ContinuePendingUnwind resume:{}", m_resume_target); |
3455 | 0 | } |
3456 | | |
3457 | | ByteString Yield::to_byte_string_impl(Bytecode::Executable const& executable) const |
3458 | 0 | { |
3459 | 0 | if (m_continuation_label.has_value()) { |
3460 | 0 | return ByteString::formatted("Yield continuation:{}, {}", |
3461 | 0 | m_continuation_label.value(), |
3462 | 0 | format_operand("value"sv, m_value, executable)); |
3463 | 0 | } |
3464 | 0 | return ByteString::formatted("Yield return {}", |
3465 | 0 | format_operand("value"sv, m_value, executable)); |
3466 | 0 | } |
3467 | | |
3468 | | ByteString PrepareYield::to_byte_string_impl(Bytecode::Executable const& executable) const |
3469 | 0 | { |
3470 | 0 | return ByteString::formatted("PrepareYield {}, {}", |
3471 | 0 | format_operand("dst"sv, m_dest, executable), |
3472 | 0 | format_operand("value"sv, m_value, executable)); |
3473 | 0 | } |
3474 | | |
3475 | | ByteString Await::to_byte_string_impl(Bytecode::Executable const& executable) const |
3476 | 0 | { |
3477 | 0 | return ByteString::formatted("Await {}, continuation:{}", |
3478 | 0 | format_operand("argument"sv, m_argument, executable), |
3479 | 0 | m_continuation_label); |
3480 | 0 | } |
3481 | | |
3482 | | ByteString GetByValue::to_byte_string_impl(Bytecode::Executable const& executable) const |
3483 | 0 | { |
3484 | 0 | return ByteString::formatted("GetByValue {}, {}, {}", |
3485 | 0 | format_operand("dst"sv, m_dst, executable), |
3486 | 0 | format_operand("base"sv, m_base, executable), |
3487 | 0 | format_operand("property"sv, m_property, executable)); |
3488 | 0 | } |
3489 | | |
3490 | | ByteString GetByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3491 | 0 | { |
3492 | 0 | return ByteString::formatted("GetByValueWithThis {}, {}, {}", |
3493 | 0 | format_operand("dst"sv, m_dst, executable), |
3494 | 0 | format_operand("base"sv, m_base, executable), |
3495 | 0 | format_operand("property"sv, m_property, executable)); |
3496 | 0 | } |
3497 | | |
3498 | | ByteString PutByValue::to_byte_string_impl(Bytecode::Executable const& executable) const |
3499 | 0 | { |
3500 | 0 | auto kind = property_kind_to_string(m_kind); |
3501 | 0 | return ByteString::formatted("PutByValue {}, {}, {}, kind:{}", |
3502 | 0 | format_operand("base"sv, m_base, executable), |
3503 | 0 | format_operand("property"sv, m_property, executable), |
3504 | 0 | format_operand("src"sv, m_src, executable), |
3505 | 0 | kind); |
3506 | 0 | } |
3507 | | |
3508 | | ByteString PutByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3509 | 0 | { |
3510 | 0 | auto kind = property_kind_to_string(m_kind); |
3511 | 0 | return ByteString::formatted("PutByValueWithThis {}, {}, {}, {}, kind:{}", |
3512 | 0 | format_operand("base"sv, m_base, executable), |
3513 | 0 | format_operand("property"sv, m_property, executable), |
3514 | 0 | format_operand("src"sv, m_src, executable), |
3515 | 0 | format_operand("this"sv, m_this_value, executable), |
3516 | 0 | kind); |
3517 | 0 | } |
3518 | | |
3519 | | ByteString DeleteByValue::to_byte_string_impl(Bytecode::Executable const& executable) const |
3520 | 0 | { |
3521 | 0 | return ByteString::formatted("DeleteByValue {}, {}, {}", |
3522 | 0 | format_operand("dst"sv, dst(), executable), |
3523 | 0 | format_operand("base"sv, m_base, executable), |
3524 | 0 | format_operand("property"sv, m_property, executable)); |
3525 | 0 | } |
3526 | | |
3527 | | ByteString DeleteByValueWithThis::to_byte_string_impl(Bytecode::Executable const& executable) const |
3528 | 0 | { |
3529 | 0 | return ByteString::formatted("DeleteByValueWithThis {}, {}, {}, {}", |
3530 | 0 | format_operand("dst"sv, dst(), executable), |
3531 | 0 | format_operand("base"sv, m_base, executable), |
3532 | 0 | format_operand("property"sv, m_property, executable), |
3533 | 0 | format_operand("this"sv, m_this_value, executable)); |
3534 | 0 | } |
3535 | | |
3536 | | ByteString GetIterator::to_byte_string_impl(Executable const& executable) const |
3537 | 0 | { |
3538 | 0 | auto hint = m_hint == IteratorHint::Sync ? "sync" : "async"; |
3539 | 0 | return ByteString::formatted("GetIterator {}, {}, hint:{}", |
3540 | 0 | format_operand("dst"sv, m_dst, executable), |
3541 | 0 | format_operand("iterable"sv, m_iterable, executable), |
3542 | 0 | hint); |
3543 | 0 | } |
3544 | | |
3545 | | ByteString GetMethod::to_byte_string_impl(Bytecode::Executable const& executable) const |
3546 | 0 | { |
3547 | 0 | return ByteString::formatted("GetMethod {}, {}, {}", |
3548 | 0 | format_operand("dst"sv, m_dst, executable), |
3549 | 0 | format_operand("object"sv, m_object, executable), |
3550 | 0 | executable.identifier_table->get(m_property)); |
3551 | 0 | } |
3552 | | |
3553 | | ByteString GetObjectPropertyIterator::to_byte_string_impl(Bytecode::Executable const& executable) const |
3554 | 0 | { |
3555 | 0 | return ByteString::formatted("GetObjectPropertyIterator {}, {}", |
3556 | 0 | format_operand("dst"sv, dst(), executable), |
3557 | 0 | format_operand("object"sv, object(), executable)); |
3558 | 0 | } |
3559 | | |
3560 | | ByteString IteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const |
3561 | 0 | { |
3562 | 0 | if (!m_completion_value.has_value()) |
3563 | 0 | return ByteString::formatted("IteratorClose {}, completion_type={} completion_value=<empty>", |
3564 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable), |
3565 | 0 | to_underlying(m_completion_type)); |
3566 | | |
3567 | 0 | auto completion_value_string = m_completion_value->to_string_without_side_effects(); |
3568 | 0 | return ByteString::formatted("IteratorClose {}, completion_type={} completion_value={}", |
3569 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable), |
3570 | 0 | to_underlying(m_completion_type), completion_value_string); |
3571 | 0 | } |
3572 | | |
3573 | | ByteString AsyncIteratorClose::to_byte_string_impl(Bytecode::Executable const& executable) const |
3574 | 0 | { |
3575 | 0 | if (!m_completion_value.has_value()) { |
3576 | 0 | return ByteString::formatted("AsyncIteratorClose {}, completion_type:{} completion_value:<empty>", |
3577 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable), |
3578 | 0 | to_underlying(m_completion_type)); |
3579 | 0 | } |
3580 | | |
3581 | 0 | return ByteString::formatted("AsyncIteratorClose {}, completion_type:{}, completion_value:{}", |
3582 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable), |
3583 | 0 | to_underlying(m_completion_type), m_completion_value); |
3584 | 0 | } |
3585 | | |
3586 | | ByteString IteratorNext::to_byte_string_impl(Executable const& executable) const |
3587 | 0 | { |
3588 | 0 | return ByteString::formatted("IteratorNext {}, {}", |
3589 | 0 | format_operand("dst"sv, m_dst, executable), |
3590 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable)); |
3591 | 0 | } |
3592 | | |
3593 | | ByteString ResolveThisBinding::to_byte_string_impl(Bytecode::Executable const&) const |
3594 | 0 | { |
3595 | 0 | return "ResolveThisBinding"sv; |
3596 | 0 | } |
3597 | | |
3598 | | ByteString ResolveSuperBase::to_byte_string_impl(Bytecode::Executable const& executable) const |
3599 | 0 | { |
3600 | 0 | return ByteString::formatted("ResolveSuperBase {}", |
3601 | 0 | format_operand("dst"sv, m_dst, executable)); |
3602 | 0 | } |
3603 | | |
3604 | | ByteString GetNewTarget::to_byte_string_impl(Bytecode::Executable const& executable) const |
3605 | 0 | { |
3606 | 0 | return ByteString::formatted("GetNewTarget {}", format_operand("dst"sv, m_dst, executable)); |
3607 | 0 | } |
3608 | | |
3609 | | ByteString GetImportMeta::to_byte_string_impl(Bytecode::Executable const& executable) const |
3610 | 0 | { |
3611 | 0 | return ByteString::formatted("GetImportMeta {}", format_operand("dst"sv, m_dst, executable)); |
3612 | 0 | } |
3613 | | |
3614 | | ByteString TypeofBinding::to_byte_string_impl(Bytecode::Executable const& executable) const |
3615 | 0 | { |
3616 | 0 | return ByteString::formatted("TypeofBinding {}, {}", |
3617 | 0 | format_operand("dst"sv, m_dst, executable), |
3618 | 0 | executable.identifier_table->get(m_identifier)); |
3619 | 0 | } |
3620 | | |
3621 | | ByteString BlockDeclarationInstantiation::to_byte_string_impl(Bytecode::Executable const&) const |
3622 | 0 | { |
3623 | 0 | return "BlockDeclarationInstantiation"sv; |
3624 | 0 | } |
3625 | | |
3626 | | ByteString ImportCall::to_byte_string_impl(Bytecode::Executable const& executable) const |
3627 | 0 | { |
3628 | 0 | return ByteString::formatted("ImportCall {}, {}, {}", |
3629 | 0 | format_operand("dst"sv, m_dst, executable), |
3630 | 0 | format_operand("specifier"sv, m_specifier, executable), |
3631 | 0 | format_operand("options"sv, m_options, executable)); |
3632 | 0 | } |
3633 | | |
3634 | | ByteString Catch::to_byte_string_impl(Bytecode::Executable const& executable) const |
3635 | 0 | { |
3636 | 0 | return ByteString::formatted("Catch {}", |
3637 | 0 | format_operand("dst"sv, m_dst, executable)); |
3638 | 0 | } |
3639 | | |
3640 | | ByteString LeaveFinally::to_byte_string_impl(Bytecode::Executable const&) const |
3641 | 0 | { |
3642 | 0 | return ByteString::formatted("LeaveFinally"); |
3643 | 0 | } |
3644 | | |
3645 | | ByteString RestoreScheduledJump::to_byte_string_impl(Bytecode::Executable const&) const |
3646 | 0 | { |
3647 | 0 | return ByteString::formatted("RestoreScheduledJump"); |
3648 | 0 | } |
3649 | | |
3650 | | ByteString GetObjectFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const |
3651 | 0 | { |
3652 | 0 | return ByteString::formatted("GetObjectFromIteratorRecord {}, {}", |
3653 | 0 | format_operand("object"sv, m_object, executable), |
3654 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable)); |
3655 | 0 | } |
3656 | | |
3657 | | ByteString GetNextMethodFromIteratorRecord::to_byte_string_impl(Bytecode::Executable const& executable) const |
3658 | 0 | { |
3659 | 0 | return ByteString::formatted("GetNextMethodFromIteratorRecord {}, {}", |
3660 | 0 | format_operand("next_method"sv, m_next_method, executable), |
3661 | 0 | format_operand("iterator_record"sv, m_iterator_record, executable)); |
3662 | 0 | } |
3663 | | |
3664 | | ByteString End::to_byte_string_impl(Bytecode::Executable const& executable) const |
3665 | 0 | { |
3666 | 0 | return ByteString::formatted("End {}", format_operand("value"sv, m_value, executable)); |
3667 | 0 | } |
3668 | | |
3669 | | ByteString Dump::to_byte_string_impl(Bytecode::Executable const& executable) const |
3670 | 0 | { |
3671 | 0 | return ByteString::formatted("Dump '{}', {}", m_text, |
3672 | 0 | format_operand("value"sv, m_value, executable)); |
3673 | 0 | } |
3674 | | |
3675 | | } |