mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 09:28:13 +00:00
refactor(core/rust): clean up macro namespace
This commit is contained in:
parent
567de6579e
commit
b406fc22f3
@ -1,3 +1,5 @@
|
||||
use crate::error::value_error;
|
||||
|
||||
pub mod cosi;
|
||||
pub mod ed25519;
|
||||
mod ffi;
|
||||
|
@ -26,13 +26,14 @@ pub enum Error {
|
||||
ValueErrorParam(&'static CStr, Obj),
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! value_error {
|
||||
($msg:expr) => {
|
||||
$crate::error::Error::ValueError($msg)
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use value_error;
|
||||
|
||||
#[cfg(feature = "micropython")]
|
||||
impl Error {
|
||||
/// Create an exception instance matching the error code. The result of this
|
||||
|
@ -13,35 +13,33 @@
|
||||
extern crate num_derive;
|
||||
|
||||
#[macro_use]
|
||||
mod error;
|
||||
// use trezorhal for its macros early
|
||||
#[macro_use]
|
||||
mod trezorhal;
|
||||
mod macros;
|
||||
|
||||
#[cfg(feature = "crypto")]
|
||||
mod crypto;
|
||||
#[cfg(feature = "debug")]
|
||||
mod debug;
|
||||
mod error;
|
||||
mod io;
|
||||
mod maybe_trace;
|
||||
#[cfg(feature = "micropython")]
|
||||
#[macro_use]
|
||||
mod micropython;
|
||||
#[cfg(feature = "protobuf")]
|
||||
mod protobuf;
|
||||
mod storage;
|
||||
mod strutil;
|
||||
mod time;
|
||||
#[cfg(feature = "ui_debug")]
|
||||
mod trace;
|
||||
#[cfg(feature = "translations")]
|
||||
pub mod translations;
|
||||
mod translations;
|
||||
mod trezorhal;
|
||||
|
||||
// mod ui is `pub` because of the re-export pattern in individual models, which
|
||||
// would trigger a brickload of "unused symbol" warnings otherwise.
|
||||
// TODO: maybe get rid of the re-export pattern :shrugs:
|
||||
#[cfg(feature = "ui")]
|
||||
#[macro_use]
|
||||
pub mod ui;
|
||||
pub mod strutil;
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
#[macro_use]
|
||||
pub mod debug;
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
#[cfg(not(test))]
|
||||
|
@ -1,74 +1,49 @@
|
||||
/// Create an object for an exported function taking no arguments.
|
||||
macro_rules! obj_fn_0 {
|
||||
($f:expr) => {{
|
||||
macro_rules! _obj_fn_make_fixed {
|
||||
($type:ident, $member:ident, $f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
|
||||
ffi::mp_obj_fun_builtin_fixed_t {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_0,
|
||||
type_: &$crate::micropython::ffi::$type,
|
||||
},
|
||||
fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _0: Some($f) },
|
||||
fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { $member: Some($f) },
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking no arguments.
|
||||
macro_rules! obj_fn_0 {
|
||||
($f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_0, _0, $f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking 1 arg.
|
||||
macro_rules! obj_fn_1 {
|
||||
($f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
|
||||
ffi::mp_obj_fun_builtin_fixed_t {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_1,
|
||||
},
|
||||
fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _1: Some($f) },
|
||||
}
|
||||
}
|
||||
}};
|
||||
($f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_1, _1, $f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking 2 args.
|
||||
macro_rules! obj_fn_2 {
|
||||
($f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
|
||||
ffi::mp_obj_fun_builtin_fixed_t {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_2,
|
||||
},
|
||||
fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _2: Some($f) },
|
||||
}
|
||||
}
|
||||
}};
|
||||
($f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_2, _2, $f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking 3 args.
|
||||
macro_rules! obj_fn_3 {
|
||||
($f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
|
||||
ffi::mp_obj_fun_builtin_fixed_t {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_3,
|
||||
},
|
||||
fun: ffi::_mp_obj_fun_builtin_fixed_t__bindgen_ty_1 { _3: Some($f) },
|
||||
}
|
||||
}
|
||||
}};
|
||||
($f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_fixed!(mp_type_fun_builtin_3, _3, $f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking a variable number of args.
|
||||
macro_rules! obj_fn_var {
|
||||
($min:expr, $max:expr, $f:expr) => {{
|
||||
macro_rules! _obj_fn_make_var {
|
||||
($min:expr, $max:expr, takes_kw: $takes_kw:expr, $var_or_kw:ident: $f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
@ -77,29 +52,28 @@ macro_rules! obj_fn_var {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_var,
|
||||
},
|
||||
sig: ($min << 17u32) | ($max << 1u32) | 0, // min, max, takes_kw
|
||||
fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 { var: Some($f) },
|
||||
sig: ($min << 17u32) | ($max << 1u32) | $takes_kw,
|
||||
fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 {
|
||||
$var_or_kw: Some($f),
|
||||
},
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking a variable number of args
|
||||
/// between min and max
|
||||
macro_rules! obj_fn_var {
|
||||
($min:expr, $max:expr, $f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_var!($min, $max, takes_kw:0, var:$f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Create an object for an exported function taking key-value args.
|
||||
macro_rules! obj_fn_kw {
|
||||
($min:expr, $f:expr) => {{
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
use $crate::micropython::ffi;
|
||||
|
||||
ffi::mp_obj_fun_builtin_var_t {
|
||||
base: ffi::mp_obj_base_t {
|
||||
type_: &ffi::mp_type_fun_builtin_var,
|
||||
},
|
||||
sig: ($min << 17u32) | (0xffff << 1u32) | 1, // min, max, takes_kw
|
||||
fun: ffi::_mp_obj_fun_builtin_var_t__bindgen_ty_1 { kw: Some($f) },
|
||||
}
|
||||
}
|
||||
}};
|
||||
($min:expr, $f:expr) => {
|
||||
crate::micropython::macros::_obj_fn_make_var!($min, 0xffff, takes_kw:1, kw:$f)
|
||||
};
|
||||
}
|
||||
|
||||
/// Construct fixed static const `Map` from `key` => `val` pairs.
|
||||
@ -261,6 +235,36 @@ macro_rules! attr_tuple {
|
||||
});
|
||||
}
|
||||
|
||||
// required because they are used in expansion of macros below
|
||||
pub(crate) use _obj_fn_make_fixed;
|
||||
pub(crate) use _obj_fn_make_var;
|
||||
|
||||
pub(crate) use attr_tuple;
|
||||
pub(crate) use obj_dict;
|
||||
pub(crate) use obj_fn_0;
|
||||
pub(crate) use obj_fn_1;
|
||||
pub(crate) use obj_fn_2;
|
||||
pub(crate) use obj_fn_3;
|
||||
pub(crate) use obj_fn_kw;
|
||||
pub(crate) use obj_fn_var;
|
||||
pub(crate) use obj_map;
|
||||
pub(crate) use obj_module;
|
||||
pub(crate) use obj_type;
|
||||
|
||||
// from https://docs.rs/ufmt/latest/ufmt/
|
||||
// like `std::format!` it returns a `std::String` but uses `uwrite!` instead of
|
||||
// `write!`
|
||||
macro_rules! uformat {
|
||||
// IMPORTANT use `tt` fragments instead of `expr` fragments (i.e. `$($exprs:expr),*`)
|
||||
($len:expr, $($tt:tt)*) => {{
|
||||
let mut s = heapless::String::<$len>::new();
|
||||
match ufmt::uwrite!(&mut s, $($tt)*) {
|
||||
Ok(_) => Ok(s),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Print arbitrary amounts of slices into a terminal.
|
||||
/// Does not include a newline at the end.
|
||||
/// Does not do anything when not in debugging mode.
|
||||
|
@ -10,7 +10,7 @@ use super::{
|
||||
qstr::Qstr,
|
||||
runtime::{catch_exception, raise_exception},
|
||||
};
|
||||
use crate::error::Error;
|
||||
use crate::error::{value_error, Error};
|
||||
|
||||
/// Perform a call and convert errors into a raised MicroPython exception.
|
||||
/// Should only called when returning from Rust to C. See `raise_exception` for
|
||||
|
@ -1,4 +1,7 @@
|
||||
use crate::{error::Error, micropython::qstr::Qstr};
|
||||
use crate::{
|
||||
error::{value_error, Error},
|
||||
micropython::qstr::Qstr,
|
||||
};
|
||||
|
||||
pub const fn experimental_not_enabled() -> Error {
|
||||
value_error!(c"Experimental features are disabled.")
|
||||
|
@ -6,6 +6,7 @@ use crate::{
|
||||
dict::Dict,
|
||||
ffi,
|
||||
gc::Gc,
|
||||
macros::{obj_fn_1, obj_fn_2, obj_fn_3, obj_module, obj_type},
|
||||
map::Map,
|
||||
module::Module,
|
||||
obj::{Obj, ObjBase},
|
||||
|
@ -2,7 +2,7 @@ use core::{mem, str};
|
||||
|
||||
use crate::{
|
||||
crypto::{cosi, ed25519, merkle::merkle_root, sha256},
|
||||
error::Error,
|
||||
error::{value_error, Error},
|
||||
io::InputStream,
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
use spin::{RwLock, RwLockReadGuard};
|
||||
|
||||
use crate::{error::Error, trezorhal::translations};
|
||||
use crate::{
|
||||
error::{value_error, Error},
|
||||
trezorhal::translations,
|
||||
};
|
||||
|
||||
use super::blob::Translations;
|
||||
|
||||
|
@ -4,6 +4,9 @@ use crate::{
|
||||
micropython::{
|
||||
buffer::{get_buffer, StrBuffer},
|
||||
ffi,
|
||||
macros::{
|
||||
attr_tuple, obj_dict, obj_fn_0, obj_fn_1, obj_fn_2, obj_map, obj_module, obj_type,
|
||||
},
|
||||
map::Map,
|
||||
module::Module,
|
||||
obj::Obj,
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use super::ffi;
|
||||
use crate::error::Error;
|
||||
use crate::error::{value_error, Error};
|
||||
use core::ptr;
|
||||
|
||||
use num_traits::FromPrimitive;
|
||||
|
@ -1,6 +1,8 @@
|
||||
use crate::{
|
||||
error::Error,
|
||||
micropython::{ffi, obj::Obj, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type, util},
|
||||
micropython::{
|
||||
ffi, macros::obj_type, obj::Obj, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type, util,
|
||||
},
|
||||
ui::{ui_features::ModelUI, UIFeaturesCommon},
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
error::Error,
|
||||
error::{value_error, Error},
|
||||
trezorhal::uzlib::{UzlibContext, UZLIB_WINDOW_SIZE},
|
||||
ui::{
|
||||
component::image::Image,
|
||||
|
@ -9,6 +9,7 @@ use crate::{
|
||||
micropython::{
|
||||
buffer::StrBuffer,
|
||||
gc::Gc,
|
||||
macros::{obj_dict, obj_fn_1, obj_fn_2, obj_fn_var, obj_map, obj_type},
|
||||
map::Map,
|
||||
obj::{Obj, ObjBase},
|
||||
qstr::Qstr,
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::micropython::{qstr::Qstr, simple_type::SimpleTypeObj, typ::Type};
|
||||
use crate::micropython::{macros::obj_type, qstr::Qstr, simple_type::SimpleTypeObj, typ::Type};
|
||||
|
||||
static CONFIRMED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CONFIRMED, };
|
||||
static CANCELLED_TYPE: Type = obj_type! { name: Qstr::MP_QSTR_CANCELLED, };
|
||||
|
@ -1,10 +1,18 @@
|
||||
use core::{cmp::Ordering, convert::TryInto};
|
||||
|
||||
use crate::{
|
||||
error::Error,
|
||||
error::{value_error, Error},
|
||||
io::BinaryData,
|
||||
micropython::{
|
||||
gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj, qstr::Qstr, util,
|
||||
gc::Gc,
|
||||
iter::IterBuf,
|
||||
list::List,
|
||||
macros::{obj_fn_1, obj_fn_kw, obj_module},
|
||||
map::Map,
|
||||
module::Module,
|
||||
obj::Obj,
|
||||
qstr::Qstr,
|
||||
util,
|
||||
},
|
||||
strutil::TString,
|
||||
translations::TR,
|
||||
|
@ -6,8 +6,16 @@ use crate::{
|
||||
error::Error,
|
||||
maybe_trace::MaybeTrace,
|
||||
micropython::{
|
||||
buffer::StrBuffer, gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj,
|
||||
qstr::Qstr, util,
|
||||
buffer::StrBuffer,
|
||||
gc::Gc,
|
||||
iter::IterBuf,
|
||||
list::List,
|
||||
macros::{obj_fn_0, obj_fn_1, obj_fn_kw, obj_module},
|
||||
map::Map,
|
||||
module::Module,
|
||||
obj::Obj,
|
||||
qstr::Qstr,
|
||||
util,
|
||||
},
|
||||
strutil::TString,
|
||||
translations::TR,
|
||||
|
@ -1,10 +1,18 @@
|
||||
use core::{cmp::Ordering, convert::TryInto};
|
||||
|
||||
use crate::{
|
||||
error::Error,
|
||||
error::{value_error, Error},
|
||||
io::BinaryData,
|
||||
micropython::{
|
||||
gc::Gc, iter::IterBuf, list::List, map::Map, module::Module, obj::Obj, qstr::Qstr, util,
|
||||
gc::Gc,
|
||||
iter::IterBuf,
|
||||
list::List,
|
||||
macros::{obj_fn_1, obj_fn_kw, obj_module},
|
||||
map::Map,
|
||||
module::Module,
|
||||
obj::Obj,
|
||||
qstr::Qstr,
|
||||
util,
|
||||
},
|
||||
strutil::TString,
|
||||
translations::TR,
|
||||
|
Loading…
Reference in New Issue
Block a user