1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:27:35 +00:00

LibJS: Implement generator functions (only in bytecode mode)

This commit is contained in:
Ali Mohammad Pur 2021-06-11 01:38:30 +04:30 committed by Andreas Kling
parent c53a86a3fe
commit 3234697eca
21 changed files with 407 additions and 34 deletions

View file

@ -293,6 +293,8 @@ struct CommonPropertyNames {
FlyString catch_ { "catch" };
FlyString delete_ { "delete" };
FlyString for_ { "for" };
FlyString return_ { "return" };
FlyString throw_ { "throw" };
#define __ENUMERATE(x) FlyString x { #x };
ENUMERATE_STANDARD_PROPERTY_NAMES(__ENUMERATE)
#undef __ENUMERATE

View file

@ -0,0 +1,163 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/TemporaryChange.h>
#include <LibJS/Bytecode/Generator.h>
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
GeneratorObject* GeneratorObject::create(GlobalObject& global_object, Value initial_value, ScriptFunction* generating_function, ScopeObject* generating_scope, Bytecode::RegisterWindow frame)
{
auto object = global_object.heap().allocate<GeneratorObject>(global_object, global_object);
object->m_generating_function = generating_function;
object->m_scope = generating_scope;
object->m_frame = move(frame);
object->m_previous_value = initial_value;
return object;
}
GeneratorObject::GeneratorObject(GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
}
void GeneratorObject::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
Object::initialize(global_object);
define_native_function(vm.names.next, next);
define_native_function(vm.names.return_, return_);
define_native_function(vm.names.throw_, throw_);
}
GeneratorObject::~GeneratorObject()
{
}
void GeneratorObject::visit_edges(Cell::Visitor& visitor)
{
Object::visit_edges(visitor);
visitor.visit(m_scope);
visitor.visit(m_generating_function);
if (m_previous_value.is_object())
visitor.visit(&m_previous_value.as_object());
}
GeneratorObject* GeneratorObject::typed_this(VM& vm, GlobalObject& global_object)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
if (!is<GeneratorObject>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Generator");
return nullptr;
}
return static_cast<GeneratorObject*>(this_object);
}
Value GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional<Value> value_to_throw)
{
auto bytecode_interpreter = Bytecode::Interpreter::current();
VERIFY(bytecode_interpreter);
auto generated_value = [](Value value) {
if (value.is_object())
return value.as_object().get("result");
return value.is_empty() ? js_undefined() : value;
};
auto generated_continuation = [&](Value value) -> Bytecode::BasicBlock const* {
if (value.is_object())
return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(value.as_object().get("continuation").to_double(global_object)));
return nullptr;
};
Value previous_generated_value { generated_value(m_previous_value) };
if (vm.exception())
return {};
auto result = Object::create_empty(global_object);
result->put("value", previous_generated_value);
if (m_done) {
result->put("done", Value(true));
return result;
}
// Extract the continuation
auto next_block = generated_continuation(m_previous_value);
if (vm.exception())
return {};
if (!next_block) {
// The generator has terminated, now we can simply return done=true.
m_done = true;
result->put("done", Value(true));
return result;
}
// Make sure it's an actual block
VERIFY(!m_generating_function->bytecode_executable()->basic_blocks.find_if([next_block](auto& block) { return block == next_block; }).is_end());
// Restore the snapshot registers
bytecode_interpreter->enter_frame(m_frame);
// Pretend that 'yield' returned the passed value, or threw
if (value_to_throw.has_value()) {
vm.throw_exception(global_object, value_to_throw.release_value());
bytecode_interpreter->accumulator() = js_undefined();
} else {
bytecode_interpreter->accumulator() = vm.argument(0);
}
// Temporarily switch to the captured scope
TemporaryChange change { vm.call_frame().scope, m_scope };
m_previous_value = bytecode_interpreter->run(*m_generating_function->bytecode_executable(), next_block);
bytecode_interpreter->leave_frame();
m_done = generated_continuation(m_previous_value) == nullptr;
result->put("value", generated_value(m_previous_value));
result->put("done", Value(m_done));
if (vm.exception())
return {};
return result;
}
JS_DEFINE_NATIVE_FUNCTION(GeneratorObject::next)
{
auto object = typed_this(vm, global_object);
if (!object)
return {};
return object->next_impl(vm, global_object, {});
}
JS_DEFINE_NATIVE_FUNCTION(GeneratorObject::return_)
{
auto object = typed_this(vm, global_object);
if (!object)
return {};
object->m_done = true;
return object->next_impl(vm, global_object, {});
}
JS_DEFINE_NATIVE_FUNCTION(GeneratorObject::throw_)
{
auto object = typed_this(vm, global_object);
if (!object)
return {};
return object->next_impl(vm, global_object, vm.argument(0));
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/ScriptFunction.h>
namespace JS {
class GeneratorObject final : public Object {
JS_OBJECT(GeneratorObject, Object);
public:
static GeneratorObject* create(GlobalObject&, Value, ScriptFunction*, ScopeObject*, Bytecode::RegisterWindow);
explicit GeneratorObject(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~GeneratorObject() override;
void visit_edges(Cell::Visitor&) override;
static GeneratorObject* typed_this(VM&, GlobalObject&);
private:
JS_DECLARE_NATIVE_FUNCTION(next);
JS_DECLARE_NATIVE_FUNCTION(return_);
JS_DECLARE_NATIVE_FUNCTION(throw_);
Value next_impl(VM&, GlobalObject&, Optional<Value> value_to_throw);
ScopeObject* m_scope { nullptr };
ScriptFunction* m_generating_function { nullptr };
Value m_previous_value;
Bytecode::RegisterWindow m_frame;
bool m_done { false };
};
}

View file

@ -13,7 +13,9 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/ScriptFunction.h>
#include <LibJS/Runtime/Value.h>
@ -31,18 +33,19 @@ static ScriptFunction* typed_this(VM& vm, GlobalObject& global_object)
return static_cast<ScriptFunction*>(this_object);
}
ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_strict, bool is_arrow_function)
ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_generator, bool is_strict, bool is_arrow_function)
{
return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_scope, *global_object.function_prototype(), is_strict, is_arrow_function);
return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_scope, *global_object.function_prototype(), is_generator, is_strict, is_arrow_function);
}
ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_strict, bool is_arrow_function)
ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_generator, bool is_strict, bool is_arrow_function)
: Function(prototype, is_arrow_function ? vm().this_value(global_object) : Value(), {})
, m_name(name)
, m_body(body)
, m_parameters(move(parameters))
, m_parent_scope(parent_scope)
, m_function_length(m_function_length)
, m_is_generator(is_generator)
, m_is_strict(is_strict)
, m_is_arrow_function(is_arrow_function)
{
@ -152,15 +155,20 @@ Value ScriptFunction::execute_function_body()
if (bytecode_interpreter) {
prepare_arguments();
if (!m_bytecode_executable.has_value()) {
m_bytecode_executable = Bytecode::Generator::generate(m_body);
m_bytecode_executable = Bytecode::Generator::generate(m_body, m_is_generator);
if constexpr (JS_BYTECODE_DEBUG) {
dbgln("Compiled Bytecode::Block for function '{}':", m_name);
for (auto& block : m_bytecode_executable->basic_blocks)
block.dump(*m_bytecode_executable);
}
}
return bytecode_interpreter->run(*m_bytecode_executable);
auto result = bytecode_interpreter->run(*m_bytecode_executable);
if (!m_is_generator)
return result;
return GeneratorObject::create(global_object(), result, this, vm.call_frame().scope, bytecode_interpreter->snapshot_frame());
} else {
VERIFY(!m_is_generator);
OwnPtr<Interpreter> local_interpreter;
ast_interpreter = vm.interpreter_if_exists();

View file

@ -16,9 +16,9 @@ class ScriptFunction final : public Function {
JS_OBJECT(ScriptFunction, Function);
public:
static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_strict, bool is_arrow_function = false);
static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_generator, bool is_strict, bool is_arrow_function = false);
ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_strict, bool is_arrow_function = false);
ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_generator, bool is_strict, bool is_arrow_function = false);
virtual void initialize(GlobalObject&) override;
virtual ~ScriptFunction();
@ -33,6 +33,8 @@ public:
void set_is_class_constructor() { m_is_class_constructor = true; };
auto& bytecode_executable() const { return m_bytecode_executable; }
protected:
virtual bool is_strict_mode() const final { return m_is_strict; }
@ -51,6 +53,7 @@ private:
Optional<Bytecode::Executable> m_bytecode_executable;
ScopeObject* m_parent_scope { nullptr };
i32 m_function_length { 0 };
bool m_is_generator { false };
bool m_is_strict { false };
bool m_is_arrow_function { false };
bool m_is_class_constructor { false };