|
1 | 1 | use std::cell::Cell;
|
| 2 | +use std::collections::hash_map::DefaultHasher; |
2 | 3 | use std::hash::{Hash, Hasher};
|
3 | 4 | use std::ops::Deref;
|
4 | 5 |
|
5 | 6 | use num_traits::ToPrimitive;
|
6 | 7 |
|
7 |
| -use crate::function::{OptionalArg, PyFuncArgs}; |
8 |
| -use crate::pyobject::{ |
9 |
| - PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, |
10 |
| -}; |
| 8 | +use crate::function::OptionalArg; |
| 9 | +use crate::pyobject::{PyContext, PyIteratorValue, PyObjectRef, PyRef, PyResult, PyValue}; |
11 | 10 | use crate::vm::VirtualMachine;
|
12 | 11 |
|
13 | 12 | use super::objint;
|
14 |
| -use super::objtype::{self, PyClassRef}; |
| 13 | +use super::objtype::PyClassRef; |
15 | 14 |
|
16 | 15 | #[derive(Debug)]
|
17 | 16 | pub struct PyBytes {
|
@@ -94,102 +93,60 @@ fn bytes_new(
|
94 | 93 | }
|
95 | 94 |
|
96 | 95 | impl PyBytesRef {
|
97 |
| - fn eq(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
98 |
| - arg_check!( |
99 |
| - vm, |
100 |
| - args, |
101 |
| - required = [(a, Some(vm.ctx.bytes_type())), (b, None)] |
102 |
| - ); |
103 |
| - |
104 |
| - let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) { |
105 |
| - get_value(a).to_vec() == get_value(b).to_vec() |
| 96 | + fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { |
| 97 | + if let Ok(other) = other.downcast::<PyBytes>() { |
| 98 | + vm.ctx.new_bool(self.value == other.value) |
106 | 99 | } else {
|
107 |
| - return Ok(vm.ctx.not_implemented()); |
108 |
| - }; |
109 |
| - Ok(vm.ctx.new_bool(result)) |
| 100 | + vm.ctx.not_implemented() |
| 101 | + } |
110 | 102 | }
|
111 | 103 |
|
112 |
| - fn ge(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
113 |
| - arg_check!( |
114 |
| - vm, |
115 |
| - args, |
116 |
| - required = [(a, Some(vm.ctx.bytes_type())), (b, None)] |
117 |
| - ); |
118 |
| - |
119 |
| - let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) { |
120 |
| - get_value(a).to_vec() >= get_value(b).to_vec() |
| 104 | + fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { |
| 105 | + if let Ok(other) = other.downcast::<PyBytes>() { |
| 106 | + vm.ctx.new_bool(self.value >= other.value) |
121 | 107 | } else {
|
122 |
| - return Ok(vm.ctx.not_implemented()); |
123 |
| - }; |
124 |
| - Ok(vm.ctx.new_bool(result)) |
| 108 | + vm.ctx.not_implemented() |
| 109 | + } |
125 | 110 | }
|
126 | 111 |
|
127 |
| - fn gt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
128 |
| - arg_check!( |
129 |
| - vm, |
130 |
| - args, |
131 |
| - required = [(a, Some(vm.ctx.bytes_type())), (b, None)] |
132 |
| - ); |
133 |
| - |
134 |
| - let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) { |
135 |
| - get_value(a).to_vec() > get_value(b).to_vec() |
| 112 | + fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { |
| 113 | + if let Ok(other) = other.downcast::<PyBytes>() { |
| 114 | + vm.ctx.new_bool(self.value > other.value) |
136 | 115 | } else {
|
137 |
| - return Ok(vm.ctx.not_implemented()); |
138 |
| - }; |
139 |
| - Ok(vm.ctx.new_bool(result)) |
| 116 | + vm.ctx.not_implemented() |
| 117 | + } |
140 | 118 | }
|
141 | 119 |
|
142 |
| - fn le(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
143 |
| - arg_check!( |
144 |
| - vm, |
145 |
| - args, |
146 |
| - required = [(a, Some(vm.ctx.bytes_type())), (b, None)] |
147 |
| - ); |
148 |
| - |
149 |
| - let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) { |
150 |
| - get_value(a).to_vec() <= get_value(b).to_vec() |
| 120 | + fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { |
| 121 | + if let Ok(other) = other.downcast::<PyBytes>() { |
| 122 | + vm.ctx.new_bool(self.value <= other.value) |
151 | 123 | } else {
|
152 |
| - return Ok(vm.ctx.not_implemented()); |
153 |
| - }; |
154 |
| - Ok(vm.ctx.new_bool(result)) |
| 124 | + vm.ctx.not_implemented() |
| 125 | + } |
155 | 126 | }
|
156 | 127 |
|
157 |
| - fn lt(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
158 |
| - arg_check!( |
159 |
| - vm, |
160 |
| - args, |
161 |
| - required = [(a, Some(vm.ctx.bytes_type())), (b, None)] |
162 |
| - ); |
163 |
| - |
164 |
| - let result = if objtype::isinstance(b, &vm.ctx.bytes_type()) { |
165 |
| - get_value(a).to_vec() < get_value(b).to_vec() |
| 128 | + fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef { |
| 129 | + if let Ok(other) = other.downcast::<PyBytes>() { |
| 130 | + vm.ctx.new_bool(self.value < other.value) |
166 | 131 | } else {
|
167 |
| - return Ok(vm.ctx.not_implemented()); |
168 |
| - }; |
169 |
| - Ok(vm.ctx.new_bool(result)) |
| 132 | + vm.ctx.not_implemented() |
| 133 | + } |
170 | 134 | }
|
171 | 135 |
|
172 |
| - fn len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
173 |
| - arg_check!(vm, args, required = [(a, Some(vm.ctx.bytes_type()))]); |
174 |
| - |
175 |
| - let byte_vec = get_value(a).to_vec(); |
176 |
| - Ok(vm.ctx.new_int(byte_vec.len())) |
| 136 | + fn len(self, _vm: &VirtualMachine) -> usize { |
| 137 | + self.value.len() |
177 | 138 | }
|
178 | 139 |
|
179 |
| - fn hash(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
180 |
| - arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]); |
181 |
| - let data = get_value(zelf); |
182 |
| - let mut hasher = std::collections::hash_map::DefaultHasher::new(); |
183 |
| - data.hash(&mut hasher); |
184 |
| - let hash = hasher.finish(); |
185 |
| - Ok(vm.ctx.new_int(hash)) |
| 140 | + fn hash(self, _vm: &VirtualMachine) -> u64 { |
| 141 | + let mut hasher = DefaultHasher::new(); |
| 142 | + self.value.hash(&mut hasher); |
| 143 | + hasher.finish() |
186 | 144 | }
|
187 | 145 |
|
188 |
| - fn repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { |
189 |
| - arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]); |
190 |
| - let value = get_value(obj); |
191 |
| - let data = String::from_utf8(value.to_vec()).unwrap(); |
192 |
| - Ok(vm.new_str(format!("b'{}'", data))) |
| 146 | + fn repr(self, _vm: &VirtualMachine) -> String { |
| 147 | + // TODO: don't just unwrap |
| 148 | + let data = String::from_utf8(self.value.clone()).unwrap(); |
| 149 | + format!("b'{}'", data) |
193 | 150 | }
|
194 | 151 |
|
195 | 152 | fn iter(obj: PyBytesRef, _vm: &VirtualMachine) -> PyIteratorValue {
|
|
0 commit comments