mrb_mt_foreach(mrb_state *mrb, struct RClass *c, mrb_mt_foreach_func *fn, void *p)
{
  mt_tbl *t = c->mt;
  int i;

  if (t == NULL) return;
  if (t->alloc == 0) return;
  if (t->size == 0) return;

  mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
  union mt_ptr *vals = t->ptr;
  for (i=0; i<t->alloc; i++) {
    mrb_sym key = keys[i];
    if (MT_KEY_SYM(key)) {
      mrb_method_t m;

      if (key & MT_FUNC_P) {
        MRB_METHOD_FROM_FUNC(m, vals[i].func);
      }
      else {
        MRB_METHOD_FROM_PROC(m, vals[i].proc);
      }
      if (key & MT_NOARG_P) {
        MRB_METHOD_NOARG_SET(m);
      }

      if (fn(mrb, MT_KEY_SYM(key), m, p) != 0)
        return;
    }
  }
  return;
}
mrb_mt_foreach(mrb_state *mrb, struct RClass *c, mrb_mt_foreach_func *fn, void *p)
{
  mt_tbl *t = c->mt;
  int i;

  if (t == NULL) return;
  if (t->alloc == 0) return;
  if (t->size == 0) return;

  mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
  union mt_ptr *vals = t->ptr;
  for (i=0; i<t->alloc; i++) {
    mrb_sym key = keys[i];
    if (MT_KEY_SYM(key)) {
      mrb_method_t m;

      if (key & MT_FUNC_P) {
        MRB_METHOD_FROM_FUNC(m, vals[i].func);
      }
      else {
        MRB_METHOD_FROM_PROC(m, vals[i].proc);
      }
      if (key & MT_NOARG_P) {
        MRB_METHOD_NOARG_SET(m);
      }

      if (fn(mrb, MT_KEY_SYM(key), m, p) != 0)
        return;
    }
  }
  return;
}