From 37dbc68bf5b8a67a125adb3c4ac82f55312e67fc Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 12 Jan 2022 08:12:28 +0800 Subject: [PATCH] Use target_family for wasm. --- Cargo.toml | 5 +--- src/api/deprecated.rs | 6 ++-- src/api/files.rs | 3 +- src/engine.rs | 58 +++++++++++++----------------------- src/func/builtin.rs | 3 +- src/lib.rs | 12 ++++---- src/module/resolvers/file.rs | 3 +- src/module/resolvers/mod.rs | 3 +- src/packages/arithmetic.rs | 12 ++++---- src/packages/iter_basic.rs | 8 ++--- src/packages/logic.rs | 8 ++--- src/packages/math_basic.rs | 16 +++++----- src/packages/string_basic.rs | 4 +-- src/packages/time_basic.rs | 5 ++-- src/parser.rs | 18 ++++++++++- src/types/dynamic.rs | 14 ++++----- src/types/error.rs | 2 +- tests/time.rs | 3 +- 18 files changed, 82 insertions(+), 101 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d2b2cd1..006c67bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,10 +97,7 @@ default-features = false features = ["maths"] optional = true -[target.'cfg(target_arch = "wasm32")'.dependencies] -instant = { version = "0.1.10" } # WASM implementation of std::time::Instant - -[target.'cfg(target_arch = "wasm64")'.dependencies] +[target.'cfg(target_family = "wasm")'.dependencies] instant = { version = "0.1.10" } # WASM implementation of std::time::Instant [package.metadata.docs.rs] diff --git a/src/api/deprecated.rs b/src/api/deprecated.rs index 166daf4f..dd8ebd7a 100644 --- a/src/api/deprecated.rs +++ b/src/api/deprecated.rs @@ -20,8 +20,7 @@ impl Engine { /// This method will be removed in the next major version. #[deprecated(since = "1.1.0", note = "use `run_file` instead")] #[cfg(not(feature = "no_std"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] #[inline(always)] pub fn consume_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> { self.run_file(path) @@ -39,8 +38,7 @@ impl Engine { /// This method will be removed in the next major version. #[deprecated(since = "1.1.0", note = "use `run_file_with_scope` instead")] #[cfg(not(feature = "no_std"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] #[inline(always)] pub fn consume_file_with_scope( &self, diff --git a/src/api/files.rs b/src/api/files.rs index 82267f01..8c058f27 100644 --- a/src/api/files.rs +++ b/src/api/files.rs @@ -1,7 +1,6 @@ //! Module that defines the public file-based API of [`Engine`]. #![cfg(not(feature = "no_std"))] -#![cfg(not(target_arch = "wasm32"))] -#![cfg(not(target_arch = "wasm64"))] +#![cfg(not(target_family = "wasm"))] use crate::types::dynamic::Variant; use crate::{Engine, RhaiResultOf, Scope, AST, ERR}; diff --git a/src/engine.rs b/src/engine.rs index 0487f652..8bbe3046 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -203,40 +203,6 @@ pub fn make_setter(id: &str) -> Identifier { buf } -/// Is this function an anonymous function? -#[cfg(not(feature = "no_function"))] -#[inline(always)] -#[must_use] -pub fn is_anonymous_fn(fn_name: &str) -> bool { - fn_name.starts_with(FN_ANONYMOUS) -} - -/// Print to `stdout` -#[inline] -#[allow(unused_variables)] -fn print_to_stdout(s: &str) { - #[cfg(not(feature = "no_std"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] - println!("{}", s); -} - -/// Debug to `stdout` -#[inline] -#[allow(unused_variables)] -fn debug_to_stdout(s: &str, source: Option<&str>, pos: Position) { - #[cfg(not(feature = "no_std"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] - if let Some(source) = source { - println!("{}{:?} | {}", source, pos, s); - } else if pos.is_none() { - println!("{}", s); - } else { - println!("{:?} | {}", pos, s); - } -} - impl Engine { /// Create a new [`Engine`]. #[inline] @@ -247,16 +213,32 @@ impl Engine { #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] { engine.module_resolver = Some(Box::new(crate::module::resolvers::FileModuleResolver::new())); } // default print/debug implementations - engine.print = Some(Box::new(print_to_stdout)); - engine.debug = Some(Box::new(debug_to_stdout)); + #[cfg(not(feature = "no_std"))] + #[cfg(not(target_family = "wasm"))] + { + engine.print = Some(Box::new(|s| println!("{}", s))); + engine.debug = Some(Box::new(|s, source, pos| { + if let Some(source) = source { + println!("{}{:?} | {}", source, pos, s); + } else if pos.is_none() { + println!("{}", s); + } else { + println!("{:?} | {}", pos, s); + } + })); + } + #[cfg(any(feature = "no_std", target_family = "wasm"))] + { + engine.print = None; + engine.debug = None; + } engine.register_global_module(StandardPackage::new().as_shared_module()); diff --git a/src/func/builtin.rs b/src/func/builtin.rs index a84b437c..f2284fbd 100644 --- a/src/func/builtin.rs +++ b/src/func/builtin.rs @@ -41,8 +41,7 @@ fn is_numeric(type_id: TypeId) -> bool { #[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i32"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] let result = result || type_id == TypeId::of::() || type_id == TypeId::of::(); #[cfg(not(feature = "no_float"))] diff --git a/src/lib.rs b/src/lib.rs index 3c6e616e..39e02851 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,8 +41,8 @@ //! engine.register_fn("compute", compute_something); //! //! # #[cfg(not(feature = "no_std"))] -//! # #[cfg(not(target_arch = "wasm32"))] -//! # #[cfg(not(target_arch = "wasm64"))] +//! # #[cfg(not(target_family = "wasm"))] +//! # //! // Evaluate the script, expecting a 'bool' result //! let result = engine.eval_file::("my_script.rhai".into())?; //! @@ -365,17 +365,15 @@ compile_error!("`wasm-bindgen` cannot be used with `no-std`"); #[cfg(feature = "stdweb")] compile_error!("`stdweb` cannot be used with `no-std`"); -#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] +#[cfg(target_family = "wasm")] #[cfg(feature = "no_std")] compile_error!("`no_std` cannot be used for WASM target"); -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] #[cfg(feature = "wasm-bindgen")] compile_error!("`wasm-bindgen` cannot be used for non-WASM target"); -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] #[cfg(feature = "stdweb")] compile_error!("`stdweb` cannot be used non-WASM target"); diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 6d65a063..71828d81 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -1,6 +1,5 @@ #![cfg(not(feature = "no_std"))] -#![cfg(not(target_arch = "wasm32"))] -#![cfg(not(target_arch = "wasm64"))] +#![cfg(not(target_family = "wasm"))] use crate::func::native::shared_write_lock; use crate::{ diff --git a/src/module/resolvers/mod.rs b/src/module/resolvers/mod.rs index 82d350c8..cf14c2b8 100644 --- a/src/module/resolvers/mod.rs +++ b/src/module/resolvers/mod.rs @@ -11,8 +11,7 @@ mod stat; pub use collection::ModuleResolversCollection; pub use dummy::DummyModuleResolver; #[cfg(not(feature = "no_std"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] pub use file::FileModuleResolver; pub use stat::StaticModuleResolver; diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index b908cb49..7d135620 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -193,8 +193,8 @@ def_package! { reg_functions!(lib += arith_numbers; i8, u8, i16, u16, i32, u32, u64); reg_functions!(lib += signed_numbers; i8, i16, i32); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + { reg_functions!(lib += arith_num_128; i128, u128); reg_functions!(lib += signed_num_128; i128); @@ -238,8 +238,8 @@ gen_arithmetic_functions!(arith_numbers => i8, u8, i16, u16, i32, u32, u64); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] + gen_arithmetic_functions!(arith_num_128 => i128, u128); gen_signed_functions!(signed_basic => INT); @@ -250,8 +250,8 @@ gen_signed_functions!(signed_numbers => i8, i16, i32); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] + gen_signed_functions!(signed_num_128 => i128); #[cfg(not(feature = "no_float"))] diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 6dbd8eaf..eca73120 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -303,8 +303,8 @@ def_package! { { reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + reg_range!(lib | "range" => i128, u128); } @@ -315,8 +315,8 @@ def_package! { { reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + reg_range!(lib | step "range" => i128, u128); } diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 0e435c1e..e577be58 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -47,8 +47,8 @@ def_package! { { reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + reg_functions!(lib += num_128; i128, u128); } @@ -71,8 +71,8 @@ gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] + gen_cmp_functions!(num_128 => i128, u128); #[cfg(not(feature = "no_float"))] diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index a6dfa939..83f2efac 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -66,8 +66,8 @@ def_package! { { reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64)); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + reg_functions!(lib += num_128_to_int::to_int(i128, u128)); } @@ -86,8 +86,8 @@ def_package! { { reg_functions!(lib += numbers_to_float::to_float(i8, u8, i16, u16, i32, u32, i64, u32)); - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + reg_functions!(lib += num_128_to_float::to_float(i128, u128)); } } @@ -522,8 +522,8 @@ gen_conversion_as_functions!(numbers_to_float => to_float (i8, u8, i16, u16, i32 #[cfg(not(feature = "no_float"))] #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] + gen_conversion_as_functions!(num_128_to_float => to_float (i128, u128) -> FLOAT); gen_conversion_as_functions!(basic_to_int => to_int (char) -> INT); @@ -534,8 +534,8 @@ gen_conversion_as_functions!(numbers_to_int => to_int (i8, u8, i16, u16, i32, u3 #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] + gen_conversion_as_functions!(num_128_to_int => to_int (i128, u128) -> INT); #[cfg(feature = "decimal")] diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 736fdd95..1debcbb2 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -274,8 +274,8 @@ mod number_formatting { to_binary(value) } - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] + pub mod num_128 { #[rhai_fn(name = "to_hex")] pub fn u128_to_hex(value: u128) -> ImmutableString { diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 5f6721bf..54254611 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -7,11 +7,10 @@ use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT}; #[cfg(not(feature = "no_float"))] use crate::FLOAT; -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] use std::time::{Duration, Instant}; -#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] +#[cfg(target_family = "wasm")] use instant::{Duration, Instant}; def_package! { diff --git a/src/parser.rs b/src/parser.rs index c4c5311f..9bfc84f3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -242,6 +242,22 @@ impl ParseSettings { } } +/// Make an anonymous function. +#[cfg(not(feature = "no_function"))] +#[inline] +#[must_use] +pub fn make_anonymous_fn(hash: u64) -> String { + format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash) +} + +/// Is this function an anonymous function? +#[cfg(not(feature = "no_function"))] +#[inline(always)] +#[must_use] +pub fn is_anonymous_fn(fn_name: &str) -> bool { + fn_name.starts_with(crate::engine::FN_ANONYMOUS) +} + impl Expr { /// Convert a [`Variable`][Expr::Variable] into a [`Property`][Expr::Property]. /// All other variants are untouched. @@ -3253,7 +3269,7 @@ fn parse_anon_fn( params.iter().for_each(|p| p.hash(hasher)); body.hash(hasher); let hash = hasher.finish(); - let fn_name = state.get_identifier("", format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash)); + let fn_name = state.get_identifier("", make_anonymous_fn(hash)); // Define the function let script = ScriptFnDef { diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 3cb2f857..800ffb2b 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -15,12 +15,11 @@ use std::{ }; #[cfg(not(feature = "no_std"))] -#[cfg(not(target_arch = "wasm32"))] -#[cfg(not(target_arch = "wasm64"))] +#[cfg(not(target_family = "wasm"))] use std::time::Instant; #[cfg(not(feature = "no_std"))] -#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] +#[cfg(target_family = "wasm")] use instant::Instant; /// The message: data type was checked @@ -525,8 +524,7 @@ impl Hash for Dynamic { value_any.downcast_ref::().expect(CHECKED).hash(state); } - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] if type_id == TypeId::of::() { TypeId::of::().hash(state); value_any.downcast_ref::().expect(CHECKED).hash(state); @@ -650,8 +648,7 @@ impl fmt::Display for Dynamic { #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] if _type_id == TypeId::of::() { return fmt::Display::fmt(_value_any.downcast_ref::().expect(CHECKED), f); } else if _type_id == TypeId::of::() { @@ -756,8 +753,7 @@ impl fmt::Debug for Dynamic { #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] - #[cfg(not(target_arch = "wasm32"))] - #[cfg(not(target_arch = "wasm64"))] + #[cfg(not(target_family = "wasm"))] if _type_id == TypeId::of::() { return fmt::Debug::fmt(_value_any.downcast_ref::().expect(CHECKED), f); } else if _type_id == TypeId::of::() { diff --git a/src/types/error.rs b/src/types/error.rs index 78cf4e24..c07e0fca 100644 --- a/src/types/error.rs +++ b/src/types/error.rs @@ -115,7 +115,7 @@ impl fmt::Display for EvalAltResult { Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?, #[cfg(not(feature = "no_function"))] - Self::ErrorInFunctionCall(s, src, err, _) if crate::engine::is_anonymous_fn(s) => { + Self::ErrorInFunctionCall(s, src, err, _) if crate::parser::is_anonymous_fn(s) => { write!(f, "{} in call to closure", err)?; if !src.is_empty() { write!(f, " @ '{}'", src)?; diff --git a/tests/time.rs b/tests/time.rs index 03df04bc..7a35da15 100644 --- a/tests/time.rs +++ b/tests/time.rs @@ -1,6 +1,5 @@ #![cfg(not(feature = "no_std"))] -#![cfg(not(target_arch = "wasm32"))] -#![cfg(not(target_arch = "wasm64"))] +#![cfg(not(target_family = "wasm"))] use rhai::{Engine, EvalAltResult};