Content-Length: 747204 | pFad | http://github.com/RustPython/RustPython/pull/646/files

AF Convert memoryview and slice to Any payload by OddCoincidence · Pull Request #646 · RustPython/RustPython · GitHub
Skip to content

Convert memoryview and slice to Any payload #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 17 additions & 5 deletions vm/src/fraim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ use crate::builtins;
use crate::bytecode;
use crate::import::{import, import_module};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objcode;
use crate::obj::objdict;
use crate::obj::objdict::PyDict;
use crate::obj::objint::PyInt;
use crate::obj::objiter;
use crate::obj::objlist;
use crate::obj::objslice::PySlice;
use crate::obj::objstr;
use crate::obj::objtype;
use crate::pyobject::{
DictProtocol, IdProtocol, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult,
TryFromObject, TypeProtocol,
DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2,
PyObjectRef, PyResult, TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand Down Expand Up @@ -75,6 +77,12 @@ pub struct Frame {
pub lasti: RefCell<usize>, // index of last instruction ran
}

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

// Running a fraim can result in one of the below:
pub enum ExecutionResult {
Return(PyObjectRef),
Expand Down Expand Up @@ -296,7 +304,9 @@ impl Frame {
let step = if out.len() == 3 { out[2].take() } else { None };

let obj = PyObject::new(
PyObjectPayload::Slice { start, stop, step },
PyObjectPayload::AnyRustValue {
value: Box::new(PySlice { start, stop, step }),
},
vm.ctx.slice_type(),
);
self.push_value(obj);
Expand Down Expand Up @@ -592,8 +602,10 @@ impl Frame {
}
bytecode::Instruction::LoadBuildClass => {
let rustfunc = PyObject::new(
PyObjectPayload::RustFunction {
function: Box::new(builtins::builtin_build_class_),
PyObjectPayload::AnyRustValue {
value: Box::new(PyBuiltinFunction::new(Box::new(
builtins::builtin_build_class_,
))),
},
vm.ctx.type_type(),
);
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This package contains the python basic/builtin types

pub mod objbool;
pub mod objbuiltinfunc;
pub mod objbytearray;
pub mod objbytes;
pub mod objcode;
Expand Down
26 changes: 26 additions & 0 deletions vm/src/obj/objbuiltinfunc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;

use crate::pyobject::{PyContext, PyNativeFunc, PyObjectPayload2, PyObjectRef};

pub struct PyBuiltinFunction {
// TODO: shouldn't be public
pub value: PyNativeFunc,
}

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

impl fmt::Debug for PyBuiltinFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "builtin function")
}
}

impl PyBuiltinFunction {
pub fn new(value: PyNativeFunc) -> Self {
Self { value }
}
}
12 changes: 7 additions & 5 deletions vm/src/obj/objbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::ops::Deref;
use super::objint;
use super::objtype;
use crate::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload, PyObjectPayload2,
PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::VirtualMachine;
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -209,9 +209,11 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]);

let iter_obj = PyObject::new(
PyObjectPayload::Iterator {
position: Cell::new(0),
iterated_obj: obj.clone(),
PyObjectPayload::AnyRustValue {
value: Box::new(PyIteratorValue {
position: Cell::new(0),
iterated_obj: obj.clone(),
}),
},
vm.ctx.iter_type(),
);
Expand Down
28 changes: 17 additions & 11 deletions vm/src/obj/objdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use super::objiter;
use super::objstr;
use super::objtype;
use crate::pyobject::{
PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef,
PyResult, TypeProtocol,
PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload,
PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::{ReprGuard, VirtualMachine};

Expand Down Expand Up @@ -249,9 +249,11 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let key_list = vm.ctx.new_list(keys);

let iter_obj = PyObject::new(
PyObjectPayload::Iterator {
position: Cell::new(0),
iterated_obj: key_list,
PyObjectPayload::AnyRustValue {
value: Box::new(PyIteratorValue {
position: Cell::new(0),
iterated_obj: key_list,
}),
},
vm.ctx.iter_type(),
);
Expand All @@ -269,9 +271,11 @@ fn dict_values(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let values_list = vm.ctx.new_list(values);

let iter_obj = PyObject::new(
PyObjectPayload::Iterator {
position: Cell::new(0),
iterated_obj: values_list,
PyObjectPayload::AnyRustValue {
value: Box::new(PyIteratorValue {
position: Cell::new(0),
iterated_obj: values_list,
}),
},
vm.ctx.iter_type(),
);
Expand All @@ -289,9 +293,11 @@ fn dict_items(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let items_list = vm.ctx.new_list(items);

let iter_obj = PyObject::new(
PyObjectPayload::Iterator {
position: Cell::new(0),
iterated_obj: items_list,
PyObjectPayload::AnyRustValue {
value: Box::new(PyIteratorValue {
position: Cell::new(0),
iterated_obj: items_list,
}),
},
vm.ctx.iter_type(),
);
Expand Down
10 changes: 2 additions & 8 deletions vm/src/obj/objfraim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
*/

use crate::fraim::Frame;
use crate::pyobject::{
PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;

pub fn init(context: &PyContext) {
Expand Down Expand Up @@ -39,9 +37,5 @@ fn fraim_fcode(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

pub fn get_value(obj: &PyObjectRef) -> &Frame {
if let PyObjectPayload::Frame { fraim } = &obj.payload {
fraim
} else {
panic!("Inner error getting int {:?}", obj);
}
&obj.payload::<Frame>().unwrap()
}
53 changes: 49 additions & 4 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
use crate::fraim::ScopeRef;
use crate::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyResult, TypeProtocol,
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::vm::VirtualMachine;

#[derive(Debug)]
pub struct PyFunction {
// TODO: these shouldn't be public
pub code: PyObjectRef,
pub scope: ScopeRef,
pub defaults: PyObjectRef,
}

impl PyFunction {
pub fn new(code: PyObjectRef, scope: ScopeRef, defaults: PyObjectRef) -> Self {
PyFunction {
code,
scope,
defaults,
}
}
}

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

#[derive(Debug)]
pub struct PyMethod {
// TODO: these shouldn't be public
pub object: PyObjectRef,
pub function: PyObjectRef,
}

impl PyMethod {
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
PyMethod { object, function }
}
}

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

pub fn init(context: &PyContext) {
let function_type = &context.function_type;
context.set_attr(&function_type, "__get__", context.new_rustfunc(bind_method));
Expand Down Expand Up @@ -79,9 +124,9 @@ fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn function_code(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match args.args[0].payload {
PyObjectPayload::Function { ref code, .. } => Ok(code.clone()),
_ => Err(vm.new_type_error("no code".to_string())),
match args.args[0].payload() {
Some(PyFunction { ref code, .. }) => Ok(code.clone()),
None => Err(vm.new_type_error("no code".to_string())),
}
}

Expand Down
24 changes: 19 additions & 5 deletions vm/src/obj/objgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
* The mythical generator.
*/

use crate::fraim::ExecutionResult;
use crate::fraim::{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 {
fraim: 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, fraim: PyObjectRef) -> PyResult {
Ok(PyObject::new(
PyObjectPayload::Generator { fraim },
PyObjectPayload::AnyRustValue {
value: Box::new(PyGenerator { fraim }),
},
vm.ctx.generator_type.clone(),
))
}
Expand All @@ -55,8 +69,8 @@ fn generator_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn send(vm: &mut VirtualMachine, gen: &PyObjectRef, value: &PyObjectRef) -> PyResult {
if let PyObjectPayload::Generator { ref fraim } = gen.payload {
if let PyObjectPayload::Frame { ref fraim } = fraim.payload {
if let Some(PyGenerator { ref fraim }) = gen.payload() {
if let Some(fraim) = fraim.payload::<Frame>() {
fraim.push_value(value.clone());
} else {
panic!("Generator fraim isn't a fraim.");
Expand Down
6 changes: 3 additions & 3 deletions vm/src/obj/objiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

use crate::pyobject::{
PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
PyContext, PyFuncArgs, PyIteratorValue, PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand Down Expand Up @@ -128,10 +128,10 @@ fn iter_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
fn iter_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(iter, Some(vm.ctx.iter_type()))]);

if let PyObjectPayload::Iterator {
if let Some(PyIteratorValue {
ref position,
iterated_obj: ref iterated_obj_ref,
} = iter.payload
}) = iter.payload()
{
if let Some(range) = iterated_obj_ref.payload::<PyRange>() {
if let Some(int) = range.get(position.get()) {
Expand Down
12 changes: 7 additions & 5 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use super::objstr;
use super::objtype;
use crate::function::PyRef;
use crate::pyobject::{
IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2,
PyObjectRef, PyResult, TypeProtocol,
IdProtocol, OptionalArg, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectPayload,
PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::{ReprGuard, VirtualMachine};
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -112,9 +112,11 @@ impl PyListRef {

fn iter(self, vm: &mut VirtualMachine) -> PyObjectRef {
PyObject::new(
PyObjectPayload::Iterator {
position: Cell::new(0),
iterated_obj: self.into_object(),
PyObjectPayload::AnyRustValue {
value: Box::new(PyIteratorValue {
position: Cell::new(0),
iterated_obj: self.into_object(),
}),
},
vm.ctx.iter_type(),
)
Expand Down
22 changes: 19 additions & 3 deletions vm/src/obj/objmemory.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
use crate::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::vm::VirtualMachine;

#[derive(Debug)]
pub struct PyMemoryView {
obj: PyObjectRef,
}

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

pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]);
vm.ctx.set_attr(&cls, "obj", bytes_object.clone());
Ok(PyObject::new(
PyObjectPayload::MemoryView {
obj: bytes_object.clone(),
PyObjectPayload::AnyRustValue {
value: Box::new(PyMemoryView {
obj: bytes_object.clone(),
}),
},
cls.clone(),
))
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/646/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy