Permalink
Browse files

Implement associative_delegate.

  • Loading branch information...
jnthn committed Feb 9, 2013
1 parent 08684cd commit 44361425d023dc8c22700c78329ef1e9e06f9b43
Showing with 67 additions and 16 deletions.
  1. +51 −4 src/6model/reprs/P6opaque.c
  2. +16 −12 src/pmc/sixmodelobject.pmc
@@ -809,12 +809,54 @@ static STable * get_elem_stable(PARROT_INTERP, STable *st) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"This type does not support get_elem_stable");
}
+PARROT_DOES_NOT_RETURN
+static void die_no_ass_del(PARROT_INTERP) {
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
+ "This type does not support associative operations");
+}
+static PMC * at_key_boxed(PARROT_INTERP, STable *st, void *data, STRING *key) {
+ P6opaqueREPRData *repr_data = (P6opaqueREPRData *)st->REPR_data;
+ if (repr_data->ass_del_slot == -1)
+ die_no_ass_del(interp);
+ return VTABLE_get_pmc_keyed_str(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->ass_del_slot]),
+ key);
+}
+static void bind_key_boxed(PARROT_INTERP, STable *st, void *data, STRING *key, PMC *value) {
+ P6opaqueREPRData *repr_data = (P6opaqueREPRData *)st->REPR_data;
+ if (repr_data->ass_del_slot == -1)
+ die_no_ass_del(interp);
+ VTABLE_set_pmc_keyed_str(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->ass_del_slot]),
+ key, value);
+}
+static INTVAL exists_key(PARROT_INTERP, STable *st, void *data, STRING *key) {
+ P6opaqueREPRData *repr_data = (P6opaqueREPRData *)st->REPR_data;
+ if (repr_data->ass_del_slot == -1)
+ die_no_ass_del(interp);
+ return VTABLE_exists_keyed_str(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->ass_del_slot]),
+ key);
+}
+static void delete_key(PARROT_INTERP, STable *st, void *data, STRING *key) {
+ P6opaqueREPRData *repr_data = (P6opaqueREPRData *)st->REPR_data;
+ if (repr_data->ass_del_slot == -1)
+ die_no_ass_del(interp);
+ VTABLE_delete_keyed_str(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->ass_del_slot]),
+ key);
+}
static INTVAL elems(PARROT_INTERP, STable *st, void *data) {
P6opaqueREPRData *repr_data = (P6opaqueREPRData *)st->REPR_data;
- if (repr_data->pos_del_slot == -1)
- die_no_pos_del(interp);
- return VTABLE_elements(interp,
- get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->pos_del_slot]));
+ if (repr_data->pos_del_slot >= 0)
+ return VTABLE_elements(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->pos_del_slot]));
+ else if (repr_data->ass_del_slot >= 0)
+ return VTABLE_elements(interp,
+ get_pmc_at_offset(data, repr_data->attribute_offsets[repr_data->ass_del_slot]));
+ else
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
+ "This type does not support elems");
}
/* This Parrot-specific addition to the API is used to mark an object. */
@@ -1225,6 +1267,11 @@ REPROps * P6opaque_initialize(PARROT_INTERP) {
this_repr->pos_funcs->unshift_boxed = unshift_boxed;
this_repr->pos_funcs->shift_boxed = shift_boxed;
this_repr->pos_funcs->get_elem_stable = get_elem_stable;
+ this_repr->ass_funcs = mem_allocate_typed(REPROps_Associative);
+ this_repr->ass_funcs->at_key_boxed = at_key_boxed;
+ this_repr->ass_funcs->bind_key_boxed = bind_key_boxed;
+ this_repr->ass_funcs->exists_key = exists_key;
+ this_repr->ass_funcs->delete_key = delete_key;
this_repr->elems = elems;
this_repr->gc_mark = gc_mark;
this_repr->gc_free = gc_free;
View
@@ -338,8 +338,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
return VTABLE_get_pmc_keyed(interp, val, key);
}
else
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
- "SixModelObject does not implement get_pmc_keyed");
+ return REPR(decont)->ass_funcs->at_key_boxed(interp, STABLE(decont),
+ OBJECT_BODY(decont), VTABLE_get_string(interp, key));
}
}
@@ -393,8 +393,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
return VTABLE_get_pmc_keyed_str(interp, val, key);
}
else
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
- "SixModelObject does not implement get_pmc_keyed_str");
+ return REPR(decont)->ass_funcs->at_key_boxed(interp, STABLE(decont),
+ OBJECT_BODY(decont), key);
}
VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
@@ -426,8 +426,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
VTABLE_set_pmc_keyed(interp, val, key, value);
}
else
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
- "SixModelObject does not implement set_pmc_keyed");
+ REPR(decont)->ass_funcs->bind_key_boxed(interp, STABLE(decont),
+ OBJECT_BODY(decont), VTABLE_get_string(interp, key), value);
}
}
@@ -479,8 +479,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
VTABLE_set_pmc_keyed_str(interp, val, key, value);
}
else
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
- "SixModelObject does not implement set_pmc_keyed_str");
+ REPR(decont)->ass_funcs->bind_key_boxed(interp, STABLE(decont),
+ OBJECT_BODY(decont), key, value);
}
VTABLE INTVAL exists_keyed(PMC *key) {
@@ -513,7 +513,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
return VTABLE_exists_keyed(interp, val, key);
}
else
- return SUPER(key);
+ return REPR(decont)->ass_funcs->exists_key(interp, STABLE(decont),
+ OBJECT_BODY(decont), VTABLE_get_string(interp, key));
}
}
@@ -559,7 +560,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
return VTABLE_exists_keyed_str(interp, val, key);
}
else
- return SUPER(key);
+ return REPR(decont)->ass_funcs->exists_key(interp, STABLE(decont),
+ OBJECT_BODY(decont), key);
}
VTABLE void delete_keyed(PMC *key) {
@@ -591,7 +593,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
VTABLE_delete_keyed(interp, val, key);
}
else
- SUPER(key);
+ REPR(decont)->ass_funcs->delete_key(interp, STABLE(decont),
+ OBJECT_BODY(decont), VTABLE_get_string(interp, key));
}
}
@@ -635,7 +638,8 @@ pmclass SixModelObject manual_attrs dynpmc group nqp {
VTABLE_delete_keyed_str(interp, val, key);
}
else
- SUPER(key);
+ REPR(decont)->ass_funcs->delete_key(interp, STABLE(decont),
+ OBJECT_BODY(decont), key);
}
VTABLE void unshift_pmc(PMC *value) {

0 comments on commit 4436142

Please sign in to comment.