-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyCell.cpp
More file actions
100 lines (82 loc) · 2.57 KB
/
Copy pathPyCell.cpp
File metadata and controls
100 lines (82 loc) · 2.57 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
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
#include "PyCell.hpp"
#include "MemoryError.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
#include "vm/VM.hpp"
namespace py {
template<> PyCell *as(PyObject *obj)
{
if (obj->type() == types::cell()) { return static_cast<PyCell *>(obj); }
return nullptr;
}
template<> const PyCell *as(const PyObject *obj)
{
if (obj->type() == types::cell()) { return static_cast<const PyCell *>(obj); }
return nullptr;
}
PyCell::PyCell(PyType *type) : PyBaseObject(type) {}
PyCell::PyCell(const Value &content)
: PyBaseObject(types::BuiltinTypes::the().cell()), m_content(content)
{}
PyResult<PyCell *> PyCell::create()
{
auto *obj = VirtualMachine::the().heap().allocate<PyCell>(Value{ nullptr });
if (!obj) { return Err(memory_error(sizeof(PyCell))); }
return Ok(obj);
}
PyResult<PyCell *> PyCell::create(const Value &content)
{
if (std::holds_alternative<PyObject *>(content)) { ASSERT(std::get<PyObject *>(content)); }
auto *obj = VirtualMachine::the().heap().allocate<PyCell>(content);
if (!obj) { return Err(memory_error(sizeof(PyCell))); }
return Ok(obj);
}
std::string PyCell::to_string() const
{
if (std::holds_alternative<PyObject *>(m_content)) {
if (auto *obj = std::get<PyObject *>(m_content)) {
return fmt::format("<cell at {}: {} object at {}>",
(void *)this,
obj->type()->to_string(),
(void *)obj);
} else {
return "<cell: empty>";
}
} else {
return fmt::format("<cell at {}: {} object at {}>",
(void *)this,
PyObject::from(m_content).unwrap()->type()->to_string(),
(void *)&m_content);
}
}
void PyCell::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
if (std::holds_alternative<PyObject *>(m_content)) {
if (auto *obj = std::get<PyObject *>(m_content)) { visitor.visit(*obj); }
}
}
PyType *PyCell::static_type() const { return types::cell(); }
const Value &PyCell::content() const { return m_content; }
bool PyCell::empty() const
{
if (std::holds_alternative<PyObject *>(m_content)) {
if (!std::get<PyObject *>(m_content)) { return true; }
}
return false;
}
PyResult<PyObject *> PyCell::__repr__() const { return PyString::create(to_string()); }
void PyCell::set_cell(const Value &new_value) { m_content = new_value; }
namespace {
std::once_flag cell_flag;
std::unique_ptr<TypePrototype> register_cell() { return std::move(klass<PyCell>("cell").type); }
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyCell::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(cell_flag, []() { type = register_cell(); });
return std::move(type);
};
}
}// namespace py