Skip to content

Commit e1c4161

Browse files
committed
slots
1 parent ef4b595 commit e1c4161

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

crates/vm/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustpython_common::static_cell;
1515
/// Iterates SLOT_DEFS and creates a PyWrapper for each slot that:
1616
/// 1. Has a function set in the type's slots
1717
/// 2. Doesn't already have an attribute in the type's dict
18-
fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
18+
pub fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
1919
for def in SLOT_DEFS.iter() {
2020
// Skip __new__ - it has special handling
2121
if def.name == "__new__" {

crates/vm/src/protocol/number.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,115 @@ impl From<&PyNumberMethods> for PyNumberSlots {
352352
}
353353

354354
impl PyNumberSlots {
355+
/// Copy from static PyNumberMethods
356+
pub fn copy_from(&self, methods: &PyNumberMethods) {
357+
if let Some(f) = methods.add {
358+
self.add.store(Some(f));
359+
}
360+
if let Some(f) = methods.subtract {
361+
self.subtract.store(Some(f));
362+
}
363+
if let Some(f) = methods.multiply {
364+
self.multiply.store(Some(f));
365+
}
366+
if let Some(f) = methods.remainder {
367+
self.remainder.store(Some(f));
368+
}
369+
if let Some(f) = methods.divmod {
370+
self.divmod.store(Some(f));
371+
}
372+
if let Some(f) = methods.power {
373+
self.power.store(Some(f));
374+
}
375+
if let Some(f) = methods.negative {
376+
self.negative.store(Some(f));
377+
}
378+
if let Some(f) = methods.positive {
379+
self.positive.store(Some(f));
380+
}
381+
if let Some(f) = methods.absolute {
382+
self.absolute.store(Some(f));
383+
}
384+
if let Some(f) = methods.boolean {
385+
self.boolean.store(Some(f));
386+
}
387+
if let Some(f) = methods.invert {
388+
self.invert.store(Some(f));
389+
}
390+
if let Some(f) = methods.lshift {
391+
self.lshift.store(Some(f));
392+
}
393+
if let Some(f) = methods.rshift {
394+
self.rshift.store(Some(f));
395+
}
396+
if let Some(f) = methods.and {
397+
self.and.store(Some(f));
398+
}
399+
if let Some(f) = methods.xor {
400+
self.xor.store(Some(f));
401+
}
402+
if let Some(f) = methods.or {
403+
self.or.store(Some(f));
404+
}
405+
if let Some(f) = methods.int {
406+
self.int.store(Some(f));
407+
}
408+
if let Some(f) = methods.float {
409+
self.float.store(Some(f));
410+
}
411+
if let Some(f) = methods.inplace_add {
412+
self.inplace_add.store(Some(f));
413+
}
414+
if let Some(f) = methods.inplace_subtract {
415+
self.inplace_subtract.store(Some(f));
416+
}
417+
if let Some(f) = methods.inplace_multiply {
418+
self.inplace_multiply.store(Some(f));
419+
}
420+
if let Some(f) = methods.inplace_remainder {
421+
self.inplace_remainder.store(Some(f));
422+
}
423+
if let Some(f) = methods.inplace_power {
424+
self.inplace_power.store(Some(f));
425+
}
426+
if let Some(f) = methods.inplace_lshift {
427+
self.inplace_lshift.store(Some(f));
428+
}
429+
if let Some(f) = methods.inplace_rshift {
430+
self.inplace_rshift.store(Some(f));
431+
}
432+
if let Some(f) = methods.inplace_and {
433+
self.inplace_and.store(Some(f));
434+
}
435+
if let Some(f) = methods.inplace_xor {
436+
self.inplace_xor.store(Some(f));
437+
}
438+
if let Some(f) = methods.inplace_or {
439+
self.inplace_or.store(Some(f));
440+
}
441+
if let Some(f) = methods.floor_divide {
442+
self.floor_divide.store(Some(f));
443+
}
444+
if let Some(f) = methods.true_divide {
445+
self.true_divide.store(Some(f));
446+
}
447+
if let Some(f) = methods.inplace_floor_divide {
448+
self.inplace_floor_divide.store(Some(f));
449+
}
450+
if let Some(f) = methods.inplace_true_divide {
451+
self.inplace_true_divide.store(Some(f));
452+
}
453+
if let Some(f) = methods.index {
454+
self.index.store(Some(f));
455+
}
456+
if let Some(f) = methods.matrix_multiply {
457+
self.matrix_multiply.store(Some(f));
458+
}
459+
if let Some(f) = methods.inplace_matrix_multiply {
460+
self.inplace_matrix_multiply.store(Some(f));
461+
}
462+
}
463+
355464
pub fn left_binary_op(&self, op_slot: PyNumberBinaryOp) -> Option<PyNumberBinaryFunc> {
356465
use PyNumberBinaryOp::*;
357466
match op_slot {

crates/vm/src/types/slot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,10 @@ pub trait AsNumber: PyPayload {
17101710
#[pyslot]
17111711
fn as_number() -> &'static PyNumberMethods;
17121712

1713+
fn extend_slots(slots: &mut PyTypeSlots) {
1714+
slots.as_number.copy_from(Self::as_number());
1715+
}
1716+
17131717
fn clone_exact(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
17141718
// not all AsNumber requires this implementation.
17151719
unimplemented!()

0 commit comments

Comments
 (0)