|
| 1 | +use crate::PyObject; |
| 2 | +use crate::pystate::with_vm; |
| 3 | +use core::ffi::{c_int, c_uint, c_ulong}; |
| 4 | +use rustpython_vm::builtins::PyType; |
| 5 | +use rustpython_vm::{AsObject, Context, Py}; |
| 6 | + |
| 7 | +const PY_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; |
| 8 | +const PY_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25; |
| 9 | +const PY_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; |
| 10 | +const PY_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27; |
| 11 | +const PY_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; |
| 12 | +const PY_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29; |
| 13 | +const PY_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30; |
| 14 | +const PY_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; |
| 15 | + |
| 16 | +pub type PyTypeObject = Py<PyType>; |
| 17 | + |
| 18 | +#[unsafe(no_mangle)] |
| 19 | +pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject { |
| 20 | + unsafe { (*op).class() } |
| 21 | +} |
| 22 | + |
| 23 | +#[unsafe(no_mangle)] |
| 24 | +pub unsafe extern "C" fn Py_IS_TYPE(op: *mut PyObject, ty: *mut PyTypeObject) -> c_int { |
| 25 | + with_vm(|_vm| { |
| 26 | + let obj = unsafe { &*op }; |
| 27 | + let ty = unsafe { &*ty }; |
| 28 | + obj.class().is(ty) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +#[unsafe(no_mangle)] |
| 33 | +pub unsafe extern "C" fn PyType_GetFlags(ptr: *const PyTypeObject) -> c_ulong { |
| 34 | + let ctx = Context::genesis(); |
| 35 | + let zoo = &ctx.types; |
| 36 | + let exp_zoo = &ctx.exceptions; |
| 37 | + |
| 38 | + let ty = unsafe { &*ptr }; |
| 39 | + let mut flags = ty.slots.flags.bits(); |
| 40 | + |
| 41 | + if ty.is_subtype(zoo.int_type) { |
| 42 | + flags |= PY_TPFLAGS_LONG_SUBCLASS; |
| 43 | + } |
| 44 | + if ty.is_subtype(zoo.list_type) { |
| 45 | + flags |= PY_TPFLAGS_LIST_SUBCLASS |
| 46 | + } |
| 47 | + if ty.is_subtype(zoo.tuple_type) { |
| 48 | + flags |= PY_TPFLAGS_TUPLE_SUBCLASS; |
| 49 | + } |
| 50 | + if ty.is_subtype(zoo.bytes_type) { |
| 51 | + flags |= PY_TPFLAGS_BYTES_SUBCLASS; |
| 52 | + } |
| 53 | + if ty.is_subtype(zoo.str_type) { |
| 54 | + flags |= PY_TPFLAGS_UNICODE_SUBCLASS; |
| 55 | + } |
| 56 | + if ty.is_subtype(zoo.dict_type) { |
| 57 | + flags |= PY_TPFLAGS_DICT_SUBCLASS; |
| 58 | + } |
| 59 | + if ty.is_subtype(exp_zoo.base_exception_type) { |
| 60 | + flags |= PY_TPFLAGS_BASE_EXC_SUBCLASS; |
| 61 | + } |
| 62 | + if ty.is_subtype(zoo.type_type) { |
| 63 | + flags |= PY_TPFLAGS_TYPE_SUBCLASS; |
| 64 | + } |
| 65 | + |
| 66 | + flags |
| 67 | +} |
| 68 | + |
| 69 | +#[unsafe(no_mangle)] |
| 70 | +pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject { |
| 71 | + with_vm(|vm| { |
| 72 | + let ctx = &vm.ctx; |
| 73 | + match constant_id { |
| 74 | + 0 => ctx.none.as_object(), |
| 75 | + 1 => ctx.false_value.as_object(), |
| 76 | + 2 => ctx.true_value.as_object(), |
| 77 | + 3 => ctx.ellipsis.as_object(), |
| 78 | + 4 => ctx.not_implemented.as_object(), |
| 79 | + _ => panic!("Invalid constant_id passed to Py_GetConstantBorrowed"), |
| 80 | + } |
| 81 | + .as_raw() |
| 82 | + }) |
| 83 | +} |
0 commit comments