Refactor wasm feature gates.

This commit is contained in:
Stephen Chung 2022-01-04 22:16:20 +08:00
parent d99953c101
commit 328f6910b6
21 changed files with 110 additions and 64 deletions

View File

@ -20,7 +20,8 @@ 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(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
#[inline(always)]
pub fn consume_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> {
self.run_file(path)
@ -38,7 +39,8 @@ 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(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
#[inline(always)]
pub fn consume_file_with_scope(
&self,

View File

@ -1,6 +1,7 @@
//! Module that defines the public file-based API of [`Engine`].
#![cfg(not(feature = "no_std"))]
#![cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#![cfg(not(target_arch = "wasm32"))]
#![cfg(not(target_arch = "wasm64"))]
use crate::types::dynamic::Variant;
use crate::{Engine, RhaiResultOf, Scope, AST, ERR};
@ -165,8 +166,6 @@ impl Engine {
/// Evaluate a file, returning any error (if any).
///
/// Not available under `no_std` or `WASM`.
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[inline]
pub fn run_file(&self, path: std::path::PathBuf) -> RhaiResultOf<()> {
Self::read_file(path).and_then(|contents| self.run(&contents))
@ -180,8 +179,6 @@ impl Engine {
/// If not [`OptimizationLevel::None`][crate::OptimizationLevel::None], constants defined within
/// the scope are propagated throughout the script _including_ functions. This allows functions
/// to be optimized based on dynamic global constants.
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[inline]
pub fn run_file_with_scope(
&self,

View File

@ -1,5 +1,9 @@
//! Module defining script options.
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, Not, Sub, SubAssign};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// A type representing the access mode of a function.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum FnAccess {
@ -9,10 +13,6 @@ pub enum FnAccess {
Private,
}
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, Not, Sub, SubAssign};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// A type that holds a configuration option with bit-flags.
/// Exported under the `internals` feature only.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Default)]

View File

@ -352,8 +352,13 @@ impl<'a> Target<'a> {
/// Convert a shared or reference `Target` into a target with an owned value.
#[inline(always)]
#[must_use]
pub fn into_owned(self) -> Target<'static> {
self.take_or_clone().into()
pub fn into_owned(self) -> Self {
match self {
Self::RefMut(r) => Self::TempValue(r.clone()),
#[cfg(not(feature = "no_closure"))]
Self::SharedValue { value, .. } => Self::TempValue(value),
_ => self,
}
}
/// Propagate a changed value back to the original source.
/// This has no effect for direct references.
@ -1073,7 +1078,8 @@ pub fn is_anonymous_fn(fn_name: &str) -> bool {
#[allow(unused_variables)]
fn print_to_stdout(s: &str) {
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
println!("{}", s);
}
@ -1082,7 +1088,8 @@ fn print_to_stdout(s: &str) {
#[allow(unused_variables)]
fn debug_to_stdout(s: &str, source: Option<&str>, pos: Position) {
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
if let Some(source) = source {
println!("{}{:?} | {}", source, pos, s);
} else if pos.is_none() {
@ -1102,7 +1109,8 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
{
engine.module_resolver =
Some(Box::new(crate::module::resolvers::FileModuleResolver::new()));

View File

@ -25,7 +25,12 @@ const BUILTIN: &str = "data type was checked";
#[inline]
#[must_use]
fn is_numeric(type_id: TypeId) -> bool {
let result = type_id == TypeId::of::<u8>()
let result = false;
#[cfg(not(feature = "only_i64"))]
#[cfg(not(feature = "only_i32"))]
let result = result
|| type_id == TypeId::of::<u8>()
|| type_id == TypeId::of::<u16>()
|| type_id == TypeId::of::<u32>()
|| type_id == TypeId::of::<u64>()
@ -34,7 +39,10 @@ fn is_numeric(type_id: TypeId) -> bool {
|| type_id == TypeId::of::<i32>()
|| type_id == TypeId::of::<i64>();
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(feature = "only_i32"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
let result = result || type_id == TypeId::of::<u128>() || type_id == TypeId::of::<i128>();
#[cfg(not(feature = "no_float"))]

View File

@ -1142,7 +1142,7 @@ impl Engine {
} else {
// Turn it into a method call only if the object is not shared and not a simple value
is_ref_mut = true;
let obj_ref = target.take_ref().expect("reference");
let obj_ref = target.take_ref().expect("ref");
args.push(obj_ref);
args.extend(arg_values.iter_mut());
}
@ -1217,7 +1217,7 @@ impl Engine {
// Turn it into a method call only if the object is not shared and not a simple value
let (first, rest) = arg_values.split_first_mut().expect("not empty");
first_arg_value = Some(first);
let obj_ref = target.take_ref().expect("reference");
let obj_ref = target.take_ref().expect("ref");
args.push(obj_ref);
args.extend(rest.iter_mut());
}

View File

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

View File

@ -702,15 +702,11 @@ impl Module {
) -> &mut Self {
self.update_fn_metadata(hash_fn, arg_names);
if !comments.as_ref().is_empty() {
let comments = comments.as_ref();
if !comments.is_empty() {
let f = self.functions.get_mut(&hash_fn).expect("exists");
f.comments = Some(
comments
.as_ref()
.iter()
.map(|s| s.as_ref().into())
.collect(),
);
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
}
self
@ -869,15 +865,11 @@ impl Module {
) -> u64 {
let hash = self.set_fn(name, namespace, access, arg_names, arg_types, func);
if !comments.as_ref().is_empty() {
let comments = comments.as_ref();
if !comments.is_empty() {
let f = self.functions.get_mut(&hash).expect("exists");
f.comments = Some(
comments
.as_ref()
.iter()
.map(|s| s.as_ref().into())
.collect(),
);
f.comments = Some(comments.iter().map(|s| s.as_ref().into()).collect());
}
hash

View File

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

View File

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

View File

@ -193,7 +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(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
{
reg_functions!(lib += arith_num_128; i128, u128);
reg_functions!(lib += signed_num_128; i128);
@ -237,7 +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(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
gen_arithmetic_functions!(arith_num_128 => i128, u128);
gen_signed_functions!(signed_basic => INT);
@ -248,7 +250,8 @@ gen_signed_functions!(signed_numbers => i8, i16, i32);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
gen_signed_functions!(signed_num_128 => i128);
#[cfg(not(feature = "no_float"))]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -93,7 +93,8 @@ impl Serializer for &mut DynamicSerializer {
type SerializeSeq = DynamicSerializer;
type SerializeTuple = DynamicSerializer;
type SerializeTupleStruct = DynamicSerializer;
#[cfg(not(any(feature = "no_object", feature = "no_index")))]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
type SerializeTupleVariant = TupleVariantSerializer;
#[cfg(any(feature = "no_object", feature = "no_index"))]
type SerializeTupleVariant = serde::ser::Impossible<Dynamic, RhaiError>;
@ -629,13 +630,15 @@ impl SerializeStruct for DynamicSerializer {
}
}
#[cfg(not(any(feature = "no_object", feature = "no_index")))]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
struct TupleVariantSerializer {
variant: &'static str,
array: crate::Array,
}
#[cfg(not(any(feature = "no_object", feature = "no_index")))]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
impl serde::ser::SerializeTupleVariant for TupleVariantSerializer {
type Ok = Dynamic;
type Error = RhaiError;

View File

@ -15,7 +15,8 @@ use std::{
};
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
use std::time::Instant;
#[cfg(not(feature = "no_std"))]
@ -524,7 +525,8 @@ impl Hash for Dynamic {
value_any.downcast_ref::<i64>().expect(CHECKED).hash(state);
}
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
if type_id == TypeId::of::<u128>() {
TypeId::of::<u128>().hash(state);
value_any.downcast_ref::<u128>().expect(CHECKED).hash(state);
@ -636,13 +638,20 @@ impl fmt::Display for Dynamic {
}
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))]
if _type_id == TypeId::of::<f32>() {
return fmt::Display::fmt(_value_any.downcast_ref::<f32>().expect(CHECKED), f);
} else if _type_id == TypeId::of::<f64>() {
}
#[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")]
if _type_id == TypeId::of::<f64>() {
return fmt::Display::fmt(_value_any.downcast_ref::<f64>().expect(CHECKED), f);
}
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
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>() {
@ -735,13 +744,20 @@ impl fmt::Debug for Dynamic {
}
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))]
if _type_id == TypeId::of::<f32>() {
return fmt::Debug::fmt(_value_any.downcast_ref::<f32>().expect(CHECKED), f);
} else if _type_id == TypeId::of::<f64>() {
}
#[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")]
if _type_id == TypeId::of::<f64>() {
return fmt::Debug::fmt(_value_any.downcast_ref::<f64>().expect(CHECKED), f);
}
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
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>() {
@ -942,7 +958,6 @@ impl Dynamic {
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))]
pub const FLOAT_PI: Self = Self::from_float(FloatConstants::PI);
/// A [`Dynamic`] containing π/2.
///

View File

@ -1,4 +1,5 @@
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
#![cfg(not(feature = "no_index"))]
#![cfg(not(feature = "no_module"))]
use rhai::plugin::*;
use rhai::{Engine, EvalAltResult, INT};

View File

@ -1,4 +1,5 @@
#![cfg(not(any(feature = "no_index", feature = "no_module")))]
#![cfg(not(feature = "no_index"))]
#![cfg(not(feature = "no_module"))]
use rhai::plugin::*;
use rhai::{Engine, EvalAltResult, Module, INT};

View File

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