Skip to content

Commit f10fa6d

Browse files
Remove outer RefCell from PyObjectRef
1 parent 027a684 commit f10fa6d

33 files changed

+380
-425
lines changed

vm/src/frame.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl Frame {
281281

282282
let mut out: Vec<Option<BigInt>> = elements
283283
.into_iter()
284-
.map(|x| match x.borrow().payload {
284+
.map(|x| match x.payload {
285285
PyObjectPayload::Integer { ref value } => Some(value.clone()),
286286
PyObjectPayload::None => None,
287287
_ => panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x),
@@ -531,7 +531,7 @@ impl Frame {
531531
} else {
532532
let msg = format!(
533533
"Can only raise BaseException derived types, not {}",
534-
exception.borrow()
534+
exception
535535
);
536536
let type_error_type = vm.ctx.exceptions.type_error.clone();
537537
let type_error = vm.new_exception(type_error_type, msg);
@@ -564,7 +564,7 @@ impl Frame {
564564
}
565565
bytecode::Instruction::PrintExpr => {
566566
let expr = self.pop_value();
567-
match expr.borrow().payload {
567+
match expr.payload {
568568
PyObjectPayload::None => (),
569569
_ => {
570570
let repr = vm.to_repr(&expr)?;
@@ -591,9 +591,9 @@ impl Frame {
591591
}
592592
bytecode::Instruction::StoreLocals => {
593593
let locals = self.pop_value();
594-
match self.locals.borrow_mut().payload {
595-
PyObjectPayload::Scope { ref mut scope } => {
596-
scope.locals = locals;
594+
match self.locals.payload {
595+
PyObjectPayload::Scope { ref scope } => {
596+
//scope.locals = locals; // FIXME
597597
}
598598
_ => panic!("We really expect our scope to be a scope!"),
599599
}
@@ -854,7 +854,7 @@ impl Frame {
854854
}
855855

856856
fn delete_name(&mut self, vm: &mut VirtualMachine, name: &str) -> FrameResult {
857-
let locals = match self.locals.borrow().payload {
857+
let locals = match self.locals.payload {
858858
PyObjectPayload::Scope { ref scope } => scope.locals.clone(),
859859
_ => panic!("We really expect our scope to be a scope!"),
860860
};
@@ -1127,7 +1127,7 @@ impl fmt::Debug for Frame {
11271127
let stack_str = self
11281128
.stack
11291129
.iter()
1130-
.map(|elem| format!("\n > {:?}", elem.borrow()))
1130+
.map(|elem| format!("\n > {:?}", elem))
11311131
.collect::<Vec<_>>()
11321132
.join("");
11331133
let block_str = self
@@ -1136,12 +1136,12 @@ impl fmt::Debug for Frame {
11361136
.map(|elem| format!("\n > {:?}", elem))
11371137
.collect::<Vec<_>>()
11381138
.join("");
1139-
let local_str = match self.locals.borrow().payload {
1140-
PyObjectPayload::Scope { ref scope } => match scope.locals.borrow().payload {
1139+
let local_str = match self.locals.payload {
1140+
PyObjectPayload::Scope { ref scope } => match scope.locals.payload {
11411141
PyObjectPayload::Dict { ref elements } => {
1142-
objdict::get_key_value_pairs_from_content(elements)
1142+
objdict::get_key_value_pairs_from_content(&elements.borrow())
11431143
.iter()
1144-
.map(|elem| format!("\n {:?} = {:?}", elem.0.borrow(), elem.1.borrow()))
1144+
.map(|elem| format!("\n {:?} = {:?}", elem.0, elem.1))
11451145
.collect::<Vec<_>>()
11461146
.join("")
11471147
}

vm/src/obj/objbool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ impl IntoPyObject for bool {
1212
}
1313

1414
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObjectRef> {
15-
let result = match obj.borrow().payload {
15+
let result = match obj.payload {
1616
PyObjectPayload::Integer { ref value } => !value.is_zero(),
1717
PyObjectPayload::Float { value } => value != 0.0,
18-
PyObjectPayload::Sequence { ref elements } => !elements.is_empty(),
19-
PyObjectPayload::Dict { ref elements } => !elements.is_empty(),
18+
PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(),
19+
PyObjectPayload::Dict { ref elements } => !elements.borrow().is_empty(),
2020
PyObjectPayload::String { ref value } => !value.is_empty(),
2121
PyObjectPayload::None { .. } => false,
2222
_ => {
2323
if let Ok(f) = vm.get_method(obj.clone(), "__bool__") {
2424
let bool_res = vm.invoke(f, PyFuncArgs::default())?;
25-
let v = match bool_res.borrow().payload {
25+
let v = match bool_res.payload {
2626
PyObjectPayload::Integer { ref value } => !value.is_zero(),
2727
_ => return Err(vm.new_type_error(String::from("TypeError"))),
2828
};
@@ -60,7 +60,7 @@ pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {
6060

6161
// Retrieve inner int value:
6262
pub fn get_value(obj: &PyObjectRef) -> bool {
63-
if let PyObjectPayload::Integer { value } = &obj.borrow().payload {
63+
if let PyObjectPayload::Integer { value } = &obj.payload {
6464
!value.is_zero()
6565
} else {
6666
panic!("Inner error getting inner boolean");

vm/src/obj/objbytearray.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Implementation of the python bytearray object.
22
3+
use std::cell::RefCell;
4+
35
use crate::pyobject::{PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyResult, TypeProtocol};
46

57
use super::objint;
@@ -140,7 +142,12 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
140142
} else {
141143
vec![]
142144
};
143-
Ok(PyObject::new(PyObjectPayload::Bytes { value }, cls.clone()))
145+
Ok(PyObject::new(
146+
PyObjectPayload::Bytes {
147+
value: RefCell::new(value),
148+
},
149+
cls.clone(),
150+
))
144151
}
145152

146153
fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -283,10 +290,9 @@ fn bytearray_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
283290

284291
fn bytearray_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
285292
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
286-
let mut mut_obj = zelf.borrow_mut();
287-
match mut_obj.payload {
288-
PyObjectPayload::Bytes { ref mut value } => {
289-
value.clear();
293+
match zelf.payload {
294+
PyObjectPayload::Bytes { ref value } => {
295+
value.borrow_mut().clear();
290296
Ok(vm.get_none())
291297
}
292298
_ => panic!("Bytearray has incorrect payload."),
@@ -307,17 +313,11 @@ fn bytearray_pop(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
307313
fn bytearray_lower(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
308314
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytearray_type()))]);
309315
let value = get_value(obj).to_vec().to_ascii_lowercase();
310-
Ok(PyObject::new(
311-
PyObjectPayload::Bytes { value },
312-
vm.ctx.bytearray_type(),
313-
))
316+
Ok(vm.ctx.new_bytearray(value))
314317
}
315318

316319
fn bytearray_upper(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
317320
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytearray_type()))]);
318321
let value = get_value(obj).to_vec().to_ascii_uppercase();
319-
Ok(PyObject::new(
320-
PyObjectPayload::Bytes { value },
321-
vm.ctx.bytearray_type(),
322-
))
322+
Ok(vm.ctx.new_bytearray(value))
323323
}

vm/src/obj/objbytes.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
use std::cell::RefCell;
2+
use std::hash::{Hash, Hasher};
3+
use std::ops::Deref;
4+
use std::ops::DerefMut;
5+
16
use super::objint;
27
use super::objtype;
38
use crate::pyobject::{
49
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
510
};
611
use crate::vm::VirtualMachine;
712
use num_traits::ToPrimitive;
8-
use std::cell::Ref;
9-
use std::cell::RefMut;
10-
use std::hash::{Hash, Hasher};
11-
use std::ops::Deref;
12-
use std::ops::DerefMut;
1313

1414
// Binary data support
1515

@@ -70,7 +70,12 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7070
vec![]
7171
};
7272

73-
Ok(PyObject::new(PyObjectPayload::Bytes { value }, cls.clone()))
73+
Ok(PyObject::new(
74+
PyObjectPayload::Bytes {
75+
value: RefCell::new(value),
76+
},
77+
cls.clone(),
78+
))
7479
}
7580

7681
fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -98,11 +103,7 @@ fn bytes_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
98103
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
99104
get_value(a).to_vec() >= get_value(b).to_vec()
100105
} else {
101-
return Err(vm.new_type_error(format!(
102-
"Cannot compare {} and {} using '>'",
103-
a.borrow(),
104-
b.borrow()
105-
)));
106+
return Err(vm.new_type_error(format!("Cannot compare {} and {} using '>'", a, b)));
106107
};
107108
Ok(vm.ctx.new_bool(result))
108109
}
@@ -117,11 +118,7 @@ fn bytes_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
117118
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
118119
get_value(a).to_vec() > get_value(b).to_vec()
119120
} else {
120-
return Err(vm.new_type_error(format!(
121-
"Cannot compare {} and {} using '>='",
122-
a.borrow(),
123-
b.borrow()
124-
)));
121+
return Err(vm.new_type_error(format!("Cannot compare {} and {} using '>='", a, b)));
125122
};
126123
Ok(vm.ctx.new_bool(result))
127124
}
@@ -136,11 +133,7 @@ fn bytes_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
136133
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
137134
get_value(a).to_vec() <= get_value(b).to_vec()
138135
} else {
139-
return Err(vm.new_type_error(format!(
140-
"Cannot compare {} and {} using '<'",
141-
a.borrow(),
142-
b.borrow()
143-
)));
136+
return Err(vm.new_type_error(format!("Cannot compare {} and {} using '<'", a, b)));
144137
};
145138
Ok(vm.ctx.new_bool(result))
146139
}
@@ -155,11 +148,7 @@ fn bytes_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
155148
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
156149
get_value(a).to_vec() < get_value(b).to_vec()
157150
} else {
158-
return Err(vm.new_type_error(format!(
159-
"Cannot compare {} and {} using '<='",
160-
a.borrow(),
161-
b.borrow()
162-
)));
151+
return Err(vm.new_type_error(format!("Cannot compare {} and {} using '<='", a, b)));
163152
};
164153
Ok(vm.ctx.new_bool(result))
165154
}
@@ -181,23 +170,19 @@ fn bytes_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
181170
}
182171

183172
pub fn get_value<'a>(obj: &'a PyObjectRef) -> impl Deref<Target = Vec<u8>> + 'a {
184-
Ref::map(obj.borrow(), |py_obj| {
185-
if let PyObjectPayload::Bytes { ref value } = py_obj.payload {
186-
value
187-
} else {
188-
panic!("Inner error getting bytearray {:?}", obj);
189-
}
190-
})
173+
if let PyObjectPayload::Bytes { ref value } = obj.payload {
174+
value.borrow()
175+
} else {
176+
panic!("Inner error getting bytearray {:?}", obj);
177+
}
191178
}
192179

193180
pub fn get_mut_value<'a>(obj: &'a PyObjectRef) -> impl DerefMut<Target = Vec<u8>> + 'a {
194-
RefMut::map(obj.borrow_mut(), |py_obj| {
195-
if let PyObjectPayload::Bytes { ref mut value } = py_obj.payload {
196-
value
197-
} else {
198-
panic!("Inner error getting bytearray {:?}", obj);
199-
}
200-
})
181+
if let PyObjectPayload::Bytes { ref value } = obj.payload {
182+
value.borrow_mut()
183+
} else {
184+
panic!("Inner error getting bytearray {:?}", obj);
185+
}
201186
}
202187

203188
fn bytes_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn init(context: &PyContext) {
2929
}
3030

3131
pub fn get_value(obj: &PyObjectRef) -> bytecode::CodeObject {
32-
if let PyObjectPayload::Code { code } = &obj.borrow().payload {
32+
if let PyObjectPayload::Code { code } = &obj.payload {
3333
code.clone()
3434
} else {
3535
panic!("Inner error getting code {:?}", obj)

vm/src/obj/objcomplex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn init(context: &PyContext) {
4545
}
4646

4747
pub fn get_value(obj: &PyObjectRef) -> Complex64 {
48-
if let PyObjectPayload::Complex { value } = &obj.borrow().payload {
48+
if let PyObjectPayload::Complex { value } = &obj.payload {
4949
*value
5050
} else {
5151
panic!("Inner error getting complex");
@@ -117,7 +117,7 @@ fn complex_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
117117
v1.im,
118118
)))
119119
} else {
120-
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
120+
Err(vm.new_type_error(format!("Cannot add {} and {}", i, i2)))
121121
}
122122
}
123123

@@ -136,7 +136,7 @@ fn complex_radd(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
136136
v1.im,
137137
)))
138138
} else {
139-
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
139+
Err(vm.new_type_error(format!("Cannot add {} and {}", i, i2)))
140140
}
141141
}
142142

vm/src/obj/objdict.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::pyobject::{
66
TypeProtocol,
77
};
88
use crate::vm::{ReprGuard, VirtualMachine};
9-
use std::cell::{Ref, RefCell, RefMut};
9+
use std::cell::RefCell;
1010
use std::collections::HashMap;
1111
use std::ops::{Deref, DerefMut};
1212

@@ -19,30 +19,26 @@ pub type DictContentType = HashMap<String, (PyObjectRef, PyObjectRef)>;
1919
pub fn new(dict_type: PyObjectRef) -> PyObjectRef {
2020
PyObject::new(
2121
PyObjectPayload::Dict {
22-
elements: HashMap::new(),
22+
elements: RefCell::new(HashMap::new()),
2323
},
2424
dict_type.clone(),
2525
)
2626
}
2727

2828
pub fn get_elements<'a>(obj: &'a PyObjectRef) -> impl Deref<Target = DictContentType> + 'a {
29-
Ref::map(obj.borrow(), |py_obj| {
30-
if let PyObjectPayload::Dict { ref elements } = py_obj.payload {
31-
elements
32-
} else {
33-
panic!("Cannot extract dict elements");
34-
}
35-
})
29+
if let PyObjectPayload::Dict { ref elements } = obj.payload {
30+
elements.borrow()
31+
} else {
32+
panic!("Cannot extract dict elements");
33+
}
3634
}
3735

3836
fn get_mut_elements<'a>(obj: &'a PyObjectRef) -> impl DerefMut<Target = DictContentType> + 'a {
39-
RefMut::map(obj.borrow_mut(), |py_obj| {
40-
if let PyObjectPayload::Dict { ref mut elements } = py_obj.payload {
41-
elements
42-
} else {
43-
panic!("Cannot extract dict elements");
44-
}
45-
})
37+
if let PyObjectPayload::Dict { ref elements } = obj.payload {
38+
elements.borrow_mut()
39+
} else {
40+
panic!("Cannot extract dict elements");
41+
}
4642
}
4743

4844
pub fn set_item(
@@ -309,12 +305,16 @@ fn dict_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
309305
}
310306

311307
pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, dict_type: PyObjectRef) {
312-
(*dict_type.borrow_mut()).payload = PyObjectPayload::Class {
313-
name: String::from("dict"),
314-
dict: RefCell::new(HashMap::new()),
315-
mro: vec![object_type],
316-
};
317-
(*dict_type.borrow_mut()).typ = Some(type_type.clone());
308+
// this is not ideal
309+
let ptr = PyObjectRef::into_raw(dict_type.clone()) as *mut PyObject;
310+
unsafe {
311+
(*ptr).payload = PyObjectPayload::Class {
312+
name: String::from("dict"),
313+
dict: RefCell::new(HashMap::new()),
314+
mro: vec![object_type],
315+
};
316+
(*ptr).typ = Some(type_type.clone());
317+
}
318318
}
319319

320320
pub fn init(context: &PyContext) {

0 commit comments

Comments
 (0)