Skip to content

Optimize the ABI boundary of wasm-compiled proc macros #14

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

Merged
merged 5 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "watt"
version = "0.1.3"
version = "0.2.0"
authors = ["David Tolnay <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "Runtime for executing Rust procedural macros compiled as WebAssembly."
Expand All @@ -18,4 +18,7 @@ include = [
]

[workspace]
members = ["demo/caller", "demo/wa", "runtime/tests"]
members = ["demo/caller", "demo/wa", "runtime/tests", "proc-macro"]

[patch.crates-io]
watt = { path = "." }
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WebAssembly.

```toml
[dependencies]
watt = "0.1"
watt = "0.2"
```

*Compiler support: requires rustc 1.35+*
Expand Down Expand Up @@ -69,20 +69,18 @@ everything is analogous to what will be shown here for `#[proc_macro]`.

When your macro is ready, there are just a few changes we need to make to the
signature and the Cargo.toml. In your lib.rs, change each of your macro entry
points to a no\_mangle extern "C" function, and change the TokenStream in the
signature from proc\_macro to proc\_macro2. Finally, add a call to
`proc_macro2::set_wasm_panic_hook()` at the top of your macro to ensure panics
get printed to the console; this is optional but helpful!
points to use the attribute from the `proc_macro2` crate instead of the bare
version of the macro. For example use `#[proc_macro2::proc_macro]` instead of
`#[proc_macro]`. Next change the TokenStream in the signature from proc\_macro
to proc\_macro2.

It will look like:

```rust
use proc_macro2::TokenStream;

#[no_mangle]
pub extern "C" fn my_macro(input: TokenStream) -> TokenStream {
proc_macro2::set_wasm_panic_hook();

#[proc_macro2::proc_macro]
pub fn my_macro(input: TokenStream) -> TokenStream {
/* same as before */
}
```
Expand All @@ -98,7 +96,7 @@ change it instead to say:

```toml
[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[patch.crates-io]
proc-macro2 = { git = "https://github.com/dtolnay/watt" }
Expand All @@ -118,7 +116,7 @@ bytes into the Watt runtime. In a new Cargo.toml, put:
proc-macro = true

[dependencies]
watt = "0.1"
watt = "0.2"
```

And in its src/lib.rs put:
Expand Down
2 changes: 1 addition & 1 deletion demo/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[dependencies]
proc-macro2 = "1.0"
Expand Down
6 changes: 2 additions & 4 deletions demo/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;

#[no_mangle]
pub extern "C" fn demo(input: TokenStream) -> TokenStream {
proc_macro2::set_wasm_panic_hook();

#[proc_macro2::proc_macro_derive(Demo)]
pub fn demo(input: TokenStream) -> TokenStream {
let input: DeriveInput = match syn::parse2(input) {
Ok(input) => input,
Err(err) => return err.to_compile_error(),
Expand Down
2 changes: 1 addition & 1 deletion demo/wa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ publish = false
proc-macro = true

[dependencies]
watt = { path = "../.." }
watt = "0.2"
3 changes: 3 additions & 0 deletions proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ publish = false
[features]
proc-macro = []
default = ["proc-macro"]

[dependencies]
proc-macro2-macros = { path = 'macros' }
8 changes: 8 additions & 0 deletions proc-macro/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "proc-macro2-macros"
version = "0.1.0"
authors = ["Alex Crichton <[email protected]>"]
edition = "2018"

[lib]
proc-macro = true
56 changes: 56 additions & 0 deletions proc-macro/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use std::str::FromStr;

#[proc_macro_attribute]
pub fn expand_macro(_attr: TokenStream, mut tokens: TokenStream) -> TokenStream {
let name = tokens.clone().into_iter().nth(2).unwrap();
let source = format!(
"
#[export_name = \"{name}\"]
pub extern \"C\" fn _macro_{name}(a: u32) -> u32 {{
let a = proc_macro2::__internal::raw_to_token_stream(a);
proc_macro2::__internal::token_stream_into_raw({name}(a))
}}
",
name = name
);
tokens.extend(TokenStream::from_str(&source).unwrap());
return tokens;
}

#[proc_macro_attribute]
pub fn expand_attribute(_attr: TokenStream, mut tokens: TokenStream) -> TokenStream {
let name = tokens.clone().into_iter().nth(2).unwrap();
let source = format!(
"
#[export_name = \"{name}\"]
pub extern \"C\" fn _macro_{name}(a: u32, b: u32) -> u32 {{
let a = proc_macro2::__internal::raw_to_token_stream(a);
let b = proc_macro2::__internal::raw_to_token_stream(b);
proc_macro2::__internal::token_stream_into_raw({name}(a, b))
}}
",
name = name
);
tokens.extend(TokenStream::from_str(&source).unwrap());
return tokens;
}

#[proc_macro_attribute]
pub fn expand_derive(_attr: TokenStream, mut tokens: TokenStream) -> TokenStream {
let name = tokens.clone().into_iter().nth(2).unwrap();
let source = format!(
"
#[export_name = \"{name}\"]
pub extern \"C\" fn _macro_{name}(a: u32) -> u32 {{
let a = proc_macro2::__internal::raw_to_token_stream(a);
proc_macro2::__internal::token_stream_into_raw({name}(a))
}}
",
name = name
);
tokens.extend(TokenStream::from_str(&source).unwrap());
return tokens;
}
122 changes: 122 additions & 0 deletions proc-macro/src/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use super::*;
use std::str;

pub fn decode(mut buf: &[u8]) -> TokenStream {
let ret = TokenStream::decode(&mut buf);
assert!(buf.is_empty());
return ret;
}

trait Decode {
fn decode(data: &mut &[u8]) -> Self;
}

fn byte(data: &mut &[u8]) -> u8 {
let ret = data[0];
*data = &data[1..];
ret
}

fn str<'a>(data: &mut &'a [u8]) -> &'a str {
let len = u32::decode(data) as usize;
let ret = str::from_utf8(&data[..len]).unwrap();
*data = &data[len..];
return ret;
}

impl Decode for TokenStream {
fn decode(data: &mut &[u8]) -> Self {
let mut tokens = Vec::new();
loop {
match byte(data) {
0 => break,
1 => tokens.push(TokenTree::Group(Group::decode(data))),
2 => tokens.push(TokenTree::Ident(Ident::decode(data))),
3 => tokens.push(TokenTree::Punct(Punct::decode(data))),
_ => tokens.push(TokenTree::Literal(Literal::decode(data))),
}
}
TokenStream { inner: tokens }
}
}

impl Decode for Group {
fn decode(data: &mut &[u8]) -> Self {
let delimiter = Delimiter::decode(data);
let span = Span::decode(data);
let stream = TokenStream::decode(data);
let mut ret = Group::new(delimiter, stream);
ret.set_span(span);
return ret;
}
}

impl Decode for Delimiter {
fn decode(data: &mut &[u8]) -> Self {
match byte(data) {
0 => Delimiter::Parenthesis,
1 => Delimiter::Brace,
2 => Delimiter::Bracket,
_ => Delimiter::None,
}
}
}

impl Decode for Span {
fn decode(data: &mut &[u8]) -> Self {
Span {
handle: u32::decode(data),
}
}
}

impl Decode for u32 {
fn decode(data: &mut &[u8]) -> Self {
let ret = ((data[0] as u32) << 0)
| ((data[1] as u32) << 8)
| ((data[2] as u32) << 16)
| ((data[3] as u32) << 24);
*data = &data[4..];
return ret;
}
}

impl Decode for Ident {
fn decode(data: &mut &[u8]) -> Self {
let span = Span::decode(data);
let name = str(data);
Ident::new(name, span)
}
}

impl Decode for Punct {
fn decode(data: &mut &[u8]) -> Self {
let mut p = Punct::new(
char::from_u32(u32::decode(data)).unwrap(),
Spacing::decode(data),
);
p.set_span(Span::decode(data));
return p;
}
}

impl Decode for Spacing {
fn decode(data: &mut &[u8]) -> Self {
match byte(data) {
0 => Spacing::Alone,
_ => Spacing::Joint,
}
}
}

impl Decode for Literal {
fn decode(data: &mut &[u8]) -> Self {
Literal {
span: Span::decode(data),
kind: match byte(data) {
0 => LiteralKind::Local(str(data).to_string()),
_ => LiteralKind::Watt(handle::Literal(u32::decode(data))),
},
}
}
}
Loading