Skip to content

Commit 5c78127

Browse files
committed
Use more specific ref type than PyObjectRef in PyContext
1 parent 79a7aed commit 5c78127

28 files changed

Lines changed: 409 additions & 380 deletions

vm/src/builtins.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn builtin_format(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
295295
}
296296

297297
fn catch_attr_exception<T>(ex: PyObjectRef, default: T, vm: &VirtualMachine) -> PyResult<T> {
298-
if objtype::isinstance(&ex, &vm.ctx.exceptions.attribute_error) {
298+
if objtype::isinstance(&ex, vm.ctx.exceptions.attribute_error.as_object()) {
299299
Ok(default)
300300
} else {
301301
Err(ex)
@@ -510,7 +510,7 @@ fn builtin_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
510510
match vm.call_method(iterator, "__next__", vec![]) {
511511
Ok(value) => Ok(value),
512512
Err(value) => {
513-
if objtype::isinstance(&value, &vm.ctx.exceptions.stop_iteration) {
513+
if objtype::isinstance(&value, vm.ctx.exceptions.stop_iteration.as_object()) {
514514
match default_value {
515515
None => Err(value),
516516
Some(value) => Ok(value.clone()),
@@ -780,27 +780,27 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
780780
"__import__" => ctx.new_rustfunc(builtin_import),
781781

782782
// Constants
783-
"NotImplemented" => ctx.not_implemented.clone(),
783+
"NotImplemented" => ctx.not_implemented(),
784784

785785
// Exceptions:
786-
"BaseException" => ctx.exceptions.base_exception_type.clone(),
787-
"Exception" => ctx.exceptions.exception_type.clone(),
788-
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone(),
789-
"AssertionError" => ctx.exceptions.assertion_error.clone(),
790-
"AttributeError" => ctx.exceptions.attribute_error.clone(),
791-
"NameError" => ctx.exceptions.name_error.clone(),
792-
"OverflowError" => ctx.exceptions.overflow_error.clone(),
793-
"RuntimeError" => ctx.exceptions.runtime_error.clone(),
794-
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone(),
795-
"TypeError" => ctx.exceptions.type_error.clone(),
796-
"ValueError" => ctx.exceptions.value_error.clone(),
797-
"IndexError" => ctx.exceptions.index_error.clone(),
798-
"ImportError" => ctx.exceptions.import_error.clone(),
799-
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone(),
800-
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
801-
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
802-
"KeyError" => ctx.exceptions.key_error.clone(),
803-
"OSError" => ctx.exceptions.os_error.clone(),
786+
"BaseException" => ctx.exceptions.base_exception_type.clone().into_object(),
787+
"Exception" => ctx.exceptions.exception_type.clone().into_object(),
788+
"ArithmeticError" => ctx.exceptions.arithmetic_error.clone().into_object(),
789+
"AssertionError" => ctx.exceptions.assertion_error.clone().into_object(),
790+
"AttributeError" => ctx.exceptions.attribute_error.clone().into_object(),
791+
"NameError" => ctx.exceptions.name_error.clone().into_object(),
792+
"OverflowError" => ctx.exceptions.overflow_error.clone().into_object(),
793+
"RuntimeError" => ctx.exceptions.runtime_error.clone().into_object(),
794+
"NotImplementedError" => ctx.exceptions.not_implemented_error.clone().into_object(),
795+
"TypeError" => ctx.exceptions.type_error.clone().into_object(),
796+
"ValueError" => ctx.exceptions.value_error.clone().into_object(),
797+
"IndexError" => ctx.exceptions.index_error.clone().into_object(),
798+
"ImportError" => ctx.exceptions.import_error.clone().into_object(),
799+
"FileNotFoundError" => ctx.exceptions.file_not_found_error.clone().into_object(),
800+
"StopIteration" => ctx.exceptions.stop_iteration.clone().into_object(),
801+
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone().into_object(),
802+
"KeyError" => ctx.exceptions.key_error.clone().into_object(),
803+
"OSError" => ctx.exceptions.os_error.clone().into_object(),
804804
});
805805

806806
#[cfg(not(target_arch = "wasm32"))]

vm/src/exceptions.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::function::PyFuncArgs;
22
use crate::obj::objsequence;
33
use crate::obj::objtype;
4+
use crate::obj::objtype::PyClassRef;
45
use crate::pyobject::{create_type, PyContext, PyObjectRef, PyResult, TypeProtocol};
56
use crate::vm::VirtualMachine;
67

@@ -65,7 +66,10 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
6566
arg_check!(
6667
vm,
6768
args,
68-
required = [(exc, Some(vm.ctx.exceptions.exception_type.clone()))]
69+
required = [(
70+
exc,
71+
Some(vm.ctx.exceptions.exception_type.clone().into_object())
72+
)]
6973
);
7074
let type_name = objtype::get_type_name(&exc.typ());
7175
let msg = if let Ok(m) = vm.get_attribute(exc.clone(), "msg") {
@@ -82,31 +86,31 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
8286

8387
#[derive(Debug)]
8488
pub struct ExceptionZoo {
85-
pub arithmetic_error: PyObjectRef,
86-
pub assertion_error: PyObjectRef,
87-
pub attribute_error: PyObjectRef,
88-
pub base_exception_type: PyObjectRef,
89-
pub exception_type: PyObjectRef,
90-
pub file_not_found_error: PyObjectRef,
91-
pub import_error: PyObjectRef,
92-
pub index_error: PyObjectRef,
93-
pub key_error: PyObjectRef,
94-
pub module_not_found_error: PyObjectRef,
95-
pub name_error: PyObjectRef,
96-
pub not_implemented_error: PyObjectRef,
97-
pub os_error: PyObjectRef,
98-
pub overflow_error: PyObjectRef,
99-
pub permission_error: PyObjectRef,
100-
pub runtime_error: PyObjectRef,
101-
pub stop_iteration: PyObjectRef,
102-
pub syntax_error: PyObjectRef,
103-
pub type_error: PyObjectRef,
104-
pub value_error: PyObjectRef,
105-
pub zero_division_error: PyObjectRef,
89+
pub arithmetic_error: PyClassRef,
90+
pub assertion_error: PyClassRef,
91+
pub attribute_error: PyClassRef,
92+
pub base_exception_type: PyClassRef,
93+
pub exception_type: PyClassRef,
94+
pub file_not_found_error: PyClassRef,
95+
pub import_error: PyClassRef,
96+
pub index_error: PyClassRef,
97+
pub key_error: PyClassRef,
98+
pub module_not_found_error: PyClassRef,
99+
pub name_error: PyClassRef,
100+
pub not_implemented_error: PyClassRef,
101+
pub os_error: PyClassRef,
102+
pub overflow_error: PyClassRef,
103+
pub permission_error: PyClassRef,
104+
pub runtime_error: PyClassRef,
105+
pub stop_iteration: PyClassRef,
106+
pub syntax_error: PyClassRef,
107+
pub type_error: PyClassRef,
108+
pub value_error: PyClassRef,
109+
pub zero_division_error: PyClassRef,
106110
}
107111

108112
impl ExceptionZoo {
109-
pub fn new(type_type: &PyObjectRef, object_type: &PyObjectRef) -> Self {
113+
pub fn new(type_type: &PyClassRef, object_type: &PyClassRef) -> Self {
110114
// Sorted By Hierarchy then alphabetized.
111115
let base_exception_type = create_type("BaseException", &type_type, &object_type);
112116
let exception_type = create_type("Exception", &type_type, &base_exception_type);
@@ -159,13 +163,13 @@ impl ExceptionZoo {
159163
pub fn init(context: &PyContext) {
160164
let base_exception_type = &context.exceptions.base_exception_type;
161165
context.set_attr(
162-
&base_exception_type,
166+
base_exception_type,
163167
"__init__",
164168
context.new_rustfunc(exception_init),
165169
);
166170
let exception_type = &context.exceptions.exception_type;
167171
context.set_attr(
168-
&exception_type,
172+
exception_type,
169173
"__str__",
170174
context.new_rustfunc(exception_str),
171175
);

vm/src/frame.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl Frame {
253253
// Add an entry in the traceback:
254254
assert!(objtype::isinstance(
255255
&exception,
256-
&vm.ctx.exceptions.base_exception_type
256+
vm.ctx.exceptions.base_exception_type.as_object()
257257
));
258258
let traceback = vm
259259
.get_attribute(exception.clone(), "__traceback__")
@@ -656,11 +656,17 @@ impl Frame {
656656
0 | 2 | 3 => panic!("Not implemented!"),
657657
_ => panic!("Invalid parameter for RAISE_VARARGS, must be between 0 to 3"),
658658
};
659-
if objtype::isinstance(&exception, &vm.ctx.exceptions.base_exception_type) {
659+
if objtype::isinstance(
660+
&exception,
661+
vm.ctx.exceptions.base_exception_type.as_object(),
662+
) {
660663
info!("Exception raised: {:?}", exception);
661664
Err(exception)
662665
} else if objtype::isinstance(&exception, &vm.ctx.type_type())
663-
&& objtype::issubclass(&exception, &vm.ctx.exceptions.base_exception_type)
666+
&& objtype::issubclass(
667+
&exception,
668+
vm.ctx.exceptions.base_exception_type.as_object(),
669+
)
664670
{
665671
let exception = vm.new_empty_exception(exception)?;
666672
info!("Exception raised: {:?}", exception);

vm/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ macro_rules! extend_class {
143143
( $ctx:expr, $class:expr, { $($name:expr => $value:expr),* $(,)* }) => {
144144
let class = $class;
145145
$(
146-
$ctx.set_attr(&class, $name, $value);
146+
$ctx.set_attr(class, $name, $value);
147147
)*
148148
}
149149
}

vm/src/obj/objbool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ The builtins True and False are the only two instances of the class bool.
6565
The class bool is a subclass of the class int, and cannot be subclassed.";
6666

6767
let bool_type = &context.bool_type;
68-
context.set_attr(&bool_type, "__new__", context.new_rustfunc(bool_new));
69-
context.set_attr(&bool_type, "__repr__", context.new_rustfunc(bool_repr));
70-
context.set_attr(&bool_type, "__doc__", context.new_str(bool_doc.to_string()));
68+
context.set_attr(bool_type, "__new__", context.new_rustfunc(bool_new));
69+
context.set_attr(bool_type, "__repr__", context.new_rustfunc(bool_repr));
70+
context.set_attr(bool_type, "__doc__", context.new_str(bool_doc.to_string()));
7171
}
7272

7373
pub fn not(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult {

vm/src/obj/objbytearray.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,84 +61,80 @@ pub fn init(context: &PyContext) {
6161
- any object implementing the buffer API.\n \
6262
- an integer";
6363

64+
context.set_attr(bytearray_type, "__eq__", context.new_rustfunc(bytearray_eq));
6465
context.set_attr(
65-
&bytearray_type,
66-
"__eq__",
67-
context.new_rustfunc(bytearray_eq),
68-
);
69-
context.set_attr(
70-
&bytearray_type,
66+
bytearray_type,
7167
"__new__",
7268
context.new_rustfunc(bytearray_new),
7369
);
7470
context.set_attr(
75-
&bytearray_type,
71+
bytearray_type,
7672
"__repr__",
7773
context.new_rustfunc(bytearray_repr),
7874
);
7975
context.set_attr(
80-
&bytearray_type,
76+
bytearray_type,
8177
"__len__",
8278
context.new_rustfunc(bytesarray_len),
8379
);
8480
context.set_attr(
85-
&bytearray_type,
81+
bytearray_type,
8682
"__doc__",
8783
context.new_str(bytearray_doc.to_string()),
8884
);
8985
context.set_attr(
90-
&bytearray_type,
86+
bytearray_type,
9187
"isalnum",
9288
context.new_rustfunc(bytearray_isalnum),
9389
);
9490
context.set_attr(
95-
&bytearray_type,
91+
bytearray_type,
9692
"isalpha",
9793
context.new_rustfunc(bytearray_isalpha),
9894
);
9995
context.set_attr(
100-
&bytearray_type,
96+
bytearray_type,
10197
"isascii",
10298
context.new_rustfunc(bytearray_isascii),
10399
);
104100
context.set_attr(
105-
&bytearray_type,
101+
bytearray_type,
106102
"isdigit",
107103
context.new_rustfunc(bytearray_isdigit),
108104
);
109105
context.set_attr(
110-
&bytearray_type,
106+
bytearray_type,
111107
"islower",
112108
context.new_rustfunc(bytearray_islower),
113109
);
114110
context.set_attr(
115-
&bytearray_type,
111+
bytearray_type,
116112
"isspace",
117113
context.new_rustfunc(bytearray_isspace),
118114
);
119115
context.set_attr(
120-
&bytearray_type,
116+
bytearray_type,
121117
"isupper",
122118
context.new_rustfunc(bytearray_isupper),
123119
);
124120
context.set_attr(
125-
&bytearray_type,
121+
bytearray_type,
126122
"istitle",
127123
context.new_rustfunc(bytearray_istitle),
128124
);
129125
context.set_attr(
130-
&bytearray_type,
126+
bytearray_type,
131127
"clear",
132128
context.new_rustfunc(bytearray_clear),
133129
);
134-
context.set_attr(&bytearray_type, "pop", context.new_rustfunc(bytearray_pop));
130+
context.set_attr(bytearray_type, "pop", context.new_rustfunc(bytearray_pop));
135131
context.set_attr(
136-
&bytearray_type,
132+
bytearray_type,
137133
"lower",
138134
context.new_rustfunc(bytearray_lower),
139135
);
140136
context.set_attr(
141-
&bytearray_type,
137+
bytearray_type,
142138
"upper",
143139
context.new_rustfunc(bytearray_upper),
144140
);

vm/src/obj/objbytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl PyValue for PyBytes {
4343

4444
// Fill bytes class methods:
4545
pub fn init(context: &PyContext) {
46-
let bytes_type = &context.bytes_type;
46+
let bytes_type = context.bytes_type.as_object();
4747

4848
let bytes_doc =
4949
"bytes(iterable_of_ints) -> bytes\n\

vm/src/obj/objcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl PyValue for PyCode {
3232
}
3333

3434
pub fn init(context: &PyContext) {
35-
let code_type = &context.code_type;
35+
let code_type = context.code_type.as_object();
3636
context.set_attr(code_type, "__new__", context.new_rustfunc(code_new));
3737
context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr));
3838

vm/src/obj/objcomplex.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,22 @@ pub fn init(context: &PyContext) {
3434
"Create a complex number from a real part and an optional imaginary part.\n\n\
3535
This is equivalent to (real + imag*1j) where imag defaults to 0.";
3636

37-
context.set_attr(&complex_type, "__abs__", context.new_rustfunc(complex_abs));
38-
context.set_attr(&complex_type, "__add__", context.new_rustfunc(complex_add));
37+
context.set_attr(complex_type, "__abs__", context.new_rustfunc(complex_abs));
38+
context.set_attr(complex_type, "__add__", context.new_rustfunc(complex_add));
39+
context.set_attr(complex_type, "__radd__", context.new_rustfunc(complex_radd));
40+
context.set_attr(complex_type, "__eq__", context.new_rustfunc(complex_eq));
41+
context.set_attr(complex_type, "__neg__", context.new_rustfunc(complex_neg));
42+
context.set_attr(complex_type, "__new__", context.new_rustfunc(complex_new));
43+
context.set_attr(complex_type, "real", context.new_property(complex_real));
44+
context.set_attr(complex_type, "imag", context.new_property(complex_imag));
3945
context.set_attr(
40-
&complex_type,
41-
"__radd__",
42-
context.new_rustfunc(complex_radd),
43-
);
44-
context.set_attr(&complex_type, "__eq__", context.new_rustfunc(complex_eq));
45-
context.set_attr(&complex_type, "__neg__", context.new_rustfunc(complex_neg));
46-
context.set_attr(&complex_type, "__new__", context.new_rustfunc(complex_new));
47-
context.set_attr(&complex_type, "real", context.new_property(complex_real));
48-
context.set_attr(&complex_type, "imag", context.new_property(complex_imag));
49-
context.set_attr(
50-
&complex_type,
46+
complex_type,
5147
"__doc__",
5248
context.new_str(complex_doc.to_string()),
5349
);
50+
context.set_attr(complex_type, "__repr__", context.new_rustfunc(complex_repr));
5451
context.set_attr(
55-
&complex_type,
56-
"__repr__",
57-
context.new_rustfunc(complex_repr),
58-
);
59-
context.set_attr(
60-
&complex_type,
52+
complex_type,
6153
"conjugate",
6254
context.new_rustfunc(complex_conjugate),
6355
);

vm/src/obj/objellipsis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::pyobject::{PyContext, PyResult, TypeProtocol};
33
use crate::vm::VirtualMachine;
44

55
pub fn init(context: &PyContext) {
6-
let ellipsis_type = &context.ellipsis_type;
6+
let ellipsis_type = context.ellipsis_type.as_object();
77
context.set_attr(ellipsis_type, "__new__", context.new_rustfunc(ellipsis_new));
88
context.set_attr(
99
ellipsis_type,

0 commit comments

Comments
 (0)