Add new API mrb_iv_foreach() to iterate over instance variables.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2018-12-11 09:48:15 +09:00
parent f6b2e6231a
commit a0df27d8e0
2 changed files with 13 additions and 2 deletions
+4
View File
@@ -131,6 +131,10 @@ void mrb_gc_mark_iv(mrb_state*, struct RObject*);
size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*);
void mrb_gc_free_iv(mrb_state*, struct RObject*);
/* return non zero to break the loop */
typedef int (iv_foreach_func)(mrb_state*,mrb_sym,mrb_value,void*);
MRB_API void mrb_iv_foreach(mrb_state *mrb, mrb_value obj, iv_foreach_func *func, void *p);
MRB_END_DECL
#endif /* MRUBY_VARIABLE_H */
+9 -2
View File
@@ -9,8 +9,7 @@
#include <mruby/class.h>
#include <mruby/proc.h>
#include <mruby/string.h>
typedef int (iv_foreach_func)(mrb_state*,mrb_sym,mrb_value,void*);
#include <mruby/variable.h>
#ifndef MRB_IV_SEGMENT_SIZE
#define MRB_IV_SEGMENT_SIZE 4
@@ -359,6 +358,14 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
mrb_write_barrier(mrb, (struct RBasic*)obj);
}
/* Iterates over the instance variable table. */
MRB_API void
mrb_iv_foreach(mrb_state *mrb, mrb_value obj, iv_foreach_func *func, void *p)
{
if (!obj_iv_p(obj)) return;
iv_foreach(mrb, mrb_obj_ptr(obj)->iv, func, p);
}
static inline mrb_bool
namespace_p(enum mrb_vtype tt)
{