|
| 1 | +#include "PyCallableProxyType.hpp" |
| 2 | +#include "runtime/MemoryError.hpp" |
| 3 | +#include "runtime/PyModule.hpp" |
| 4 | +#include "runtime/PyNone.hpp" |
| 5 | +#include "runtime/PyType.hpp" |
| 6 | +#include "runtime/ValueError.hpp" |
| 7 | +#include "runtime/types/api.hpp" |
| 8 | +#include "vm/VM.hpp" |
| 9 | + |
| 10 | +namespace py { |
| 11 | + |
| 12 | +namespace { |
| 13 | + static PyType *s_weak_callableproxy = nullptr; |
| 14 | +} |
| 15 | + |
| 16 | +PyCallableProxyType::PyCallableProxyType(PyType *type) : PyBaseObject(type) {} |
| 17 | + |
| 18 | +PyCallableProxyType::PyCallableProxyType(PyObject *object, PyObject *callback) |
| 19 | + : PyBaseObject(s_weak_callableproxy), m_object(object), m_callback(callback) |
| 20 | +{} |
| 21 | + |
| 22 | +PyResult<PyCallableProxyType *> PyCallableProxyType::create(PyObject *object, PyObject *callback) |
| 23 | +{ |
| 24 | + auto *result = |
| 25 | + VirtualMachine::the().heap().allocate_weakref<PyCallableProxyType>(object, callback); |
| 26 | + if (!result) { return Err(memory_error(sizeof(PyCallableProxyType))); } |
| 27 | + return Ok(result); |
| 28 | +} |
| 29 | + |
| 30 | +void PyCallableProxyType::visit_graph(Visitor &visitor) |
| 31 | +{ |
| 32 | + PyObject::visit_graph(visitor); |
| 33 | + if (m_callback) visitor.visit(*m_callback); |
| 34 | +} |
| 35 | + |
| 36 | +std::string PyCallableProxyType::to_string() const |
| 37 | +{ |
| 38 | + if (!m_object) { |
| 39 | + return fmt::format("<weakproxy at {} empty", static_cast<const void *>(this)); |
| 40 | + } |
| 41 | + return fmt::format("<weakproxy at {} to {} at {}>", |
| 42 | + static_cast<const void *>(this), |
| 43 | + m_object->type()->name(), |
| 44 | + static_cast<const void *>(m_object)); |
| 45 | +} |
| 46 | + |
| 47 | +PyResult<PyObject *> PyCallableProxyType::__new__(const PyType *type, PyTuple *args, PyDict *kwargs) |
| 48 | +{ |
| 49 | + ASSERT(type == s_weak_callableproxy) |
| 50 | + ASSERT(args && args->size() > 0) |
| 51 | + ASSERT(!kwargs || kwargs->size() == 0) |
| 52 | + |
| 53 | + auto *obj = PyObject::from(args->elements()[0]).unwrap(); |
| 54 | + auto *callback = [args]() -> PyObject * { |
| 55 | + if (args->size() > 1) { return PyObject::from(args->elements()[1]).unwrap(); } |
| 56 | + return nullptr; |
| 57 | + }(); |
| 58 | + |
| 59 | + return PyCallableProxyType::create(obj, callback); |
| 60 | +} |
| 61 | + |
| 62 | +PyResult<PyObject *> PyCallableProxyType::__str__() const |
| 63 | +{ |
| 64 | + if (!m_object) { |
| 65 | + // FIXME: should be a ReferenceError |
| 66 | + return Err(value_error("weakly-referenced object no longer exists")); |
| 67 | + } |
| 68 | + return m_object->str(); |
| 69 | +} |
| 70 | + |
| 71 | +PyResult<PyObject *> PyCallableProxyType::__repr__() const |
| 72 | +{ |
| 73 | + if (!m_object) { |
| 74 | + // FIXME: should be a ReferenceError |
| 75 | + return Err(value_error("weakly-referenced object no longer exists")); |
| 76 | + } |
| 77 | + return PyString::create(to_string()); |
| 78 | +} |
| 79 | + |
| 80 | +PyResult<PyObject *> PyCallableProxyType::__getattribute__(PyObject *attribute) const |
| 81 | +{ |
| 82 | + auto obj = [this]() -> PyResult<const PyObject *> { |
| 83 | + if (type() == s_weak_callableproxy) { |
| 84 | + if (!is_alive()) { |
| 85 | + // FIXME: should be a ReferenceError |
| 86 | + return Err(value_error("weakly-referenced object no longer exists")); |
| 87 | + } else { |
| 88 | + return Ok(m_object); |
| 89 | + } |
| 90 | + } |
| 91 | + return Ok(this); |
| 92 | + }(); |
| 93 | + if (obj.is_err()) { return Err(obj.unwrap_err()); } |
| 94 | + |
| 95 | + auto attr = [attribute]() -> PyResult<PyObject *> { |
| 96 | + if (attribute->type() == s_weak_callableproxy) { |
| 97 | + if (!static_cast<const PyCallableProxyType *>(attribute)->is_alive()) { |
| 98 | + // FIXME: should be a ReferenceError |
| 99 | + return Err(value_error("weakly-referenced object no longer exists")); |
| 100 | + } else { |
| 101 | + return Ok(static_cast<const PyCallableProxyType *>(attribute)->m_object); |
| 102 | + } |
| 103 | + } |
| 104 | + return Ok(attribute); |
| 105 | + }(); |
| 106 | + if (attr.is_err()) { return attr; } |
| 107 | + |
| 108 | + return obj.unwrap()->get_attribute(attr.unwrap()); |
| 109 | +} |
| 110 | + |
| 111 | +PyResult<PyObject *> PyCallableProxyType::__call__(PyTuple *args, PyDict *kwargs) |
| 112 | +{ |
| 113 | + if (!m_object) { |
| 114 | + // FIXME: should be a ReferenceError |
| 115 | + return Err(value_error("weakly-referenced object no longer exists")); |
| 116 | + } |
| 117 | + return m_object->call(args, kwargs); |
| 118 | +} |
| 119 | + |
| 120 | +PyType *PyCallableProxyType::register_type(PyModule *module, std::string_view name) |
| 121 | +{ |
| 122 | + if (!s_weak_callableproxy) { |
| 123 | + s_weak_callableproxy = klass<PyCallableProxyType>(module, "weakcallableproxy") |
| 124 | + .attr("__callback__", &PyCallableProxyType::m_callback) |
| 125 | + .finalize(); |
| 126 | + } |
| 127 | + module->add_symbol(PyString::create(std::string(name)).unwrap(), s_weak_callableproxy); |
| 128 | + return s_weak_callableproxy; |
| 129 | +} |
| 130 | + |
| 131 | +bool PyCallableProxyType::is_alive() const |
| 132 | +{ |
| 133 | + if (m_object |
| 134 | + && !VirtualMachine::the().heap().has_weakref_object(bit_cast<uint8_t *>(m_object))) { |
| 135 | + m_object = nullptr; |
| 136 | + } |
| 137 | + return m_object != nullptr; |
| 138 | +} |
| 139 | + |
| 140 | +}// namespace py |
0 commit comments