Skip to content

Commit 6dd7a7b

Browse files
committed
Refactor rb_obj_field_get to handle complex shapes
This allow to get or set fields without having to worry about the shape type.
1 parent d9502a8 commit 6dd7a7b

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

variable.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,25 +1302,53 @@ VALUE
13021302
rb_obj_field_get(VALUE obj, rb_shape_t *target_shape)
13031303
{
13041304
RUBY_ASSERT(!SPECIAL_CONST_P(obj));
1305-
RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj));
13061305
RUBY_ASSERT(target_shape->type == SHAPE_IVAR || target_shape->type == SHAPE_OBJ_ID);
13071306

1308-
attr_index_t attr_index = target_shape->next_field_index - 1;
1307+
if (rb_shape_too_complex_p(target_shape)) {
1308+
st_table *fields_hash;
1309+
switch (BUILTIN_TYPE(obj)) {
1310+
case T_CLASS:
1311+
case T_MODULE:
1312+
ASSERT_vm_locking();
1313+
fields_hash = RCLASS_FIELDS_HASH(obj);
1314+
break;
1315+
case T_OBJECT:
1316+
fields_hash = ROBJECT_FIELDS_HASH(obj);
1317+
break;
1318+
default:
1319+
RUBY_ASSERT(FL_TEST_RAW(obj, FL_EXIVAR));
1320+
struct gen_fields_tbl *fields_tbl = NULL;
1321+
rb_ivar_generic_fields_tbl_lookup(obj, &fields_tbl);
1322+
RUBY_ASSERT(fields_tbl);
1323+
fields_hash = fields_tbl->as.complex.table;
1324+
break;
1325+
}
1326+
VALUE value = Qundef;
1327+
st_lookup(fields_hash, target_shape->edge_name, &value);
1328+
RUBY_ASSERT(!UNDEF_P(value));
1329+
return value;
1330+
}
13091331

1332+
attr_index_t attr_index = target_shape->next_field_index - 1;
1333+
VALUE *fields;
13101334
switch (BUILTIN_TYPE(obj)) {
13111335
case T_CLASS:
13121336
case T_MODULE:
13131337
ASSERT_vm_locking();
1314-
return RCLASS_FIELDS(obj)[attr_index];
1338+
fields = RCLASS_FIELDS(obj);
1339+
break;
13151340
case T_OBJECT:
1316-
return ROBJECT_FIELDS(obj)[attr_index];
1341+
fields = ROBJECT_FIELDS(obj);
1342+
break;
13171343
default:
13181344
RUBY_ASSERT(FL_TEST_RAW(obj, FL_EXIVAR));
13191345
struct gen_fields_tbl *fields_tbl = NULL;
13201346
rb_ivar_generic_fields_tbl_lookup(obj, &fields_tbl);
13211347
RUBY_ASSERT(fields_tbl);
1322-
return fields_tbl->as.shape.fields[attr_index];
1348+
fields = fields_tbl->as.shape.fields;
1349+
break;
13231350
}
1351+
return fields[attr_index];
13241352
}
13251353

13261354
VALUE

0 commit comments

Comments
 (0)