- Notifications
You must be signed in to change notification settings - Fork1.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@qingshi163 what have to be done to finish this PR? |
awesome! |
youknowone left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 clonedPyNumberMethods
with edited values. instead ofCow
for every object whenas_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 |
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 simplifyingAsNumber
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.
Uh oh!
There was an error while loading.Please reload this page.
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
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
|
at least, no regression after using references
|
1b9b663
to7a61d07
CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
number protocol of#3244