diff --git a/Cargo.lock b/Cargo.lock index 20b06a09..e2ac3233 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1164,6 +1164,7 @@ dependencies = [ "cidr", "criterion", "fnv", + "getrandom", "indoc", "memmem", "rand", diff --git a/Cargo.toml b/Cargo.toml index 934e4cbb..93dbf7a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ cfg-if = "1" cidr = { version = "0.2", features = ["serde"] } criterion = "0.5" fnv = "1.0.6" +getrandom = { version = "0.3" } indoc = "2" libc = "0.2.42" memmem = "0.1.1" diff --git a/engine/Cargo.toml b/engine/Cargo.toml index bef09b75..d9a265e1 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -40,3 +40,9 @@ indoc.workspace = true [features] default = [ "regex" ] regex = ["dep:regex-automata"] + +[target.'cfg(target_family = "wasm")'.dependencies] +# By default, getrandom doesn't have any source of randomness on wasm32-unknown. +# This optional dependency allows us to build with `--features getrandom/wasm_js`. +# For more information see: https://docs.rs/getrandom/#webassembly-support +getrandom.workspace = true diff --git a/engine/src/ast/field_expr.rs b/engine/src/ast/field_expr.rs index 70e1f81e..f238fde5 100644 --- a/engine/src/ast/field_expr.rs +++ b/engine/src/ast/field_expr.rs @@ -655,20 +655,24 @@ impl Expr for ComparisonExpr { use rand::{Rng, rng}; use sliceslice::wasm32::*; - impl Compare for Wasm32Searcher { + struct WasmSearcher(Wasm32Searcher>); + + impl Compare for WasmSearcher { #[inline] fn compare<'e>( &self, value: &LhsValue<'e>, _: &'e ExecutionContext<'e, U>, ) -> bool { - self.search_in(cast_value!(value, Bytes)) + unsafe { self.0.search_in(cast_value!(value, Bytes)) } } } let position = rng().random_range(1..bytes.len()); - return unsafe { search!(Wasm32Searcher::with_position(bytes, position)) }; + return unsafe { + search!(WasmSearcher(Wasm32Searcher::with_position(bytes, position))) + }; } search!(TwoWaySearcher::new(bytes))