mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-06-23 01:40:26 +00:00
Move the remaining control-flow, conversion, creation, delete, binding, private-name and environment-related fallback handlers into AsmInt. This keeps the generic fallback path shrinking while leaving complex behavior in C++ slow paths.
2423 lines
104 KiB
C++
2423 lines
104 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/Bytecode/PropertyNameIterator.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;
|
|
}
|
|
|
|
inline ThrowCompletionOr<GC::Ref<Array>> iterator_to_array(VM& vm, Value iterator)
|
|
{
|
|
auto& iterator_record = static_cast<IteratorRecord&>(iterator.as_cell());
|
|
|
|
auto array = MUST(Array::create(*vm.current_realm(), 0));
|
|
size_t index = 0;
|
|
|
|
while (true) {
|
|
auto value = TRY(iterator_step_value(vm, iterator_record));
|
|
if (!value.has_value())
|
|
return array;
|
|
|
|
MUST(array->create_data_property_or_throw(index, value.release_value()));
|
|
index++;
|
|
}
|
|
}
|
|
|
|
struct FastPropertyNameIteratorData {
|
|
Vector<PropertyKey> properties;
|
|
PropertyNameIterator::FastPath fast_path { PropertyNameIterator::FastPath::None };
|
|
u32 indexed_property_count { 0 };
|
|
bool receiver_has_magical_length_property { false };
|
|
GC::Ptr<Shape> shape;
|
|
GC::Ptr<PrototypeChainValidity> prototype_chain_validity;
|
|
};
|
|
|
|
static bool shape_has_enumerable_string_property(Shape const& shape)
|
|
{
|
|
bool has_enumerable_string_property = false;
|
|
shape.for_each_property_in_insertion_order([&](auto const& property_key, auto const& metadata) {
|
|
if (property_key.is_string() && metadata.attributes.is_enumerable()) {
|
|
has_enumerable_string_property = true;
|
|
return IterationDecision::Break;
|
|
}
|
|
return IterationDecision::Continue;
|
|
});
|
|
return has_enumerable_string_property;
|
|
}
|
|
|
|
static bool property_name_iterator_fast_path_is_still_eligible(Object& object, PropertyNameIterator::FastPath fast_path, u32 indexed_property_count)
|
|
{
|
|
Object const* object_to_check = &object;
|
|
bool is_receiver = true;
|
|
|
|
while (object_to_check) {
|
|
if (!object_to_check->eligible_for_own_property_enumeration_fast_path())
|
|
return false;
|
|
|
|
if (is_receiver) {
|
|
if (fast_path == PropertyNameIterator::FastPath::PackedIndexed) {
|
|
if (object_to_check->indexed_storage_kind() != IndexedStorageKind::Packed)
|
|
return false;
|
|
if (object_to_check->indexed_array_like_size() != indexed_property_count)
|
|
return false;
|
|
} else if (object_to_check->indexed_array_like_size() != 0) {
|
|
return false;
|
|
}
|
|
} else if (object_to_check->indexed_array_like_size() != 0) {
|
|
return false;
|
|
}
|
|
|
|
object_to_check = object_to_check->prototype();
|
|
is_receiver = false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool object_property_iterator_cache_matches(Object& object, ObjectPropertyIteratorCacheData const& cache)
|
|
{
|
|
// A cache entry represents the fully flattened key snapshot for one bytecode
|
|
// site. Reusing it is only valid while the receiver still has the same local
|
|
// state and the prototype chain validity token says nothing above it changed.
|
|
if (object.has_magical_length_property() != cache.receiver_has_magical_length_property())
|
|
return false;
|
|
|
|
auto& shape = object.shape();
|
|
if (&shape != cache.shape())
|
|
return false;
|
|
|
|
if (shape.is_dictionary() && shape.dictionary_generation() != cache.shape_dictionary_generation())
|
|
return false;
|
|
|
|
if (cache.prototype_chain_validity() && !cache.prototype_chain_validity()->is_valid())
|
|
return false;
|
|
|
|
return property_name_iterator_fast_path_is_still_eligible(object, cache.fast_path(), cache.indexed_property_count());
|
|
}
|
|
|
|
static ThrowCompletionOr<Optional<FastPropertyNameIteratorData>> try_get_fast_property_name_iterator_data(Object& object)
|
|
{
|
|
auto& vm = object.vm();
|
|
FastPropertyNameIteratorData result {};
|
|
result.fast_path = PropertyNameIterator::FastPath::PlainNamed;
|
|
result.receiver_has_magical_length_property = object.has_magical_length_property();
|
|
result.shape = &object.shape();
|
|
|
|
GC::RootHashTable<GC::Ref<Object>> seen_objects;
|
|
size_t estimated_properties_count = 0;
|
|
bool prototype_chain_has_enumerable_named_properties = false;
|
|
for (auto object_to_check = GC::Ptr { &object }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
|
|
seen_objects.set(*object_to_check);
|
|
if (!object_to_check->eligible_for_own_property_enumeration_fast_path())
|
|
return Optional<FastPropertyNameIteratorData> {};
|
|
if (&object == object_to_check.ptr()) {
|
|
if (object_to_check->indexed_array_like_size() != 0) {
|
|
if (object_to_check->indexed_storage_kind() != IndexedStorageKind::Packed)
|
|
return Optional<FastPropertyNameIteratorData> {};
|
|
result.fast_path = PropertyNameIterator::FastPath::PackedIndexed;
|
|
result.indexed_property_count = object_to_check->indexed_array_like_size();
|
|
} else {
|
|
result.fast_path = PropertyNameIterator::FastPath::PlainNamed;
|
|
}
|
|
} else if (object_to_check->indexed_array_like_size() != 0) {
|
|
// The fast path only knows how to synthesize a packed indexed prefix
|
|
// for the receiver itself. As soon as indexed properties appear in
|
|
// the prototype chain, we fall back to the generic enumeration path.
|
|
return Optional<FastPropertyNameIteratorData> {};
|
|
} else if (!prototype_chain_has_enumerable_named_properties) {
|
|
prototype_chain_has_enumerable_named_properties = shape_has_enumerable_string_property(object_to_check->shape());
|
|
}
|
|
estimated_properties_count += object_to_check->shape().property_count();
|
|
}
|
|
seen_objects.clear_with_capacity();
|
|
|
|
if (auto* prototype = object.shape().prototype()) {
|
|
result.prototype_chain_validity = prototype->shape().prototype_chain_validity();
|
|
if (!result.prototype_chain_validity)
|
|
return Optional<FastPropertyNameIteratorData> {};
|
|
}
|
|
|
|
if (!prototype_chain_has_enumerable_named_properties) {
|
|
// Common case: only the receiver contributes enumerable string keys, so
|
|
// we can copy them straight from the shape without any shadowing work.
|
|
result.properties.ensure_capacity(object.shape().property_count());
|
|
object.shape().for_each_property_in_insertion_order([&](auto const& property_key, auto const& metadata) {
|
|
if (property_key.is_string() && metadata.attributes.is_enumerable())
|
|
result.properties.append(property_key);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
result.properties.ensure_capacity(estimated_properties_count);
|
|
|
|
GC::ConservativeHashTable<PropertyKey> seen_non_enumerable_properties;
|
|
Optional<GC::ConservativeHashTable<PropertyKey>> seen_properties;
|
|
auto ensure_seen_properties = [&] {
|
|
if (seen_properties.has_value())
|
|
return;
|
|
// Prototype shadowing ignores enumerability, so once we start looking
|
|
// above the receiver we need an explicit visited set for names we have
|
|
// already decided to expose from lower objects.
|
|
seen_properties.emplace();
|
|
seen_properties->ensure_capacity(result.properties.size());
|
|
for (auto const& property : result.properties)
|
|
seen_properties->set(property);
|
|
};
|
|
|
|
bool in_prototype_chain = false;
|
|
for (auto object_to_check = GC::Ptr { &object }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
|
|
seen_objects.set(*object_to_check);
|
|
|
|
// Arrays keep a non-enumerable magical `length` property outside the shape
|
|
// table, but it still shadows enumerable `length` properties higher up the
|
|
// prototype chain during for-in.
|
|
if (object_to_check->has_magical_length_property())
|
|
seen_non_enumerable_properties.set(vm.names.length);
|
|
|
|
object_to_check->shape().for_each_property_in_insertion_order([&](auto const& property_key, auto const& metadata) {
|
|
if (!property_key.is_string())
|
|
return;
|
|
|
|
bool enumerable = metadata.attributes.is_enumerable();
|
|
if (!enumerable)
|
|
seen_non_enumerable_properties.set(property_key);
|
|
if (in_prototype_chain && enumerable) {
|
|
if (seen_non_enumerable_properties.contains(property_key))
|
|
return;
|
|
ensure_seen_properties();
|
|
if (seen_properties->contains(property_key))
|
|
return;
|
|
}
|
|
if (enumerable)
|
|
result.properties.append(property_key);
|
|
if (seen_properties.has_value())
|
|
seen_properties->set(property_key);
|
|
});
|
|
in_prototype_chain = true;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 14.7.5.9 EnumerateObjectProperties ( O ), https://tc39.es/ecma262/#sec-enumerate-object-properties
|
|
inline ThrowCompletionOr<GC::Ref<PropertyNameIterator>> get_object_property_iterator(VM& vm, Value value, ObjectPropertyIteratorCache* cache = nullptr)
|
|
{
|
|
// While the spec does provide an algorithm, it allows us to implement it ourselves so long as we meet the following invariants:
|
|
// 1- Returned property keys do not include keys that are Symbols
|
|
// 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
|
|
// 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
|
|
// 4- A property name will be returned by the iterator's next method at most once in any enumeration.
|
|
// 5- Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively;
|
|
// 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.
|
|
// 6- The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed.
|
|
// 7- The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument.
|
|
// 8- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
|
|
// 9- Property attributes of the target object must be obtained by calling its [[GetOwnProperty]] internal method
|
|
|
|
// Invariant 3 effectively allows the implementation to ignore newly added keys, and we do so (similar to other implementations).
|
|
auto object = TRY(value.to_object(vm));
|
|
// Note: While the spec doesn't explicitly require these to be ordered, it says that the values should be retrieved via OwnPropertyKeys,
|
|
// so we just keep the order consistent anyway.
|
|
|
|
if (cache && cache->data) {
|
|
if (object_property_iterator_cache_matches(*object, *cache->data)) {
|
|
if (cache->reusable_property_name_iterator) {
|
|
// We keep one iterator object per bytecode site alive so hot
|
|
// loops can recycle it without allocating a new cell each time.
|
|
auto& iterator = static_cast<PropertyNameIterator&>(*cache->reusable_property_name_iterator);
|
|
cache->reusable_property_name_iterator = nullptr;
|
|
iterator.reset_with_cache_data(object, *cache->data, cache);
|
|
return iterator;
|
|
}
|
|
|
|
return PropertyNameIterator::create(vm.realm(), object, *cache->data, cache);
|
|
}
|
|
}
|
|
|
|
if (auto fast_iterator_data = TRY(try_get_fast_property_name_iterator_data(*object)); fast_iterator_data.has_value()) {
|
|
VERIFY(fast_iterator_data->shape);
|
|
auto cache_data = vm.heap().allocate<ObjectPropertyIteratorCacheData>(
|
|
vm,
|
|
move(fast_iterator_data->properties),
|
|
fast_iterator_data->fast_path,
|
|
fast_iterator_data->indexed_property_count,
|
|
fast_iterator_data->receiver_has_magical_length_property,
|
|
*fast_iterator_data->shape,
|
|
fast_iterator_data->prototype_chain_validity);
|
|
if (cache)
|
|
cache->data = cache_data;
|
|
if (cache && cache->reusable_property_name_iterator) {
|
|
auto& iterator = static_cast<PropertyNameIterator&>(*cache->reusable_property_name_iterator);
|
|
cache->reusable_property_name_iterator = nullptr;
|
|
iterator.reset_with_cache_data(object, cache_data, cache);
|
|
return iterator;
|
|
}
|
|
|
|
return PropertyNameIterator::create(vm.realm(), object, cache_data, cache);
|
|
}
|
|
|
|
size_t estimated_properties_count = 0;
|
|
GC::RootHashTable<GC::Ref<Object>> seen_objects;
|
|
for (auto object_to_check = GC::Ptr { object.ptr() }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
|
|
seen_objects.set(*object_to_check);
|
|
estimated_properties_count += object_to_check->own_properties_count();
|
|
}
|
|
seen_objects.clear_with_capacity();
|
|
|
|
GC::ConservativeVector<PropertyKey> properties;
|
|
properties.ensure_capacity(estimated_properties_count);
|
|
|
|
GC::ConservativeHashTable<PropertyKey> seen_non_enumerable_properties;
|
|
Optional<GC::ConservativeHashTable<PropertyKey>> seen_properties;
|
|
auto ensure_seen_properties = [&] {
|
|
if (seen_properties.has_value())
|
|
return;
|
|
seen_properties.emplace();
|
|
seen_properties->ensure_capacity(properties.size());
|
|
for (auto const& property : properties)
|
|
seen_properties->set(property);
|
|
};
|
|
|
|
// Collect all keys immediately (invariant no. 5)
|
|
bool in_prototype_chain = false;
|
|
for (auto object_to_check = GC::Ptr { object.ptr() }; object_to_check && !seen_objects.contains(*object_to_check); object_to_check = TRY(object_to_check->internal_get_prototype_of())) {
|
|
seen_objects.set(*object_to_check);
|
|
TRY(object_to_check->for_each_own_property_with_enumerability([&](PropertyKey const& property_key, bool enumerable) -> ThrowCompletionOr<void> {
|
|
if (!enumerable)
|
|
seen_non_enumerable_properties.set(property_key);
|
|
if (in_prototype_chain && enumerable) {
|
|
if (seen_non_enumerable_properties.contains(property_key))
|
|
return {};
|
|
ensure_seen_properties();
|
|
if (seen_properties->contains(property_key))
|
|
return {};
|
|
}
|
|
if (enumerable)
|
|
properties.append(property_key);
|
|
if (seen_properties.has_value())
|
|
seen_properties->set(property_key);
|
|
return {};
|
|
}));
|
|
in_prototype_chain = true;
|
|
}
|
|
|
|
return PropertyNameIterator::create(vm.realm(), object, move(properties));
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
ThrowCompletionOr<void> ImportCall::execute_impl(VM& vm) const
|
|
{
|
|
auto specifier = vm.get(m_specifier);
|
|
auto options_value = vm.get(m_options);
|
|
vm.set(dst(), TRY(perform_import_call(vm, specifier, options_value)));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> IteratorToArray::execute_impl(VM& vm) const
|
|
{
|
|
auto& iterator_object = vm.get(m_iterator_object).as_object();
|
|
auto iterator_next_method = vm.get(m_iterator_next_method);
|
|
auto iterator_done_property = vm.get(m_iterator_done_property).as_bool();
|
|
IteratorRecordImpl iterator_record { .done = iterator_done_property, .iterator = iterator_object, .next_method = iterator_next_method };
|
|
|
|
auto array = MUST(Array::create(*vm.current_realm(), 0));
|
|
size_t index = 0;
|
|
|
|
while (true) {
|
|
auto value_or_error = iterator_step_value(vm, iterator_record);
|
|
if (iterator_record.done)
|
|
vm.set(m_iterator_done_property, Value(true));
|
|
auto value = TRY(value_or_error);
|
|
if (!value.has_value())
|
|
break;
|
|
|
|
MUST(array->create_data_property_or_throw(index, value.release_value()));
|
|
index++;
|
|
}
|
|
|
|
vm.set(dst(), array);
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> CopyObjectExcludingProperties::execute_impl(VM& vm) const
|
|
{
|
|
auto& realm = *vm.current_realm();
|
|
|
|
auto from_object = vm.get(m_from_object);
|
|
|
|
auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
|
|
|
|
GC::ConservativeHashTable<PropertyKey> excluded_names;
|
|
for (size_t i = 0; i < m_excluded_names_count; ++i) {
|
|
excluded_names.set(TRY(vm.get(m_excluded_names[i]).to_property_key(vm)));
|
|
}
|
|
|
|
TRY(to_object->copy_data_properties(vm, from_object, excluded_names));
|
|
|
|
vm.set(dst(), to_object);
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> ConcatString::execute_impl(VM& vm) const
|
|
{
|
|
auto string = TRY(vm.get(src()).to_primitive_string(vm));
|
|
vm.set(dst(), PrimitiveString::create(vm, vm.get(dst()).as_string(), string));
|
|
return {};
|
|
}
|
|
|
|
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> GetById::execute_impl(VM& vm) const
|
|
{
|
|
auto base_value = vm.get(base());
|
|
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
|
|
|
|
vm.set(dst(), TRY(get_by_id<GetByIdMode::Normal>(vm, [&] { return vm.get_identifier(m_base_identifier); }, [&] -> PropertyKey const& { return vm.get_property_key(m_property); }, base_value, base_value, cache)));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> GetByIdWithThis::execute_impl(VM& vm) const
|
|
{
|
|
auto base_value = vm.get(m_base);
|
|
auto this_value = vm.get(m_this_value);
|
|
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
|
|
vm.set(dst(), TRY(get_by_id<GetByIdMode::Normal>(vm, [] { return Optional<Utf16FlyString const&> {}; }, [&] -> PropertyKey const& { return vm.get_property_key(m_property); }, base_value, this_value, cache)));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> GetLength::execute_impl(VM& vm) const
|
|
{
|
|
auto base_value = vm.get(base());
|
|
auto& executable = vm.current_executable();
|
|
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
|
|
vm.set(dst(), TRY(get_by_id<GetByIdMode::Length>(vm, [&] { return vm.get_identifier(m_base_identifier); }, [&] { return executable.get_property_key(*executable.length_identifier); }, base_value, base_value, cache)));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> GetLengthWithThis::execute_impl(VM& vm) const
|
|
{
|
|
auto base_value = vm.get(m_base);
|
|
auto this_value = vm.get(m_this_value);
|
|
auto& executable = vm.current_executable();
|
|
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
|
|
vm.set(dst(), TRY(get_by_id<GetByIdMode::Length>(vm, [] { return Optional<Utf16FlyString const&> {}; }, [&] -> PropertyKey const& { return executable.get_property_key(*executable.length_identifier); }, base_value, this_value, cache)));
|
|
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> PutBySpread::execute_impl(VM& vm) const
|
|
{
|
|
auto value = vm.get(m_src);
|
|
auto base = vm.get(m_base);
|
|
|
|
// a. Let baseObj be ? ToObject(V.[[Base]]).
|
|
auto object = TRY(base.to_object(vm));
|
|
|
|
TRY(object->copy_data_properties(vm, value, {}));
|
|
|
|
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> PutByIdWithThis::execute_impl(VM& vm) const
|
|
{
|
|
auto value = vm.get(m_src);
|
|
auto base = vm.get(m_base);
|
|
auto const& name = vm.get_property_key(m_property);
|
|
auto& cache = vm.current_executable().property_lookup_caches[m_cache];
|
|
TRY(put_by_property_key(vm, base, vm.get(m_this_value), value, {}, name, 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());
|
|
}
|
|
|
|
ThrowCompletionOr<void> CallDirectEval::execute_impl(VM& vm) const
|
|
{
|
|
return execute_call<CallType::DirectEval>(vm, vm.get(m_callee), vm.get(m_this_value), { 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
|
|
|
|
template<CallType call_type>
|
|
NEVER_INLINE static ThrowCompletionOr<void> call_with_argument_array(
|
|
VM& vm,
|
|
Value callee,
|
|
Value this_value,
|
|
Value 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();
|
|
|
|
auto& argument_array = arguments.as_array_exotic_object();
|
|
auto argument_array_length = argument_array.indexed_array_like_size();
|
|
|
|
size_t argument_count = argument_array_length;
|
|
size_t registers_and_locals_count = 0;
|
|
ReadonlySpan<Value> constants;
|
|
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(argument_array_length, 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 = argument_array_length;
|
|
|
|
for (size_t i = 0; i < insn_argument_count; ++i) {
|
|
if (auto maybe_value = argument_array.indexed_get(i); maybe_value.has_value())
|
|
callee_context_argument_values[i] = maybe_value.release_value().value;
|
|
else
|
|
callee_context_argument_values[i] = js_undefined();
|
|
}
|
|
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> CallWithArgumentArray::execute_impl(VM& vm) const
|
|
{
|
|
return call_with_argument_array<CallType::Call>(vm, vm.get(callee()), vm.get(this_value()), vm.get(arguments()), dst(), expression_string(), strict());
|
|
}
|
|
|
|
ThrowCompletionOr<void> CallDirectEvalWithArgumentArray::execute_impl(VM& vm) const
|
|
{
|
|
return call_with_argument_array<CallType::DirectEval>(vm, vm.get(callee()), vm.get(this_value()), vm.get(arguments()), dst(), expression_string(), strict());
|
|
}
|
|
|
|
ThrowCompletionOr<void> CallConstructWithArgumentArray::execute_impl(VM& vm) const
|
|
{
|
|
return call_with_argument_array<CallType::Construct>(vm, vm.get(callee()), js_undefined(), vm.get(arguments()), dst(), expression_string(), strict());
|
|
}
|
|
|
|
// 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
|
|
ThrowCompletionOr<void> SuperCallWithArgumentArray::execute_impl(VM& vm) const
|
|
{
|
|
|
|
// 1. Let newTarget be GetNewTarget().
|
|
auto new_target = vm.get_new_target();
|
|
|
|
// 2. Assert: Type(newTarget) is Object.
|
|
VERIFY(new_target.is_object());
|
|
|
|
// 3. Let _superConstructor_ be GetSuperConstructor().
|
|
auto super_constructor = vm.get(m_super_constructor);
|
|
|
|
// 4. Let _argList_ be ? ArgumentListEvaluation of |Arguments|.
|
|
// NOTE: The bytecode generator performs this step before emitting this instruction.
|
|
|
|
// 5. If IsConstructor(_superConstructor_) is *false*, throw a *TypeError* exception.
|
|
if (!super_constructor.is_constructor()) [[unlikely]]
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, "Super constructor");
|
|
|
|
auto& function = super_constructor.as_function();
|
|
|
|
auto& argument_array = vm.get(m_arguments).as_array_exotic_object();
|
|
size_t argument_array_length = 0;
|
|
|
|
if (m_is_synthetic) {
|
|
argument_array_length = MUST(length_of_array_like(vm, argument_array));
|
|
} else {
|
|
argument_array_length = argument_array.indexed_array_like_size();
|
|
}
|
|
|
|
size_t argument_count = argument_array_length;
|
|
size_t registers_and_locals_count = 0;
|
|
ReadonlySpan<Value> constants;
|
|
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(argument_array_length, 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 = argument_array_length;
|
|
|
|
if (m_is_synthetic) {
|
|
for (size_t i = 0; i < insn_argument_count; ++i)
|
|
callee_context_argument_values[i] = argument_array.get_without_side_effects(PropertyKey { i });
|
|
} else {
|
|
for (size_t i = 0; i < insn_argument_count; ++i) {
|
|
if (auto maybe_value = argument_array.indexed_get(i); maybe_value.has_value())
|
|
callee_context_argument_values[i] = maybe_value.release_value().value;
|
|
else
|
|
callee_context_argument_values[i] = js_undefined();
|
|
}
|
|
}
|
|
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;
|
|
|
|
// 6. Let result be ? Construct(func, argList, newTarget).
|
|
auto result = TRY(function.internal_construct(*callee_context, new_target.as_function()));
|
|
|
|
// 7. Let thisER be GetThisEnvironment().
|
|
auto& this_environment = as<FunctionEnvironment>(*get_this_environment(vm));
|
|
|
|
// 8. Perform ? thisER.BindThisValue(result).
|
|
TRY(this_environment.bind_this_value(vm, result));
|
|
|
|
// 9. Let F be thisER.[[FunctionObject]].
|
|
auto& f = as<ECMAScriptFunctionObject>(this_environment.function_object());
|
|
|
|
// 10. Assert: F is an ECMAScript function object.
|
|
// NOTE: This is implied by the strong C++ type.
|
|
|
|
// 11. Perform ? InitializeInstanceElements(result, F).
|
|
TRY(result->initialize_instance_elements(f));
|
|
|
|
// 12. Return result.
|
|
vm.set(m_dst, result);
|
|
return {};
|
|
}
|
|
|
|
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> GetByValueWithThis::execute_impl(VM& vm) const
|
|
{
|
|
auto property_key_value = vm.get(m_property);
|
|
auto object = TRY(vm.get(m_base).to_object(vm));
|
|
auto property_key = TRY(property_key_value.to_property_key(vm));
|
|
vm.set(dst(), TRY(object->internal_get(property_key, vm.get(m_this_value))));
|
|
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 {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> PutByValueWithThis::execute_impl(VM& vm) const
|
|
{
|
|
auto value = vm.get(m_src);
|
|
auto base = vm.get(m_base);
|
|
auto this_value = vm.get(m_this_value);
|
|
auto property_key = TRY(vm.get(m_property).to_property_key(vm));
|
|
TRY(put_by_property_key(vm, base, this_value, value, {}, property_key, m_kind, strict()));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> GetIterator::execute_impl(VM& vm) const
|
|
{
|
|
auto iterator_record = TRY(get_iterator_impl(vm, vm.get(iterable()), m_hint));
|
|
vm.set(m_dst_iterator_object, iterator_record.iterator);
|
|
vm.set(m_dst_iterator_next, iterator_record.next_method);
|
|
vm.set(m_dst_iterator_done, Value(iterator_record.done));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> GetMethod::execute_impl(VM& vm) const
|
|
{
|
|
auto const& property_key = vm.get_property_key(m_property);
|
|
auto method = TRY(vm.get(m_object).get_method(vm, property_key));
|
|
vm.set(dst(), method ?: js_undefined());
|
|
return {};
|
|
}
|
|
|
|
NEVER_INLINE ThrowCompletionOr<void> GetObjectPropertyIterator::execute_impl(VM& vm) const
|
|
{
|
|
auto* cache = &vm.current_executable().object_property_iterator_caches[m_cache];
|
|
vm.set(m_dst_iterator, TRY(get_object_property_iterator(vm, vm.get(m_object), cache)));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> IteratorClose::execute_impl(VM& vm) const
|
|
{
|
|
auto& iterator_object = vm.get(m_iterator_object).as_object();
|
|
auto iterator_next_method = vm.get(m_iterator_next);
|
|
auto iterator_done_property = vm.get(m_iterator_done).as_bool();
|
|
IteratorRecordImpl iterator_record { .done = iterator_done_property, .iterator = iterator_object, .next_method = iterator_next_method };
|
|
|
|
// FIXME: Return the value of the resulting completion.
|
|
TRY(iterator_close(vm, iterator_record, Completion { m_completion_type, vm.get(m_completion_value) }));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> IteratorNext::execute_impl(VM& vm) const
|
|
{
|
|
auto& iterator_object = vm.get(m_iterator_object).as_object();
|
|
auto iterator_next_method = vm.get(m_iterator_next);
|
|
auto iterator_done_property = vm.get(m_iterator_done).as_bool();
|
|
IteratorRecordImpl iterator_record { .done = iterator_done_property, .iterator = iterator_object, .next_method = iterator_next_method };
|
|
auto result = JS::iterator_next(vm, iterator_record);
|
|
if (iterator_record.done)
|
|
vm.set(m_iterator_done, Value(true));
|
|
vm.set(m_dst, TRY(result));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> IteratorNextUnpack::execute_impl(VM& vm) const
|
|
{
|
|
auto& iterator_object = vm.get(m_iterator_object).as_object();
|
|
auto iterator_next_method = vm.get(m_iterator_next);
|
|
auto iterator_done_property = vm.get(m_iterator_done).as_bool();
|
|
IteratorRecordImpl iterator_record { .done = iterator_done_property, .iterator = iterator_object, .next_method = iterator_next_method };
|
|
auto iteration_result_or_done_or_error = iterator_step(vm, iterator_record);
|
|
if (iterator_record.done)
|
|
vm.set(m_iterator_done, Value(true));
|
|
auto iteration_result_or_done = TRY(iteration_result_or_done_or_error);
|
|
if (iteration_result_or_done.has<IterationDone>()) {
|
|
vm.set(m_dst_done, Value(true));
|
|
return {};
|
|
}
|
|
auto& iteration_result = iteration_result_or_done.get<IterationResult>();
|
|
vm.set(m_dst_done, TRY(iteration_result.done));
|
|
auto value = move(iteration_result.value);
|
|
if (value.is_throw_completion())
|
|
vm.set(m_iterator_done, Value(true));
|
|
vm.set(m_dst_value, TRY(value));
|
|
return {};
|
|
}
|
|
|
|
ThrowCompletionOr<void> ObjectPropertyIteratorNext::execute_impl(VM& vm) const
|
|
{
|
|
auto& iterator = static_cast<PropertyNameIterator&>(vm.get(m_iterator_object).as_object());
|
|
Value value;
|
|
bool done = false;
|
|
TRY(iterator.next(vm, done, value));
|
|
vm.set(m_dst_done, Value(done));
|
|
if (!done)
|
|
vm.set(m_dst_value, value);
|
|
return {};
|
|
}
|
|
|
|
NEVER_INLINE ThrowCompletionOr<void> NewClass::execute_impl(VM& vm) const
|
|
{
|
|
Value super_class;
|
|
if (m_super_class.has_value())
|
|
super_class = vm.get(m_super_class.value());
|
|
GC::RootVector<Value> element_keys;
|
|
element_keys.ensure_capacity(m_element_keys_count);
|
|
for (size_t i = 0; i < m_element_keys_count; ++i) {
|
|
Value element_key;
|
|
if (m_element_keys[i].has_value())
|
|
element_key = vm.get(m_element_keys[i].value());
|
|
element_keys.unchecked_append(element_key);
|
|
}
|
|
|
|
auto& running_execution_context = vm.running_execution_context();
|
|
auto* class_environment = &as<Environment>(vm.get(m_class_environment).as_cell());
|
|
auto& outer_environment = running_execution_context.lexical_environment;
|
|
|
|
auto const& blueprint = vm.current_executable().class_blueprints[m_class_blueprint_index];
|
|
|
|
Optional<Utf16FlyString> binding_name;
|
|
Utf16FlyString class_name;
|
|
if (!blueprint.has_name && m_lhs_name.has_value()) {
|
|
class_name = vm.get_identifier(m_lhs_name.value());
|
|
} else {
|
|
class_name = blueprint.name;
|
|
binding_name = class_name;
|
|
}
|
|
|
|
auto* retval = TRY(construct_class(vm, blueprint, vm.current_executable(), class_environment, outer_environment, super_class, element_keys, binding_name, class_name));
|
|
vm.set(dst(), retval);
|
|
return {};
|
|
}
|
|
|
|
}
|