Skip to content

Commit

Permalink
Define API for accessing tp_iterkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed May 20, 2024
1 parent 95b6eba commit 4ca24ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/deemon/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -4845,6 +4845,12 @@ DFUNDEF WUNUSED NONNULL((1, 2)) Dee_ssize_t /* TODO: Refactor more code to use t
DFUNDEF WUNUSED NONNULL((1, 2)) Dee_ssize_t
(DCALL DeeObject_Enumerate)(DeeObject *__restrict self, Dee_enumerate_t proc, void *arg);

/* Construct an iterator for the keys of "self". That is:
* - everything for which `DeeObject_HasItem()' returns true
* - everything passed as "key"-argumented to the callback taken by `DeeObject_Enumerate()' */
DFUNDEF WUNUSED NONNULL((1)) DREF DeeObject *
(DCALL DeeObject_IterKeys)(DeeObject *__restrict self);

/* Same as `DeeObject_Enumerate()', but only valid when "self" uses integers for indices
* or is a mapping where all keys are integers. In the former case, [start,end) can be
* given in order to only enumerate indices that fall within that range (any index that
Expand Down
1 change: 1 addition & 0 deletions include/deemon/super.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ DFUNDEF WUNUSED NONNULL((1, 2, 3)) Dee_ssize_t (DCALL DeeObject_TForeach)(DeeTyp
DFUNDEF WUNUSED NONNULL((1, 2, 3)) Dee_ssize_t (DCALL DeeObject_TForeachPair)(DeeTypeObject *tp_self, DeeObject *self, Dee_foreach_pair_t proc, void *arg);
DFUNDEF WUNUSED NONNULL((1, 2, 3)) Dee_ssize_t (DCALL DeeObject_TEnumerate)(DeeTypeObject *tp_self, DeeObject *self, Dee_enumerate_t proc, void *arg);
DFUNDEF WUNUSED NONNULL((1, 2, 3)) Dee_ssize_t (DCALL DeeObject_TEnumerateIndex)(DeeTypeObject *tp_self, DeeObject *self, Dee_enumerate_index_t proc, void *arg, size_t start, size_t end);
DFUNDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *(DCALL DeeObject_TIterKeys)(DeeTypeObject *tp_self, DeeObject *self);
DFUNDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *(DCALL DeeObject_TIter)(DeeTypeObject *tp_self, DeeObject *self);
DFUNDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *(DCALL DeeObject_TIterNext)(DeeTypeObject *tp_self, DeeObject *self);
DFUNDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *(DCALL DeeObject_TSizeOb)(DeeTypeObject *tp_self, DeeObject *self);
Expand Down
7 changes: 6 additions & 1 deletion src/deemon/objects/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ super_enumerate_index(Super *__restrict me, Dee_enumerate_index_t proc,
return DeeObject_TEnumerateIndex(me->s_type, me->s_self, proc, arg, start, end);
}

PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL
super_iterkeys(Super *__restrict self) {
return DeeObject_TIterKeys(self->s_type, self->s_self);
}

PRIVATE WUNUSED NONNULL((1, 2)) int DCALL
super_bounditem(Super *self, DeeObject *index) {
return DeeObject_TBoundItem(self->s_type, self->s_self, index);
Expand Down Expand Up @@ -787,7 +792,7 @@ PRIVATE struct type_seq super_seq = {
/* .tp_foreach_pair = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, Dee_foreach_pair_t, void *))&super_foreach_pair,
/* .tp_enumerate = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, Dee_enumerate_t, void *))&super_enumerate,
/* .tp_enumerate_index = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, Dee_enumerate_index_t, void *, size_t, size_t))&super_enumerate_index,
/* .tp_iterkeys = */ NULL,
/* .tp_iterkeys = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&super_iterkeys,
/* .tp_bounditem = */ (int (DCALL *)(DeeObject *, DeeObject *))&super_bounditem,
/* .tp_hasitem = */ (int (DCALL *)(DeeObject *, DeeObject *))&super_hasitem,
/* .tp_size = */ (size_t (DCALL *)(DeeObject *__restrict))&super_size,
Expand Down
17 changes: 16 additions & 1 deletion src/deemon/runtime/operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -19767,10 +19767,25 @@ DEFINE_OPERATOR(Dee_ssize_t, Enumerate,
unlikely(DeeType_InheritIter(tp_self) && tp_self->tp_seq->tp_enumerate))
return (*tp_self->tp_seq->tp_enumerate)(self, proc, arg);
return DeeError_Throwf(&DeeError_NotImplemented,
"Cannot enumerate non-sequence type `%r'",
"Cannot enumerate keys of non-sequence type `%r'",
tp_self);
}

/* Construct an iterator for the keys of "self". That is:
* - everything for which `DeeObject_HasItem()' returns true
* - everything passed as "key"-argumented to the callback taken by `DeeObject_Enumerate()' */
DEFINE_OPERATOR(DREF DeeObject *, IterKeys,
(DeeObject *RESTRICT_IF_NOTYPE self)) {
LOAD_TP_SELF;
if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_iterkeys) ||
unlikely(DeeType_InheritIter(tp_self) && tp_self->tp_seq->tp_iterkeys))
return (*tp_self->tp_seq->tp_iterkeys)(self);
DeeError_Throwf(&DeeError_NotImplemented,
"Cannot enumerate keys of non-sequence type `%r'",
tp_self);
return NULL;
}

/* Same as `DeeObject_Enumerate()', but only valid when "self" uses integers for indices
* or is a mapping where all keys are integers. In the former case, [start,end)
* can be given in order to allow the implementation to only enumerate indices that fall
Expand Down

0 comments on commit 4ca24ca

Please sign in to comment.