Use target_family for wasm.
This commit is contained in:
parent
f0e9d4a557
commit
37dbc68bf5
@ -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]
|
||||
|
@ -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,
|
||||
|
@ -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};
|
||||
|
@ -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());
|
||||
|
||||
|
@ -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::<u128>() || type_id == TypeId::of::<i128>();
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
|
12
src/lib.rs
12
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::<bool>("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");
|
||||
|
||||
|
@ -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::{
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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"))]
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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"))]
|
||||
|
@ -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")]
|
||||
|
@ -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 {
|
||||
|
@ -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! {
|
||||
|
@ -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 {
|
||||
|
@ -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::<i64>().expect(CHECKED).hash(state);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[cfg(not(target_arch = "wasm64"))]
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
if type_id == TypeId::of::<u128>() {
|
||||
TypeId::of::<u128>().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_i64"))]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[cfg(not(target_arch = "wasm64"))]
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
if _type_id == TypeId::of::<u128>() {
|
||||
return fmt::Display::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f);
|
||||
} 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_i64"))]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[cfg(not(target_arch = "wasm64"))]
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
if _type_id == TypeId::of::<u128>() {
|
||||
return fmt::Debug::fmt(_value_any.downcast_ref::<u128>().expect(CHECKED), f);
|
||||
} else if _type_id == TypeId::of::<i128>() {
|
||||
|
@ -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)?;
|
||||
|
@ -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};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user