-
Notifications
You must be signed in to change notification settings - Fork 375
Add #[entry_point] macro attribute to generate entry points #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
0b1a0d4
1a51800
7a39f70
549f482
8e78f35
3a9a3a9
2388e20
5d96ee2
81529d6
88951ff
2c69c2f
89f3593
45a0765
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "cosmwasm-derive" | ||
version = "0.13.0" | ||
authors = ["Simon Warta <[email protected]>"] | ||
edition = "2018" | ||
description = "Standard library for Wasm based smart contracts on Cosmos blockchains" | ||
repository = "https://github.com/CosmWasm/cosmwasm/tree/master/packages/std" | ||
license = "Apache-2.0" | ||
readme = "README.md" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[features] | ||
default = [] | ||
|
||
[dependencies] | ||
syn = { version = "1.0", features = ["full"] } | ||
|
||
[dev-dependencies] |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||
#[macro_use] | ||||||||||||||||||||||||||||||||||||||||||
extern crate syn; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
use proc_macro::TokenStream; | ||||||||||||||||||||||||||||||||||||||||||
use std::str::FromStr; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
#[proc_macro_attribute] | ||||||||||||||||||||||||||||||||||||||||||
pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream { | ||||||||||||||||||||||||||||||||||||||||||
let cloned = item.clone(); | ||||||||||||||||||||||||||||||||||||||||||
let function = parse_macro_input!(cloned as syn::ItemFn); | ||||||||||||||||||||||||||||||||||||||||||
let name = function.sig.ident.to_string(); | ||||||||||||||||||||||||||||||||||||||||||
// The first argument is `deps`, the rest is region pointers | ||||||||||||||||||||||||||||||||||||||||||
webmaster128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
let args = function.sig.inputs.len() - 1; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// E.g. "ptr0: u32, ptr1: u32, ptr2: u32," | ||||||||||||||||||||||||||||||||||||||||||
let typed_ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}: u32,", acc, i)); | ||||||||||||||||||||||||||||||||||||||||||
// E.g. "ptr0, ptr1, ptr2," | ||||||||||||||||||||||||||||||||||||||||||
let ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{},", acc, i)); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well spotted. I did not see this because the code is automatically formatted when using cargo expand. |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
let new_code = format!( | ||||||||||||||||||||||||||||||||||||||||||
r##" | ||||||||||||||||||||||||||||||||||||||||||
#[cfg(target_arch = "wasm32")] | ||||||||||||||||||||||||||||||||||||||||||
mod __wasm_export_{name} {{ // new module to avoid conflict of function name | ||||||||||||||||||||||||||||||||||||||||||
#[no_mangle] | ||||||||||||||||||||||||||||||||||||||||||
extern "C" fn {name}({typed_ptrs}) -> u32 {{ | ||||||||||||||||||||||||||||||||||||||||||
cosmwasm_std::do_{name}(&super::{name}, {ptrs}) | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at cosmwasm/packages/std/src/exports.rs Lines 96 to 115 in b38e790
We have the concrete type info from the function we are decorating, the rest is mostly boilerplate. (I would do that as a separate PR however) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jupp, much to explore from here. I also though about Multi-value regions in #602, such that we can have always have one pointer input and one pointer output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that would actually make the auto-generation harder, as you are accepting a variable number of input slices and then mapping them to a fixed number of typed variables. But yeah, plenty of room to explore here :) |
||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||
"##, | ||||||||||||||||||||||||||||||||||||||||||
name = name, | ||||||||||||||||||||||||||||||||||||||||||
typed_ptrs = typed_ptrs, | ||||||||||||||||||||||||||||||||||||||||||
ptrs = ptrs | ||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||
let entry = TokenStream::from_str(&new_code).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||
item.extend(entry); | ||||||||||||||||||||||||||||||||||||||||||
item | ||||||||||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.