Skip to content

Commit a98ceb2

Browse files
authored
Merge pull request #1063 from schungx/master
Change instant to web-time for WASM targets.
2 parents 6b132e5 + 987215c commit a98ceb2

30 files changed

+292
-281
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Breaking Changes
88
----------------
99

1010
* `stdweb` support is removed. The feature flag `stdweb` is also removed. Use `wasm-bindgen` instead.
11+
* [`web-time`](https://crates.io/crates/web-time) is used for WASM targets instead of [`instant`](https://crates.io/crates/instant), which is no longer maintained.
1112

1213
Bug fixes
1314
---------
@@ -23,6 +24,11 @@ Enhancements
2324
* The methods `sort_desc`, `order`, `order_by` and `order_desc` are added to arrays.
2425
* An exception is now thrown when attempting to use `sort` on an array containing unsupported element types.
2526

27+
New features
28+
------------
29+
30+
* `AST::new_from_module` is added to create an `AST` from a shared `Module`.
31+
2632

2733
Version 1.23.6
2834
==============

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ no_std = ["no-std-compat", "num-traits/libm", "core-error", "libm", "hashbrown",
118118
#! ### JavaScript Interface for WASM
119119

120120
## Use [`wasm-bindgen`](https://crates.io/crates/wasm-bindgen) as JavaScript interface.
121-
wasm-bindgen = ["getrandom/wasm_js", "instant/wasm-bindgen"]
121+
wasm-bindgen = ["getrandom/wasm_js"]
122122

123123
#! ### Features used in testing environments only
124124

@@ -154,7 +154,7 @@ codegen-units = 1
154154
#panic = 'abort' # remove stack backtrace for no-std
155155

156156
[target.'cfg(target_family = "wasm")'.dependencies]
157-
instant = { version = "0.1.10" } # WASM implementation of std::time::Instant
157+
web-time = { version = "1.1.0" } # WASM implementation of std::time::Instant
158158

159159
[package.metadata.docs.rs]
160160
features = ["document-features", "metadata", "serde", "internals", "decimal", "debugging"]
@@ -163,6 +163,3 @@ features = ["document-features", "metadata", "serde", "internals", "decimal", "d
163163
# Notice that a custom modified version of `rustyline` is used which supports bracketed paste on Windows.
164164
# This can be moved to the official version when bracketed paste is added.
165165
rustyline = { git = "https://github.com/schungx/rustyline", branch = "v15_fake" }
166-
167-
# Patch SmartString to resolve an UB issue.
168-
#smartstring = { git = "https://github.com/bodil/smartstring", ref = "refs/pull/34/head" }

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ Targets and builds
3232
Versions
3333
--------
3434

35-
The [`ahash`](https://crates.io/crates/ahash) crate that Rhai depends on has a breaking change since version `0.8.12` which bumps [`getrandom`](https://crates.io/crates/getrandom) to version `0.3`, braking certain `no-std` and WASM builds.
36-
37-
Version [`1.23.5`](https://crates.io/crates/rhai/1.23.5): Use this version when building for `no-std` or WASM.
38-
39-
Version [`1.23.6`](https://crates.io/crates/rhai/1.23.6): This is the main version for `std` builds.
35+
As of this version, [`stdweb`](https://crates.io/crates/stdweb) is no longer supported for WASM builds
36+
because of changes to [`getrandom`](https://crates.io/crates/getrandom) starting from version 0.3.
4037

4138

4239
Standard features

codegen/src/custom_type.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,14 @@ fn extract_type_from_option(ty: &syn::Type) -> Option<&syn::Type> {
197197
// TODO store (with lazy static) the vec of string
198198
// TODO maybe optimization, reverse the order of segments
199199
fn extract_option_segment(path: &Path) -> Option<&PathSegment> {
200-
let idents_of_path = path
201-
.segments
202-
.iter()
203-
.into_iter()
204-
.fold(String::new(), |mut acc, v| {
205-
acc.push_str(&v.ident.to_string());
206-
acc.push('|');
207-
acc
208-
});
200+
let idents_of_path = path.segments.iter().fold(String::new(), |mut acc, v| {
201+
acc.push_str(&v.ident.to_string());
202+
acc.push('|');
203+
acc
204+
});
209205
vec!["Option|", "std|option|Option|", "core|option|Option|"]
210206
.into_iter()
211-
.find(|s| &idents_of_path == *s)
207+
.find(|s| idents_of_path == *s)
212208
.and_then(|_| path.segments.last())
213209
}
214210

@@ -368,7 +364,7 @@ fn scan_fields(fields: &[&Field], accessors: &mut Vec<TokenStream>, errors: &mut
368364
(Some(func), _) => func,
369365
(None, Some(func)) => quote! { |obj: &mut Self| #func(&*obj) },
370366
(None, None) => {
371-
if let Some(_) = option_type {
367+
if option_type.is_some() {
372368
quote! { |obj: &mut Self| obj.#field_name.clone().map_or(Dynamic::UNIT, Dynamic::from) }
373369
} else {
374370
quote! { |obj: &mut Self| obj.#field_name.clone() }

src/api/call_fn.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct CallFnOptions<'t> {
2222
pub eval_ast: bool,
2323
/// Rewind the [`Scope`] after the function call? Default `true`.
2424
pub rewind_scope: bool,
25-
/// Call only scripted functions from the [`AST`] instead of functions in all namespaces.
25+
/// Call functions in all namespaces instead of only scripted functions within the [`AST`].
2626
pub in_all_namespaces: bool,
2727
}
2828

@@ -77,7 +77,7 @@ impl<'a> CallFnOptions<'a> {
7777
/// Call functions in all namespaces instead of only scripted functions within the [`AST`].
7878
#[inline(always)]
7979
#[must_use]
80-
pub fn in_all_namespaces(mut self, value: bool) -> Self {
80+
pub const fn in_all_namespaces(mut self, value: bool) -> Self {
8181
self.in_all_namespaces = value;
8282
self
8383
}
@@ -291,12 +291,10 @@ impl Engine {
291291
} else if !in_all_namespaces {
292292
Err(ERR::ErrorFunctionNotFound(name.into(), Position::NONE).into())
293293
} else {
294-
let has_this = if let Some(this_ptr) = this_ptr.as_deref_mut() {
294+
let has_this = this_ptr.as_deref_mut().map_or(false, |this_ptr| {
295295
args.insert(0, this_ptr);
296296
true
297-
} else {
298-
false
299-
};
297+
});
300298

301299
self.eval_fn_call_with_arguments::<Dynamic>(name, args, has_this, has_this)
302300
}

src/api/definitions/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -490,16 +490,19 @@ impl FuncMetadata {
490490
}
491491
first = false;
492492

493-
let (param_name, param_type) = self.params_info.get(i).map_or(("_", "?".into()), |s| {
494-
let (name, typ) = s.split_once(':').unwrap_or((s, ""));
495-
(
496-
name.trim().split(' ').last().unwrap().trim(),
497-
match typ.trim() {
498-
"" | "?" | "_" => "?".into(),
499-
typ => def_type_name(typ, def.engine),
500-
},
501-
)
502-
});
493+
let (param_name, param_type) = self.params_info.get(i).map_or_else(
494+
|| ("_", "?".into()),
495+
|s| {
496+
let (name, typ) = s.split_once(':').unwrap_or((s, ""));
497+
(
498+
name.trim().split(' ').next_back().unwrap().trim(),
499+
match typ.trim() {
500+
"" | "?" | "_" => "?".into(),
501+
typ => def_type_name(typ, def.engine),
502+
},
503+
)
504+
},
505+
);
503506

504507
if operator {
505508
write!(writer, "{param_type}")?;

src/api/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,15 @@ impl Engine {
335335
pub fn eval_fn_call<T: Variant + Clone>(
336336
&self,
337337
fn_name: impl AsRef<str>,
338-
mut this_ptr: Option<&mut Dynamic>,
338+
this_ptr: Option<&mut Dynamic>,
339339
args: impl FuncArgs,
340340
) -> RhaiResultOf<T> {
341341
let mut has_this = false;
342342
let arg_values = &mut FnArgsVec::new_const();
343343
args.parse(arg_values);
344344
let args = &mut arg_values.iter_mut().collect::<FnArgsVec<_>>();
345345

346-
if let Some(this_ptr) = this_ptr.as_deref_mut() {
346+
if let Some(this_ptr) = this_ptr {
347347
args.insert(0, this_ptr);
348348
has_this = true;
349349
}

src/api/files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Engine {
4141
}
4242
None => contents.clear(),
4343
}
44-
};
44+
}
4545

4646
Ok(contents)
4747
}

src/ast/ast.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,23 @@ impl AST {
104104
ast.set_source(source);
105105
ast
106106
}
107-
/// Create an empty [`AST`].
107+
/// Create a new [`AST`] from a shared [`Module`][crate::Module].
108+
#[cfg(not(feature = "no_function"))]
108109
#[inline]
109110
#[must_use]
111+
pub fn new_from_module(module: impl Into<crate::SharedModule>) -> Self {
112+
let module = module.into();
113+
114+
if let Some(source) = module.id() {
115+
let source: crate::SmartString = source.into();
116+
Self::new_with_source(std::iter::empty(), module, source)
117+
} else {
118+
Self::new(std::iter::empty(), module)
119+
}
120+
}
121+
/// Create an empty [`AST`].
122+
#[inline(always)]
123+
#[must_use]
110124
pub fn empty() -> Self {
111125
Self {
112126
source: None,

src/ast/expr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl FnCallExpr {
234234
/// Is this function call an operator expression?
235235
#[inline(always)]
236236
#[must_use]
237-
pub fn is_operator_call(&self) -> bool {
237+
pub const fn is_operator_call(&self) -> bool {
238238
self.op_token.is_some()
239239
}
240240
/// Convert this into an [`Expr::FnCall`].
@@ -276,12 +276,12 @@ pub enum Expr {
276276
/// [String][ImmutableString] constant.
277277
StringConstant(ImmutableString, Position),
278278
/// An interpolated [string][ImmutableString].
279-
InterpolatedString(ThinVec<Expr>, Position),
279+
InterpolatedString(ThinVec<Self>, Position),
280280
/// [ expr, ... ]
281-
Array(ThinVec<Expr>, Position),
281+
Array(ThinVec<Self>, Position),
282282
/// #{ name:expr, ... }
283283
Map(
284-
Box<(StaticVec<(Ident, Expr)>, BTreeMap<Identifier, Dynamic>)>,
284+
Box<(StaticVec<(Ident, Self)>, BTreeMap<Identifier, Dynamic>)>,
285285
Position,
286286
),
287287
/// ()
@@ -330,11 +330,11 @@ pub enum Expr {
330330
/// * [`BREAK`][ASTFlags::BREAK] = terminate the chain (recurse into the chain if unset)
331331
Index(Box<BinaryExpr>, ASTFlags, Position),
332332
/// lhs `&&` rhs
333-
And(Box<StaticVec<Expr>>, Position),
333+
And(Box<StaticVec<Self>>, Position),
334334
/// lhs `||` rhs
335-
Or(Box<StaticVec<Expr>>, Position),
335+
Or(Box<StaticVec<Self>>, Position),
336336
/// lhs `??` rhs
337-
Coalesce(Box<StaticVec<Expr>>, Position),
337+
Coalesce(Box<StaticVec<Self>>, Position),
338338
/// Custom syntax
339339
#[cfg(not(feature = "no_custom_syntax"))]
340340
Custom(Box<CustomExpr>, Position),
@@ -764,7 +764,7 @@ impl Expr {
764764
Self::Map(x, ..) => x.0.iter().map(|(.., v)| v).all(Self::is_pure),
765765

766766
Self::And(x, ..) | Self::Or(x, ..) | Self::Coalesce(x, ..) => {
767-
x.iter().all(Expr::is_pure)
767+
x.iter().all(Self::is_pure)
768768
}
769769

770770
Self::Stmt(x) => x.iter().all(Stmt::is_pure),

0 commit comments

Comments
 (0)