Skip to content

Commit 1d854a8

Browse files
committed
Modify isinstance call signature to take reference to a python reference.
1 parent af1f8f9 commit 1d854a8

17 files changed

+104
-131
lines changed

vm/src/builtins.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
177177
// TODO: handle optional global and locals
178178

179179
// Determine code object:
180-
let code_obj = if objtype::isinstance(source, vm.ctx.str_type()) {
180+
let code_obj = if objtype::isinstance(source, &vm.ctx.str_type()) {
181181
let mode = compile::Mode::Exec;
182182
let source = objstr::get_value(source);
183183
compile::compile(vm, &source, mode, None)?
@@ -258,7 +258,7 @@ fn builtin_id(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
258258
fn builtin_isinstance(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
259259
arg_check!(vm, args, required = [(obj, None), (typ, None)]);
260260

261-
let isinstance = objtype::isinstance(obj, typ.clone());
261+
let isinstance = objtype::isinstance(obj, typ);
262262
Ok(vm.context().new_bool(isinstance))
263263
}
264264

@@ -268,7 +268,7 @@ fn builtin_issubclass(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
268268
}
269269

270270
let cls1 = &args.args[0];
271-
let cls2 = args.args[1].clone();
271+
let cls2 = &args.args[1];
272272

273273
Ok(vm.context().new_bool(objtype::issubclass(cls1, cls2)))
274274
}
@@ -374,7 +374,7 @@ fn builtin_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
374374
match vm.call_method(iterator, "__next__", vec![]) {
375375
Ok(value) => Ok(value),
376376
Err(value) => {
377-
if objtype::isinstance(&value, vm.ctx.exceptions.stop_iteration.clone()) {
377+
if objtype::isinstance(&value, &vm.ctx.exceptions.stop_iteration) {
378378
match default_value {
379379
None => Err(value),
380380
Some(value) => Ok(value.clone()),

vm/src/exceptions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ fn exception_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2424
pub fn print_exception(vm: &mut VirtualMachine, exc: &PyObjectRef) {
2525
if let Some(tb) = exc.get_attr("__traceback__") {
2626
println!("Traceback (most recent call last):");
27-
if objtype::isinstance(&tb, vm.ctx.list_type()) {
27+
if objtype::isinstance(&tb, &vm.ctx.list_type()) {
2828
let mut elements = objlist::get_elements(&tb);
2929
elements.reverse();
3030
for element in elements {
31-
if objtype::isinstance(&element, vm.ctx.tuple_type()) {
31+
if objtype::isinstance(&element, &vm.ctx.tuple_type()) {
3232
let element = objtuple::get_elements(&element);
3333
let filename = if let Ok(x) = vm.to_str(element[0].clone()) {
3434
objstr::get_value(&x)

vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl Frame {
120120
// Add an entry in the traceback:
121121
assert!(objtype::isinstance(
122122
&exception,
123-
vm.ctx.exceptions.base_exception_type.clone()
123+
&vm.ctx.exceptions.base_exception_type
124124
));
125125
let traceback = vm
126126
.get_attribute(exception.clone(), &"__traceback__".to_string())
@@ -551,7 +551,7 @@ impl Frame {
551551
0 | 2 | 3 => panic!("Not implemented!"),
552552
_ => panic!("Invalid paramter for RAISE_VARARGS, must be between 0 to 3"),
553553
};
554-
if objtype::isinstance(&exception, vm.ctx.exceptions.base_exception_type.clone()) {
554+
if objtype::isinstance(&exception, &vm.ctx.exceptions.base_exception_type) {
555555
info!("Exception raised: {:?}", exception);
556556
Some(Err(exception))
557557
} else {

vm/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ macro_rules! arg_check {
6363
{
6464
match expected_type {
6565
Some(expected_type) => {
66-
if !objtype::isinstance(arg, expected_type.clone()) {
66+
if !objtype::isinstance(arg, &expected_type) {
6767
let arg_typ = arg.typ().clone();
6868
let actual_type = arg_typ.borrow().str().clone();
6969
return Err($vm.new_type_error(format!(

vm/src/obj/objbool.rs

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

3939
pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {
40-
if objtype::isinstance(obj, vm.ctx.bool_type()) {
40+
if objtype::isinstance(obj, &vm.ctx.bool_type()) {
4141
let value = get_value(obj);
4242
Ok(vm.ctx.new_bool(!value))
4343
} else {

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn bytes_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2222
args,
2323
required = [(zelf, Some(vm.ctx.bytes_type())), (arg, None)]
2424
);
25-
let val = if objtype::isinstance(arg, vm.ctx.list_type()) {
25+
let val = if objtype::isinstance(arg, &vm.ctx.list_type()) {
2626
let mut data_bytes = vec![];
2727
for elem in objlist::get_elements(arg) {
2828
let v = match objint::to_int(vm, &elem, 10) {
@@ -46,7 +46,7 @@ fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4646
required = [(a, Some(vm.ctx.bytes_type())), (b, None)]
4747
);
4848

49-
let result = if objtype::isinstance(b, vm.ctx.bytes_type()) {
49+
let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) {
5050
get_value(a) == get_value(b)
5151
} else {
5252
false

vm/src/obj/objfloat.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ fn float_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1818
args,
1919
required = [(zelf, Some(vm.ctx.float_type())), (arg, None)]
2020
);
21-
let val = if objtype::isinstance(arg, vm.ctx.float_type()) {
21+
let val = if objtype::isinstance(arg, &vm.ctx.float_type()) {
2222
get_value(arg)
23-
} else if objtype::isinstance(arg, vm.ctx.int_type()) {
23+
} else if objtype::isinstance(arg, &vm.ctx.int_type()) {
2424
objint::get_value(arg) as f64
2525
} else {
2626
return Err(vm.new_type_error("Cannot construct int".to_string()));
@@ -49,10 +49,10 @@ fn float_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4949
required = [(zelf, Some(vm.ctx.float_type())), (other, None)]
5050
);
5151
let zelf = get_value(zelf);
52-
let result = if objtype::isinstance(other, vm.ctx.float_type()) {
52+
let result = if objtype::isinstance(other, &vm.ctx.float_type()) {
5353
let other = get_value(other);
5454
zelf == other
55-
} else if objtype::isinstance(other, vm.ctx.int_type()) {
55+
} else if objtype::isinstance(other, &vm.ctx.int_type()) {
5656
let other = objint::get_value(other) as f64;
5757
zelf == other
5858
} else {
@@ -89,9 +89,9 @@ fn float_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8989
);
9090

9191
let v1 = get_value(i);
92-
if objtype::isinstance(i2, vm.ctx.float_type()) {
92+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
9393
Ok(vm.ctx.new_float(v1 + get_value(i2)))
94-
} else if objtype::isinstance(i2, vm.ctx.int_type()) {
94+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
9595
Ok(vm.ctx.new_float(v1 + objint::get_value(i2) as f64))
9696
} else {
9797
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
@@ -105,7 +105,8 @@ fn float_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
105105
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
106106
);
107107
let args = PyFuncArgs::new(vec![i.clone(), i2.clone()], vec![]);
108-
if objtype::isinstance(i2, vm.ctx.float_type()) || objtype::isinstance(i2, vm.ctx.int_type()) {
108+
if objtype::isinstance(i2, &vm.ctx.float_type()) || objtype::isinstance(i2, &vm.ctx.int_type())
109+
{
109110
let r1 = float_floordiv(vm, args.clone());
110111
let r2 = float_mod(vm, args.clone());
111112
Ok(vm.ctx.new_tuple(vec![r1.unwrap(), r2.unwrap()]))
@@ -120,9 +121,9 @@ fn float_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
120121
args,
121122
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
122123
);
123-
if objtype::isinstance(i2, vm.ctx.float_type()) {
124+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
124125
Ok(vm.ctx.new_float((get_value(i) / get_value(i2)).floor()))
125-
} else if objtype::isinstance(i2, vm.ctx.int_type()) {
126+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
126127
Ok(vm
127128
.ctx
128129
.new_float((get_value(i) / objint::get_value(i2) as f64).floor()))
@@ -138,9 +139,9 @@ fn float_sub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
138139
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
139140
);
140141
let v1 = get_value(i);
141-
if objtype::isinstance(i2, vm.ctx.float_type()) {
142+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
142143
Ok(vm.ctx.new_float(v1 - get_value(i2)))
143-
} else if objtype::isinstance(i2, vm.ctx.int_type()) {
144+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
144145
Ok(vm.ctx.new_float(v1 - objint::get_value(i2) as f64))
145146
} else {
146147
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
@@ -153,9 +154,9 @@ fn float_mod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
153154
args,
154155
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
155156
);
156-
if objtype::isinstance(i2, vm.ctx.float_type()) {
157+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
157158
Ok(vm.ctx.new_float(get_value(i) % get_value(i2)))
158-
} else if objtype::isinstance(i2, vm.ctx.int_type()) {
159+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
159160
Ok(vm
160161
.ctx
161162
.new_float(get_value(i) % objint::get_value(i2) as f64))
@@ -172,10 +173,10 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172173
);
173174

174175
let v1 = get_value(i);
175-
if objtype::isinstance(i2, vm.ctx.float_type()) {
176+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
176177
let result = v1.powf(get_value(i2));
177178
Ok(vm.ctx.new_float(result))
178-
} else if objtype::isinstance(i2, vm.ctx.int_type()) {
179+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
179180
let result = v1.powf(objint::get_value(i2) as f64);
180181
Ok(vm.ctx.new_float(result))
181182
} else {

vm/src/obj/objint.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2020
required = [(cls, None)],
2121
optional = [(val_option, None)]
2222
);
23-
if !objtype::issubclass(cls, vm.ctx.int_type()) {
23+
if !objtype::issubclass(cls, &vm.ctx.int_type()) {
2424
return Err(vm.new_type_error(format!("{:?} is not a subtype of int", cls)));
2525
}
2626

@@ -38,11 +38,11 @@ fn int_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3838

3939
// Casting function:
4040
pub fn to_int(vm: &mut VirtualMachine, obj: &PyObjectRef, base: u32) -> Result<i32, PyObjectRef> {
41-
let val = if objtype::isinstance(obj, vm.ctx.int_type()) {
41+
let val = if objtype::isinstance(obj, &vm.ctx.int_type()) {
4242
get_value(obj)
43-
} else if objtype::isinstance(obj, vm.ctx.float_type()) {
43+
} else if objtype::isinstance(obj, &vm.ctx.float_type()) {
4444
objfloat::get_value(obj) as i32
45-
} else if objtype::isinstance(obj, vm.ctx.str_type()) {
45+
} else if objtype::isinstance(obj, &vm.ctx.str_type()) {
4646
let s = objstr::get_value(obj);
4747
match i32::from_str_radix(&s, base) {
4848
Ok(v) => v,
@@ -85,11 +85,11 @@ fn int_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8585
args,
8686
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
8787
);
88-
let result = if objtype::isinstance(other, vm.ctx.int_type()) {
88+
let result = if objtype::isinstance(other, &vm.ctx.int_type()) {
8989
let zelf = i32::from_pyobj(zelf);
9090
let other = i32::from_pyobj(other);
9191
zelf == other
92-
} else if objtype::isinstance(other, vm.ctx.float_type()) {
92+
} else if objtype::isinstance(other, &vm.ctx.float_type()) {
9393
let zelf = i32::from_pyobj(zelf) as f64;
9494
let other = objfloat::get_value(other);
9595
zelf == other
@@ -178,9 +178,9 @@ fn int_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
178178
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
179179
);
180180
let i = i32::from_pyobj(i);
181-
if objtype::isinstance(i2, vm.ctx.int_type()) {
181+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
182182
Ok(vm.ctx.new_int(i + get_value(i2)))
183-
} else if objtype::isinstance(i2, vm.ctx.float_type()) {
183+
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
184184
Ok(vm.ctx.new_float(i as f64 + objfloat::get_value(i2)))
185185
} else {
186186
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
@@ -193,7 +193,7 @@ fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
193193
args,
194194
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
195195
);
196-
if objtype::isinstance(i2, vm.ctx.int_type()) {
196+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
197197
Ok(vm.ctx.new_int(get_value(i) / get_value(i2)))
198198
} else {
199199
Err(vm.new_type_error(format!("Cannot floordiv {:?} and {:?}", i, i2)))
@@ -207,9 +207,9 @@ fn int_sub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
207207
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
208208
);
209209
let i = i32::from_pyobj(i);
210-
if objtype::isinstance(i2, vm.ctx.int_type()) {
210+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
211211
Ok(vm.ctx.new_int(i - get_value(i2)))
212-
} else if objtype::isinstance(i2, vm.ctx.float_type()) {
212+
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
213213
Ok(vm.ctx.new_float(i as f64 - objfloat::get_value(i2)))
214214
} else {
215215
Err(vm.new_type_error(format!("Cannot substract {:?} and {:?}", i, i2)))
@@ -222,9 +222,9 @@ fn int_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
222222
args,
223223
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
224224
);
225-
if objtype::isinstance(i2, vm.ctx.int_type()) {
225+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
226226
Ok(vm.ctx.new_int(get_value(i) * get_value(i2)))
227-
} else if objtype::isinstance(i2, vm.ctx.float_type()) {
227+
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
228228
Ok(vm
229229
.ctx
230230
.new_float(get_value(i) as f64 * objfloat::get_value(i2)))
@@ -240,9 +240,9 @@ fn int_truediv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
240240
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
241241
);
242242
let v1 = get_value(i);
243-
if objtype::isinstance(i2, vm.ctx.int_type()) {
243+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
244244
Ok(vm.ctx.new_float(v1 as f64 / get_value(i2) as f64))
245-
} else if objtype::isinstance(i2, vm.ctx.float_type()) {
245+
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
246246
Ok(vm.ctx.new_float(v1 as f64 / objfloat::get_value(i2)))
247247
} else {
248248
Err(vm.new_type_error(format!("Cannot divide {:?} and {:?}", i, i2)))
@@ -256,7 +256,7 @@ fn int_mod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
256256
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
257257
);
258258
let v1 = get_value(i);
259-
if objtype::isinstance(i2, vm.ctx.int_type()) {
259+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
260260
Ok(vm.ctx.new_int(v1 % get_value(i2)))
261261
} else {
262262
Err(vm.new_type_error(format!("Cannot modulo {:?} and {:?}", i, i2)))
@@ -270,10 +270,10 @@ fn int_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
270270
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
271271
);
272272
let v1 = get_value(i);
273-
if objtype::isinstance(i2, vm.ctx.int_type()) {
273+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
274274
let v2 = get_value(i2);
275275
Ok(vm.ctx.new_int(v1.pow(v2 as u32)))
276-
} else if objtype::isinstance(i2, vm.ctx.float_type()) {
276+
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
277277
let v2 = objfloat::get_value(i2);
278278
Ok(vm.ctx.new_float((v1 as f64).powf(v2)))
279279
} else {
@@ -288,7 +288,7 @@ fn int_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
288288
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
289289
);
290290
let args = PyFuncArgs::new(vec![i.clone(), i2.clone()], vec![]);
291-
if objtype::isinstance(i2, vm.ctx.int_type()) {
291+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
292292
let r1 = int_floordiv(vm, args.clone());
293293
let r2 = int_mod(vm, args.clone());
294294
Ok(vm.ctx.new_tuple(vec![r1.unwrap(), r2.unwrap()]))
@@ -304,7 +304,7 @@ fn int_xor(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
304304
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
305305
);
306306
let v1 = get_value(i);
307-
if objtype::isinstance(i2, vm.ctx.int_type()) {
307+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
308308
let v2 = get_value(i2);
309309
Ok(vm.ctx.new_int(v1 ^ v2))
310310
} else {
@@ -319,7 +319,7 @@ fn int_or(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
319319
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
320320
);
321321
let v1 = get_value(i);
322-
if objtype::isinstance(i2, vm.ctx.int_type()) {
322+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
323323
let v2 = get_value(i2);
324324
Ok(vm.ctx.new_int(v1 | v2))
325325
} else {
@@ -334,7 +334,7 @@ fn int_and(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
334334
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
335335
);
336336
let v1 = get_value(i);
337-
if objtype::isinstance(i2, vm.ctx.int_type()) {
337+
if objtype::isinstance(i2, &vm.ctx.int_type()) {
338338
let v2 = get_value(i2);
339339
Ok(vm.ctx.new_int(v1 & v2))
340340
} else {

vm/src/obj/objiter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use super::objtype; // Required for arg_check! to use isinstance
1818
*/
1919
pub fn get_iter(vm: &mut VirtualMachine, iter_target: &PyObjectRef) -> PyResult {
2020
// Check what we are going to iterate over:
21-
let iterated_obj = if objtype::isinstance(iter_target, vm.ctx.iter_type()) {
21+
let iterated_obj = if objtype::isinstance(iter_target, &vm.ctx.iter_type()) {
2222
// If object is already an iterator, return that one.
2323
return Ok(iter_target.clone());
24-
} else if objtype::isinstance(iter_target, vm.ctx.list_type()) {
24+
} else if objtype::isinstance(iter_target, &vm.ctx.list_type()) {
2525
iter_target.clone()
2626
// } else if hasattr(iter_target, "__iter__") {
2727
} else {
@@ -56,7 +56,7 @@ pub fn get_next_object(
5656
Ok(value) => Ok(Some(value)),
5757
Err(next_error) => {
5858
// Check if we have stopiteration, or something else:
59-
if objtype::isinstance(&next_error, vm.ctx.exceptions.stop_iteration.clone()) {
59+
if objtype::isinstance(&next_error, &vm.ctx.exceptions.stop_iteration) {
6060
Ok(None)
6161
} else {
6262
Err(next_error)

0 commit comments

Comments
 (0)