Skip to content

Commit ecc92ff

Browse files
Convert int to Any payload
1 parent d7f8961 commit ecc92ff

File tree

6 files changed

+90
-74
lines changed

6 files changed

+90
-74
lines changed

vm/src/frame.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
extern crate rustpython_parser;
2-
3-
use self::rustpython_parser::ast;
41
use std::cell::RefCell;
52
use std::fmt;
63
use std::path::PathBuf;
4+
use std::rc::Rc;
5+
6+
use num_bigint::BigInt;
7+
8+
use rustpython_parser::ast;
79

810
use crate::builtins;
911
use crate::bytecode;
@@ -12,6 +14,7 @@ use crate::obj::objbool;
1214
use crate::obj::objcode;
1315
use crate::obj::objdict;
1416
use crate::obj::objdict::PyDict;
17+
use crate::obj::objint::PyInt;
1518
use crate::obj::objiter;
1619
use crate::obj::objlist;
1720
use crate::obj::objstr;
@@ -21,8 +24,6 @@ use crate::pyobject::{
2124
TypeProtocol,
2225
};
2326
use crate::vm::VirtualMachine;
24-
use num_bigint::BigInt;
25-
use std::rc::Rc;
2627

2728
/*
2829
* So a scope is a linked list of scopes.
@@ -286,10 +287,14 @@ impl Frame {
286287

287288
let mut out: Vec<Option<BigInt>> = elements
288289
.into_iter()
289-
.map(|x| match x.payload {
290-
PyObjectPayload::Integer { ref value } => Some(value.clone()),
291-
PyObjectPayload::None => None,
292-
_ => panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x),
290+
.map(|x| {
291+
if x.is(&vm.ctx.none()) {
292+
None
293+
} else if let Some(i) = x.payload::<PyInt>() {
294+
Some(i.value.clone())
295+
} else {
296+
panic!("Expect Int or None as BUILD_SLICE arguments")
297+
}
293298
})
294299
.collect();
295300

vm/src/obj/objbool.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use super::objdict::PyDict;
2-
use super::objfloat::PyFloat;
3-
use super::objstr::PyString;
4-
use super::objtype;
1+
use num_traits::Zero;
2+
53
use crate::pyobject::{
64
IntoPyObject, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
75
};
86
use crate::vm::VirtualMachine;
9-
use num_traits::Zero;
7+
8+
use super::objdict::PyDict;
9+
use super::objfloat::PyFloat;
10+
use super::objint::PyInt;
11+
use super::objstr::PyString;
12+
use super::objtype;
1013

1114
impl IntoPyObject for bool {
1215
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
@@ -24,16 +27,18 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
2427
if let Some(dict) = obj.payload::<PyDict>() {
2528
return Ok(!dict.entries.borrow().is_empty());
2629
}
30+
if let Some(i) = obj.payload::<PyInt>() {
31+
return Ok(!i.value.is_zero());
32+
}
2733
let result = match obj.payload {
28-
PyObjectPayload::Integer { ref value } => !value.is_zero(),
2934
PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(),
3035
PyObjectPayload::None { .. } => false,
3136
_ => {
3237
if let Ok(f) = vm.get_method(obj.clone(), "__bool__") {
3338
let bool_res = vm.invoke(f, PyFuncArgs::default())?;
34-
match bool_res.payload {
35-
PyObjectPayload::Integer { ref value } => !value.is_zero(),
36-
_ => return Err(vm.new_type_error(String::from("TypeError"))),
39+
match bool_res.payload::<PyInt>() {
40+
Some(i) => !i.value.is_zero(),
41+
None => return Err(vm.new_type_error(String::from("TypeError"))),
3742
}
3843
} else {
3944
true
@@ -67,11 +72,7 @@ pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {
6772

6873
// Retrieve inner int value:
6974
pub fn get_value(obj: &PyObjectRef) -> bool {
70-
if let PyObjectPayload::Integer { value } = &obj.payload {
71-
!value.is_zero()
72-
} else {
73-
panic!("Inner error getting inner boolean");
74-
}
75+
!obj.payload::<PyInt>().unwrap().value.is_zero()
7576
}
7677

7778
fn bool_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {

vm/src/obj/objint.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
use super::objfloat;
2-
use super::objstr;
3-
use super::objtype;
1+
use std::hash::{Hash, Hasher};
2+
3+
use num_bigint::{BigInt, ToBigInt};
4+
use num_integer::Integer;
5+
use num_traits::{Pow, Signed, ToPrimitive, Zero};
6+
47
use crate::format::FormatSpec;
58
use crate::pyobject::{
6-
FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
7-
PyResult, TryFromObject, TypeProtocol,
9+
FromPyObjectRef, IntoPyObject, PyContext, PyFuncArgs, PyObject, PyObjectPayload,
10+
PyObjectPayload2, PyObjectRef, PyResult, TryFromObject, TypeProtocol,
811
};
912
use crate::vm::VirtualMachine;
10-
use num_bigint::{BigInt, ToBigInt};
11-
use num_integer::Integer;
12-
use num_traits::{Pow, Signed, ToPrimitive, Zero};
13-
use std::hash::{Hash, Hasher};
1413

15-
// This proxy allows for easy switching between types.
16-
type IntType = BigInt;
14+
use super::objfloat;
15+
use super::objstr;
16+
use super::objtype;
1717

18-
pub type PyInt = BigInt;
18+
#[derive(Debug)]
19+
pub struct PyInt {
20+
// TODO: shouldn't be public
21+
pub value: BigInt,
22+
}
1923

20-
impl IntoPyObject for PyInt {
21-
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
22-
Ok(ctx.new_int(self))
24+
impl PyInt {
25+
pub fn new<T: ToBigInt>(i: T) -> Self {
26+
PyInt {
27+
value: i.to_bigint().unwrap(),
28+
}
2329
}
2430
}
2531

26-
// TODO: macro to impl for all primitive ints
32+
impl PyObjectPayload2 for PyInt {
33+
fn required_type(ctx: &PyContext) -> PyObjectRef {
34+
ctx.int_type()
35+
}
36+
}
2737

2838
impl IntoPyObject for usize {
2939
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
@@ -77,13 +87,15 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7787
None => Zero::zero(),
7888
};
7989
Ok(PyObject::new(
80-
PyObjectPayload::Integer { value: val },
90+
PyObjectPayload::AnyRustValue {
91+
value: Box::new(PyInt::new(val)),
92+
},
8193
cls.clone(),
8294
))
8395
}
8496

8597
// Casting function:
86-
pub fn to_int(vm: &mut VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult<IntType> {
98+
pub fn to_int(vm: &mut VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult<BigInt> {
8799
let val = if objtype::isinstance(obj, &vm.ctx.int_type()) {
88100
get_value(obj)
89101
} else if objtype::isinstance(obj, &vm.ctx.float_type()) {
@@ -111,12 +123,8 @@ pub fn to_int(vm: &mut VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult
111123
}
112124

113125
// Retrieve inner int value:
114-
pub fn get_value(obj: &PyObjectRef) -> IntType {
115-
if let PyObjectPayload::Integer { value } = &obj.payload {
116-
value.clone()
117-
} else {
118-
panic!("Inner error getting int {:?}", obj);
119-
}
126+
pub fn get_value(obj: &PyObjectRef) -> BigInt {
127+
obj.payload::<PyInt>().unwrap().value.clone()
120128
}
121129

122130
impl FromPyObjectRef for BigInt {

vm/src/obj/objrange.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use std::cell::Cell;
22
use std::ops::Mul;
33

4-
use super::objint;
5-
use super::objtype;
4+
use num_bigint::{BigInt, Sign};
5+
use num_integer::Integer;
6+
use num_traits::{One, Signed, ToPrimitive, Zero};
7+
68
use crate::pyobject::{
79
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
810
TypeProtocol,
911
};
1012
use crate::vm::VirtualMachine;
11-
use num_bigint::{BigInt, Sign};
12-
use num_integer::Integer;
13-
use num_traits::{One, Signed, ToPrimitive, Zero};
13+
14+
use super::objint::{self, PyInt};
15+
use super::objtype;
1416

1517
#[derive(Debug, Clone)]
1618
pub struct PyRange {
@@ -284,14 +286,15 @@ fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
284286

285287
let range = get_value(zelf);
286288

289+
if let Some(i) = subscript.payload::<PyInt>() {
290+
return if let Some(int) = range.get(i.value.clone()) {
291+
Ok(vm.ctx.new_int(int))
292+
} else {
293+
Err(vm.new_index_error("range object index out of range".to_string()))
294+
};
295+
}
296+
287297
match subscript.payload {
288-
PyObjectPayload::Integer { ref value } => {
289-
if let Some(int) = range.get(value) {
290-
Ok(vm.ctx.new_int(int))
291-
} else {
292-
Err(vm.new_index_error("range object index out of range".to_string()))
293-
}
294-
}
295298
PyObjectPayload::Slice {
296299
ref start,
297300
ref stop,

vm/src/obj/objsequence.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::marker::Sized;
33
use std::ops::{Deref, DerefMut, Range};
44

55
use super::objbool;
6-
use super::objint;
6+
use super::objint::{self, PyInt};
77
use crate::pyobject::{IdProtocol, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol};
88
use crate::vm::VirtualMachine;
99
use num_bigint::BigInt;
@@ -141,8 +141,8 @@ pub fn get_item(
141141
elements: &[PyObjectRef],
142142
subscript: PyObjectRef,
143143
) -> PyResult {
144-
match &subscript.payload {
145-
PyObjectPayload::Integer { value } => match value.to_i32() {
144+
if let Some(i) = subscript.payload::<PyInt>() {
145+
return match i.value.to_i32() {
146146
Some(value) => {
147147
if let Some(pos_index) = elements.to_vec().get_pos(value) {
148148
let obj = elements[pos_index].clone();
@@ -154,8 +154,9 @@ pub fn get_item(
154154
None => {
155155
Err(vm.new_index_error("cannot fit 'int' into an index-sized integer".to_string()))
156156
}
157-
},
158-
157+
};
158+
}
159+
match &subscript.payload {
159160
PyObjectPayload::Slice { .. } => Ok(PyObject::new(
160161
match &sequence.payload {
161162
PyObjectPayload::Sequence { .. } => PyObjectPayload::Sequence {

vm/src/pyobject.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::obj::objfloat::{self, PyFloat};
2626
use crate::obj::objframe;
2727
use crate::obj::objfunction;
2828
use crate::obj::objgenerator;
29-
use crate::obj::objint;
29+
use crate::obj::objint::{self, PyInt};
3030
use crate::obj::objiter;
3131
use crate::obj::objlist;
3232
use crate::obj::objmap;
@@ -236,12 +236,14 @@ impl PyContext {
236236
);
237237

238238
let true_value = PyObject::new(
239-
PyObjectPayload::Integer { value: One::one() },
239+
PyObjectPayload::AnyRustValue {
240+
value: Box::new(PyInt::new(BigInt::one())),
241+
},
240242
bool_type.clone(),
241243
);
242244
let false_value = PyObject::new(
243-
PyObjectPayload::Integer {
244-
value: Zero::zero(),
245+
PyObjectPayload::AnyRustValue {
246+
value: Box::new(PyInt::new(BigInt::zero())),
245247
},
246248
bool_type.clone(),
247249
);
@@ -474,8 +476,8 @@ impl PyContext {
474476

475477
pub fn new_int<T: ToBigInt>(&self, i: T) -> PyObjectRef {
476478
PyObject::new(
477-
PyObjectPayload::Integer {
478-
value: i.to_bigint().unwrap(),
479+
PyObjectPayload::AnyRustValue {
480+
value: Box::new(PyInt::new(i)),
479481
},
480482
self.int_type(),
481483
)
@@ -1445,9 +1447,6 @@ into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E));
14451447
/// of rust data for a particular python object. Determine the python type
14461448
/// by using for example the `.typ()` method on a python object.
14471449
pub enum PyObjectPayload {
1448-
Integer {
1449-
value: BigInt,
1450-
},
14511450
Sequence {
14521451
elements: RefCell<Vec<PyObjectRef>>,
14531452
},
@@ -1524,7 +1523,6 @@ pub enum PyObjectPayload {
15241523
impl fmt::Debug for PyObjectPayload {
15251524
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15261525
match self {
1527-
PyObjectPayload::Integer { ref value } => write!(f, "int {}", value),
15281526
PyObjectPayload::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
15291527
PyObjectPayload::Sequence { .. } => write!(f, "list or tuple"),
15301528
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),

0 commit comments

Comments
 (0)