//! Generator objects (ECMA-262 11th edition, ,
//! sections 14.4 and 25.4), in a realm with [`crate::Options::es2015`].
//!
//! A call to a generator function runs its parameters' initialization and stops at
//! [`Op::GeneratorStart`]: its frame, with the operands above the frame's base and the `finally`
//! blocks it is inside, moves into a new generator object, which the call returns. `next`,
//! `return` and `throw` push the frame back on the interpreter's frame stack and go on in the same
//! loop, so a generator driven from JavaScript uses no Rust stack, and each resumption takes a unit
//! of fuel and counts against the frame limit. `yield` moves the frame back into the object. A
//! suspended generator's frame is reachable only through its object, so the collector keeps it
//! while the object is reachable and frees it with the object.
//!
//! [`Op::GeneratorStart`]: crate::bytecode::Op::GeneratorStart
use std::rc::Rc;
use crate::LimitKind;
use crate::builtins::iterator::iter_result;
use crate::bytecode::Code;
use crate::heap::{Class, EnvId, OBJECT_BYTES, ObjId, Object, key};
use crate::interp::{Abrupt, Frame, Interp, JsResult, Prepared, ThrowSite, internal};
use crate::value::Value;
/// How a generator is resumed: by `next`, `throw` or `return` (sections 25.4.1.3 and 25.4.1.4).
/// The code after a `yield` reads it as the number [`ResumeMode::number`] gives.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResumeMode {
Next,
Throw,
Return,
}
impl ResumeMode {
fn number(self) -> f64 {
match self {
ResumeMode::Next => 0.0,
ResumeMode::Throw => 1.0,
ResumeMode::Return => 2.0,
}
}
fn of(v: &Value) -> JsResult {
match v {
Value::Number(n) if *n == 0.0 => Ok(ResumeMode::Next),
Value::Number(n) if *n == 1.0 => Ok(ResumeMode::Throw),
Value::Number(n) if *n == 2.0 => Ok(ResumeMode::Return),
_ => internal("a resumption mode that is not one"),
}
}
}
/// The estimated bytes of a suspended generator's frame besides its operands.
const GENERATOR_BYTES: usize = 128;
/// The estimated bytes of each operand and `finally` entry a suspended frame holds.
const SLOT_BYTES: usize = 16;
/// A generator's frame while it is suspended: what [`Frame`] holds, with the operands above its
/// base and its `finally` entries, their stack slots counted from its base.
#[derive(Clone)]
pub(crate) struct SavedFrame {
pub(crate) code: Rc,
pc: usize,
pub(crate) env: EnvId,
pub(crate) var_env: EnvId,
env_depth: u32,
pub(crate) this: Value,
pub(crate) callee: Option,
pub(crate) completion: Value,
pub(crate) stack: Vec,
/// [`Interp::finally_sites`] entries.
pub(crate) sites: Vec<(ThrowSite, usize)>,
/// [`Interp::finally_marks`] entries.
pub(crate) marks: Vec<(ObjId, u32, usize)>,
}
/// A generator's [[GeneratorState]] (section 25.4.3), with its frame while it is suspended.
#[derive(Clone)]
pub(crate) enum GenState {
SuspendedStart(SavedFrame),
SuspendedYield(SavedFrame),
Executing,
Completed,
}
impl GenState {
/// The suspended frame, if the generator is suspended.
pub(crate) fn saved(&self) -> Option<&SavedFrame> {
match self {
GenState::SuspendedStart(s) | GenState::SuspendedYield(s) => Some(s),
GenState::Executing | GenState::Completed => None,
}
}
}
/// The bytes a suspended frame adds to its generator object, which a collection counts.
pub(crate) fn saved_bytes(s: &SavedFrame) -> usize {
GENERATOR_BYTES.saturating_add(SLOT_BYTES.saturating_mul(s.stack.len().saturating_add(s.sites.len()).saturating_add(s.marks.len())))
}
/// A generator resumed by `next`, `return` or `throw`, ready to be pushed: the value it was
/// resumed with and how, unless it had not started.
pub(crate) struct Resumed {
generator: ObjId,
saved: SavedFrame,
received: Option<(Value, ResumeMode)>,
}
impl Interp {
fn generator_state_mut(&mut self, g: ObjId) -> Option<&mut GenState> {
match self.heap.object_mut(g).map(|o| &mut o.class) {
Some(Class::Generator(s)) => Some(s),
_ => None,
}
}
/// Marks the generator `g`, whose frame was popped without being saved, completed.
pub(crate) fn end_generator(&mut self, g: Option) {
if let Some(state) = g.and_then(|g| self.generator_state_mut(g))
&& let GenState::SuspendedStart(saved) | GenState::SuspendedYield(saved) = std::mem::replace(state, GenState::Completed)
{
self.release_saved(&saved);
}
}
/// Takes a saved frame that left its generator object off the heap's total.
fn release_saved(&mut self, saved: &SavedFrame) {
self.heap.bytes = self.heap.bytes.saturating_sub(saved_bytes(saved));
}
/// Gives a frame's caller `v`: the Rust call that pushed a boundary frame, or the frame below.
fn deliver(&mut self, boundary: bool, v: Value) -> JsResult