Skip to content

Commit

Permalink
Added mrb_class_under_defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Jones committed Nov 6, 2016
1 parent 0b8d8dd commit 8d6aa06
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/mruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,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 child class was defined, and false if the child class was not defined.
*
* Example:
* void
* mrb_example_gem_init(mrb_state* mrb) {
* struct RClass *example_parent, *example_child;
* mrb_bool cd;
*
* example_parent = mrb_define_module(mrb, "ExampleParent");
*
* example_child = mrb_define_class(mrb, "ExampleChild", mrb->object_class);
* cd = mrb_class_under_defined(mrb, example_parent, "ExampleChild");
*
* // If mrb_class_under_defined returns 1 then puts "True"
* // If mrb_class_under_defined 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 parent class.
* @param [const char *] name A string representing the name of the child class.
* @return [mrb_bool] A boolean value.
*/
MRB_API mrb_bool mrb_class_under_defined(mrb_state *mrb, struct RClass *outer, const char *name);

/**
* Gets a child class.
* @param [mrb_state*] mrb The current mruby state.
Expand Down
10 changes: 10 additions & 0 deletions src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_under_defined(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)
{
Expand Down

0 comments on commit 8d6aa06

Please sign in to comment.