Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
generator
  • Loading branch information
OddCoincidence committed Mar 9, 2019
commit 6eea40799be84c0624cec7ba3fe073bad6aae65c
20 changes: 17 additions & 3 deletions vm/src/obj/objgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

use crate::frame::{ExecutionResult, Frame};
use crate::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::vm::VirtualMachine;

#[derive(Debug)]
pub struct PyGenerator {
frame: PyObjectRef,
}

impl PyObjectPayload2 for PyGenerator {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.generator_type()
}
}

pub fn init(context: &PyContext) {
let generator_type = &context.generator_type;
context.set_attr(
Expand All @@ -29,7 +41,9 @@ pub fn init(context: &PyContext) {

pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult {
Ok(PyObject::new(
PyObjectPayload::Generator { frame },
PyObjectPayload::AnyRustValue {
value: Box::new(PyGenerator { frame }),
},
vm.ctx.generator_type.clone(),
))
}
Expand All @@ -55,7 +69,7 @@ fn generator_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn send(vm: &mut VirtualMachine, gen: &PyObjectRef, value: &PyObjectRef) -> PyResult {
if let PyObjectPayload::Generator { ref frame } = gen.payload {
if let Some(PyGenerator { ref frame }) = gen.payload() {
if let Some(frame) = frame.payload::<Frame>() {
frame.push_value(value.clone());
} else {
Expand Down
4 changes: 0 additions & 4 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,9 +1508,6 @@ pub enum PyObjectPayload {
MemoryView {
obj: PyObjectRef,
},
Generator {
frame: PyObjectRef,
},
WeakRef {
referent: PyObjectWeakRef,
},
Expand All @@ -1534,7 +1531,6 @@ impl fmt::Debug for PyObjectPayload {
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
PyObjectPayload::Slice { .. } => write!(f, "slice"),
PyObjectPayload::Generator { .. } => write!(f, "generator"),
PyObjectPayload::AnyRustValue { value } => value.fmt(f),
}
}
Expand Down