summaryrefslogtreecommitdiff
path: root/variable.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-12-05 06:35:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-12-05 06:35:07 +0000
commit1cc1ea378a0912bfcbe55399369da47a724427aa (patch)
tree41b62895491f6a5c88ef6ae5990f9d2c24e3c109 /variable.c
parent7b154ee93e66ab894e68080a6fa3b5ae6f14f93e (diff)
* marshal.c (w_object): dump instance variables when using
marshal_dump. [ruby-core:24211] * variable.c (rb_ivar_count): added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'variable.c')
-rw-r--r--variable.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/variable.c b/variable.c
index ddeae93a1b..62256a049e 100644
--- a/variable.c
+++ b/variable.c
@@ -1197,6 +1197,44 @@ rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
}
}
+st_index_t
+rb_ivar_count(VALUE obj)
+{
+ st_table *tbl;
+ switch (TYPE(obj)) {
+ case T_OBJECT:
+ if ((tbl = ROBJECT_IV_INDEX_TBL(obj)) != 0) {
+ st_index_t i, num = tbl->num_entries, count = 0;
+ const VALUE *const ivptr = ROBJECT_IVPTR(obj);
+ for (i = count = 0; i < num; ++i) {
+ if (ivptr[i] != Qundef) {
+ count++;
+ }
+ }
+ return count;
+ }
+ break;
+ case T_CLASS:
+ case T_MODULE:
+ if ((tbl = RCLASS_IV_TBL(obj)) != 0) {
+ return tbl->num_entries;
+ }
+ break;
+ default:
+ if (!generic_iv_tbl) break;
+ if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
+ st_data_t data;
+
+ if (st_lookup(generic_iv_tbl, (st_data_t)obj, &data) &&
+ (tbl = (st_table *)data) != 0) {
+ return tbl->num_entries;
+ }
+ }
+ break;
+ }
+ return 0;
+}
+
static int
ivar_i(ID key, VALUE val, VALUE ary)
{