-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Implement Number Protocol #3507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
243a130
to
d5585f0
Compare
@qingshi163 what have to be done to finish this PR? |
d5585f0
to
55659c3
Compare
5215967
to
40a8890
Compare
awesome! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have questions about cow and OnceCell part, but it looks working!
This is the way to go and we can fix other stuff later.
Do you have remaining plans to do on this PR?
vm/src/protocol/number.rs
Outdated
@@ -0,0 +1,242 @@ | |||
use std::borrow::Cow; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vm/src/builtins/int.rs
Outdated
@@ -263,7 +265,10 @@ impl Constructor for PyInt { | |||
val | |||
}; | |||
|
|||
try_int(&val, vm) | |||
// try_int(&val, vm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// try_int(&val, vm) |
vm/src/types/slot.rs
Outdated
pub trait AsNumber: PyPayload { | ||
#[inline] | ||
#[pyslot] | ||
fn slot_as_number(zelf: &PyObject, vm: &VirtualMachine) -> Cow<'static, PyNumberMethods> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this slot need to return Cow? Which part prevent this to be &'static PyNumberMethods
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for heap object we need to use Cow::Owned witch implemented in as_number_wrapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not decided by object, but decided by type. Then I think the type can hold a cloned PyNumberMethods
with edited values. instead of Cow
for every object when as_number
is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found AsMapping has the same mechanism. Then it is ok for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be stored in the type struct, but we can fix it later
There is quite a lot of things not done yet, i think all the mathematic operations should be implemented via number protocol |
How about finishing this PR as current state as core implementation of AsNumber trait, and working on other types in following other PRs? |
@qingshi163 are you going to make this ready for review? or do you want to do more work on it? |
I am still busy with it |
f1bcb2e
to
4da2080
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #3750 changes also be able to be adapt here.
They can be done later, but I attached a commit simplifying AsNumber
interfaces.
vm/src/builtins/float.rs
Outdated
if let Some(f) = val.try_to_f64(vm)? { | ||
f | ||
if cls.is(vm.ctx.types.float_type) && val.class().is(PyFloat::class(vm)) { | ||
unsafe { val.downcast_unchecked::<PyFloat>().value } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is a regression. val object will not be reused even for same type.
vm/src/builtins/int.rs
Outdated
negative: Some(|number, vm| { | ||
Self::number_downcast(number) | ||
.value | ||
.clone() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/num/latest/num/struct.BigInt.html#impl-Neg-1
Neg is also implemented for &BigInt
. This clone is redundant.
vm/src/builtins/int.rs
Outdated
absolute: Some(|number, vm| Self::number_downcast(number).value.abs().to_pyresult(vm)), | ||
boolean: Some(|number, _vm| Ok(Self::number_downcast(number).value.is_zero())), | ||
invert: Some(|number, vm| { | ||
let value = Self::number_downcast(number).value.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not
also implemented for reference.
https://docs.rs/num/latest/num/struct.BigInt.html#impl-Not-1
/* Number implementations must check *both* | ||
arguments for proper type and implement the necessary conversions | ||
in the slot functions themselves. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean we need to check the first argument(&PyNumber) is a correct PyNumber object?
In this case, I feel like the type will fit better in &PyObject
rather than &PyNumber
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is from cpython but as the source shows that cpython only does the check for binary operations which could be called invertly but assume the type is correct for other functions
|
06a0d1c
to
1fd5d0a
Compare
at least, no regression after using references
|
1b9b663
to
7a61d07
Compare
7a61d07
to
0c9d9a5
Compare
0c9d9a5
to
09fc676
Compare
number protocol of #3244