Giter Site home page Giter Site logo

Comments (8)

clauswilke avatar clauswilke commented on May 22, 2024

I've figured out what the problem is. The code of extendr_macros on crates.io doesn't match the code in the git repo. To show this, I've copied the entire lib.rs file from crates.io below, in the details. You can see that it defines a macro export_function but no macro extendr. I also note that the version of extendr_macros on crates.io (and in the github repo) is listed as 0.1.2, when the version of extendr_api is 0.1.3. Maybe all that is needed to fix this is to update the version of extendr_macros and re-publish on crates.io?

extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{parse_macro_input, parse_quote, ItemFn, FnArg, Expr};
use syn::punctuated::Punctuated;
//use syn::parse::Parse;
use syn::Token;
use quote::{quote, format_ident};

//type CompileError = Box<dyn std::error::Error>;

// All arguments are SEXP for .Call in R.
fn translate_formal(input : &FnArg) -> FnArg {
    match input {
        FnArg::Typed(ref pattype) => {
            let pat = &pattype.pat.as_ref();
            return parse_quote!{ #pat : extendr_api::SEXP };
        },
        _ => ()
    }
    panic!("Exported function argument must be a primitive or Robj.");
}

// Convert SEXP arguments into native types if we can.
fn translate_actual(input : &FnArg) -> Expr {
    match input {
        FnArg::Typed(ref pattype) => {
            let pat = &pattype.pat.as_ref();
            let ty = &pattype.ty.as_ref();
            return parse_quote!{ extendr_api::unwrap_or_throw(from_robj::<#ty>(&new_borrowed(#pat))) };
        },
        _ => ()
    }
    panic!("Exported function argument must be a primitive or Robj.");
}

#[proc_macro_attribute]
/// Generate bindings for a single function.
pub fn export_function(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let func = parse_macro_input!(item as ItemFn);
    let func_name = &func.sig.ident;
    let wrap_name = format_ident!("__wrap__{}", func_name);

    let formal_args : Punctuated<FnArg, Token![,]> = func.sig.inputs.iter()
        .map(|input| {
            translate_formal(input)
        }).collect();

    let actual_args : Punctuated<Expr, Token![,]> = func.sig.inputs.iter()
        .map(|input| {
            translate_actual(input)
        }).collect();

        //println!("export_function func: {:#?}", func);

    let expanded = quote! {
        // #[allow(dead_code)]
        #func

        #[no_mangle]
        pub extern "C" fn #wrap_name(#formal_args) -> extendr_api::SEXP {
            use extendr_api::{from_robj, new_borrowed};
            unsafe {
                extendr_api::Robj::from(#func_name(#actual_args)).get()
            }
        }
    };
    
    //println!("res: {}", expanded);
    TokenStream::from(expanded)
}

from extendr.

clauswilke avatar clauswilke commented on May 22, 2024

Thinking about the right order of doing things, so we end up with fully functioning crates on crates.io: Maybe first merge the libR-sys PR (extendr/libR-sys#7), which will increase the version of libR-sys. Then publish that on crates.io. Then increase the version dependency in extendr, and then increase extendr-{api, macros} versions to 0.1.4.

from extendr.

andy-thomason avatar andy-thomason commented on May 22, 2024

Thanks for your hard work, @clauswilke and others.

from extendr.

andy-thomason avatar andy-thomason commented on May 22, 2024

I've updated some of the documentation and switched the docs to docs.rs.

Please let me know if this work for everyone.

A CI test on Appveyor or Azure would be a useful thing to keep this working.

from extendr.

clauswilke avatar clauswilke commented on May 22, 2024

Sounds good to me.

from extendr.

clauswilke avatar clauswilke commented on May 22, 2024

Just to be sure it doesn't fall through the cracks: extendr-macros needs a version bump also, here:

version = "0.1.2"

and here:
extendr-macros = { path = "../extendr-macros", version="0.1.2" }

from extendr.

andy-thomason avatar andy-thomason commented on May 22, 2024

I've created a PR for this and will publish the crate.

from extendr.

clauswilke avatar clauswilke commented on May 22, 2024

I believe this is fixed now.

from extendr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.