ladybird/Libraries/LibJS/Bytecode/Interpreter.cpp
Andreas Kling 5bc002d71c LibJS: Move property and call handlers into AsmInt
Move property access, iterator, object property iterator, import, class
and argument-array call opcodes out of the generic fallback path. Keep
the semantic work in C++ slow paths and dispatch to them from AsmInt.
2026-06-14 20:27:59 +02:00

1706 lines
73 KiB
C++

/*
* Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <AK/HashTable.h>
#include <AK/NumericLimits.h>
#include <AK/TemporaryChange.h>
#include <LibGC/ConservativeHashTable.h>
#include <LibGC/RootHashMap.h>
#include <LibGC/RootHashTable.h>
#include <LibJS/Bytecode/AsmInterpreter/AsmInterpreter.h>
#include <LibJS/Bytecode/BasicBlock.h>
#include <LibJS/Bytecode/Builtins.h>
#include <LibJS/Bytecode/Debug.h>
#include <LibJS/Bytecode/FormatOperand.h>
#include <LibJS/Bytecode/Instruction.h>
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Op.h>
#include <LibJS/Bytecode/PropertyAccess.h>
#include <LibJS/Export.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/AsyncFromSyncIterator.h>
#include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
#include <LibJS/Runtime/AsyncGenerator.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/ClassConstruction.h>
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/MathObject.h>
#include <LibJS/Runtime/ModuleEnvironment.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibJS/SourceTextModule.h>
#include <math.h>
namespace JS {
using namespace Bytecode;
bool Bytecode::g_dump_bytecode = false;
ALWAYS_INLINE static ThrowCompletionOr<bool> loosely_inequals(VM& vm, Value src1, Value src2)
{
if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return src1.encoded() != src2.encoded();
}
return !TRY(is_loosely_equal(vm, src1, src2));
}
ALWAYS_INLINE static ThrowCompletionOr<bool> loosely_equals(VM& vm, Value src1, Value src2)
{
if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return src1.encoded() == src2.encoded();
}
return TRY(is_loosely_equal(vm, src1, src2));
}
ALWAYS_INLINE static ThrowCompletionOr<bool> strict_inequals(VM&, Value src1, Value src2)
{
if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return src1.encoded() != src2.encoded();
}
return !is_strictly_equal(src1, src2);
}
ALWAYS_INLINE static ThrowCompletionOr<bool> strict_equals(VM&, Value src1, Value src2)
{
if (src1.tag() == src2.tag()) {
if (src1.is_int32() || src1.is_object() || src1.is_boolean() || src1.is_nullish())
return src1.encoded() == src2.encoded();
}
return is_strictly_equal(src1, src2);
}
ALWAYS_INLINE Value VM::do_yield(Value value, Optional<Label> continuation, bool value_is_iterator_result)
{
auto& context = running_execution_context();
if (continuation.has_value())
context.yield_continuation = continuation->address();
else
context.yield_continuation = ExecutionContext::no_yield_continuation;
context.yield_is_await = false;
context.yield_value_is_iterator_result = value_is_iterator_result;
return value;
}
// 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
ThrowCompletionOr<Value> VM::run(Script& script_record, GC::Ptr<Environment> lexical_environment_override)
{
auto& vm = this->vm();
// 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
auto& global_environment = script_record.realm().global_environment();
// NOTE: Spec steps are rearranged in order to compute number of registers+constants+locals before construction of the execution context.
// 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
auto instantiation_result = script_record.global_declaration_instantiation(vm, global_environment);
Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion(js_undefined());
// 11. Let script be scriptRecord.[[ECMAScriptCode]].
GC::Ptr<Executable> executable = script_record.cached_executable();
if (executable && g_dump_bytecode)
executable->dump();
u32 registers_and_locals_count = 0;
ReadonlySpan<Value> constants;
if (executable) {
registers_and_locals_count = executable->registers_and_locals_count;
constants = executable->constants;
}
// 2. Let scriptContext be a new ECMAScript code execution context.
auto& stack = vm.interpreter_stack();
auto* stack_mark = stack.top();
auto* script_context = stack.allocate(registers_and_locals_count, constants, 0);
if (!script_context) [[unlikely]]
return vm.throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
ScopeGuard deallocate_guard = [&stack, stack_mark] { stack.deallocate(stack_mark); };
// 3. Set the Function of scriptContext to null.
// NOTE: This was done during execution context construction.
// 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
script_context->realm = &script_record.realm();
// 5. Set the ScriptOrModule of scriptContext to scriptRecord.
script_context->script_or_module = GC::Ref<Script>(script_record);
// 6. Set the VariableEnvironment of scriptContext to globalEnv.
script_context->variable_environment = &global_environment;
// 7. Set the LexicalEnvironment of scriptContext to globalEnv.
script_context->lexical_environment = &global_environment;
// Non-standard: Override the lexical environment if requested.
if (lexical_environment_override)
script_context->lexical_environment = lexical_environment_override;
// 8. Set the PrivateEnvironment of scriptContext to null.
// 9. Suspend the currently running execution context.
// 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
TRY(vm.push_execution_context(*script_context, {}));
// 13. If result.[[Type]] is normal, then
if (executable && result.type() == Completion::Type::Normal) {
// a. Set result to Completion(Evaluation of script).
result = run_executable(*script_context, *executable, 0, {});
// b. If result is a normal completion and result.[[Value]] is empty, then
if (result.type() == Completion::Type::Normal && result.value().is_special_empty_value()) {
// i. Set result to NormalCompletion(undefined).
result = normal_completion(js_undefined());
}
}
// 14. Suspend scriptContext and remove it from the execution context stack.
vm.pop_execution_context();
// 15. Assert: The execution context stack is not empty.
VERIFY(!vm.execution_context_stack().is_empty());
// FIXME: 16. Resume the context that is now on the top of the execution context stack as the running execution context.
vm.finish_execution_generation();
// 17. Return ? result.
if (result.is_abrupt()) {
VERIFY(result.type() == Completion::Type::Throw);
return result.release_error();
}
return result.value();
}
ThrowCompletionOr<Value> VM::run(SourceTextModule& module)
{
// FIXME: This is not a entry point as defined in the spec, but is convenient.
// To avoid work we use link_and_eval_module however that can already be
// dangerous if the vm loaded other modules.
auto& vm = this->vm();
TRY(vm.link_and_eval_module(module));
vm.run_queued_promise_jobs();
vm.run_queued_finalization_registry_cleanup_jobs();
return js_undefined();
}
VM::HandleExceptionResponse VM::handle_exception(u32 program_counter, Value exception)
{
for (;;) {
auto handlers = current_executable().exception_handlers_for_offset(program_counter);
if (handlers.has_value()) {
reg(Register::exception()) = exception;
m_running_execution_context->program_counter = handlers->handler_offset;
return HandleExceptionResponse::ContinueInThisExecutable;
}
// If we're in an inline frame, unwind to the caller and try its handlers.
if (m_running_execution_context->caller_frame) {
auto* callee_frame = m_running_execution_context;
auto* caller_frame = callee_frame->caller_frame;
auto caller_pc = callee_frame->caller_return_pc;
vm().interpreter_stack().deallocate(callee_frame);
m_running_execution_context = caller_frame;
// NB: caller_pc is the return address (one past the Call instruction).
// For handler lookup we need a PC inside the Call instruction,
// since the exception occurred during that call, not after it.
// Exception handler ranges use an exclusive end offset, so using
// caller_pc directly would miss a handler ending right at that address.
program_counter = caller_pc - 1;
continue;
}
reg(Register::exception()) = exception;
return HandleExceptionResponse::ExitFromExecutable;
}
}
ExecutionContext* VM::push_inline_frame(
ECMAScriptFunctionObject& callee_function,
Executable& callee_executable,
ReadonlySpan<Operand> arguments,
u32 return_pc,
u32 dst_raw,
Value this_value,
Object* new_target,
bool is_construct)
{
auto& stack = vm().interpreter_stack();
u32 insn_argument_count = arguments.size();
size_t registers_and_locals_count = callee_executable.registers_and_locals_count;
size_t argument_count = max(insn_argument_count, static_cast<u32>(callee_function.formal_parameter_count()));
auto* callee_context = stack.allocate(registers_and_locals_count, callee_executable.constants, argument_count);
if (!callee_context) [[unlikely]]
return nullptr;
// Copy arguments from caller's registers into callee's argument slots.
auto* callee_argument_values = callee_context->arguments_data();
for (u32 i = 0; i < insn_argument_count; ++i)
callee_argument_values[i] = get(arguments[i]);
for (size_t i = insn_argument_count; i < argument_count; ++i)
callee_argument_values[i] = js_undefined();
callee_context->passed_argument_count = insn_argument_count;
// Set up caller linkage so Return can restore the caller frame.
callee_context->caller_frame = m_running_execution_context;
callee_context->caller_dst_raw = dst_raw;
callee_context->caller_return_pc = return_pc;
callee_context->caller_is_construct = is_construct;
// Inlined PrepareForOrdinaryCall (avoids function call overhead on hot path).
callee_context->function = &callee_function;
callee_context->realm = callee_function.realm();
callee_context->script_or_module = callee_function.m_script_or_module;
if (callee_function.function_environment_needed()) {
auto local_environment = new_function_environment(callee_function, new_target);
auto function_environment_bindings_count = callee_function.shared_data().m_function_environment_bindings_count;
local_environment->set_environment_shape_cache(callee_function.shared_data().m_function_environment_shape, function_environment_bindings_count);
local_environment->ensure_capacity(function_environment_bindings_count);
callee_context->lexical_environment = local_environment;
callee_context->variable_environment = local_environment;
} else {
callee_context->lexical_environment = callee_function.environment();
callee_context->variable_environment = callee_function.environment();
}
callee_context->private_environment = callee_function.m_private_environment;
// Inline JS-to-JS frames stay out of the VM execution context stack and
// are tracked through caller_frame instead.
m_running_execution_context = callee_context;
// Bind this if the function uses it.
if (callee_function.uses_this())
callee_function.ordinary_call_bind_this(vm(), *callee_context, this_value);
// Set up execution context fields that run_executable normally does.
// NB: We must use the callee's realm (not the caller's) for global_object
// and global_declarative_environment, since the caller's realm may differ
// in cross-realm calls (e.g. iframe <-> parent).
callee_context->executable = callee_executable;
// Set this value register.
auto* values = callee_context->registers_and_constants_and_locals_and_arguments();
values[Register::this_value().index()] = callee_context->this_value.value_or(js_special_empty_value());
return callee_context;
}
NEVER_INLINE bool VM::try_inline_call(Instruction const& insn, u32 current_pc)
{
auto& instruction = static_cast<Op::Call const&>(insn);
auto callee = get(instruction.callee());
if (!callee.is_object())
return false;
auto& callee_object = callee.as_object();
if (!is<ECMAScriptFunctionObject>(callee_object))
return false;
auto& callee_function = static_cast<ECMAScriptFunctionObject&>(callee_object);
if (!callee_function.can_inline_call())
return false;
auto& callee_executable = callee_function.inline_call_executable();
u32 return_pc = current_pc + instruction.length();
auto* callee_context = push_inline_frame(
callee_function, callee_executable,
instruction.arguments(), return_pc, instruction.dst().raw(),
get(instruction.this_value()), nullptr, false);
if (!callee_context) [[unlikely]]
return false;
return true;
}
NEVER_INLINE bool VM::try_inline_call_construct(Instruction const& insn, u32 current_pc)
{
auto& instruction = static_cast<Op::CallConstruct const&>(insn);
auto callee = get(instruction.callee());
if (!callee.is_object())
return false;
auto& callee_object = callee.as_object();
if (!is<ECMAScriptFunctionObject>(callee_object))
return false;
auto& callee_function = static_cast<ECMAScriptFunctionObject&>(callee_object);
if (!callee_function.has_constructor()
|| callee_function.constructor_kind() != ConstructorKind::Base
|| !callee_function.bytecode_executable())
return false;
// OrdinaryCreateFromConstructor: create the this object.
auto prototype_or_error = get_prototype_from_constructor(vm(), callee_function, &Intrinsics::object_prototype);
if (prototype_or_error.is_error()) [[unlikely]]
return false;
auto this_argument = Object::create(realm(), prototype_or_error.release_value());
u32 return_pc = current_pc + instruction.length();
auto* callee_context = push_inline_frame(
callee_function, *callee_function.bytecode_executable(),
instruction.arguments(), return_pc, instruction.dst().raw(),
this_argument, &callee_function, true);
if (!callee_context) [[unlikely]]
return false;
// Ensure this_value is set for construct return semantics.
if (!callee_context->this_value.has_value())
callee_context->this_value = Value(this_argument);
// InitializeInstanceElements (can throw).
auto init_result = this_argument->initialize_instance_elements(callee_function);
if (init_result.is_throw_completion()) [[unlikely]] {
m_running_execution_context = callee_context->caller_frame;
vm().interpreter_stack().deallocate(callee_context);
return false;
}
return true;
}
NEVER_INLINE void VM::pop_inline_frame(Value return_value)
{
auto* callee_frame = m_running_execution_context;
auto* caller_frame = callee_frame->caller_frame;
auto caller_dst_raw = callee_frame->caller_dst_raw;
auto caller_pc = callee_frame->caller_return_pc;
// For base constructor calls, apply construct return semantics.
if (callee_frame->caller_is_construct && !return_value.is_object())
return_value = callee_frame->this_value.value();
vm().interpreter_stack().deallocate(callee_frame);
m_running_execution_context = caller_frame;
caller_frame->program_counter = caller_pc;
caller_frame->registers_and_constants_and_locals_and_arguments()[caller_dst_raw] = return_value;
vm().finish_execution_generation();
}
NEVER_INLINE void VM::unwind_inline_frame_for_exception()
{
auto* callee_frame = m_running_execution_context;
VERIFY(callee_frame);
VERIFY(callee_frame->caller_frame);
auto* caller_frame = callee_frame->caller_frame;
vm().interpreter_stack().deallocate(callee_frame);
m_running_execution_context = caller_frame;
}
void VM::run_bytecode(size_t entry_point)
{
if (vm().interpreter_stack().is_exhausted() || vm().did_reach_stack_space_limit()) [[unlikely]] {
reg(Register::exception()) = vm().throw_completion<InternalError>(ErrorType::CallStackSizeExceeded).value();
return;
}
AsmInterpreter::run(*this, entry_point);
}
Utf16FlyString const& VM::get_identifier(IdentifierTableIndex index) const
{
return m_running_execution_context->executable->get_identifier(index);
}
PropertyKey const& VM::get_property_key(PropertyKeyTableIndex index) const
{
return m_running_execution_context->executable->get_property_key(index);
}
DeclarativeEnvironment& VM::global_declarative_environment()
{
return realm().global_declarative_environment();
}
ThrowCompletionOr<Value> VM::run_executable(ExecutionContext& context, Executable& executable, u32 entry_point)
{
dbgln_if(JS_BYTECODE_DEBUG, "VM will run bytecode unit {}", &executable);
auto const is_outermost_bytecode_execution = m_run_executable_depth == 0;
TemporaryChange restore_run_executable_depth { m_run_executable_depth, m_run_executable_depth + 1 };
// NOTE: This is how we "push" a new execution context onto the VM's
// execution context stack.
TemporaryChange restore_running_execution_context { m_running_execution_context, &context };
context.executable = executable;
VERIFY(executable.registers_and_locals_count + executable.constants.size() == executable.registers_and_locals_and_constants_count);
VERIFY(executable.registers_and_locals_and_constants_count <= context.registers_and_constants_and_locals_and_arguments_span().size());
// NOTE: We only copy the `this` value from ExecutionContext if it's not already set.
// If we are re-entering an async/generator context, the `this` value
// may have already been cached by a ResolveThisBinding instruction,
// and subsequent instructions expect this value to be set.
if (reg(Register::this_value()).is_special_empty_value())
reg(Register::this_value()) = context.this_value.value_or(js_special_empty_value());
run_bytecode(entry_point);
dbgln_if(JS_BYTECODE_DEBUG, "VM did run bytecode unit {}", context.executable);
if constexpr (JS_BYTECODE_DEBUG) {
auto* values = context.registers_and_constants_and_locals_and_arguments();
for (size_t i = 0; i < executable.number_of_registers; ++i) {
String value_string;
if (values[i].is_special_empty_value())
value_string = "(empty)"_string;
else
value_string = values[i].to_string_without_side_effects();
dbgln("[{:3}] {}", i, value_string);
}
}
if (is_outermost_bytecode_execution && !vm().is_executing_module())
vm().run_queued_promise_jobs();
vm().finish_execution_generation();
auto exception = reg(Register::exception());
if (!exception.is_special_empty_value()) [[unlikely]]
return JS::throw_completion(exception);
return reg(Register::return_value());
}
void VM::catch_exception(Operand dst)
{
set(dst, reg(Register::exception()));
reg(Register::exception()) = js_special_empty_value();
}
// NOTE: This function assumes that the index is valid within the TypedArray,
// and that the TypedArray is not detached.
template<typename T>
inline Value fast_typed_array_get_element(TypedArrayBase& typed_array, u32 index)
{
Checked<u32> offset_into_array_buffer = index;
offset_into_array_buffer *= sizeof(T);
offset_into_array_buffer += typed_array.byte_offset();
if (offset_into_array_buffer.has_overflow()) [[unlikely]] {
return js_undefined();
}
auto const& array_buffer = *typed_array.viewed_array_buffer();
auto const* slot = reinterpret_cast<T const*>(array_buffer.span().offset_pointer(offset_into_array_buffer.value()));
return Value { *slot };
}
// NOTE: This function assumes that the index is valid within the TypedArray,
// and that the TypedArray is not detached.
template<typename T>
inline void fast_typed_array_set_element(TypedArrayBase& typed_array, u32 index, T value)
{
Checked<u32> offset_into_array_buffer = index;
offset_into_array_buffer *= sizeof(T);
offset_into_array_buffer += typed_array.byte_offset();
if (offset_into_array_buffer.has_overflow()) [[unlikely]] {
return;
}
auto& array_buffer = *typed_array.viewed_array_buffer();
auto* slot = reinterpret_cast<T*>(array_buffer.span().offset_pointer(offset_into_array_buffer.value()));
*slot = value;
}
static COLD Completion throw_null_or_undefined_property_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, IdentifierTableIndex property_identifier, Executable const& executable)
{
VERIFY(base_value.is_nullish());
if (base_identifier.has_value())
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithPropertyAndName, executable.get_identifier(property_identifier), base_value, executable.get_identifier(base_identifier.value()));
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithProperty, executable.get_identifier(property_identifier), base_value);
}
static COLD Completion throw_null_or_undefined_property_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, Value property, Executable const& executable)
{
VERIFY(base_value.is_nullish());
if (base_identifier.has_value())
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithPropertyAndName, property, base_value, executable.get_identifier(base_identifier.value()));
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefinedWithProperty, property, base_value);
}
ALWAYS_INLINE ThrowCompletionOr<GC::Ref<Object>> base_object_for_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, IdentifierTableIndex property_identifier, Executable const& executable)
{
if (auto base_object = base_object_for_get_impl(vm, base_value)) [[likely]]
return GC::Ref { *base_object };
// NOTE: At this point this is guaranteed to throw (null or undefined).
return throw_null_or_undefined_property_get(vm, base_value, base_identifier, property_identifier, executable);
}
ALWAYS_INLINE ThrowCompletionOr<GC::Ref<Object>> base_object_for_get(VM& vm, Value base_value, Optional<IdentifierTableIndex> base_identifier, Value property, Executable const& executable)
{
if (auto base_object = base_object_for_get_impl(vm, base_value)) [[likely]]
return GC::Ref { *base_object };
// NOTE: At this point this is guaranteed to throw (null or undefined).
return throw_null_or_undefined_property_get(vm, base_value, base_identifier, property, executable);
}
inline ThrowCompletionOr<Value> get_by_value(VM& vm, Optional<IdentifierTableIndex> base_identifier, Value base_value, Value property_key_value, Executable const& executable)
{
// OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects.
if (base_value.is_object() && property_key_value.is_non_negative_int32()) {
auto& object = base_value.as_object();
auto index = static_cast<u32>(property_key_value.as_i32());
// For "non-typed arrays":
if (!object.may_interfere_with_indexed_property_access()
&& object.indexed_storage_kind() != IndexedStorageKind::None) {
auto maybe_value = object.indexed_get(index);
if (maybe_value.has_value()) {
auto value = maybe_value->value;
if (!value.is_accessor())
return value;
}
}
// For typed arrays:
if (object.is_typed_array()) {
auto& typed_array = static_cast<TypedArrayBase&>(object);
auto canonical_index = CanonicalIndex { CanonicalIndex::Type::Index, index };
if (is_valid_integer_index(typed_array, canonical_index)) {
switch (typed_array.kind()) {
case TypedArrayBase::Kind::Uint8Array:
return fast_typed_array_get_element<u8>(typed_array, index);
case TypedArrayBase::Kind::Uint16Array:
return fast_typed_array_get_element<u16>(typed_array, index);
case TypedArrayBase::Kind::Uint32Array:
return fast_typed_array_get_element<u32>(typed_array, index);
case TypedArrayBase::Kind::Int8Array:
return fast_typed_array_get_element<i8>(typed_array, index);
case TypedArrayBase::Kind::Int16Array:
return fast_typed_array_get_element<i16>(typed_array, index);
case TypedArrayBase::Kind::Int32Array:
return fast_typed_array_get_element<i32>(typed_array, index);
case TypedArrayBase::Kind::Uint8ClampedArray:
return fast_typed_array_get_element<u8>(typed_array, index);
case TypedArrayBase::Kind::Float16Array:
return fast_typed_array_get_element<f16>(typed_array, index);
case TypedArrayBase::Kind::Float32Array:
return fast_typed_array_get_element<float>(typed_array, index);
case TypedArrayBase::Kind::Float64Array:
return fast_typed_array_get_element<double>(typed_array, index);
default:
// FIXME: Support more TypedArray kinds.
break;
}
}
switch (typed_array.kind()) {
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
case TypedArrayBase::Kind::ClassName: \
return typed_array_get_element<Type>(typed_array, canonical_index);
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
}
}
}
auto object = TRY(base_object_for_get(vm, base_value, base_identifier, property_key_value, executable));
auto property_key = TRY(property_key_value.to_property_key(vm));
if (base_value.is_string()) {
auto string_value = TRY(base_value.as_string().get(vm, property_key));
if (string_value.has_value())
return *string_value;
}
return TRY(object->internal_get(property_key, base_value));
}
inline ThrowCompletionOr<Value> get_global(VM& vm, IdentifierTableIndex identifier_index, Strict strict, GlobalVariableCache& cache)
{
auto& binding_object = vm.global_object();
auto& declarative_record = vm.global_declarative_environment();
auto& shape = binding_object.shape();
if (cache.environment_serial_number == declarative_record.environment_serial_number()) {
// OPTIMIZATION: For global var bindings, if the shape of the global object hasn't changed,
// we can use the cached property offset.
auto* entry = cache.first_entry();
if (entry && &shape == entry->shape && (!shape.is_dictionary() || shape.dictionary_generation() == entry->shape_dictionary_generation)) {
auto value = binding_object.get_direct(entry->property_offset);
return TRY(get_cached_property_value(vm, value, &binding_object));
}
// OPTIMIZATION: For global lexical bindings, if the global declarative environment hasn't changed,
// we can use the cached environment binding index.
if (cache.has_environment_binding_index) {
if (cache.in_module_environment) {
auto module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>();
return (*module)->environment()->get_binding_value_direct(vm, cache.environment_binding_index);
}
return declarative_record.get_binding_value_direct(vm, cache.environment_binding_index);
}
}
cache.environment_serial_number = declarative_record.environment_serial_number();
auto& identifier = vm.get_identifier(identifier_index);
if (auto* module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>()) {
// NOTE: GetGlobal is used to access variables stored in the module environment and global environment.
// The module environment is checked first since it precedes the global environment in the environment chain.
auto& module_environment = *(*module)->environment();
Optional<size_t> index;
if (TRY(module_environment.has_binding(identifier, &index))) {
if (index.has_value()) {
cache.environment_binding_index = static_cast<u32>(index.value());
cache.has_environment_binding_index = true;
cache.in_module_environment = true;
return TRY(module_environment.get_binding_value_direct(vm, index.value()));
}
return TRY(module_environment.get_binding_value(vm, identifier, true));
}
}
Optional<size_t> offset;
if (TRY(declarative_record.has_binding(identifier, &offset))) {
cache.environment_binding_index = static_cast<u32>(offset.value());
cache.has_environment_binding_index = true;
cache.in_module_environment = false;
return TRY(declarative_record.get_binding_value(vm, identifier, strict == Strict::Yes));
}
if (TRY(binding_object.has_property(identifier))) [[likely]] {
CacheableGetPropertyMetadata cacheable_metadata;
auto value = TRY(binding_object.internal_get(identifier, &binding_object, &cacheable_metadata));
if (cacheable_metadata.type == CacheableGetPropertyMetadata::Type::GetOwnProperty) {
cache.update(PropertyLookupCache::Entry::Type::GetOwnProperty, [&](auto& entry) {
entry.shape = shape;
entry.property_offset = cacheable_metadata.property_offset.value();
if (shape.is_dictionary()) {
entry.shape_dictionary_generation = shape.dictionary_generation();
}
});
}
return value;
}
return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, identifier);
}
static COLD Completion throw_type_error_for_callee(VM& vm, Value callee, StringView callee_type, Optional<StringTableIndex> const expression_string)
{
if (expression_string.has_value())
return vm.throw_completion<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee, callee_type, vm.current_executable().get_string(*expression_string));
return vm.throw_completion<TypeError>(ErrorType::IsNotA, callee, callee_type);
}
inline ThrowCompletionOr<void> throw_if_needed_for_call(VM& vm, Value callee, Op::CallType call_type, Optional<StringTableIndex> const expression_string)
{
if ((call_type == Op::CallType::Call || call_type == Op::CallType::DirectEval)
&& !callee.is_function()) [[unlikely]]
return throw_type_error_for_callee(vm, callee, "function"sv, expression_string);
if (call_type == Op::CallType::Construct && !callee.is_constructor()) [[unlikely]]
return throw_type_error_for_callee(vm, callee, "constructor"sv, expression_string);
return {};
}
inline ThrowCompletionOr<void> put_by_value(VM& vm, Value base, Optional<Utf16FlyString const&> const base_identifier, Value property_key_value, Value value, PutKind kind, Strict strict)
{
// OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects.
if (kind == PutKind::Normal
&& base.is_object() && property_key_value.is_non_negative_int32()) {
auto& object = base.as_object();
auto index = static_cast<u32>(property_key_value.as_i32());
// For "non-typed arrays":
if (!object.may_interfere_with_indexed_property_access()
&& object.indexed_storage_kind() != IndexedStorageKind::None
&& object.indexed_storage_kind() != IndexedStorageKind::Dictionary) {
auto maybe_value = object.indexed_get(index);
if (maybe_value.has_value()) {
auto existing_value = maybe_value->value;
if (!existing_value.is_accessor()) {
object.indexed_put(index, value);
return {};
}
}
}
// For typed arrays:
if (object.is_typed_array()) {
auto& typed_array = static_cast<TypedArrayBase&>(object);
auto canonical_index = CanonicalIndex { CanonicalIndex::Type::Index, index };
if (is_valid_integer_index(typed_array, canonical_index)) {
if (value.is_int32()) {
switch (typed_array.kind()) {
case TypedArrayBase::Kind::Uint8Array:
fast_typed_array_set_element<u8>(typed_array, index, static_cast<u8>(value.as_i32()));
return {};
case TypedArrayBase::Kind::Uint16Array:
fast_typed_array_set_element<u16>(typed_array, index, static_cast<u16>(value.as_i32()));
return {};
case TypedArrayBase::Kind::Uint32Array:
fast_typed_array_set_element<u32>(typed_array, index, static_cast<u32>(value.as_i32()));
return {};
case TypedArrayBase::Kind::Int8Array:
fast_typed_array_set_element<i8>(typed_array, index, static_cast<i8>(value.as_i32()));
return {};
case TypedArrayBase::Kind::Int16Array:
fast_typed_array_set_element<i16>(typed_array, index, static_cast<i16>(value.as_i32()));
return {};
case TypedArrayBase::Kind::Int32Array:
fast_typed_array_set_element<i32>(typed_array, index, value.as_i32());
return {};
case TypedArrayBase::Kind::Uint8ClampedArray:
fast_typed_array_set_element<u8>(typed_array, index, clamp(value.as_i32(), 0, 255));
return {};
default:
break;
}
} else if (value.is_double()) {
switch (typed_array.kind()) {
case TypedArrayBase::Kind::Float16Array:
fast_typed_array_set_element<f16>(typed_array, index, static_cast<f16>(value.as_double()));
return {};
case TypedArrayBase::Kind::Float32Array:
fast_typed_array_set_element<float>(typed_array, index, static_cast<float>(value.as_double()));
return {};
case TypedArrayBase::Kind::Float64Array:
fast_typed_array_set_element<double>(typed_array, index, value.as_double());
return {};
case TypedArrayBase::Kind::Int8Array:
fast_typed_array_set_element<i8>(typed_array, index, MUST(value.to_i8(vm)));
return {};
case TypedArrayBase::Kind::Int16Array:
fast_typed_array_set_element<i16>(typed_array, index, MUST(value.to_i16(vm)));
return {};
case TypedArrayBase::Kind::Int32Array:
fast_typed_array_set_element<i32>(typed_array, index, MUST(value.to_i32(vm)));
return {};
case TypedArrayBase::Kind::Uint8Array:
fast_typed_array_set_element<u8>(typed_array, index, MUST(value.to_u8(vm)));
return {};
case TypedArrayBase::Kind::Uint16Array:
fast_typed_array_set_element<u16>(typed_array, index, MUST(value.to_u16(vm)));
return {};
case TypedArrayBase::Kind::Uint32Array:
fast_typed_array_set_element<u32>(typed_array, index, MUST(value.to_u32(vm)));
return {};
default:
break;
}
}
// FIXME: Support more TypedArray kinds.
}
if (typed_array.kind() == TypedArrayBase::Kind::Uint32Array && value.is_integral_number()) {
auto integer = value.as_double();
if (AK::is_within_range<u32>(integer) && is_valid_integer_index(typed_array, canonical_index)) {
fast_typed_array_set_element<u32>(typed_array, index, static_cast<u32>(integer));
return {};
}
}
switch (typed_array.kind()) {
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
case TypedArrayBase::Kind::ClassName: \
return typed_array_set_element<Type>(typed_array, canonical_index, value);
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
}
return {};
}
}
auto property_key = TRY(property_key_value.to_property_key(vm));
TRY(put_by_property_key(vm, base, base, value, base_identifier, property_key, kind, strict));
return {};
}
struct CalleeAndThis {
Value callee;
Value this_value;
};
inline ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(VM& vm, EnvironmentCoordinate const& cache)
{
VERIFY(cache.is_valid());
Value callee = js_undefined();
auto const* environment = vm.running_execution_context().lexical_environment.ptr();
for (size_t i = 0; i < cache.hops; ++i)
environment = environment->outer_environment();
callee = TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, cache.index));
auto this_value = js_undefined();
if (auto base_object = environment->with_base_object()) [[unlikely]]
this_value = base_object;
return CalleeAndThis {
.callee = callee,
.this_value = this_value,
};
}
template<typename EnvironmentPointer>
static EnvironmentPointer get_cacheable_environment(EnvironmentPointer environment, EnvironmentCoordinate const& cache)
{
VERIFY(cache.is_valid());
for (size_t i = 0; i < cache.hops; ++i) {
if (!environment->is_declarative_environment() || environment->is_permanently_screwed_by_eval()) [[unlikely]]
return nullptr;
environment = environment->outer_environment();
if (!environment) [[unlikely]]
return nullptr;
}
if (environment->is_declarative_environment() && !environment->is_permanently_screwed_by_eval()) [[likely]]
return environment;
return nullptr;
}
template<typename EnvironmentPointer>
static EnvironmentPointer get_cached_environment(EnvironmentPointer environment, EnvironmentCoordinate& cache)
{
if (!cache.is_valid()) [[unlikely]]
return nullptr;
if (auto* cached_environment = get_cacheable_environment(environment, cache)) [[likely]]
return cached_environment;
cache = {};
return nullptr;
}
template<typename EnvironmentPointer>
static void update_environment_coordinate_cache(EnvironmentPointer environment, Reference const& reference, EnvironmentCoordinate& cache)
{
if (!reference.environment_coordinate().has_value())
return;
auto candidate = reference.environment_coordinate().value();
if (get_cacheable_environment(environment, candidate))
cache = candidate;
}
ByteString Instruction::to_byte_string(Bytecode::Executable const& executable) const
{
#define __BYTECODE_OP(op) \
case Instruction::Type::op: \
return static_cast<Bytecode::Op::op const&>(*this).to_byte_string_impl(executable);
switch (type()) {
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
default:
VERIFY_NOT_REACHED();
}
#undef __BYTECODE_OP
}
}
namespace JS::Bytecode::Op {
#define JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
ThrowCompletionOr<void> OpTitleCase::execute_impl(VM& vm) const \
{ \
auto lhs = vm.get(m_lhs); \
auto rhs = vm.get(m_rhs); \
vm.set(m_dst, Value { TRY(op_snake_case(vm, lhs, rhs)) }); \
return {}; \
}
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DEFINE_EXECUTE_FOR_COMMON_BINARY_OP)
ThrowCompletionOr<void> Add::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
if (!Checked<i32>::addition_would_overflow(lhs.as_i32(), rhs.as_i32())) [[likely]] {
vm.set(m_dst, Value(lhs.as_i32() + rhs.as_i32()));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) + static_cast<i64>(rhs.as_i32());
vm.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
vm.set(m_dst, Value(lhs.as_double() + rhs.as_double()));
return {};
}
vm.set(m_dst, TRY(add(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> Mul::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
if (!Checked<i32>::multiplication_would_overflow(lhs.as_i32(), rhs.as_i32())) [[likely]] {
auto lhs_i32 = lhs.as_i32();
auto rhs_i32 = rhs.as_i32();
auto result = lhs_i32 * rhs_i32;
if (result != 0) [[likely]] {
vm.set(m_dst, Value(result));
return {};
}
// NB: When the mathematical result is zero, the sign depends on the operand
// signs. We can determine it directly here instead of widening to double.
auto is_negative_zero = (lhs_i32 < 0) != (rhs_i32 < 0);
vm.set(m_dst, is_negative_zero ? Value(-0.0) : Value(0));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) * static_cast<i64>(rhs.as_i32());
vm.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
vm.set(m_dst, Value(lhs.as_double() * rhs.as_double()));
return {};
}
vm.set(m_dst, TRY(mul(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> Div::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
vm.set(m_dst, Value(lhs.as_double() / rhs.as_double()));
return {};
}
vm.set(m_dst, TRY(div(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> Mod::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
auto n = lhs.as_i32();
auto d = rhs.as_i32();
if (d == 0) {
vm.set(m_dst, js_nan());
return {};
}
if (n == NumericLimits<i32>::min() && d == -1) {
vm.set(m_dst, Value(-0.0));
return {};
}
auto result = n % d;
if (result == 0 && n < 0) {
vm.set(m_dst, Value(-0.0));
return {};
}
vm.set(m_dst, Value(result));
return {};
}
vm.set(m_dst, Value(fmod(lhs.as_double(), rhs.as_double())));
return {};
}
vm.set(m_dst, TRY(mod(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> Sub::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
if (!Checked<i32>::subtraction_would_overflow(lhs.as_i32(), rhs.as_i32())) [[likely]] {
vm.set(m_dst, Value(lhs.as_i32() - rhs.as_i32()));
return {};
}
auto result = static_cast<i64>(lhs.as_i32()) - static_cast<i64>(rhs.as_i32());
vm.set(m_dst, Value(result, Value::CannotFitInInt32::Indeed));
return {};
}
vm.set(m_dst, Value(lhs.as_double() - rhs.as_double()));
return {};
}
vm.set(m_dst, TRY(sub(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> BitwiseXor::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() ^ rhs.as_i32()));
return {};
}
vm.set(m_dst, TRY(bitwise_xor(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> BitwiseAnd::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() & rhs.as_i32()));
return {};
}
vm.set(m_dst, TRY(bitwise_and(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> ToInt32::execute_impl(VM& vm) const
{
auto const value = vm.get(m_value);
if (value.is_int32()) [[likely]] {
vm.set(m_dst, value);
return {};
}
vm.set(m_dst, Value(TRY(value.to_i32(vm))));
return {};
}
ThrowCompletionOr<void> BitwiseOr::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() | rhs.as_i32()));
return {};
}
vm.set(m_dst, TRY(bitwise_or(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> UnsignedRightShift::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
vm.set(m_dst, Value(static_cast<u32>(lhs.as_i32()) >> shift_count));
return {};
}
vm.set(m_dst, TRY(unsigned_right_shift(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> RightShift::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
vm.set(m_dst, Value(lhs.as_i32() >> shift_count));
return {};
}
vm.set(m_dst, TRY(right_shift(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> LeftShift::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_int32() && rhs.is_int32()) {
auto const shift_count = static_cast<u32>(rhs.as_i32()) % 32;
vm.set(m_dst, Value(lhs.as_i32() << shift_count));
return {};
}
vm.set(m_dst, TRY(left_shift(vm, lhs, rhs)));
return {};
}
ThrowCompletionOr<void> LessThan::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() < rhs.as_i32()));
return {};
}
vm.set(m_dst, Value(lhs.as_double() < rhs.as_double()));
return {};
}
vm.set(m_dst, Value { TRY(less_than(vm, lhs, rhs)) });
return {};
}
ThrowCompletionOr<void> LessThanEquals::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() <= rhs.as_i32()));
return {};
}
vm.set(m_dst, Value(lhs.as_double() <= rhs.as_double()));
return {};
}
vm.set(m_dst, Value { TRY(less_than_equals(vm, lhs, rhs)) });
return {};
}
ThrowCompletionOr<void> GreaterThan::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() > rhs.as_i32()));
return {};
}
vm.set(m_dst, Value(lhs.as_double() > rhs.as_double()));
return {};
}
vm.set(m_dst, Value { TRY(greater_than(vm, lhs, rhs)) });
return {};
}
ThrowCompletionOr<void> GreaterThanEquals::execute_impl(VM& vm) const
{
auto const lhs = vm.get(m_lhs);
auto const rhs = vm.get(m_rhs);
if (lhs.is_number() && rhs.is_number()) [[likely]] {
if (lhs.is_int32() && rhs.is_int32()) {
vm.set(m_dst, Value(lhs.as_i32() >= rhs.as_i32()));
return {};
}
vm.set(m_dst, Value(lhs.as_double() >= rhs.as_double()));
return {};
}
vm.set(m_dst, Value { TRY(greater_than_equals(vm, lhs, rhs)) });
return {};
}
void Not::execute_impl(VM& vm) const
{
vm.set(dst(), Value(!vm.get(src()).to_boolean()));
}
#define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
ThrowCompletionOr<void> OpTitleCase::execute_impl(VM& vm) const \
{ \
vm.set(dst(), TRY(op_snake_case(vm, vm.get(src())))); \
return {}; \
}
JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
void NewArray::execute_impl(VM& vm) const
{
auto array = MUST(Array::create(vm.realm(), m_element_count));
for (size_t i = 0; i < m_element_count; i++) {
array->indexed_put(i, vm.get(m_elements[i]));
}
vm.set(dst(), array);
}
enum class BindingIsKnownToBeInitialized {
No,
Yes,
};
template<BindingIsKnownToBeInitialized binding_is_known_to_be_initialized>
static ThrowCompletionOr<void> get_binding(VM& vm, Operand dst, EnvironmentCoordinate const& cache)
{
VERIFY(cache.is_valid());
auto const* environment = vm.running_execution_context().lexical_environment.ptr();
for (size_t i = 0; i < cache.hops; ++i)
environment = environment->outer_environment();
Value value;
if constexpr (binding_is_known_to_be_initialized == BindingIsKnownToBeInitialized::No) {
value = TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, cache.index));
} else {
value = static_cast<DeclarativeEnvironment const&>(*environment).get_initialized_binding_value_direct(cache.index);
}
vm.set(dst, value);
return {};
}
ThrowCompletionOr<void> GetBinding::execute_impl(VM& vm) const
{
return get_binding<BindingIsKnownToBeInitialized::No>(vm, m_dst, m_cache);
}
ThrowCompletionOr<void> GetInitializedBinding::execute_impl(VM& vm) const
{
return get_binding<BindingIsKnownToBeInitialized::Yes>(vm, m_dst, m_cache);
}
ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(VM& vm) const
{
auto callee_and_this = TRY(get_callee_and_this_from_environment(
vm,
m_cache));
vm.set(m_callee, callee_and_this.callee);
vm.set(m_this_value, callee_and_this.this_value);
return {};
}
ThrowCompletionOr<void> GetGlobal::execute_impl(VM& vm) const
{
vm.set(dst(), TRY(get_global(vm, m_identifier, strict(), vm.current_executable().global_variable_caches[m_cache])));
return {};
}
ThrowCompletionOr<void> SetGlobal::execute_impl(VM& vm) const
{
auto& binding_object = vm.global_object();
auto& declarative_record = vm.global_declarative_environment();
auto& cache = vm.current_executable().global_variable_caches[m_cache];
auto& shape = binding_object.shape();
auto src = vm.get(m_src);
if (cache.environment_serial_number == declarative_record.environment_serial_number()) {
// OPTIMIZATION: For global var bindings, if the shape of the global object hasn't changed,
// we can use the cached property offset.
auto* entry = cache.first_entry();
if (entry && &shape == entry->shape && (!shape.is_dictionary() || shape.dictionary_generation() == entry->shape_dictionary_generation)) {
auto value = binding_object.get_direct(entry->property_offset);
if (value.is_accessor())
TRY(call(vm, value.as_accessor().setter(), &binding_object, src));
else
binding_object.put_direct(entry->property_offset, src);
return {};
}
// OPTIMIZATION: For global lexical bindings, if the global declarative environment hasn't changed,
// we can use the cached environment binding index.
if (cache.has_environment_binding_index) {
if (cache.in_module_environment) {
auto module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>();
TRY((*module)->environment()->set_mutable_binding_direct(vm, cache.environment_binding_index, src, strict() == Strict::Yes));
} else {
TRY(declarative_record.set_mutable_binding_direct(vm, cache.environment_binding_index, src, strict() == Strict::Yes));
}
return {};
}
}
cache.environment_serial_number = declarative_record.environment_serial_number();
auto& identifier = vm.get_identifier(m_identifier);
if (auto* module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>()) {
// NOTE: GetGlobal is used to access variables stored in the module environment and global environment.
// The module environment is checked first since it precedes the global environment in the environment chain.
auto& module_environment = *(*module)->environment();
Optional<size_t> index;
if (TRY(module_environment.has_binding(identifier, &index))) {
if (index.has_value()) {
cache.environment_binding_index = static_cast<u32>(index.value());
cache.has_environment_binding_index = true;
cache.in_module_environment = true;
return TRY(module_environment.set_mutable_binding_direct(vm, index.value(), src, strict() == Strict::Yes));
}
return TRY(module_environment.set_mutable_binding(vm, identifier, src, strict() == Strict::Yes));
}
}
Optional<size_t> offset;
if (TRY(declarative_record.has_binding(identifier, &offset))) {
cache.environment_binding_index = static_cast<u32>(offset.value());
cache.has_environment_binding_index = true;
cache.in_module_environment = false;
TRY(declarative_record.set_mutable_binding(vm, identifier, src, strict() == Strict::Yes));
return {};
}
if (TRY(binding_object.has_property(identifier))) {
CacheableSetPropertyMetadata cacheable_metadata;
auto success = TRY(binding_object.internal_set(identifier, src, &binding_object, &cacheable_metadata));
if (!success && strict() == Strict::Yes) [[unlikely]] {
// Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
auto property_or_error = binding_object.internal_get_own_property(identifier);
if (!property_or_error.is_error()) {
auto property = property_or_error.release_value();
if (property.has_value() && !property->writable.value_or(true)) {
return vm.throw_completion<TypeError>(ErrorType::DescWriteNonWritable, identifier);
}
}
return vm.throw_completion<TypeError>(ErrorType::ObjectSetReturnedFalse);
}
if (cacheable_metadata.type == CacheableSetPropertyMetadata::Type::ChangeOwnProperty) {
cache.update(PropertyLookupCache::Entry::Type::ChangeOwnProperty, [&](auto& entry) {
entry.shape = shape;
entry.property_offset = cacheable_metadata.property_offset.value();
if (shape.is_dictionary()) {
entry.shape_dictionary_generation = shape.dictionary_generation();
}
});
}
return {};
}
auto reference = TRY(vm.resolve_binding(identifier, strict(), &declarative_record));
TRY(reference.put_value(vm, src));
return {};
}
ThrowCompletionOr<void> GetPrivateById::execute_impl(VM& vm) const
{
auto const& name = vm.get_identifier(m_property);
auto base_value = vm.get(m_base);
auto private_reference = make_private_reference(vm, base_value, name);
vm.set(dst(), TRY(private_reference.get_value(vm)));
return {};
}
ThrowCompletionOr<void> PutById::execute_impl(VM& vm) const
{
auto value = vm.get(m_src);
auto base = vm.get(m_base);
auto const& base_identifier = vm.get_identifier(m_base_identifier);
auto const& property_key = vm.get_property_key(m_property);
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
TRY(put_by_property_key(vm, base, base, value, base_identifier, property_key, m_kind, strict(), &cache));
return {};
}
ThrowCompletionOr<void> PutPrivateById::execute_impl(VM& vm) const
{
auto value = vm.get(m_src);
auto object = TRY(vm.get(m_base).to_object(vm));
auto const& name = vm.get_identifier(m_property);
auto private_reference = make_private_reference(vm, object, name);
TRY(private_reference.put_value(vm, value));
return {};
}
ThrowCompletionOr<void> ResolveThisBinding::execute_impl(VM& vm) const
{
auto& cached_this_value = vm.reg(Register::this_value());
if (!cached_this_value.is_special_empty_value())
return {};
// OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's
// resolved once and then saved for subsequent use.
auto& running_execution_context = vm.running_execution_context();
if (auto function = running_execution_context.function; function && is<ECMAScriptFunctionObject>(*function)) {
auto& ecmascript_function = static_cast<ECMAScriptFunctionObject&>(*function);
if (!ecmascript_function.allocates_function_environment() && !ecmascript_function.this_value_needs_environment_resolution()) {
cached_this_value = running_execution_context.this_value.value();
return {};
}
}
cached_this_value = TRY(vm.resolve_this_binding());
return {};
}
void GetLexicalEnvironment::execute_impl(VM& vm) const
{
vm.set(dst(), vm.running_execution_context().lexical_environment);
}
template<CallType call_type>
NEVER_INLINE static ThrowCompletionOr<void> execute_call(
VM& vm,
Value callee,
Value this_value,
ReadonlySpan<Operand> arguments,
Operand dst,
Optional<StringTableIndex> const expression_string,
Strict strict)
{
TRY(throw_if_needed_for_call(vm, callee, call_type, expression_string));
auto& function = callee.as_function();
size_t registers_and_locals_count = 0;
ReadonlySpan<Value> constants;
size_t argument_count = arguments.size();
function.get_stack_frame_info(registers_and_locals_count, constants, argument_count);
auto& stack = vm.interpreter_stack();
auto* stack_mark = stack.top();
auto* callee_context = stack.allocate(registers_and_locals_count, constants, max(arguments.size(), argument_count));
if (!callee_context) [[unlikely]]
return vm.throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
ScopeGuard deallocate_guard = [&stack, stack_mark] { stack.deallocate(stack_mark); };
auto* callee_context_argument_values = callee_context->arguments_data();
auto const callee_context_argument_count = callee_context->argument_count;
auto const insn_argument_count = arguments.size();
for (size_t i = 0; i < insn_argument_count; ++i)
callee_context_argument_values[i] = vm.get(arguments.data()[i]);
for (size_t i = insn_argument_count; i < callee_context_argument_count; ++i)
callee_context_argument_values[i] = js_undefined();
callee_context->passed_argument_count = insn_argument_count;
Value retval;
if (call_type == CallType::DirectEval && callee == vm.realm().intrinsics().eval_function()) {
retval = TRY(perform_eval(vm, callee_context->argument_count > 0 ? callee_context->arguments_data()[0] : js_undefined(), strict == Strict::Yes ? CallerMode::Strict : CallerMode::NonStrict, EvalMode::Direct));
} else if (call_type == CallType::Construct) {
retval = TRY(function.internal_construct(*callee_context, function));
} else {
retval = TRY(function.internal_call(*callee_context, this_value));
}
vm.set(dst, retval);
return {};
}
ThrowCompletionOr<void> Call::execute_impl(VM& vm) const
{
return execute_call<CallType::Call>(vm, vm.get(m_callee), vm.get(m_this_value), { m_arguments, m_argument_count }, m_dst, m_expression_string, strict());
}
NEVER_INLINE ThrowCompletionOr<void> CallConstruct::execute_impl(VM& vm) const
{
return execute_call<CallType::Construct>(vm, vm.get(m_callee), js_undefined(), { m_arguments, m_argument_count }, m_dst, m_expression_string, strict());
}
template<Builtin builtin, typename Callback>
ALWAYS_INLINE static ThrowCompletionOr<void> execute_specialized_builtin_call(
VM& vm,
Operand callee_operand,
Operand this_value_operand,
ReadonlySpan<Operand> arguments,
Operand dst,
Optional<StringTableIndex> const expression_string,
Strict strict,
Callback callback)
{
auto callee = vm.get(callee_operand);
if (callee.is_function() && callee.as_function().builtin() == builtin) [[likely]] {
vm.set(dst, TRY(callback(arguments)));
return {};
}
return execute_call<CallType::Call>(vm, callee, vm.get(this_value_operand), arguments, dst, expression_string, strict);
}
#define JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(name, implementation) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
Operand arguments[] { m_argument }; \
return execute_specialized_builtin_call<Builtin::name>(vm, m_callee, m_this_value, arguments, m_dst, m_expression_string, strict(), [&](ReadonlySpan<Operand> arguments) -> ThrowCompletionOr<Value> { \
return implementation(vm, vm.get(arguments[0])); \
}); \
}
#define JS_DEFINE_BINARY_BUILTIN_CALL_EXECUTE_IMPL(name, implementation) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
Operand arguments[] { m_argument0, m_argument1 }; \
return execute_specialized_builtin_call<Builtin::name>(vm, m_callee, m_this_value, arguments, m_dst, m_expression_string, strict(), [&](ReadonlySpan<Operand> arguments) -> ThrowCompletionOr<Value> { \
return implementation(vm, vm.get(arguments[0]), vm.get(arguments[1])); \
}); \
}
#define JS_DEFINE_NULLARY_BUILTIN_CALL_EXECUTE_IMPL(name, implementation) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
return execute_specialized_builtin_call<Builtin::name>(vm, m_callee, m_this_value, {}, m_dst, m_expression_string, strict(), [&](ReadonlySpan<Operand>) -> ThrowCompletionOr<Value> { \
return implementation(); \
}); \
}
#define JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(name, ...) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
return execute_call<CallType::Call>(vm, vm.get(m_callee), vm.get(m_this_value), {}, m_dst, m_expression_string, strict()); \
}
#define JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(name, ...) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
Operand arguments[] { m_argument }; \
return execute_call<CallType::Call>(vm, vm.get(m_callee), vm.get(m_this_value), arguments, m_dst, m_expression_string, strict()); \
}
#define JS_DEFINE_BINARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(name, ...) \
ThrowCompletionOr<void> CallBuiltin##name::execute_impl(VM& vm) const \
{ \
Operand arguments[] { m_argument0, m_argument1 }; \
return execute_call<CallType::Call>(vm, vm.get(m_callee), vm.get(m_this_value), arguments, m_dst, m_expression_string, strict()); \
}
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathAbs, MathObject::abs_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathLog, MathObject::log_impl)
JS_DEFINE_BINARY_BUILTIN_CALL_EXECUTE_IMPL(MathPow, MathObject::pow_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathExp, MathObject::exp_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathCeil, MathObject::ceil_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathFloor, MathObject::floor_impl)
JS_DEFINE_BINARY_BUILTIN_CALL_EXECUTE_IMPL(MathImul, MathObject::imul_impl)
JS_DEFINE_NULLARY_BUILTIN_CALL_EXECUTE_IMPL(MathRandom, MathObject::random_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathRound, MathObject::round_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathSqrt, MathObject::sqrt_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathSin, MathObject::sin_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathCos, MathObject::cos_impl)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(MathTan, MathObject::tan_impl)
JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(RegExpPrototypeExec)
JS_DEFINE_BINARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(RegExpPrototypeReplace)
JS_DEFINE_BINARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(RegExpPrototypeSplit)
JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(OrdinaryHasInstance)
JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(ArrayIteratorPrototypeNext)
JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(MapIteratorPrototypeNext)
JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(SetIteratorPrototypeNext)
JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(StringIteratorPrototypeNext)
JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL(StringFromCharCode, StringConstructor::from_char_code_impl)
JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(StringPrototypeCharCodeAt)
JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL(StringPrototypeCharAt)
#undef JS_DEFINE_BINARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL
#undef JS_DEFINE_UNARY_GENERIC_BUILTIN_CALL_EXECUTE_IMPL
#undef JS_DEFINE_GENERIC_BUILTIN_CALL_EXECUTE_IMPL
#undef JS_DEFINE_NULLARY_BUILTIN_CALL_EXECUTE_IMPL
#undef JS_DEFINE_BINARY_BUILTIN_CALL_EXECUTE_IMPL
#undef JS_DEFINE_UNARY_BUILTIN_CALL_EXECUTE_IMPL
void Return::execute_impl(VM& vm) const
{
vm.do_return(vm.get(m_value));
}
ThrowCompletionOr<void> Increment::execute_impl(VM& vm) const
{
auto old_value = vm.get(dst());
// OPTIMIZATION: Fast path for Int32 values.
if (old_value.is_int32()) [[likely]] {
auto integer_value = old_value.as_i32();
if (integer_value != NumericLimits<i32>::max()) [[likely]] {
vm.set(dst(), Value { integer_value + 1 });
return {};
}
}
old_value = TRY(old_value.to_numeric(vm));
if (old_value.is_number())
vm.set(dst(), Value(old_value.as_double() + 1));
else
vm.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
return {};
}
ThrowCompletionOr<void> PostfixIncrement::execute_impl(VM& vm) const
{
auto old_value = vm.get(m_src);
// OPTIMIZATION: Fast path for Int32 values.
if (old_value.is_int32()) [[likely]] {
auto integer_value = old_value.as_i32();
if (integer_value != NumericLimits<i32>::max()) [[likely]] {
vm.set(m_dst, old_value);
vm.set(m_src, Value { integer_value + 1 });
return {};
}
}
old_value = TRY(old_value.to_numeric(vm));
vm.set(m_dst, old_value);
if (old_value.is_number())
vm.set(m_src, Value(old_value.as_double() + 1));
else
vm.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })));
return {};
}
ThrowCompletionOr<void> Decrement::execute_impl(VM& vm) const
{
auto old_value = vm.get(dst());
old_value = TRY(old_value.to_numeric(vm));
if (old_value.is_number())
vm.set(dst(), Value(old_value.as_double() - 1));
else
vm.set(dst(), BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
return {};
}
ThrowCompletionOr<void> PostfixDecrement::execute_impl(VM& vm) const
{
auto old_value = vm.get(m_src);
old_value = TRY(old_value.to_numeric(vm));
vm.set(m_dst, old_value);
if (old_value.is_number())
vm.set(m_src, Value(old_value.as_double() - 1));
else
vm.set(m_src, BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })));
return {};
}
ThrowCompletionOr<void> GetByValue::execute_impl(VM& vm) const
{
vm.set(dst(), TRY(get_by_value(vm, m_base_identifier, vm.get(m_base), vm.get(m_property), vm.current_executable())));
return {};
}
ThrowCompletionOr<void> PutByValue::execute_impl(VM& vm) const
{
auto value = vm.get(m_src);
auto base = vm.get(m_base);
auto const& base_identifier = vm.get_identifier(m_base_identifier);
auto property = vm.get(m_property);
TRY(put_by_value(vm, base, base_identifier, property, value, m_kind, strict()));
return {};
}
}