WIP - support for make_new in obj_type!

pull/3491/head
matejcik 5 months ago
parent 40f027d2f9
commit 365e350405

@ -137,8 +137,9 @@ macro_rules! obj_dict {
macro_rules! obj_type {
(name: $name:expr,
$(locals: $locals:expr,)?
$(attr_fn: $attr_fn:ident,)?
$(call_fn: $call_fn:ident,)?
$(make_new_fn: $make_new_fn:path,)?
$(attr_fn: $attr_fn:path,)?
$(call_fn: $call_fn:path,)?
) => {{
#[allow(unused_unsafe)]
unsafe {
@ -156,6 +157,11 @@ macro_rules! obj_type {
let mut call: ffi::mp_call_fun_t = None;
$(call = Some($call_fn);)?
#[allow(unused_mut)]
#[allow(unused_assignments)]
let mut make_new: ffi::mp_make_new_fun_t = None;
$(make_new = Some($make_new_fn);)?
// TODO: This is safe only if we pass in `Dict` with fixed `Map` (created by
// `Map::fixed()`, usually through `obj_map!`), because only then will
// MicroPython treat `locals_dict` as immutable, and make the mutable cast safe.
@ -171,7 +177,7 @@ macro_rules! obj_type {
flags: 0,
name,
print: None,
make_new: None,
make_new,
call,
unary_op: None,
binary_op: None,

@ -17,6 +17,14 @@ impl Type {
ObjBase { type_: self }
}
pub const fn as_obj(&'static self) -> Obj {
// SAFETY:
// - We are an object struct with a base and a type.
// - 'static lifetime holds us in place.
// - MicroPython is smart enough not to mutate `mp_obj_type_t` objects.
unsafe { Obj::from_ptr(self as *const _ as *mut _) }
}
#[cfg(feature = "debug")]
pub fn name(&self) -> &'static str {
use super::qstr::Qstr;

Loading…
Cancel
Save