From 8864c30d161e627438b86a986c19c048eb31bcb4 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 28 May 2020 17:23:51 +0900 Subject: Provide functions that take symbols instead of `const char*`. - mrb_define_class_id - mrb_define_module_id - mrb_define_method_id - mrb_define_singleton_method_id - mrb_define_module_function_id - mrb_define_const_id - mrb_undef_method_id - mrb_undef_class_method_id - mrb_class_defined_id - mrb_class_get_id - mrb_class_defined_under_id - mrb_class_get_under_id - mrb_module_get_id - mrb_module_get_under_id - mrb_define_class_under_id - mrb_define_module_under_id - mrb_exc_get_id --- include/mruby.h | 37 ++++++++++++++++--------------- src/class.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 25 deletions(-) diff --git a/include/mruby.h b/include/mruby.h index 7a678ccfd..9411f9f00 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -695,8 +695,8 @@ MRB_API struct RClass* mrb_class_get_id(mrb_state *mrb, mrb_sym name); * @param name The name of the class. * @return [struct RClass *] A reference to the class. */ -MRB_API struct RClass* mrb_exc_get(mrb_state *mrb, const char *name); MRB_API struct RClass* mrb_exc_get_id(mrb_state *mrb, mrb_sym name); +#define mrb_exc_get(mrb, name) mrb_exc_get_id(mrb, mrb_intern_cstr(mrb, name)) /** * Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined. @@ -1298,25 +1298,25 @@ MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap); + those E_* macros requires mrb_state* variable named mrb. + exception objects obtained from those macros are local to mrb */ -#define E_RUNTIME_ERROR (mrb_exc_get(mrb, "RuntimeError")) -#define E_TYPE_ERROR (mrb_exc_get(mrb, "TypeError")) -#define E_ARGUMENT_ERROR (mrb_exc_get(mrb, "ArgumentError")) -#define E_INDEX_ERROR (mrb_exc_get(mrb, "IndexError")) -#define E_RANGE_ERROR (mrb_exc_get(mrb, "RangeError")) -#define E_NAME_ERROR (mrb_exc_get(mrb, "NameError")) -#define E_NOMETHOD_ERROR (mrb_exc_get(mrb, "NoMethodError")) -#define E_SCRIPT_ERROR (mrb_exc_get(mrb, "ScriptError")) -#define E_SYNTAX_ERROR (mrb_exc_get(mrb, "SyntaxError")) -#define E_LOCALJUMP_ERROR (mrb_exc_get(mrb, "LocalJumpError")) -#define E_REGEXP_ERROR (mrb_exc_get(mrb, "RegexpError")) -#define E_FROZEN_ERROR (mrb_exc_get(mrb, "FrozenError")) - -#define E_NOTIMP_ERROR (mrb_exc_get(mrb, "NotImplementedError")) +#define E_RUNTIME_ERROR (mrb_exc_get_id(mrb, MRB_SYM(RuntimeError))) +#define E_TYPE_ERROR (mrb_exc_get_id(mrb, MRB_SYM(TypeError))) +#define E_ARGUMENT_ERROR (mrb_exc_get_id(mrb, MRB_SYM(ArgumentError))) +#define E_INDEX_ERROR (mrb_exc_get_id(mrb, MRB_SYM(IndexError))) +#define E_RANGE_ERROR (mrb_exc_get_id(mrb, MRB_SYM(RangeError))) +#define E_NAME_ERROR (mrb_exc_get_id(mrb, MRB_SYM(NameError))) +#define E_NOMETHOD_ERROR (mrb_exc_get_id(mrb, MRB_SYM(NoMethodError))) +#define E_SCRIPT_ERROR (mrb_exc_get_id(mrb, MRB_SYM(ScriptError))) +#define E_SYNTAX_ERROR (mrb_exc_get_id(mrb, MRB_SYM(SyntaxError))) +#define E_LOCALJUMP_ERROR (mrb_exc_get_id(mrb, MRB_SYM(LocalJumpError))) +#define E_REGEXP_ERROR (mrb_exc_get_id(mrb, MRB_SYM(RegexpError))) +#define E_FROZEN_ERROR (mrb_exc_get_id(mrb, MRB_SYM(FrozenError))) + +#define E_NOTIMP_ERROR (mrb_exc_get_id(mrb, MRB_SYM(NotImplementedError))) #ifndef MRB_WITHOUT_FLOAT -#define E_FLOATDOMAIN_ERROR (mrb_exc_get(mrb, "FloatDomainError")) +#define E_FLOATDOMAIN_ERROR (mrb_exc_get_id(mrb, MRB_SYM(FloatDomainError))) #endif -#define E_KEY_ERROR (mrb_exc_get(mrb, "KeyError")) +#define E_KEY_ERROR (mrb_exc_get_id(mrb, MRB_SYM(KeyError))) MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg); MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv); @@ -1346,6 +1346,7 @@ MRB_INLINE void mrb_check_frozen(mrb_state *mrb, void *o) } MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b); +MRB_API void mrb_define_alias_id(mrb_state *mrb, struct RClass *c, mrb_sym a, mrb_sym b); MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass); MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val); @@ -1382,7 +1383,7 @@ MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib); * * Implemented in mruby-fiber */ -#define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError")) +#define E_FIBER_ERROR (mrb_exc_get_id(mrb, MRB_SYM(FiberError))) MRB_API void mrb_stack_extend(mrb_state*, mrb_int); /* memory pool implementation */ diff --git a/src/class.c b/src/class.c index cf0243915..2bf26f659 100644 --- a/src/class.c +++ b/src/class.c @@ -229,6 +229,15 @@ mrb_vm_define_module(mrb_state *mrb, mrb_value outer, mrb_sym id) return define_module(mrb, id, mrb_class_ptr(outer)); } +MRB_API struct RClass* +mrb_define_module_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name) +{ + struct RClass * c = define_module(mrb, name, outer); + + setup_class(mrb, outer, c, name); + return c; +} + MRB_API struct RClass* mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name) { @@ -356,6 +365,12 @@ 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_id(mrb_state *mrb, mrb_sym name) +{ + return mrb_const_defined(mrb, mrb_obj_value(mrb->object_class), name); +} + MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name) { @@ -366,12 +381,24 @@ mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name) return mrb_const_defined_at(mrb, mrb_obj_value(outer), mrb_symbol(sym)); } +MRB_API mrb_bool +mrb_class_defined_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name) +{ + return mrb_const_defined_at(mrb, mrb_obj_value(outer), name); +} + MRB_API struct RClass* mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name) { return class_from_sym(mrb, outer, mrb_intern_cstr(mrb, name)); } +MRB_API struct RClass* +mrb_class_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name) +{ + return class_from_sym(mrb, outer, name); +} + MRB_API struct RClass* mrb_class_get(mrb_state *mrb, const char *name) { @@ -379,11 +406,16 @@ mrb_class_get(mrb_state *mrb, const char *name) } MRB_API struct RClass* -mrb_exc_get(mrb_state *mrb, const char *name) +mrb_class_get_id(mrb_state *mrb, mrb_sym name) +{ + return mrb_class_get_under_id(mrb, mrb->object_class, name); +} + +MRB_API struct RClass* +mrb_exc_get_id(mrb_state *mrb, mrb_sym name) { struct RClass *exc, *e; - mrb_value c = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), - mrb_intern_cstr(mrb, name)); + mrb_value c = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), name); if (!mrb_class_p(c)) { mrb_raise(mrb, mrb->eException_class, "exception corrupted"); @@ -404,12 +436,24 @@ mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name) return module_from_sym(mrb, outer, mrb_intern_cstr(mrb, name)); } +MRB_API struct RClass* +mrb_module_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name) +{ + return module_from_sym(mrb, outer, name); +} + MRB_API struct RClass* mrb_module_get(mrb_state *mrb, const char *name) { return mrb_module_get_under(mrb, mrb->object_class, name); } +MRB_API struct RClass* +mrb_module_get_id(mrb_state *mrb, mrb_sym name) +{ + return mrb_module_get_under_id(mrb, mrb->object_class, name); +} + /*! * Defines a class under the namespace of \a outer. * \param outer a class which contains the new class. @@ -427,9 +471,8 @@ mrb_module_get(mrb_state *mrb, const char *name) * \a super, the function just returns the defined class. */ MRB_API struct RClass* -mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super) +mrb_define_class_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name, struct RClass *super) { - mrb_sym id = mrb_intern_cstr(mrb, name); struct RClass * c; #if 0 @@ -437,11 +480,17 @@ mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, s mrb_warn(mrb, "no super class for '%C::%n', Object assumed", outer, id); } #endif - c = define_class(mrb, id, super, outer); - setup_class(mrb, outer, c, id); + c = define_class(mrb, name, super, outer); + setup_class(mrb, outer, c, name); return c; } +MRB_API struct RClass* +mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super) +{ + return mrb_define_class_under_id(mrb, outer, mrb_intern_cstr(mrb, name), super); +} + MRB_API void mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_method_t m) { @@ -1896,6 +1945,12 @@ mrb_define_alias(mrb_state *mrb, struct RClass *klass, const char *name1, const mrb_alias_method(mrb, klass, mrb_intern_cstr(mrb, name1), mrb_intern_cstr(mrb, name2)); } +MRB_API void +mrb_define_alias_id(mrb_state *mrb, struct RClass *klass, mrb_sym a, mrb_sym b) +{ + mrb_alias_method(mrb, klass, a, b); +} + /* * call-seq: * mod.to_s -> string -- cgit v1.2.3