diff options
| -rw-r--r-- | include/mruby.h | 31 | ||||
| -rw-r--r-- | mrbgems/mruby-compiler/core/codegen.c | 14 | ||||
| -rw-r--r-- | src/class.c | 10 |
3 files changed, 53 insertions, 2 deletions
diff --git a/include/mruby.h b/include/mruby.h index 25514ee2c..1b227d41a 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -567,6 +567,37 @@ MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name); MRB_API struct RClass * mrb_class_get(mrb_state *mrb, const char *name); /** + * Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined. + * + * Example: + * void + * mrb_example_gem_init(mrb_state* mrb) { + * struct RClass *example_outer, *example_inner; + * mrb_bool cd; + * + * example_outer = mrb_define_module(mrb, "ExampleOuter"); + * + * example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class); + * cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner"); + * + * // If mrb_class_defined_under returns 1 then puts "True" + * // If mrb_class_defined_under returns 0 then puts "False" + * if (cd == 1){ + * puts("True"); + * } + * else { + * puts("False"); + * } + * } + * + * @param [mrb_state*] mrb The current mruby state. + * @param [struct RClass *] outer The name of the outer class. + * @param [const char *] name A string representing the name of the inner class. + * @return [mrb_bool] A boolean value. + */ +MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name); + +/** * Gets a child class. * @param [mrb_state*] mrb The current mruby state. * @param [struct RClass *] outer The name of the parent class. diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index c898acddc..9b064b867 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -1655,11 +1655,21 @@ codegen(codegen_scope *s, node *tree, int val) } tree = tree->car; if (tree->car) { /* pre */ + int first = TRUE; t = tree->car; n = 0; while (t) { - gen_assignment(s, t->car, rhs+n, NOVAL); - n++; + if (n < len) { + gen_assignment(s, t->car, rhs+n, NOVAL); + n++; + } + else { + if (first) { + genop(s, MKOP_A(OP_LOADNIL, rhs+n)); + first = FALSE; + } + gen_assignment(s, t->car, rhs+n, NOVAL); + } t = t->cdr; } } diff --git a/src/class.c b/src/class.c index 47a6c846b..b81859ab8 100644 --- a/src/class.c +++ b/src/class.c @@ -271,6 +271,16 @@ mrb_class_defined(mrb_state *mrb, const char *name) return mrb_const_defined(mrb, mrb_obj_value(mrb->object_class), mrb_symbol(sym)); } +MRB_API mrb_bool +mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name) +{ + mrb_value sym = mrb_check_intern_cstr(mrb, name); + if (mrb_nil_p(sym)) { + return FALSE; + } + return mrb_const_defined_at(mrb, mrb_obj_value(outer), mrb_symbol(sym)); +} + MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name) { |
