Skip to content

Commit

Permalink
LibJS: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenikaliaksandr committed May 14, 2024
1 parent f7001a4 commit ca7cb06
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ ThrowCompletionOr<void> CreateVariable::execute_impl(Bytecode::Interpreter& inte

ThrowCompletionOr<void> CreateRestParams::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto const& arguments = interpreter.running_execution_context().arguments();
auto const& arguments = interpreter.running_execution_context().arguments;
auto arguments_count = interpreter.running_execution_context().passed_argument_count;
auto array = MUST(Array::create(interpreter.realm(), 0));
for (size_t rest_index = m_rest_index; rest_index < arguments_count; ++rest_index)
Expand All @@ -1341,7 +1341,7 @@ ThrowCompletionOr<void> CreateRestParams::execute_impl(Bytecode::Interpreter& in
ThrowCompletionOr<void> CreateArguments::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto const& function = interpreter.running_execution_context().function;
auto const& arguments = interpreter.running_execution_context().arguments();
auto const& arguments = interpreter.running_execution_context().arguments;
auto const& environment = interpreter.running_execution_context().lexical_environment;

auto passed_arguments = ReadonlySpan<Value> { arguments.data(), interpreter.running_execution_context().passed_argument_count };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> AsyncFunctionConstructor::construct(Func

// 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
MarkedVector<Value> args(heap());
for (auto argument : vm.running_execution_context().arguments())
for (auto argument : vm.running_execution_context().arguments)
args.append(argument);

// 3. Return CreateDynamicFunction(C, NewTarget, async, args).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> AsyncGeneratorFunctionConstructor::const

// 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
MarkedVector<Value> args(heap());
for (auto argument : vm.running_execution_context().arguments())
for (auto argument : vm.running_execution_context().arguments)
args.append(argument);

// 3. Return ? CreateDynamicFunction(C, NewTarget, asyncGenerator, args).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body(ReadonlySpan<Va

auto arguments_size = max(arguments_list.size(), m_formal_parameters.size());
auto registers_and_constants_and_locals_count = m_local_variables_names.size() + m_bytecode_executable->number_of_registers + m_bytecode_executable->constants.size();
vm.running_execution_context().registers_and_constants_and_locals_count = registers_and_constants_and_locals_count;
// vm.running_execution_context().registers_and_constants_and_locals_count = registers_and_constants_and_locals_count;

auto& registers_constants_locals_and_arguments = vm.running_execution_context().registers_constants_locals_and_arguments;
registers_constants_locals_and_arguments.resize(registers_and_constants_and_locals_count + arguments_size);
Expand All @@ -839,6 +839,8 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body(ReadonlySpan<Va
}
}

vm.running_execution_context().arguments = { registers_constants_locals_and_arguments.data() + registers_and_constants_and_locals_count, arguments_size };

auto result_and_frame = vm.bytecode_interpreter().run_executable(*m_bytecode_executable, {});

if (result_and_frame.value.is_error())
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/ExecutionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ NonnullOwnPtr<ExecutionContext> ExecutionContext::copy() const
copy->is_strict_mode = is_strict_mode;
copy->executable = executable;
copy->passed_argument_count = passed_argument_count;
copy->registers_and_constants_and_locals_count = registers_and_constants_and_locals_count;
copy->registers_constants_locals_and_arguments = registers_constants_locals_and_arguments;
copy->arguments = { copy->registers_constants_locals_and_arguments.data(), arguments.size() };
copy->unwind_contexts = unwind_contexts;
copy->saved_lexical_environments = saved_lexical_environments;
copy->previously_scheduled_jumps = previously_scheduled_jumps;
Expand Down
9 changes: 4 additions & 5 deletions Userland/Libraries/LibJS/Runtime/ExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,20 @@ struct ExecutionContext {

Value argument(size_t index) const
{
if (index >= registers_constants_locals_and_arguments.size()) [[unlikely]]
if (index >= arguments.size()) [[unlikely]]
return js_undefined();
return registers_constants_locals_and_arguments[registers_and_constants_and_locals_count + index];
return arguments[index];
}

Value& local(size_t index)
{
return registers_constants_locals_and_arguments[index];
}

u32 registers_and_constants_and_locals_count { 0 };
// u32 registers_and_constants_and_locals_count { 0 };
u32 passed_argument_count { 0 };

Span<Value> arguments() { return registers_constants_locals_and_arguments.span().slice(registers_and_constants_and_locals_count); }
ReadonlySpan<Value> arguments() const { return registers_constants_locals_and_arguments.span().slice(registers_and_constants_and_locals_count); }
Span<Value> arguments;

Vector<Value> registers_constants_locals_and_arguments;
Vector<Bytecode::UnwindInfo> unwind_contexts;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> FunctionConstructor::construct(FunctionO

// 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
MarkedVector<Value> args(heap());
for (auto argument : vm.running_execution_context().arguments())
for (auto argument : vm.running_execution_context().arguments)
args.append(argument);

// 3. Return ? CreateDynamicFunction(C, NewTarget, normal, args).
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibJS/Runtime/FunctionPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::bind)

Vector<Value> arguments;
if (vm.argument_count() > 1) {
arguments.append(vm.running_execution_context().arguments().slice(1).data(), vm.argument_count() - 1);
arguments.append(vm.running_execution_context().arguments.slice(1).data(), vm.argument_count() - 1);
}

// 3. Let F be ? BoundFunctionCreate(Target, thisArg, args).
Expand Down Expand Up @@ -129,7 +129,7 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::call)
// FIXME: 3. Perform PrepareForTailCall().

auto this_arg = vm.argument(0);
auto args = vm.argument_count() > 1 ? vm.running_execution_context().arguments().slice(1) : ReadonlySpan<Value> {};
auto args = vm.argument_count() > 1 ? vm.running_execution_context().arguments.slice(1) : ReadonlySpan<Value> {};

// 4. Return ? Call(func, thisArg, args).
return TRY(JS::call(vm, function, this_arg, args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> GeneratorFunctionConstructor::construct(

// 2. Let args be the argumentsList that was passed to this function by [[Call]] or [[Construct]].
MarkedVector<Value> args(heap());
for (auto argument : vm.running_execution_context().arguments())
for (auto argument : vm.running_execution_context().arguments)
args.append(argument);

// 3. Return ? CreateDynamicFunction(C, NewTarget, generator, args).
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibJS/Runtime/NativeFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ ThrowCompletionOr<Value> NativeFunction::internal_call(Value this_argument, Read

// 8. Perform any necessary implementation-defined initialization of calleeContext.
callee_context->this_value = this_argument;
callee_context->registers_and_constants_and_locals_count = 0;
// callee_context->registers_and_constants_and_locals_count = 0;
callee_context->registers_constants_locals_and_arguments.append(arguments_list.data(), arguments_list.size());
callee_context->arguments = callee_context->registers_constants_locals_and_arguments.span();
callee_context->program_counter = vm.bytecode_interpreter().program_counter();

callee_context->lexical_environment = caller_context.lexical_environment;
Expand Down Expand Up @@ -210,8 +211,9 @@ ThrowCompletionOr<NonnullGCPtr<Object>> NativeFunction::internal_construct(Reado
// Note: This is already the default value.

// 8. Perform any necessary implementation-defined initialization of calleeContext.
callee_context->registers_and_constants_and_locals_count = 0;
// callee_context->registers_and_constants_and_locals_count = 0;
callee_context->registers_constants_locals_and_arguments.append(arguments_list.data(), arguments_list.size());
callee_context->arguments = callee_context->registers_constants_locals_and_arguments.span();
callee_context->program_counter = vm.bytecode_interpreter().program_counter();

callee_context->lexical_environment = caller_context.lexical_environment;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::try_)
auto callbackfn = vm.argument(0);
Span<Value> args;
if (vm.argument_count() > 1) {
args = vm.running_execution_context().arguments().slice(1, vm.argument_count() - 1);
args = vm.running_execution_context().arguments.slice(1, vm.argument_count() - 1);
}

// 1. Let C be the this value.
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class VM : public RefCounted<VM> {
{
if (m_execution_context_stack.is_empty())
return 0;
return running_execution_context().arguments().size();
return running_execution_context().arguments.size();
}

Value argument(size_t index) const
Expand Down

0 comments on commit ca7cb06

Please sign in to comment.