summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp
blob: 59308d9323a160fa9bdbed620eb5dbebe518562d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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/GeneratorObjectPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>

namespace JS {

GeneratorObject* GeneratorObject::create(GlobalObject& global_object, Value initial_value, ECMAScriptFunctionObject* generating_function, Environment* generating_scope, Bytecode::RegisterWindow frame)
{
    // This is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
    auto generating_function_prototype = TRY_OR_DISCARD(generating_function->get(global_object.vm().names.prototype));
    auto* generating_function_prototype_object = TRY_OR_DISCARD(generating_function_prototype.to_object(global_object));
    auto object = global_object.heap().allocate<GeneratorObject>(global_object, global_object, *generating_function_prototype_object);
    object->m_generating_function = generating_function;
    object->m_environment = generating_scope;
    object->m_frame = move(frame);
    object->m_previous_value = initial_value;
    return object;
}

GeneratorObject::GeneratorObject(GlobalObject&, Object& prototype)
    : Object(prototype)
{
}

void GeneratorObject::initialize(GlobalObject&)
{
}

GeneratorObject::~GeneratorObject()
{
}

void GeneratorObject::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_environment);
    visitor.visit(m_generating_function);
    visitor.visit(m_previous_value);
}

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) -> Value {
        if (value.is_object())
            return TRY_OR_DISCARD(value.as_object().get("result"));
        return value.is_empty() ? js_undefined() : value;
    };

    auto generated_continuation = [&](Value value) -> Bytecode::BasicBlock const* {
        if (value.is_object()) {
            auto number_value = TRY_OR_DISCARD(value.as_object().get("continuation"));
            return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(TRY_OR_DISCARD(number_value.to_double(global_object))));
        }
        return nullptr;
    };

    Value previous_generated_value { generated_value(m_previous_value) };

    if (vm.exception())
        return {};

    auto result = Object::create(global_object, global_object.object_prototype());
    result->define_direct_property("value", previous_generated_value, JS::default_attributes);

    if (m_done) {
        result->define_direct_property("done", Value(true), JS::default_attributes);
        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->define_direct_property("done", Value(true), JS::default_attributes);
        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 environment record
    TemporaryChange change { vm.running_execution_context().lexical_environment, m_environment };

    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->define_direct_property("value", generated_value(m_previous_value), JS::default_attributes);
    result->define_direct_property("done", Value(m_done), JS::default_attributes);

    if (vm.exception())
        return {};

    return result;
}

}