1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-16 04:29:08 +00:00

WIP - support for make_new in obj_type!

This commit is contained in:
matejcik 2024-01-15 14:09:41 +01:00
parent 40f027d2f9
commit 365e350405
2 changed files with 17 additions and 3 deletions

View File

@ -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,

View File

@ -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;