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

parent f6b2e623
...@@ -131,6 +131,10 @@ void mrb_gc_mark_iv(mrb_state*, struct RObject*); ...@@ -131,6 +131,10 @@ void mrb_gc_mark_iv(mrb_state*, struct RObject*);
size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*); size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*);
void mrb_gc_free_iv(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 MRB_END_DECL
#endif /* MRUBY_VARIABLE_H */ #endif /* MRUBY_VARIABLE_H */
...@@ -9,8 +9,7 @@ ...@@ -9,8 +9,7 @@
#include <mruby/class.h> #include <mruby/class.h>
#include <mruby/proc.h> #include <mruby/proc.h>
#include <mruby/string.h> #include <mruby/string.h>
#include <mruby/variable.h>
typedef int (iv_foreach_func)(mrb_state*,mrb_sym,mrb_value,void*);
#ifndef MRB_IV_SEGMENT_SIZE #ifndef MRB_IV_SEGMENT_SIZE
#define MRB_IV_SEGMENT_SIZE 4 #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) ...@@ -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); 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 static inline mrb_bool
namespace_p(enum mrb_vtype tt) namespace_p(enum mrb_vtype tt)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment