-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathYieldFrom.cpp
More file actions
66 lines (59 loc) · 1.78 KB
/
YieldFrom.cpp
File metadata and controls
66 lines (59 loc) · 1.78 KB
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
#include "YieldFrom.hpp"
#include "interpreter/Interpreter.hpp"
#include "runtime/BaseException.hpp"
#include "runtime/PyFrame.hpp"
#include "runtime/PyGenerator.hpp"
#include "runtime/PyNone.hpp"
#include "runtime/PyString.hpp"
#include "runtime/PyTuple.hpp"
#include "runtime/PyType.hpp"
#include "runtime/StopIteration.hpp"
#include "vm/VM.hpp"
using namespace py;
PyResult<Value> YieldFrom::execute(VirtualMachine &vm, Interpreter &interpreter) const
{
ASSERT(interpreter.execution_frame()->generator() != nullptr);
auto src = vm.reg(m_receiver);
auto value = vm.reg(m_value);
ASSERT(std::holds_alternative<PyObject *>(src));
auto receiver = std::get<PyObject *>(src);
auto v = PyObject::from(value);
if (v.is_err()) { return v; }
auto result = [receiver, v = v.unwrap()]() -> PyResult<Value> {
[[maybe_unused]] RAIIStoreNonCallInstructionData non_call_instruction_data;
if (auto *generator = as<PyGenerator>(receiver)) {
return generator->send(v);
} else if (v == py_none()) {
return receiver->next();
} else {
return receiver->get_method(PyString::create("send").unwrap())
.and_then([v](PyObject *send) {
return send->call(PyTuple::create(v).unwrap(), nullptr);
});
}
}();
if (result.is_err()) {
if (result.unwrap_err()->type()->issubclass(stop_iteration()->type())) {
const auto &args = as<StopIteration>(result.unwrap_err())->args()->elements();
result = Ok(args.empty() ? py_none() : args[0]);
vm.reg(m_dst) = result.unwrap();
} else {
TODO();
}
} else {
vm.reg(m_dst) = result.unwrap();
vm.reg(0) = result.unwrap();
vm.set_instruction_pointer(vm.instruction_pointer() - 1);
vm.pop_frame(false);
}
return result;
}
std::vector<uint8_t> YieldFrom::serialize() const
{
return {
YIELD_FROM,
m_dst,
m_receiver,
m_value,
};
}