1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * The mythical generator.
 */

use crate::frame::{ExecutionResult, FrameRef};
use crate::obj::objtype::{isinstance, PyClassRef};
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;

pub type PyGeneratorRef = PyRef<PyGenerator>;

#[pyclass(name = "generator")]
#[derive(Debug)]
pub struct PyGenerator {
    frame: FrameRef,
}

impl PyValue for PyGenerator {
    fn class(vm: &VirtualMachine) -> PyClassRef {
        vm.ctx.generator_type()
    }
}

#[pyimpl]
impl PyGenerator {
    pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyGeneratorRef {
        PyGenerator { frame }.into_ref(vm)
    }

    #[pymethod(name = "__iter__")]
    fn iter(zelf: PyGeneratorRef, _vm: &VirtualMachine) -> PyGeneratorRef {
        zelf
    }

    #[pymethod(name = "__next__")]
    fn next(&self, vm: &VirtualMachine) -> PyResult {
        self.send(vm.get_none(), vm)
    }

    #[pymethod]
    fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
        self.frame.push_value(value.clone());

        let result = vm.run_frame(self.frame.clone())?;
        handle_execution_result(result, vm)
    }

    #[pymethod]
    fn throw(
        &self,
        _exc_type: PyObjectRef,
        exc_val: PyObjectRef,
        _exc_tb: PyObjectRef,
        vm: &VirtualMachine,
    ) -> PyResult {
        // TODO what should we do with the other parameters? CPython normalises them with
        //      PyErr_NormalizeException, do we want to do the same.
        if !isinstance(&exc_val, &vm.ctx.exceptions.base_exception_type) {
            return Err(vm.new_type_error("Can't throw non exception".to_string()));
        }
        let result = vm.frame_throw(self.frame.clone(), exc_val)?;
        handle_execution_result(result, vm)
    }
}

fn handle_execution_result(result: ExecutionResult, vm: &VirtualMachine) -> PyResult {
    match result {
        ExecutionResult::Yield(value) => Ok(value),
        ExecutionResult::Return(_value) => {
            // Stop iteration!
            let stop_iteration = vm.ctx.exceptions.stop_iteration.clone();
            Err(vm.new_exception(stop_iteration, "End of generator".to_string()))
        }
    }
}

pub fn init(ctx: &PyContext) {
    PyGenerator::extend_class(ctx, &ctx.types.generator_type);
}