2020-10-19 21:15:24 +02:00
|
|
|
#![feature(proc_macro_diagnostic)]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use darling::FromMeta;
|
2021-05-12 01:00:43 +02:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use proc_macro2::Span;
|
2020-10-19 21:15:24 +02:00
|
|
|
use quote::{format_ident, quote};
|
2021-05-12 01:00:43 +02:00
|
|
|
use std::iter;
|
2020-10-19 21:15:24 +02:00
|
|
|
use syn::spanned::Spanned;
|
2021-05-12 01:00:43 +02:00
|
|
|
use syn::{parse, Type, Visibility};
|
|
|
|
use syn::{ItemFn, ReturnType};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2021-04-14 09:25:10 +02:00
|
|
|
mod path;
|
|
|
|
|
|
|
|
use path::ModulePrefix;
|
|
|
|
|
2020-10-19 21:15:24 +02:00
|
|
|
#[derive(Debug, FromMeta)]
|
2021-04-14 09:25:10 +02:00
|
|
|
struct TaskArgs {
|
2020-10-19 21:15:24 +02:00
|
|
|
#[darling(default)]
|
|
|
|
pool_size: Option<usize>,
|
2021-02-02 05:14:52 +01:00
|
|
|
#[darling(default)]
|
|
|
|
send: bool,
|
2021-04-14 09:25:10 +02:00
|
|
|
#[darling(default)]
|
|
|
|
embassy_prefix: ModulePrefix,
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn task(args: TokenStream, item: TokenStream) -> TokenStream {
|
2021-02-02 05:14:52 +01:00
|
|
|
let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs);
|
2020-10-19 21:15:24 +02:00
|
|
|
let mut task_fn = syn::parse_macro_input!(item as syn::ItemFn);
|
|
|
|
|
2021-04-14 09:25:10 +02:00
|
|
|
let macro_args = match TaskArgs::from_list(¯o_args) {
|
2020-10-19 21:15:24 +02:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
return TokenStream::from(e.write_errors());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-14 09:25:10 +02:00
|
|
|
let embassy_prefix = macro_args.embassy_prefix.append("embassy");
|
|
|
|
let embassy_path = embassy_prefix.path();
|
|
|
|
|
2021-02-02 05:14:52 +01:00
|
|
|
let pool_size: usize = macro_args.pool_size.unwrap_or(1);
|
2020-10-19 21:15:24 +02:00
|
|
|
|
|
|
|
let mut fail = false;
|
|
|
|
if task_fn.sig.asyncness.is_none() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must be async")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
2021-02-14 01:41:36 +01:00
|
|
|
if !task_fn.sig.generics.params.is_empty() {
|
2020-10-19 21:15:24 +02:00
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must not be generic")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
if pool_size < 1 {
|
2021-05-12 01:00:43 +02:00
|
|
|
return parse::Error::new(Span::call_site(), "pool_size must be 1 or greater")
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut arg_names: syn::punctuated::Punctuated<syn::Ident, syn::Token![,]> =
|
|
|
|
syn::punctuated::Punctuated::new();
|
2020-12-31 15:38:10 +01:00
|
|
|
let mut args = task_fn.sig.inputs.clone();
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2020-12-31 15:38:10 +01:00
|
|
|
for arg in args.iter_mut() {
|
2020-10-19 21:15:24 +02:00
|
|
|
match arg {
|
|
|
|
syn::FnArg::Receiver(_) => {
|
|
|
|
arg.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must not have receiver arguments")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
2020-12-31 15:38:10 +01:00
|
|
|
syn::FnArg::Typed(t) => match t.pat.as_mut() {
|
|
|
|
syn::Pat::Ident(i) => {
|
|
|
|
arg_names.push(i.ident.clone());
|
|
|
|
i.mutability = None;
|
|
|
|
}
|
2020-10-19 21:15:24 +02:00
|
|
|
_ => {
|
|
|
|
arg.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("pattern matching in task arguments is not yet supporteds")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail {
|
|
|
|
return TokenStream::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = task_fn.sig.ident.clone();
|
|
|
|
|
|
|
|
let visibility = &task_fn.vis;
|
2020-10-31 16:36:09 +01:00
|
|
|
task_fn.sig.ident = format_ident!("task");
|
2021-02-02 05:14:52 +01:00
|
|
|
let impl_ty = if macro_args.send {
|
|
|
|
quote!(impl ::core::future::Future + Send + 'static)
|
|
|
|
} else {
|
|
|
|
quote!(impl ::core::future::Future + 'static)
|
|
|
|
};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2021-08-05 07:35:50 +02:00
|
|
|
let attrs = &task_fn.attrs;
|
|
|
|
|
2020-10-19 21:15:24 +02:00
|
|
|
let result = quote! {
|
2021-08-05 07:35:50 +02:00
|
|
|
#(#attrs)*
|
2021-04-14 09:25:10 +02:00
|
|
|
#visibility fn #name(#args) -> #embassy_path::executor::SpawnToken<#impl_ty> {
|
2021-08-26 00:20:52 +02:00
|
|
|
use #embassy_path::executor::raw::TaskStorage;
|
2020-10-31 16:36:09 +01:00
|
|
|
#task_fn
|
2021-02-02 05:14:52 +01:00
|
|
|
type F = #impl_ty;
|
2021-10-18 00:55:43 +02:00
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
2021-08-26 00:20:52 +02:00
|
|
|
const NEW_TASK: TaskStorage<F> = TaskStorage::new();
|
|
|
|
static POOL: [TaskStorage<F>; #pool_size] = [NEW_TASK; #pool_size];
|
|
|
|
unsafe { TaskStorage::spawn_pool(&POOL, move || task(#arg_names)) }
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|
2020-12-29 00:05:52 +01:00
|
|
|
|
2021-05-12 01:00:43 +02:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let mut f: ItemFn = syn::parse(input).expect("`#[interrupt]` must be applied to a function");
|
|
|
|
|
|
|
|
if !args.is_empty() {
|
|
|
|
return parse::Error::new(Span::call_site(), "This attribute accepts no arguments")
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
let fspan = f.span();
|
|
|
|
let ident = f.sig.ident.clone();
|
|
|
|
let ident_s = ident.to_string();
|
|
|
|
|
|
|
|
// XXX should we blacklist other attributes?
|
|
|
|
|
|
|
|
let valid_signature = f.sig.constness.is_none()
|
|
|
|
&& f.vis == Visibility::Inherited
|
|
|
|
&& f.sig.abi.is_none()
|
|
|
|
&& f.sig.inputs.is_empty()
|
|
|
|
&& f.sig.generics.params.is_empty()
|
|
|
|
&& f.sig.generics.where_clause.is_none()
|
|
|
|
&& f.sig.variadic.is_none()
|
|
|
|
&& match f.sig.output {
|
|
|
|
ReturnType::Default => true,
|
|
|
|
ReturnType::Type(_, ref ty) => match **ty {
|
|
|
|
Type::Tuple(ref tuple) => tuple.elems.is_empty(),
|
|
|
|
Type::Never(..) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if !valid_signature {
|
|
|
|
return parse::Error::new(
|
|
|
|
fspan,
|
|
|
|
"`#[interrupt]` handlers must have signature `[unsafe] fn() [-> !]`",
|
|
|
|
)
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
f.block.stmts = iter::once(
|
|
|
|
syn::parse2(quote! {{
|
|
|
|
// Check that this interrupt actually exists
|
|
|
|
let __irq_exists_check: interrupt::#ident;
|
|
|
|
}})
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.chain(f.block.stmts)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[export_name = #ident_s]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#f
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:05:52 +01:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn interrupt_declare(item: TokenStream) -> TokenStream {
|
|
|
|
let name = syn::parse_macro_input!(item as syn::Ident);
|
|
|
|
let name = format_ident!("{}", name);
|
2021-02-26 01:55:27 +01:00
|
|
|
let name_interrupt = format_ident!("{}", name);
|
2020-12-29 00:05:52 +01:00
|
|
|
let name_handler = format!("__EMBASSY_{}_HANDLER", name);
|
|
|
|
|
|
|
|
let result = quote! {
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub struct #name_interrupt(());
|
2021-05-11 03:04:59 +02:00
|
|
|
unsafe impl ::embassy::interrupt::Interrupt for #name_interrupt {
|
2021-02-26 01:55:27 +01:00
|
|
|
type Priority = crate::interrupt::Priority;
|
2021-02-14 23:26:50 +01:00
|
|
|
fn number(&self) -> u16 {
|
|
|
|
use cortex_m::interrupt::InterruptNumber;
|
2021-05-01 03:05:21 +02:00
|
|
|
let irq = InterruptEnum::#name;
|
2021-02-14 23:26:50 +01:00
|
|
|
irq.number() as u16
|
2020-12-29 00:05:52 +01:00
|
|
|
}
|
2021-02-02 05:14:52 +01:00
|
|
|
unsafe fn steal() -> Self {
|
|
|
|
Self(())
|
|
|
|
}
|
2021-01-04 22:25:39 +01:00
|
|
|
unsafe fn __handler(&self) -> &'static ::embassy::interrupt::Handler {
|
2020-12-29 00:05:52 +01:00
|
|
|
#[export_name = #name_handler]
|
2021-01-04 22:25:39 +01:00
|
|
|
static HANDLER: ::embassy::interrupt::Handler = ::embassy::interrupt::Handler::new();
|
2020-12-29 00:05:52 +01:00
|
|
|
&HANDLER
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 04:02:56 +01:00
|
|
|
|
2021-05-19 23:21:31 +02:00
|
|
|
unsafe impl ::embassy::util::Unborrow for #name_interrupt {
|
2021-03-19 04:02:56 +01:00
|
|
|
type Target = #name_interrupt;
|
|
|
|
unsafe fn unborrow(self) -> #name_interrupt {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 00:05:52 +01:00
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|
2021-11-16 12:13:43 +01:00
|
|
|
/// # interrupt_take procedural macro
|
|
|
|
///
|
|
|
|
/// core::panic! is used as a default way to panic in this macro as there is no sensible way of enabling/disabling defmt for macro generation.
|
|
|
|
/// We are aware that this brings bloat in the form of core::fmt, but the bloat is already included with e.g. array indexing panics.
|
|
|
|
/// To get rid of this bloat, use the compiler flags `-Zbuild-std=core -Zbuild-std-features=panic_immediate_abort`.
|
2020-12-29 00:05:52 +01:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn interrupt_take(item: TokenStream) -> TokenStream {
|
|
|
|
let name = syn::parse_macro_input!(item as syn::Ident);
|
|
|
|
let name = format!("{}", name);
|
2021-02-26 01:55:27 +01:00
|
|
|
let name_interrupt = format_ident!("{}", name);
|
2020-12-29 00:05:52 +01:00
|
|
|
let name_handler = format!("__EMBASSY_{}_HANDLER", name);
|
|
|
|
|
|
|
|
let result = quote! {
|
|
|
|
{
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[export_name = #name]
|
|
|
|
pub unsafe extern "C" fn trampoline() {
|
|
|
|
extern "C" {
|
|
|
|
#[link_name = #name_handler]
|
2021-01-04 22:25:39 +01:00
|
|
|
static HANDLER: ::embassy::interrupt::Handler;
|
2020-12-29 00:05:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-27 01:43:38 +01:00
|
|
|
let func = HANDLER.func.load(::embassy::export::atomic::Ordering::Relaxed);
|
|
|
|
let ctx = HANDLER.ctx.load(::embassy::export::atomic::Ordering::Relaxed);
|
|
|
|
let func: fn(*mut ()) = ::core::mem::transmute(func);
|
|
|
|
func(ctx)
|
2020-12-29 00:05:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 01:47:45 +01:00
|
|
|
static TAKEN: ::embassy::export::atomic::AtomicBool = ::embassy::export::atomic::AtomicBool::new(false);
|
2020-12-29 00:05:52 +01:00
|
|
|
|
2021-03-17 01:47:45 +01:00
|
|
|
if TAKEN.compare_exchange(false, true, ::embassy::export::atomic::Ordering::AcqRel, ::embassy::export::atomic::Ordering::Acquire).is_err() {
|
2021-11-16 12:13:43 +01:00
|
|
|
core::panic!("IRQ Already taken");
|
2020-12-29 00:05:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let irq: interrupt::#name_interrupt = unsafe { ::core::mem::transmute(()) };
|
|
|
|
irq
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|
2021-03-27 23:27:39 +01:00
|
|
|
|
2021-05-25 13:30:42 +02:00
|
|
|
#[cfg(feature = "stm32")]
|
|
|
|
#[path = "chip/stm32.rs"]
|
|
|
|
mod chip;
|
|
|
|
|
2021-03-29 02:47:10 +02:00
|
|
|
#[cfg(feature = "nrf")]
|
|
|
|
#[path = "chip/nrf.rs"]
|
|
|
|
mod chip;
|
2021-03-27 23:27:39 +01:00
|
|
|
|
2021-03-29 04:11:32 +02:00
|
|
|
#[cfg(feature = "rp")]
|
|
|
|
#[path = "chip/rp.rs"]
|
|
|
|
mod chip;
|
|
|
|
|
2021-11-21 09:33:18 +01:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "nrf",
|
|
|
|
feature = "rp",
|
|
|
|
feature = "stm32",
|
|
|
|
feature = "wasm",
|
|
|
|
feature = "std"
|
|
|
|
))]
|
2021-04-20 16:33:47 +02:00
|
|
|
#[derive(Debug, FromMeta)]
|
|
|
|
struct MainArgs {
|
|
|
|
#[darling(default)]
|
|
|
|
embassy_prefix: ModulePrefix,
|
|
|
|
|
2021-12-16 11:34:20 +01:00
|
|
|
#[allow(unused)]
|
2021-04-20 16:33:47 +02:00
|
|
|
#[darling(default)]
|
|
|
|
config: Option<syn::LitStr>,
|
2021-04-14 09:25:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 13:30:42 +02:00
|
|
|
#[cfg(any(feature = "nrf", feature = "rp", feature = "stm32"))]
|
2021-03-27 23:27:39 +01:00
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs);
|
2021-03-29 21:34:15 +02:00
|
|
|
let task_fn = syn::parse_macro_input!(item as syn::ItemFn);
|
2021-03-27 23:27:39 +01:00
|
|
|
|
2021-04-20 16:33:47 +02:00
|
|
|
let macro_args = match MainArgs::from_list(¯o_args) {
|
2021-03-27 23:27:39 +01:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
return TokenStream::from(e.write_errors());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut fail = false;
|
|
|
|
if task_fn.sig.asyncness.is_none() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must be async")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
if !task_fn.sig.generics.params.is_empty() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("main function must not be generic")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:34:15 +02:00
|
|
|
let args = task_fn.sig.inputs.clone();
|
2021-03-27 23:27:39 +01:00
|
|
|
|
2021-05-12 01:57:01 +02:00
|
|
|
if args.len() != 2 {
|
2021-03-27 23:27:39 +01:00
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
2021-05-12 01:57:01 +02:00
|
|
|
.error("main function must have 2 arguments")
|
2021-03-27 23:27:39 +01:00
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail {
|
|
|
|
return TokenStream::new();
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:33:47 +02:00
|
|
|
let embassy_prefix = macro_args.embassy_prefix;
|
|
|
|
let embassy_prefix_lit = embassy_prefix.literal();
|
|
|
|
let embassy_path = embassy_prefix.append("embassy").path();
|
|
|
|
let task_fn_body = task_fn.block;
|
|
|
|
|
|
|
|
let config = macro_args
|
|
|
|
.config
|
|
|
|
.map(|s| s.parse::<syn::Expr>().unwrap())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
syn::Expr::Verbatim(quote! {
|
|
|
|
Default::default()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let chip_setup = chip::generate(&embassy_prefix, config);
|
2021-03-27 23:27:39 +01:00
|
|
|
|
|
|
|
let result = quote! {
|
2021-04-14 09:25:10 +02:00
|
|
|
#[#embassy_path::task(embassy_prefix = #embassy_prefix_lit)]
|
2021-03-27 23:27:39 +01:00
|
|
|
async fn __embassy_main(#args) {
|
|
|
|
#task_fn_body
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cortex_m_rt::entry]
|
|
|
|
fn main() -> ! {
|
2021-03-29 02:47:10 +02:00
|
|
|
unsafe fn make_static<T>(t: &mut T) -> &'static mut T {
|
|
|
|
::core::mem::transmute(t)
|
|
|
|
}
|
2021-03-27 23:27:39 +01:00
|
|
|
|
2022-01-05 13:30:46 +01:00
|
|
|
#chip_setup
|
2021-04-20 16:33:47 +02:00
|
|
|
|
2022-01-05 13:30:46 +01:00
|
|
|
let mut executor = #embassy_path::executor::Executor::new();
|
2021-03-29 02:47:10 +02:00
|
|
|
let executor = unsafe { make_static(&mut executor) };
|
2021-03-29 04:11:32 +02:00
|
|
|
|
2021-03-27 23:27:39 +01:00
|
|
|
executor.run(|spawner| {
|
2021-07-31 18:34:07 +02:00
|
|
|
spawner.must_spawn(__embassy_main(spawner, p));
|
2021-03-27 23:27:39 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|
2021-04-12 10:41:21 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
#[proc_macro_attribute]
|
2021-04-14 09:25:10 +02:00
|
|
|
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs);
|
2021-04-12 10:41:21 +02:00
|
|
|
let task_fn = syn::parse_macro_input!(item as syn::ItemFn);
|
|
|
|
|
2021-04-20 16:33:47 +02:00
|
|
|
let macro_args = match MainArgs::from_list(¯o_args) {
|
2021-04-14 09:25:10 +02:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
return TokenStream::from(e.write_errors());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let embassy_path = macro_args.embassy_prefix.append("embassy");
|
|
|
|
|
2021-04-12 10:41:21 +02:00
|
|
|
let mut fail = false;
|
|
|
|
if task_fn.sig.asyncness.is_none() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must be async")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
if !task_fn.sig.generics.params.is_empty() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("main function must not be generic")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = task_fn.sig.inputs.clone();
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("main function must have one argument")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail {
|
|
|
|
return TokenStream::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
let task_fn_body = task_fn.block.clone();
|
|
|
|
|
2021-04-14 09:25:10 +02:00
|
|
|
let embassy_path = embassy_path.path();
|
|
|
|
let embassy_prefix_lit = macro_args.embassy_prefix.literal();
|
|
|
|
|
2021-04-12 10:41:21 +02:00
|
|
|
let result = quote! {
|
2021-04-14 09:25:10 +02:00
|
|
|
#[#embassy_path::task(embassy_prefix = #embassy_prefix_lit)]
|
2021-04-12 10:41:21 +02:00
|
|
|
async fn __embassy_main(#args) {
|
|
|
|
#task_fn_body
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> ! {
|
|
|
|
unsafe fn make_static<T>(t: &mut T) -> &'static mut T {
|
|
|
|
::core::mem::transmute(t)
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:45:20 +02:00
|
|
|
let mut executor = #embassy_path::executor::Executor::new();
|
2021-04-12 10:41:21 +02:00
|
|
|
let executor = unsafe { make_static(&mut executor) };
|
|
|
|
|
|
|
|
executor.run(|spawner| {
|
|
|
|
spawner.spawn(__embassy_main(spawner)).unwrap();
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|
2021-09-13 14:35:40 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "wasm")]
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs);
|
|
|
|
let task_fn = syn::parse_macro_input!(item as syn::ItemFn);
|
|
|
|
|
|
|
|
let macro_args = match MainArgs::from_list(¯o_args) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
return TokenStream::from(e.write_errors());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let embassy_path = macro_args.embassy_prefix.append("embassy");
|
|
|
|
|
|
|
|
let mut fail = false;
|
|
|
|
if task_fn.sig.asyncness.is_none() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("task functions must be async")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
if !task_fn.sig.generics.params.is_empty() {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("main function must not be generic")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = task_fn.sig.inputs.clone();
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
task_fn
|
|
|
|
.sig
|
|
|
|
.span()
|
|
|
|
.unwrap()
|
|
|
|
.error("main function must have one argument")
|
|
|
|
.emit();
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail {
|
|
|
|
return TokenStream::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
let task_fn_body = task_fn.block.clone();
|
|
|
|
|
|
|
|
let embassy_path = embassy_path.path();
|
|
|
|
let embassy_prefix_lit = macro_args.embassy_prefix.literal();
|
|
|
|
|
|
|
|
let result = quote! {
|
|
|
|
#[#embassy_path::task(embassy_prefix = #embassy_prefix_lit)]
|
|
|
|
async fn __embassy_main(#args) {
|
|
|
|
#task_fn_body
|
|
|
|
}
|
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen(start)]
|
|
|
|
pub fn main() -> Result<(), JsValue> {
|
|
|
|
static EXECUTOR: #embassy_path::util::Forever<#embassy_path::executor::Executor> = #embassy_path::util::Forever::new();
|
|
|
|
let executor = EXECUTOR.put(#embassy_path::executor::Executor::new());
|
|
|
|
|
|
|
|
executor.start(|spawner| {
|
|
|
|
spawner.spawn(__embassy_main(spawner)).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.into()
|
|
|
|
}
|