Add array API with closure variation that binds to this.

This commit is contained in:
Stephen Chung
2022-12-24 19:37:06 +08:00
parent ffa1f5fb4a
commit fd401f3048
6 changed files with 162 additions and 70 deletions

View File

@@ -298,6 +298,7 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("[1].map(|x| x + 41)[0]")?, 42);
assert_eq!(engine.eval::<INT>("[1].map(|| this + 41)[0]")?, 42);
assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42);
assert_eq!(
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,
@@ -316,6 +317,18 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
[3]
);
assert_eq!(
engine
.eval::<Dynamic>(
"
let x = [1, 2, 3];
x.filter(|| this > 2)
"
)?
.into_typed_array::<INT>()?,
[3]
);
assert_eq!(
engine
.eval::<Dynamic>(
@@ -340,6 +353,18 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
[2, 4, 6]
);
assert_eq!(
engine
.eval::<Dynamic>(
"
let x = [1, 2, 3];
x.map(|| this * 2)
"
)?
.into_typed_array::<INT>()?,
[2, 4, 6]
);
assert_eq!(
engine
.eval::<Dynamic>(