chore(core/rust): Add obj_module! macro

macro_obj_module
Jan Pochyla 2 years ago
parent 7ca2ae232b
commit 88ff3053b4

@ -1,3 +1,4 @@
/// Create an object for an exported function taking 1 arg.
macro_rules! obj_fn_1 {
($f:expr) => {{
#[allow(unused_unsafe)]
@ -14,6 +15,7 @@ macro_rules! obj_fn_1 {
}};
}
/// Create an object for an exported function taking 2 args.
macro_rules! obj_fn_2 {
($f:expr) => {{
#[allow(unused_unsafe)]
@ -30,6 +32,7 @@ macro_rules! obj_fn_2 {
}};
}
/// Create an object for an exported function taking 3 args.
macro_rules! obj_fn_3 {
($f:expr) => {{
#[allow(unused_unsafe)]
@ -46,6 +49,7 @@ macro_rules! obj_fn_3 {
}};
}
/// Create an object for an exported function taking a variable number of args.
macro_rules! obj_fn_var {
($min:expr, $max:expr, $f:expr) => {{
#[allow(unused_unsafe)]
@ -63,6 +67,7 @@ macro_rules! obj_fn_var {
}};
}
/// Create an object for an exported function taking key-value args.
macro_rules! obj_fn_kw {
($min:expr, $f:expr) => {{
#[allow(unused_unsafe)]
@ -111,6 +116,7 @@ macro_rules! obj_dict {
}};
}
/// Compose a `Type` object definition.
macro_rules! obj_type {
(name: $name:expr,
$(locals: $locals:expr,)?
@ -134,8 +140,8 @@ macro_rules! obj_type {
$(call = Some($call_fn);)?
// TODO: This is safe only if we pass in `Dict` with fixed `Map` (created by
// `Map::fixed()`), because only then will Micropython treat `locals_dict` as
// immutable, and make the mutable cast safe.
// `Map::fixed()`, usually through `obj_map!`), because only then will
// MicroPython treat `locals_dict` as immutable, and make the mutable cast safe.
#[allow(unused_mut)]
#[allow(unused_assignments)]
let mut locals_dict = ::core::ptr::null_mut();
@ -164,3 +170,23 @@ macro_rules! obj_type {
}
}};
}
/// Construct an extmod definition.
macro_rules! obj_module {
($globals:expr) => {{
#[allow(unused_unsafe)]
unsafe {
use $crate::micropython::ffi;
ffi::mp_obj_module_t {
base: ffi::mp_obj_base_t {
type_: &ffi::mp_type_module,
},
// 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 `globals` as immutable, and make the mutable cast safe.
globals: $globals as *const _ as *mut _,
}
}
}};
}

@ -10,6 +10,7 @@ pub mod gc;
pub mod iter;
pub mod list;
pub mod map;
pub mod module;
pub mod obj;
pub mod qstr;
pub mod runtime;

@ -0,0 +1,6 @@
use super::ffi;
pub type Module = ffi::mp_obj_module_t;
// SAFETY: We are in a single-threaded environment.
unsafe impl Sync for Module {}
Loading…
Cancel
Save