|
| 1 | +// |
| 2 | +// lib.rs |
| 3 | +// |
| 4 | +// Copyright (C) 2025 Posit Software, PBC. All rights reserved. |
| 5 | +// |
| 6 | +// |
| 7 | + |
| 8 | +//! Proc macros for the Ark kernel. |
| 9 | +//! |
| 10 | +//! ## `#[ark::register]` |
| 11 | +//! |
| 12 | +//! Registers a function as an R `.Call` entry point with automatic `Console` |
| 13 | +//! access and panic safety. Composes with `#[harp::register]`. |
| 14 | +//! |
| 15 | +//! ```ignore |
| 16 | +//! #[ark::register] |
| 17 | +//! fn ps_my_function(console: &Console, x: SEXP) -> anyhow::Result<SEXP> { |
| 18 | +//! let dc = console.device_context(); |
| 19 | +//! Ok(harp::r_null()) |
| 20 | +//! } |
| 21 | +//! ``` |
| 22 | +//! |
| 23 | +//! The macro transforms this into: |
| 24 | +//! |
| 25 | +//! ```ignore |
| 26 | +//! #[harp::register] |
| 27 | +//! unsafe extern "C-unwind" fn ps_my_function(x: SEXP) -> anyhow::Result<SEXP> { |
| 28 | +//! crate::console::Console::with(|console| { |
| 29 | +//! let dc = console.device_context(); |
| 30 | +//! Ok(harp::r_null()) |
| 31 | +//! }) |
| 32 | +//! } |
| 33 | +//! ``` |
| 34 | +//! |
| 35 | +//! `harp::register` then adds `r_unwrap()` (Rust error to R error), |
| 36 | +//! `r_sandbox()` (catches R longjumps), and ctor-based routine registration. |
| 37 | +//! |
| 38 | +//! `Console::with()` catches Rust panics (e.g. from `RefCell` borrow |
| 39 | +//! violations) and converts them to `anyhow::Error`, which `r_unwrap()` |
| 40 | +//! surfaces as a clean R error instead of crashing the session. |
| 41 | +//! |
| 42 | +//! The first parameter may be `&Console` (any name, type is matched by |
| 43 | +//! the last path segment). It is stripped from the generated C signature |
| 44 | +//! and injected at runtime. All remaining parameters must be `SEXP`. |
| 45 | +//! |
| 46 | +//! The return type must be `anyhow::Result<SEXP>`. |
| 47 | +
|
| 48 | +use proc_macro::TokenStream; |
| 49 | +use quote::quote; |
| 50 | +use syn::parse_macro_input; |
| 51 | + |
| 52 | +extern crate proc_macro; |
| 53 | + |
| 54 | +#[proc_macro_attribute] |
| 55 | +pub fn register(_attr: TokenStream, item: TokenStream) -> TokenStream { |
| 56 | + let function = parse_macro_input!(item as syn::ItemFn); |
| 57 | + match register_impl(function) { |
| 58 | + Ok(tokens) => tokens.into(), |
| 59 | + Err(err) => err.to_compile_error().into(), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn register_impl(function: syn::ItemFn) -> syn::Result<proc_macro::TokenStream> { |
| 64 | + let span = function.sig.ident.span(); |
| 65 | + |
| 66 | + // Partition parameters: optional leading `&Console` + remaining SEXP args. |
| 67 | + let mut console_ident: Option<syn::Ident> = None; |
| 68 | + let mut sexp_params: Vec<syn::FnArg> = Vec::new(); |
| 69 | + |
| 70 | + for (i, param) in function.sig.inputs.iter().enumerate() { |
| 71 | + let typed = match param { |
| 72 | + syn::FnArg::Typed(t) => t, |
| 73 | + syn::FnArg::Receiver(r) => { |
| 74 | + return Err(syn::Error::new_spanned( |
| 75 | + r, |
| 76 | + "ark::register functions cannot have a `self` parameter", |
| 77 | + )); |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + if i == 0 && is_ref_console(&typed.ty) { |
| 82 | + if let syn::Pat::Ident(pat) = &*typed.pat { |
| 83 | + console_ident = Some(pat.ident.clone()); |
| 84 | + } else { |
| 85 | + console_ident = Some(syn::Ident::new("console", span)); |
| 86 | + } |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if !is_sexp_type(&typed.ty) { |
| 91 | + return Err(syn::Error::new_spanned( |
| 92 | + &typed.ty, |
| 93 | + "ark::register parameters (other than the leading `&Console`) must be `SEXP`", |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + sexp_params.push(param.clone()); |
| 98 | + } |
| 99 | + |
| 100 | + let ident = &function.sig.ident; |
| 101 | + let vis = &function.vis; |
| 102 | + let attrs = &function.attrs; |
| 103 | + let function_block = &function.block; |
| 104 | + |
| 105 | + // Build the body: wrap in `Console::with()` if `&Console` was requested, |
| 106 | + // otherwise just invoke the block directly. |
| 107 | + let body = if let Some(console_name) = console_ident { |
| 108 | + quote! { |
| 109 | + crate::console::Console::with(|#console_name| #function_block) |
| 110 | + } |
| 111 | + } else { |
| 112 | + quote! { |
| 113 | + (|| #function_block)() |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + Ok(quote! { |
| 118 | + #(#attrs)* |
| 119 | + #[harp::register] |
| 120 | + #vis unsafe extern "C-unwind" fn #ident(#(#sexp_params),*) -> anyhow::Result<libr::SEXP> { |
| 121 | + #body |
| 122 | + } |
| 123 | + } |
| 124 | + .into()) |
| 125 | +} |
| 126 | + |
| 127 | +/// Check if a type is `&Console` (matches `&Console` or `&path::to::Console`). |
| 128 | +fn is_ref_console(ty: &syn::Type) -> bool { |
| 129 | + let syn::Type::Reference(ref_ty) = ty else { |
| 130 | + return false; |
| 131 | + }; |
| 132 | + if ref_ty.mutability.is_some() { |
| 133 | + return false; |
| 134 | + } |
| 135 | + match &*ref_ty.elem { |
| 136 | + syn::Type::Path(path) => path |
| 137 | + .path |
| 138 | + .segments |
| 139 | + .last() |
| 140 | + .is_some_and(|seg| seg.ident == "Console"), |
| 141 | + _ => false, |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Check if a type is `SEXP`. |
| 146 | +fn is_sexp_type(ty: &syn::Type) -> bool { |
| 147 | + let syn::Type::Path(path) = ty else { |
| 148 | + return false; |
| 149 | + }; |
| 150 | + path.path |
| 151 | + .segments |
| 152 | + .last() |
| 153 | + .is_some_and(|seg| seg.ident == "SEXP") |
| 154 | +} |
0 commit comments