v0.4 #1

Open
rob wants to merge 7 commits from dev into master
4 changed files with 305 additions and 16 deletions
Showing only changes of commit 160130a29e - Show all commits

View File

@ -1,2 +1,4 @@
#[cfg(feature = "std_web")] #[cfg(feature = "std_web")]
pub mod std_web; pub mod std_web;
#[cfg(feature = "web_sys")]
pub mod web_sys;

280
src/console/web_sys.rs Normal file
View File

@ -0,0 +1,280 @@
#[doc(hidden)]
pub use ::wasm_bindgen;
#[doc(hidden)]
pub use ::web_sys;
// It is necessary to expose these wrapper macros in order to pass the $crate
// meta-variable to the proc macro. This in turn allows the macros to be
// re-exported from third-party crates without breakage.
//
// TODO: a declarative macro to remove the duplication here would be nice.
#[doc(hidden)]
pub use ::weblog_proc_macro::console_assert as __weblog_console_assert;
#[macro_export]
/// Call the browser's `console.assert()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
macro_rules! console_assert {
($($input:tt)*) => {
$crate::__weblog_console_assert! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_clear as __weblog_console_clear;
#[macro_export]
/// Call the browser's `console.clear()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
macro_rules! console_clear {
($($input:tt)*) => {
$crate::__weblog_console_clear! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_count as __weblog_console_count;
#[macro_export]
/// Call the browser's `console.count()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
macro_rules! console_count {
($($input:tt)*) => {
$crate::__weblog_console_count! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_count_reset as __weblog_console_count_reset;
#[macro_export]
/// Call the browser's `console.count_reset()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count_reset)
macro_rules! console_count_reset {
($($input:tt)*) => {
$crate::__weblog_console_count_reset! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_debug as __weblog_console_debug;
#[macro_export]
/// Call the browser's `console.debug()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
macro_rules! console_debug {
($($input:tt)*) => {
$crate::__weblog_console_debug! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_dir as __weblog_console_dir;
#[macro_export]
/// Call the browser's `console.dir()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dir)
macro_rules! console_dir {
($($input:tt)*) => {
$crate::__weblog_console_dir! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_dirxml as __weblog_console_dirxml;
#[macro_export]
/// Call the browser's `console.dirxml()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml)
macro_rules! console_dirxml {
($($input:tt)*) => {
$crate::__weblog_console_dirxml! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_error as __weblog_console_error;
#[macro_export]
/// Call the browser's `console.error()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
macro_rules! console_error {
($($input:tt)*) => {
$crate::__weblog_console_error! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_exception as __weblog_console_exception;
#[macro_export]
/// Call the browser's `console.exception()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/exception)
macro_rules! console_exception {
($($input:tt)*) => {
$crate::__weblog_console_exception! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_info as __weblog_console_info;
#[macro_export]
/// Call the browser's `console.info()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
macro_rules! console_info {
($($input:tt)*) => {
$crate::__weblog_console_info! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_log as __weblog_console_log;
#[macro_export]
/// Call the browser's `console.log()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
macro_rules! console_log {
($($input:tt)*) => {
$crate::__weblog_console_log! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_table as __weblog_console_table;
#[macro_export]
/// Call the browser's `console.table()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/table)
macro_rules! console_table {
($($input:tt)*) => {
$crate::__weblog_console_table! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time as __weblog_console_time;
#[macro_export]
/// Call the browser's `console.time()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
macro_rules! console_time {
($($input:tt)*) => {
$crate::__weblog_console_time! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_end as __weblog_console_time_end;
#[macro_export]
/// Call the browser's `console.time_end()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_end)
macro_rules! console_time_end {
($($input:tt)*) => {
$crate::__weblog_console_time_end! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_log as __weblog_console_time_log;
#[macro_export]
/// Call the browser's `console.time_log()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_log)
macro_rules! console_time_log {
($($input:tt)*) => {
$crate::__weblog_console_time_log! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_stamp as __weblog_console_time_stamp;
#[macro_export]
/// Call the browser's `console.time_stamp()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_stamp)
macro_rules! console_time_stamp {
($($input:tt)*) => {
$crate::__weblog_console_time_stamp! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_trace as __weblog_console_trace;
#[macro_export]
/// Call the browser's `console.trace()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
macro_rules! console_trace {
($($input:tt)*) => {
$crate::__weblog_console_trace! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_warn as __weblog_console_warn;
#[macro_export]
/// Call the browser's `console.warn()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
macro_rules! console_warn {
($($input:tt)*) => {
$crate::__weblog_console_warn! {
#![crate = $crate]
$($input)*
}
};
}

View File

@ -75,16 +75,11 @@
//! weblog = { version = "0.3", default-features = false, features = ["stdweb"] } //! weblog = { version = "0.3", default-features = false, features = ["stdweb"] }
//! ``` //! ```
//! //!
mod console; mod console;
#[cfg(feature = "web_sys")] #[cfg(feature = "web_sys")]
pub use weblog_proc_macro::*; pub use self::console::web_sys::*;
#[cfg(feature = "web_sys")]
pub use ::web_sys;
#[cfg(feature = "web_sys")]
pub use ::wasm_bindgen;
#[cfg(feature = "std_web")] #[cfg(feature = "std_web")]
pub use self::console::std_web::*; pub use self::console::std_web::*;

View File

@ -4,7 +4,7 @@ use quote::quote;
use std::cmp; use std::cmp;
use syn::parse::{Parse, ParseStream}; use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated; use syn::punctuated::Punctuated;
use syn::{parse_macro_input, Expr, Result, Token}; use syn::{bracketed, parse_macro_input, Expr, Result, Token};
// quote_console_func builds and quotes a call to the browser's console API, based on the // quote_console_func builds and quotes a call to the browser's console API, based on the
// provided console function and argument types and the input token stream provider by // provided console function and argument types and the input token stream provider by
@ -24,10 +24,10 @@ pub fn quote_console_func(
mut arg_modes: impl Iterator<Item = ArgMode>, mut arg_modes: impl Iterator<Item = ArgMode>,
input: TokenStream, input: TokenStream,
) -> TokenStream { ) -> TokenStream {
let InArgs(in_args) = parse_macro_input!(input as InArgs); let InArgs(crate_name, in_args) = parse_macro_input!(input as InArgs);
let mut out_args = in_args let mut out_args = in_args
.iter() .iter()
.map(|arg| quote_arg(arg, arg_modes.next().unwrap_or_default())); .map(|arg| quote_arg(&crate_name, arg, arg_modes.next().unwrap_or_default()));
let num_provided = out_args.len(); let num_provided = out_args.len();
let num_fixed = cmp::min(num_provided, params_fixed); let num_fixed = cmp::min(num_provided, params_fixed);
@ -60,16 +60,18 @@ pub fn quote_console_func(
} }
} }
(quote! { ::weblog::web_sys::console::#ident(#args) }).into() (quote! { #crate_name::web_sys::console::#ident(#args) }).into()
} }
fn quote_arg(arg: &Expr, arg_mode: ArgMode) -> TokenStream2 { fn quote_arg(crate_name: &TokenStream2, arg: &Expr, arg_mode: ArgMode) -> TokenStream2 {
match arg_mode { match arg_mode {
ArgMode::PassThrough => { ArgMode::PassThrough => {
quote! { #arg } quote! { #arg }
} }
ArgMode::IntoJsValue => { ArgMode::IntoJsValue => {
quote! { &::std::convert::Into::<::weblog::wasm_bindgen::JsValue>::into(#arg) } quote! {
&::std::convert::Into::<#crate_name::wasm_bindgen::JsValue>::into(#arg)
}
} }
} }
} }
@ -114,11 +116,21 @@ impl Default for ArgMode {
} }
// InArgs is a struct required during parsing of input arguments. // InArgs is a struct required during parsing of input arguments.
struct InArgs(Punctuated<Expr, Token![,]>); struct InArgs(TokenStream2, Punctuated<Expr, Token![,]>);
impl Parse for InArgs { impl Parse for InArgs {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let in_args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?; input.parse::<Token![#]>()?;
Ok(Self(in_args)) input.parse::<Token![!]>()?;
let bracketed;
bracketed!(bracketed in input);
bracketed.parse::<Token![crate]>()?;
bracketed.parse::<Token![ = ]>()?;
let crate_name = bracketed.parse()?;
let args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?;
Ok(Self(crate_name, args))
} }
} }