|
|
|
@ -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,36 @@ macro_rules! obj_type {
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Construct an extmod definition.
|
|
|
|
|
macro_rules! obj_module {
|
|
|
|
|
($($key:expr => $val:expr),*) => ({
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
|
unsafe {
|
|
|
|
|
use $crate::micropython::ffi;
|
|
|
|
|
|
|
|
|
|
static DICT: ffi::mp_obj_dict_t = ffi::mp_obj_dict_t {
|
|
|
|
|
base: ffi::mp_obj_base_t {
|
|
|
|
|
/// SAFETY: Reasonable to assume the pointer stays valid.
|
|
|
|
|
type_: unsafe { &ffi::mp_type_dict },
|
|
|
|
|
},
|
|
|
|
|
map: Map::from_fixed_static(&[
|
|
|
|
|
$(
|
|
|
|
|
Map::at($key, $val),
|
|
|
|
|
)*
|
|
|
|
|
])
|
|
|
|
|
};
|
|
|
|
|
ffi::mp_obj_module_t {
|
|
|
|
|
base: ffi::mp_obj_base_t {
|
|
|
|
|
type_: &ffi::mp_type_module,
|
|
|
|
|
},
|
|
|
|
|
// This is safe only because we are passing in a static dict with fixed `Map`
|
|
|
|
|
// (created by `Map::from_fixed_static()`). Only then will MicroPython treat
|
|
|
|
|
// `globals` as immutable, making the mutable cast safe.
|
|
|
|
|
globals: &DICT as *const _ as *mut _,
|
|
|
|
|
}
|
|
|
|
|
}});
|
|
|
|
|
($($key:expr => $val:expr),* ,) => ({
|
|
|
|
|
obj_module!($($key => $val),*)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|