Use target_family for wasm.

This commit is contained in:
Stephen Chung 2022-01-12 08:12:28 +08:00
parent f0e9d4a557
commit 37dbc68bf5
18 changed files with 82 additions and 101 deletions

View File

@ -97,10 +97,7 @@ default-features = false
features = ["maths"] features = ["maths"]
optional = true optional = true
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_family = "wasm")'.dependencies]
instant = { version = "0.1.10" } # WASM implementation of std::time::Instant
[target.'cfg(target_arch = "wasm64")'.dependencies]
instant = { version = "0.1.10" } # WASM implementation of std::time::Instant instant = { version = "0.1.10" } # WASM implementation of std::time::Instant
[package.metadata.docs.rs] [package.metadata.docs.rs]

View File

@ -20,8 +20,7 @@ impl Engine {
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated(since = "1.1.0", note = "use `run_file` instead")] #[deprecated(since = "1.1.0", note = "use `run_file` instead")]
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
#[inline(always)] #[inline(always)]
pub fn consume_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> { pub fn consume_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> {
self.run_file(path) self.run_file(path)
@ -39,8 +38,7 @@ impl Engine {
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated(since = "1.1.0", note = "use `run_file_with_scope` instead")] #[deprecated(since = "1.1.0", note = "use `run_file_with_scope` instead")]
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
#[inline(always)] #[inline(always)]
pub fn consume_file_with_scope( pub fn consume_file_with_scope(
&self, &self,

View File

@ -1,7 +1,6 @@
//! Module that defines the public file-based API of [`Engine`]. //! Module that defines the public file-based API of [`Engine`].
#![cfg(not(feature = "no_std"))] #![cfg(not(feature = "no_std"))]
#![cfg(not(target_arch = "wasm32"))] #![cfg(not(target_family = "wasm"))]
#![cfg(not(target_arch = "wasm64"))]
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{Engine, RhaiResultOf, Scope, AST, ERR}; use crate::{Engine, RhaiResultOf, Scope, AST, ERR};

View File

@ -203,40 +203,6 @@ pub fn make_setter(id: &str) -> Identifier {
buf 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 { impl Engine {
/// Create a new [`Engine`]. /// Create a new [`Engine`].
#[inline] #[inline]
@ -247,16 +213,32 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
{ {
engine.module_resolver = engine.module_resolver =
Some(Box::new(crate::module::resolvers::FileModuleResolver::new())); Some(Box::new(crate::module::resolvers::FileModuleResolver::new()));
} }
// default print/debug implementations // default print/debug implementations
engine.print = Some(Box::new(print_to_stdout)); #[cfg(not(feature = "no_std"))]
engine.debug = Some(Box::new(debug_to_stdout)); #[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()); engine.register_global_module(StandardPackage::new().as_shared_module());

View File

@ -41,8 +41,7 @@ fn is_numeric(type_id: TypeId) -> bool {
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
let result = result || type_id == TypeId::of::<u128>() || type_id == TypeId::of::<i128>(); let result = result || type_id == TypeId::of::<u128>() || type_id == TypeId::of::<i128>();
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]

View File

@ -41,8 +41,8 @@
//! engine.register_fn("compute", compute_something); //! engine.register_fn("compute", compute_something);
//! //!
//! # #[cfg(not(feature = "no_std"))] //! # #[cfg(not(feature = "no_std"))]
//! # #[cfg(not(target_arch = "wasm32"))] //! # #[cfg(not(target_family = "wasm"))]
//! # #[cfg(not(target_arch = "wasm64"))] //! #
//! // Evaluate the script, expecting a 'bool' result //! // Evaluate the script, expecting a 'bool' result
//! let result = engine.eval_file::<bool>("my_script.rhai".into())?; //! let result = engine.eval_file::<bool>("my_script.rhai".into())?;
//! //!
@ -365,17 +365,15 @@ compile_error!("`wasm-bindgen` cannot be used with `no-std`");
#[cfg(feature = "stdweb")] #[cfg(feature = "stdweb")]
compile_error!("`stdweb` cannot be used with `no-std`"); 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")] #[cfg(feature = "no_std")]
compile_error!("`no_std` cannot be used for WASM target"); compile_error!("`no_std` cannot be used for WASM target");
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
#[cfg(feature = "wasm-bindgen")] #[cfg(feature = "wasm-bindgen")]
compile_error!("`wasm-bindgen` cannot be used for non-WASM target"); compile_error!("`wasm-bindgen` cannot be used for non-WASM target");
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
#[cfg(feature = "stdweb")] #[cfg(feature = "stdweb")]
compile_error!("`stdweb` cannot be used non-WASM target"); compile_error!("`stdweb` cannot be used non-WASM target");

View File

@ -1,6 +1,5 @@
#![cfg(not(feature = "no_std"))] #![cfg(not(feature = "no_std"))]
#![cfg(not(target_arch = "wasm32"))] #![cfg(not(target_family = "wasm"))]
#![cfg(not(target_arch = "wasm64"))]
use crate::func::native::shared_write_lock; use crate::func::native::shared_write_lock;
use crate::{ use crate::{

View File

@ -11,8 +11,7 @@ mod stat;
pub use collection::ModuleResolversCollection; pub use collection::ModuleResolversCollection;
pub use dummy::DummyModuleResolver; pub use dummy::DummyModuleResolver;
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
pub use file::FileModuleResolver; pub use file::FileModuleResolver;
pub use stat::StaticModuleResolver; pub use stat::StaticModuleResolver;

View File

@ -193,8 +193,8 @@ def_package! {
reg_functions!(lib += arith_numbers; i8, u8, i16, u16, i32, u32, u64); reg_functions!(lib += arith_numbers; i8, u8, i16, u16, i32, u32, u64);
reg_functions!(lib += signed_numbers; i8, i16, i32); reg_functions!(lib += signed_numbers; i8, i16, i32);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
{ {
reg_functions!(lib += arith_num_128; i128, u128); reg_functions!(lib += arith_num_128; i128, u128);
reg_functions!(lib += signed_num_128; i128); 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_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
gen_arithmetic_functions!(arith_num_128 => i128, u128); gen_arithmetic_functions!(arith_num_128 => i128, u128);
gen_signed_functions!(signed_basic => INT); 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_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
gen_signed_functions!(signed_num_128 => i128); gen_signed_functions!(signed_num_128 => i128);
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]

View File

@ -303,8 +303,8 @@ def_package! {
{ {
reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
reg_range!(lib | "range" => i128, u128); 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); reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
reg_range!(lib | step "range" => i128, u128); reg_range!(lib | step "range" => i128, u128);
} }

View File

@ -47,8 +47,8 @@ def_package! {
{ {
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64); reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
reg_functions!(lib += num_128; i128, u128); 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_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
gen_cmp_functions!(num_128 => i128, u128); gen_cmp_functions!(num_128 => i128, u128);
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]

View File

@ -66,8 +66,8 @@ def_package! {
{ {
reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64)); reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64));
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
reg_functions!(lib += num_128_to_int::to_int(i128, u128)); 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)); reg_functions!(lib += numbers_to_float::to_float(i8, u8, i16, u16, i32, u32, i64, u32));
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
reg_functions!(lib += num_128_to_float::to_float(i128, u128)); 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 = "no_float"))]
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
gen_conversion_as_functions!(num_128_to_float => to_float (i128, u128) -> FLOAT); gen_conversion_as_functions!(num_128_to_float => to_float (i128, u128) -> FLOAT);
gen_conversion_as_functions!(basic_to_int => to_int (char) -> INT); 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_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
gen_conversion_as_functions!(num_128_to_int => to_int (i128, u128) -> INT); gen_conversion_as_functions!(num_128_to_int => to_int (i128, u128) -> INT);
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]

View File

@ -274,8 +274,8 @@ mod number_formatting {
to_binary(value) to_binary(value)
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
pub mod num_128 { pub mod num_128 {
#[rhai_fn(name = "to_hex")] #[rhai_fn(name = "to_hex")]
pub fn u128_to_hex(value: u128) -> ImmutableString { pub fn u128_to_hex(value: u128) -> ImmutableString {

View File

@ -7,11 +7,10 @@ use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT};
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use crate::FLOAT; use crate::FLOAT;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] #[cfg(target_family = "wasm")]
use instant::{Duration, Instant}; use instant::{Duration, Instant};
def_package! { def_package! {

View File

@ -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 { impl Expr {
/// Convert a [`Variable`][Expr::Variable] into a [`Property`][Expr::Property]. /// Convert a [`Variable`][Expr::Variable] into a [`Property`][Expr::Property].
/// All other variants are untouched. /// All other variants are untouched.
@ -3253,7 +3269,7 @@ fn parse_anon_fn(
params.iter().for_each(|p| p.hash(hasher)); params.iter().for_each(|p| p.hash(hasher));
body.hash(hasher); body.hash(hasher);
let hash = hasher.finish(); 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 // Define the function
let script = ScriptFnDef { let script = ScriptFnDef {

View File

@ -15,12 +15,11 @@ use std::{
}; };
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
use std::time::Instant; use std::time::Instant;
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] #[cfg(target_family = "wasm")]
use instant::Instant; use instant::Instant;
/// The message: data type was checked /// The message: data type was checked
@ -525,8 +524,7 @@ impl Hash for Dynamic {
value_any.downcast_ref::<i64>().expect(CHECKED).hash(state); value_any.downcast_ref::<i64>().expect(CHECKED).hash(state);
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
if type_id == TypeId::of::<u128>() { if type_id == TypeId::of::<u128>() {
TypeId::of::<u128>().hash(state); TypeId::of::<u128>().hash(state);
value_any.downcast_ref::<u128>().expect(CHECKED).hash(state); value_any.downcast_ref::<u128>().expect(CHECKED).hash(state);
@ -650,8 +648,7 @@ impl fmt::Display for Dynamic {
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
if _type_id == TypeId::of::<u128>() { if _type_id == TypeId::of::<u128>() {
return fmt::Display::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f); return fmt::Display::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f);
} else if _type_id == TypeId::of::<i128>() { } else if _type_id == TypeId::of::<i128>() {
@ -756,8 +753,7 @@ impl fmt::Debug for Dynamic {
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_family = "wasm"))]
#[cfg(not(target_arch = "wasm64"))]
if _type_id == TypeId::of::<u128>() { if _type_id == TypeId::of::<u128>() {
return fmt::Debug::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f); return fmt::Debug::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f);
} else if _type_id == TypeId::of::<i128>() { } else if _type_id == TypeId::of::<i128>() {

View File

@ -115,7 +115,7 @@ impl fmt::Display for EvalAltResult {
Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?, Self::ErrorParsing(p, _) => write!(f, "Syntax error: {}", p)?,
#[cfg(not(feature = "no_function"))] #[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)?; write!(f, "{} in call to closure", err)?;
if !src.is_empty() { if !src.is_empty() {
write!(f, " @ '{}'", src)?; write!(f, " @ '{}'", src)?;

View File

@ -1,6 +1,5 @@
#![cfg(not(feature = "no_std"))] #![cfg(not(feature = "no_std"))]
#![cfg(not(target_arch = "wasm32"))] #![cfg(not(target_family = "wasm"))]
#![cfg(not(target_arch = "wasm64"))]
use rhai::{Engine, EvalAltResult}; use rhai::{Engine, EvalAltResult};