From 7bb71a7d5826accc50a521e770b5b7afe283ad90 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Tue, 26 Jun 2012 16:02:38 +0900 Subject: Remove magic numbers in mrb_funcall() --- src/class.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/class.c') diff --git a/src/class.c b/src/class.c index 7f53b7a2b..bf63198dd 100644 --- a/src/class.c +++ b/src/class.c @@ -7,6 +7,7 @@ #include "mruby.h" #include #include +#include #include "mruby/class.h" #include "mruby/proc.h" #include "mruby/string.h" @@ -863,26 +864,31 @@ mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid) } mrb_value -mrb_funcall(mrb_state *mrb, mrb_value self, const char *name, int argc,...) +mrb_funcall(mrb_state *mrb, mrb_value self, const char *name, int argc, ...) { - mrb_value args[16]; + mrb_value *args; + mrb_value result; va_list ap; int i; - if (argc == 0) { - for (i=0; i<5; i++) { - args[i] = mrb_nil_value(); - } - } - else { + if (argc != 0) { + args = mrb_malloc(mrb, sizeof(mrb_value) * argc); + assert(args != 0); + va_start(ap, argc); - // assert(argc < 16); - for (i=0; i Date: Wed, 18 Jul 2012 15:47:57 +0900 Subject: MRB_FUNCALL_ARGC_MAX support. (refs comments in #324). --- include/mrbconf.h | 4 ++++ src/class.c | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'src/class.c') diff --git a/include/mrbconf.h b/include/mrbconf.h index 5d307e66f..757192d89 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -23,6 +23,10 @@ #undef HAVE_UNISTD_H /* WINDOWS */ #define HAVE_UNISTD_H /* LINUX */ + +#define MRB_FUNCALL_ARGC_MAX 16U /* Allocate arrays using auto variable. */ +//#undef MRB_FUNCALL_ARGC_MAX /* Allocate arrays using mrb_malloc if undefned. */ + /* end of configuration */ #ifdef MRB_USE_FLOAT diff --git a/src/class.c b/src/class.c index bf63198dd..762fa1591 100644 --- a/src/class.c +++ b/src/class.c @@ -7,7 +7,6 @@ #include "mruby.h" #include #include -#include #include "mruby/class.h" #include "mruby/proc.h" #include "mruby/string.h" @@ -866,27 +865,38 @@ mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid) mrb_value mrb_funcall(mrb_state *mrb, mrb_value self, const char *name, int argc, ...) { - mrb_value *args; +#if defined(MRB_FUNCALL_ARGC_MAX) + mrb_value args[MRB_FUNCALL_ARGC_MAX]; +#else + mrb_value *args = NULL; +#endif mrb_value result; va_list ap; int i; if (argc != 0) { +#if !defined(MRB_FUNCALL_ARGC_MAX) args = mrb_malloc(mrb, sizeof(mrb_value) * argc); - assert(args != 0); +#else + if (argc > MRB_FUNCALL_ARGC_MAX) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "Too long arguments. (limit=%d)\n", MRB_FUNCALL_ARGC_MAX); + } +#endif va_start(ap, argc); for (i = 0; i < argc; i++) { args[i] = va_arg(ap, mrb_value); } va_end(ap); - } else { - args = NULL; } + result = mrb_funcall_argv(mrb, self, name, argc, args); + +#if !defined(MRB_FUNCALL_ARGC_MAX) if (args != NULL) { mrb_free(mrb, args); } +#endif return result; } -- cgit v1.2.3