diff options
author | Peter Zhu <peter@peterzhu.ca> | 2023-11-06 08:29:41 -0500 |
---|---|---|
committer | Peter Zhu <peter@peterzhu.ca> | 2023-11-06 11:10:41 -0500 |
commit | 679e98dc27c4711601801db8852c5b4bc5a9da76 (patch) | |
tree | 1f98c7a67c0660f6e022b4dffe28d676ecfec8cb /variable.c | |
parent | c747c67533ef901d10ef054d2f57a0b90702c7f9 (diff) | |
download | ruby-679e98dc27c4711601801db8852c5b4bc5a9da76.tar.gz |
Use general_ivar_set for Objects
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 80 |
1 files changed, 35 insertions, 45 deletions
diff --git a/variable.c b/variable.c index b67dbb4f09..f86d6210cb 100644 --- a/variable.c +++ b/variable.c @@ -1676,59 +1676,49 @@ rb_obj_evacuate_ivs_to_hash_table(ID key, VALUE val, st_data_t arg) return ST_CONTINUE; } -attr_index_t -rb_obj_ivar_set(VALUE obj, ID id, VALUE val) +static VALUE * +obj_ivar_set_shape_ivptr(VALUE obj, void *_data) { - attr_index_t index; - - rb_shape_t *shape = rb_shape_get_shape(obj); - uint32_t num_iv = shape->capacity; - - if (rb_shape_obj_too_complex(obj)) { - rb_complex_ivar_set(obj, id, val); - return 0; - } - - rb_shape_t *next_shape; - - if (!rb_shape_get_iv_index(shape, id, &index)) { - index = shape->next_iv_index; - if (index >= MAX_IVARS) { - rb_raise(rb_eArgError, "too many instance variables"); - } + RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); - RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); + return ROBJECT_IVPTR(obj); +} - if (UNLIKELY(shape->next_iv_index >= num_iv)) { - RUBY_ASSERT(shape->next_iv_index == num_iv); +static void +obj_ivar_set_shape_resize_ivptr(VALUE obj, attr_index_t old_capa, attr_index_t new_capa, void *_data) +{ + rb_ensure_iv_list_size(obj, old_capa, new_capa); +} - next_shape = rb_grow_iv_list(obj); - if (next_shape->type == SHAPE_OBJ_TOO_COMPLEX) { - rb_complex_ivar_set(obj, id, val); - return 0; - } - shape = next_shape; - RUBY_ASSERT(shape->type == SHAPE_CAPACITY_CHANGE); - } +static void +obj_ivar_set_set_shape(VALUE obj, rb_shape_t *shape, void *_data) +{ + rb_shape_set_shape(obj, shape); +} - next_shape = rb_shape_get_next(shape, obj, id); +static void +obj_ivar_set_transition_too_complex(VALUE obj, void *_data) +{ + rb_evict_ivars_to_hash(obj, rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX)); +} - if (next_shape->type == SHAPE_OBJ_TOO_COMPLEX) { - rb_evict_ivars_to_hash(obj, shape); - rb_complex_ivar_set(obj, id, val); - return 0; - } - else { - rb_shape_set_shape(obj, next_shape); - RUBY_ASSERT(next_shape->type == SHAPE_IVAR); - RUBY_ASSERT(index == (next_shape->next_iv_index - 1)); - } - } +static st_table * +obj_ivar_set_too_complex_table(VALUE obj, void *_data) +{ + RUBY_ASSERT(rb_shape_obj_too_complex(obj)); - RUBY_ASSERT(!rb_shape_obj_too_complex(obj)); - RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[index], val); + return ROBJECT_IV_HASH(obj); +} - return index; +attr_index_t +rb_obj_ivar_set(VALUE obj, ID id, VALUE val) +{ + return general_ivar_set(obj, id, val, NULL, + obj_ivar_set_shape_ivptr, + obj_ivar_set_shape_resize_ivptr, + obj_ivar_set_set_shape, + obj_ivar_set_transition_too_complex, + obj_ivar_set_too_complex_table).index; } /* Set the instance variable +val+ on object +obj+ at ivar name +id+. |