Skip to content

Commit bf54542

Browse files
committed
Add associative REPR API.
1 parent c123e22 commit bf54542

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

src/6model/repr_registry.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,56 @@ static void * default_get_boxed_ref(PARROT_INTERP, STable *st, void *data, INTVA
7777
"%Ss cannot box other types", st->REPR->name);
7878
}
7979
PARROT_DOES_NOT_RETURN
80-
static void die_no_idx(PARROT_INTERP, STRING *repr_name) {
80+
static void die_no_pos(PARROT_INTERP, STRING *repr_name) {
8181
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
82-
"%Ss representation does not support indexed storage", repr_name);
82+
"%Ss representation does not support positional storage", repr_name);
8383
}
8484
static void default_at_pos_native(PARROT_INTERP, STable *st, void *data, INTVAL index, NativeValue *value) {
85-
die_no_idx(interp, st->REPR->name);
85+
die_no_pos(interp, st->REPR->name);
8686
}
8787
static PMC * default_at_pos_boxed(PARROT_INTERP, STable *st, void *data, INTVAL index) {
88-
die_no_idx(interp, st->REPR->name);
88+
die_no_pos(interp, st->REPR->name);
8989
}
9090
static void default_bind_pos_native(PARROT_INTERP, STable *st, void *data, INTVAL index, NativeValue *value) {
91-
die_no_idx(interp, st->REPR->name);
91+
die_no_pos(interp, st->REPR->name);
9292
}
9393
static void default_bind_pos_boxed(PARROT_INTERP, STable *st, void *data, INTVAL index, PMC *obj) {
94-
die_no_idx(interp, st->REPR->name);
94+
die_no_pos(interp, st->REPR->name);
9595
}
9696
static INTVAL default_elems(PARROT_INTERP, STable *st, void *data) {
97-
die_no_idx(interp, st->REPR->name);
97+
die_no_pos(interp, st->REPR->name);
9898
}
9999
static void default_push_boxed(PARROT_INTERP, STable *st, void *data, PMC *obj) {
100-
die_no_idx(interp, st->REPR->name);
100+
die_no_pos(interp, st->REPR->name);
101101
}
102102
static PMC * default_pop_boxed(PARROT_INTERP, STable *st, void *data) {
103-
die_no_idx(interp, st->REPR->name);
103+
die_no_pos(interp, st->REPR->name);
104104
}
105105
static void default_unshift_boxed(PARROT_INTERP, STable *st, void *data, PMC *obj) {
106-
die_no_idx(interp, st->REPR->name);
106+
die_no_pos(interp, st->REPR->name);
107107
}
108108
static PMC * default_shift_boxed(PARROT_INTERP, STable *st, void *data) {
109-
die_no_idx(interp, st->REPR->name);
109+
die_no_pos(interp, st->REPR->name);
110110
}
111111
static STable * default_get_elem_stable(PARROT_INTERP, STable *st) {
112-
die_no_idx(interp, st->REPR->name);
112+
die_no_pos(interp, st->REPR->name);
113+
}
114+
PARROT_DOES_NOT_RETURN
115+
static void die_no_ass(PARROT_INTERP, STRING *repr_name) {
116+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
117+
"%Ss representation does not support associative storage", repr_name);
118+
}
119+
static PMC * default_at_key_boxed(PARROT_INTERP, STable *st, void *data, STRING *key) {
120+
die_no_ass(interp, st->REPR->name);
121+
}
122+
static void default_bind_key_boxed(PARROT_INTERP, STable *st, void *data, STRING *key, PMC *value) {
123+
die_no_ass(interp, st->REPR->name);
124+
}
125+
static INTVAL default_exists_key(PARROT_INTERP, STable *st, void *data, STRING *key) {
126+
die_no_ass(interp, st->REPR->name);
127+
}
128+
static void default_delete_key(PARROT_INTERP, STable *st, void *data, STRING *key) {
129+
die_no_ass(interp, st->REPR->name);
113130
}
114131

115132
/* Set default attribute functions on a REPR that lacks them. */
@@ -150,6 +167,15 @@ static void add_default_pos_funcs(PARROT_INTERP, REPROps *repr) {
150167
repr->pos_funcs->get_elem_stable = default_get_elem_stable;
151168
}
152169

170+
/* Set default associative functions on a REPR that lacks them. */
171+
static void add_default_ass_funcs(PARROT_INTERP, REPROps *repr) {
172+
repr->ass_funcs = mem_allocate_typed(REPROps_Associative);
173+
repr->ass_funcs->at_key_boxed = default_at_key_boxed;
174+
repr->ass_funcs->bind_key_boxed = default_bind_key_boxed;
175+
repr->ass_funcs->exists_key = default_exists_key;
176+
repr->ass_funcs->delete_key = default_delete_key;
177+
}
178+
153179
/* Registers a representation. It this is ever made public, it should first be
154180
* made thread-safe. */
155181
static void register_repr(PARROT_INTERP, STRING *name, REPROps *repr) {
@@ -169,6 +195,8 @@ static void register_repr(PARROT_INTERP, STRING *name, REPROps *repr) {
169195
add_default_box_funcs(interp, repr);
170196
if (!repr->pos_funcs)
171197
add_default_pos_funcs(interp, repr);
198+
if (!repr->ass_funcs)
199+
add_default_ass_funcs(interp, repr);
172200
}
173201

174202
/* Dynamically registers a representation (that is, one defined outside of

src/6model/sixmodelobject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ typedef struct SixModel_REPROps_Positional {
274274
/* Gets the STable representing the declared element type. */
275275
STable * (*get_elem_stable) (PARROT_INTERP, STable *st);
276276
} REPROps_Positional;
277+
typedef struct SixModel_REPROps_Associative {
278+
/* Gets the value at the specified key. */
279+
PMC * (*at_key_boxed) (PARROT_INTERP, STable *st, void *data, STRING *key);
280+
281+
/* Binds a value to the specified key. */
282+
void (*bind_key_boxed) (PARROT_INTERP, STable *st, void *data, STRING *key, PMC *value);
283+
284+
/* Checks if the specified key exists. */
285+
INTVAL (*exists_key) (PARROT_INTERP, STable *st, void *data, STRING *key);
286+
287+
/* Deletes the specified key. */
288+
void (*delete_key) (PARROT_INTERP, STable *st, void *data, STRING *key);
289+
} REPROps_Associative;
277290
struct SixModel_REPROps {
278291
/* Creates a new type object of this representation, and
279292
* associates it with the given HOW. Also sets up a new
@@ -310,6 +323,9 @@ struct SixModel_REPROps {
310323

311324
/* Positional REPR function table. */
312325
struct SixModel_REPROps_Positional *pos_funcs;
326+
327+
/* Associative REPR function table. */
328+
struct SixModel_REPROps_Associative *ass_funcs;
313329

314330
/* Gets the storage specification for this representation. */
315331
storage_spec (*get_storage_spec) (PARROT_INTERP, STable *st);

0 commit comments

Comments
 (0)