-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyBuiltInMethod.cpp
More file actions
94 lines (76 loc) · 2.53 KB
/
Copy pathPyBuiltInMethod.cpp
File metadata and controls
94 lines (76 loc) · 2.53 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
#include "PyBuiltInMethod.hpp"
#include "MemoryError.hpp"
#include "PyString.hpp"
#include "types/api.hpp"
#include "types/builtin.hpp"
#include "vm/VM.hpp"
namespace py {
template<> PyBuiltInMethod *as(PyObject *node)
{
if (node->type() == types::builtin_method()) { return static_cast<PyBuiltInMethod *>(node); }
return nullptr;
}
template<> const PyBuiltInMethod *as(const PyObject *node)
{
if (node->type() == types::builtin_method()) {
return static_cast<const PyBuiltInMethod *>(node);
}
return nullptr;
}
PyBuiltInMethod::PyBuiltInMethod(PyType *type) : PyBaseObject(type) {}
PyBuiltInMethod::PyBuiltInMethod(MethodDefinition &method_definition, PyObject *self)
: PyBaseObject(types::BuiltinTypes::the().builtin_method()), m_ml(method_definition),
m_self(self)
{}
void PyBuiltInMethod::visit_graph(Visitor &visitor)
{
PyObject::visit_graph(visitor);
visitor.visit(*m_self);
}
std::string PyBuiltInMethod::to_string() const
{
ASSERT(m_ml);
ASSERT(m_self);
return fmt::format("<built-in method '{}' of '{}' object at {}>",
m_ml->get().name,
m_self->type()->name(),
static_cast<const void *>(this));
}
PyResult<PyObject *> PyBuiltInMethod::__repr__() const { return PyString::create(to_string()); }
PyResult<PyObject *> PyBuiltInMethod::__call__(PyTuple *args, PyDict *kwargs)
{
ASSERT(m_ml);
if (m_ml->get().flags.flags() == MethodFlags::create().flags()) {
return m_ml->get().method(m_self, args, kwargs);
} else if (m_ml->get().flags.is_set(MethodFlags::Flag::CLASSMETHOD)) {
return m_ml->get().method(m_self->type(), args, kwargs);
} else if (m_ml->get().flags.is_set(MethodFlags::Flag::STATICMETHOD)) {
return m_ml->get().method(m_self->type(), args, kwargs);
} else {
TODO();
}
}
PyResult<PyBuiltInMethod *> PyBuiltInMethod::create(MethodDefinition &method_definition,
PyObject *self)
{
auto *obj = VirtualMachine::the().heap().allocate<PyBuiltInMethod>(method_definition, self);
if (!obj) { return Err(memory_error(sizeof(PyBuiltInMethod))); }
return Ok(obj);
}
PyType *PyBuiltInMethod::static_type() const { return types::builtin_method(); }
namespace {
std::once_flag builtin_method_flag;
std::unique_ptr<TypePrototype> register_builtin_method()
{
return std::move(klass<PyBuiltInMethod>("builtin_method").type);
}
}// namespace
std::function<std::unique_ptr<TypePrototype>()> PyBuiltInMethod::type_factory()
{
return [] {
static std::unique_ptr<TypePrototype> type = nullptr;
std::call_once(builtin_method_flag, []() { type = register_builtin_method(); });
return std::move(type);
};
}
}// namespace py