mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Implement [Symbol.iterator]() for iterators
This commit is contained in:
@@ -7,11 +7,12 @@ use crate::{
|
||||
native_function::{native_fn, NativeFunction},
|
||||
vs_array::VsArray,
|
||||
vs_class::VsClass,
|
||||
vs_symbol::VsSymbol,
|
||||
vs_value::{dynamic_make_mut, ToDynamicVal, ToVal, Val, VsType},
|
||||
LoadFunctionResult, ValTrait,
|
||||
};
|
||||
|
||||
use super::iteration_result::IterationResult;
|
||||
use super::{iteration_result::IterationResult, return_this::RETURN_THIS};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ArrayEntriesIterator {
|
||||
@@ -75,6 +76,14 @@ impl ValTrait for ArrayEntriesIterator {
|
||||
return Ok(NEXT.to_val());
|
||||
}
|
||||
|
||||
if let Val::Symbol(key) = key {
|
||||
match key {
|
||||
VsSymbol::ITERATOR => {
|
||||
return Ok(RETURN_THIS.to_val());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Val::Undefined)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,12 @@ use crate::{
|
||||
native_function::{native_fn, NativeFunction},
|
||||
vs_array::VsArray,
|
||||
vs_class::VsClass,
|
||||
vs_symbol::VsSymbol,
|
||||
vs_value::{dynamic_make_mut, ToDynamicVal, ToVal, Val, VsType},
|
||||
LoadFunctionResult, ValTrait,
|
||||
};
|
||||
|
||||
use super::iteration_result::IterationResult;
|
||||
use super::{iteration_result::IterationResult, return_this::RETURN_THIS};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ArrayIterator {
|
||||
@@ -75,6 +76,14 @@ impl ValTrait for ArrayIterator {
|
||||
return Ok(NEXT.to_val());
|
||||
}
|
||||
|
||||
if let Val::Symbol(key) = key {
|
||||
match key {
|
||||
VsSymbol::ITERATOR => {
|
||||
return Ok(RETURN_THIS.to_val());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Val::Undefined)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod array_entries_iterator;
|
||||
pub mod array_iterator;
|
||||
pub mod iteration_result;
|
||||
pub mod return_this;
|
||||
pub mod string_iterator;
|
||||
|
||||
3
valuescript_vm/src/iteration/return_this.rs
Normal file
3
valuescript_vm/src/iteration/return_this.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
use crate::native_function::{native_fn, NativeFunction};
|
||||
|
||||
pub static RETURN_THIS: NativeFunction = native_fn(|this, _| Ok(this.get().clone()));
|
||||
@@ -7,11 +7,12 @@ use crate::{
|
||||
native_function::{native_fn, NativeFunction},
|
||||
vs_array::VsArray,
|
||||
vs_class::VsClass,
|
||||
vs_symbol::VsSymbol,
|
||||
vs_value::{dynamic_make_mut, ToDynamicVal, ToVal, Val, VsType},
|
||||
LoadFunctionResult, ValTrait,
|
||||
};
|
||||
|
||||
use super::iteration_result::IterationResult;
|
||||
use super::{iteration_result::IterationResult, return_this::RETURN_THIS};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StringIterator {
|
||||
@@ -111,6 +112,14 @@ impl ValTrait for StringIterator {
|
||||
return Ok(NEXT.to_val());
|
||||
}
|
||||
|
||||
if let Val::Symbol(key) = key {
|
||||
match key {
|
||||
VsSymbol::ITERATOR => {
|
||||
return Ok(RETURN_THIS.to_val());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Val::Undefined)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user