Gate WASM target.

This commit is contained in:
Stephen Chung
2020-06-17 09:54:17 +08:00
parent b6e1f652b6
commit 7f4f737ff2
9 changed files with 75 additions and 14 deletions

View File

@@ -555,6 +555,8 @@ impl Engine {
/// Read the contents of a file into a string.
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
fn read_file(path: PathBuf) -> Result<String, Box<EvalAltResult>> {
let mut f = File::open(path.clone()).map_err(|err| {
Box::new(EvalAltResult::ErrorReadingScriptFile(
@@ -598,6 +600,8 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn compile_file(&self, path: PathBuf) -> Result<AST, Box<EvalAltResult>> {
self.compile_file_with_scope(&Scope::new(), path)
}
@@ -634,6 +638,8 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn compile_file_with_scope(
&self,
scope: &Scope,
@@ -775,6 +781,8 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> Result<T, Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
}
@@ -799,6 +807,8 @@ impl Engine {
/// # }
/// ```
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn eval_file_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
@@ -1004,6 +1014,8 @@ impl Engine {
/// Evaluate a file, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn consume_file(&self, path: PathBuf) -> Result<(), Box<EvalAltResult>> {
Self::read_file(path).and_then(|contents| self.consume(&contents))
}
@@ -1011,6 +1023,8 @@ impl Engine {
/// Evaluate a file with own scope, but throw away the result and only return error (if any).
/// Useful for when you don't need the result, but still need to keep track of possible errors.
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub fn consume_file_with_scope(
&self,
scope: &mut Scope,

View File

@@ -292,8 +292,15 @@ impl Default for Engine {
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
module_resolver: Some(Box::new(resolvers::FileModuleResolver::new())),
#[cfg(any(feature = "no_module", feature = "no_std"))]
#[cfg(any(
feature = "no_module",
feature = "no_std",
target_arch = "wasm32",
target_arch = "wasm64"
))]
module_resolver: None,
type_names: HashMap::new(),
@@ -373,6 +380,8 @@ fn extract_prop_from_setter(fn_name: &str) -> Option<&str> {
/// Print/debug to stdout
fn default_print(s: &str) {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
println!("{}", s);
}

View File

@@ -37,6 +37,8 @@
//! engine.register_fn("compute", compute_something);
//!
//! # #[cfg(not(feature = "no_std"))]
//! # #[cfg(not(target_arch = "wasm32"))]
//! # #[cfg(not(target_arch = "wasm64"))]
//! assert_eq!(
//! // Evaluate the script, expects a 'bool' return
//! engine.eval_file::<bool>("my_script.rhai".into())?,

View File

@@ -1054,6 +1054,8 @@ pub trait ModuleResolver: SendSync {
#[cfg(not(feature = "no_module"))]
pub mod resolvers {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub use super::file::FileModuleResolver;
pub use super::stat::StaticModuleResolver;
}
@@ -1063,6 +1065,8 @@ pub mod resolvers {}
/// Script file-based module resolver.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
mod file {
use super::*;
use crate::stdlib::path::PathBuf;

View File

@@ -33,6 +33,8 @@ pub use pkg_std::StandardPackage;
pub use string_basic::BasicStringPackage;
pub use string_more::MoreStringPackage;
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
pub use time_basic::BasicTimePackage;
/// Trait that all packages must implement.

View File

@@ -13,6 +13,8 @@ use crate::stdlib::{
};
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
use crate::stdlib::path::PathBuf;
/// Evaluation result.
@@ -29,6 +31,8 @@ pub enum EvalAltResult {
///
/// Never appears under the `no_std` feature.
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
ErrorReadingScriptFile(PathBuf, Position, std::io::Error),
/// Call to an unknown function. Wrapped value is the name of the function.
@@ -101,7 +105,9 @@ impl EvalAltResult {
pub(crate) fn desc(&self) -> &str {
match self {
#[cfg(not(feature = "no_std"))]
Self::ErrorReadingScriptFile(_, _, _) => "Cannot read from script file",
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
Self::ErrorReadingScriptFile(_, _, _) => "Cannot read from script file",
Self::ErrorParsing(p, _) => p.desc(),
Self::ErrorInFunctionCall(_, _, _) => "Error in called function",
@@ -160,6 +166,8 @@ impl fmt::Display for EvalAltResult {
match self {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
Self::ErrorReadingScriptFile(path, _, err) => {
write!(f, "{} '{}': {}", desc, path.display(), err)?
}
@@ -259,6 +267,8 @@ impl EvalAltResult {
pub fn position(&self) -> Position {
match self {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
Self::ErrorReadingScriptFile(_, pos, _) => *pos,
Self::ErrorParsing(_, pos)
@@ -297,6 +307,8 @@ impl EvalAltResult {
pub fn set_position(&mut self, new_position: Position) {
match self {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm64"))]
Self::ErrorReadingScriptFile(_, pos, _) => *pos = new_position,
Self::ErrorParsing(_, pos)