diff options
| -rw-r--r-- | include/mrbconf.h | 9 | ||||
| -rw-r--r-- | src/class.c | 8 | ||||
| -rw-r--r-- | src/codegen.c | 4 | ||||
| -rw-r--r-- | src/dump.c | 8 | ||||
| -rw-r--r-- | src/load.c | 4 | ||||
| -rw-r--r-- | src/numeric.c | 10 | ||||
| -rw-r--r-- | src/variable.c | 1 | ||||
| -rw-r--r-- | tools/mrbc/mrbc.c | 51 |
8 files changed, 56 insertions, 39 deletions
diff --git a/include/mrbconf.h b/include/mrbconf.h index 76c1b37f9..841ef1823 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -55,19 +55,26 @@ #ifdef MRB_USE_FLOAT typedef float mrb_float; +#define mrb_float_to_str(buf, i) sprintf((buf), "%.7e", (i)) +#define str_to_mrb_float(buf) (mrb_float)strtof((buf),NULL) #else typedef double mrb_float; +#define mrb_float_to_str(buf, i) sprintf((buf), "%.16e", (i)) +#define str_to_mrb_float(buf) (mrb_float)strtod((buf),NULL) #endif -#define readfloat(p) (mrb_float)strtod((p),NULL) #ifdef MRB_NAN_BOXING typedef int32_t mrb_int; #define MRB_INT_MIN INT32_MIN #define MRB_INT_MAX INT32_MAX +#define mrb_int_to_str(buf, i) sprintf((buf), "%d", (i)) +#define str_to_mrb_int(buf) (mrb_int)strtol((buf), NULL, 10); #else typedef int mrb_int; #define MRB_INT_MIN INT_MIN #define MRB_INT_MAX INT_MAX +#define mrb_int_to_str(buf, i) sprintf((buf), "%d", (i)) +#define str_to_mrb_int(buf) (mrb_int)strtol((buf), NULL, 10); #endif typedef short mrb_sym; diff --git a/src/class.c b/src/class.c index c4058ad67..3e54afede 100644 --- a/src/class.c +++ b/src/class.c @@ -852,7 +852,9 @@ mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid) m = mrb_method_search_vm(mrb, &c, mid); if (!m) { - mrb_raisef(mrb, E_NOMETHOD_ERROR, "no method named %s\n", mrb_sym2name(mrb, mid)); + mrb_raisef(mrb, E_NAME_ERROR, "undefined method '%s' for class %s", + mrb_sym2name(mrb, mid), + RSTRING_PTR(mrb_funcall(mrb, mrb_obj_value(c), "inspect", 0))); } return m; } @@ -1008,7 +1010,9 @@ mrb_bob_missing(mrb_state *mrb, mrb_value mod) if (!SYMBOL_P(name)) { mrb_raise(mrb, E_TYPE_ERROR, "name should be a symbol"); } - mrb_raisef(mrb, E_NOMETHOD_ERROR, "no method named %s", mrb_sym2name(mrb, mrb_symbol(name))); + mrb_raisef(mrb, E_NOMETHOD_ERROR, "undefined method '%s' for %s", + mrb_sym2name(mrb, mrb_symbol(name)), + RSTRING_PTR(mrb_funcall(mrb, mod, "inspect", 0))); /* not reached */ return mrb_nil_value(); } diff --git a/src/codegen.c b/src/codegen.c index 7c3182599..ebc9b32de 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1741,7 +1741,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_FLOAT: if (val) { char *p = (char*)tree; - mrb_float f = readfloat(p); + mrb_float f = str_to_mrb_float(p); int off = new_lit(s, mrb_float_value(f)); genop(s, MKOP_ABx(OP_LOADL, cursp(), off)); @@ -1757,7 +1757,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_FLOAT: { char *p = (char*)tree; - mrb_float f = readfloat(p); + mrb_float f = str_to_mrb_float(p); int off = new_lit(s, mrb_float_value(-f)); genop(s, MKOP_ABx(OP_LOADL, cursp(), off)); diff --git a/src/dump.c b/src/dump.c index 15e11153e..5100014d9 100644 --- a/src/dump.c +++ b/src/dump.c @@ -226,11 +226,11 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = sprintf( buf, "%d", mrb_fixnum(irep->pool[pool_no])); + len = mrb_int_to_str( buf, mrb_fixnum(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_FLOAT: - len = sprintf( buf, "%.16e", mrb_float(irep->pool[pool_no])); + len = mrb_float_to_str( buf, mrb_float(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_STRING: @@ -346,11 +346,11 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = sprintf(char_buf, "%d", mrb_fixnum(irep->pool[pool_no])); + len = mrb_int_to_str(char_buf, mrb_fixnum(irep->pool[pool_no])); break; case MRB_TT_FLOAT: - len = sprintf(char_buf, "%.16e", mrb_float(irep->pool[pool_no])); + len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: diff --git a/src/load.c b/src/load.c index 751c2619b..4d26b02ca 100644 --- a/src/load.c +++ b/src/load.c @@ -405,12 +405,12 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32 switch (tt) { //pool data case MRB_TT_FIXNUM: - fix_num = strtol(buf, NULL, 10); + fix_num = str_to_mrb_int(buf); irep->pool[i] = mrb_fixnum_value(fix_num); break; case MRB_TT_FLOAT: - f = readfloat(buf); + f = str_to_mrb_float(buf); irep->pool[i] = mrb_float_value(f); break; diff --git a/src/numeric.c b/src/numeric.c index 102e52827..c1491ac51 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -246,8 +246,8 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float * mrb_float div, mod; if (y == 0.0) { - *divp = strtod("inf", NULL); - *modp = strtod("nan", NULL); + *divp = str_to_mrb_float("inf"); + *modp = str_to_mrb_float("nan"); return; } mod = fmod(x, y); @@ -778,7 +778,7 @@ fix_mod(mrb_state *mrb, mrb_value x) mrb_int mod; if (mrb_fixnum(y) == 0) { - return mrb_float_value(strtod("nan", NULL)); + return mrb_float_value(str_to_mrb_float("nan")); } fixdivmod(mrb, a, mrb_fixnum(y), 0, &mod); return mrb_fixnum_value(mod); @@ -807,8 +807,8 @@ fix_divmod(mrb_state *mrb, mrb_value x) mrb_int div, mod; if (mrb_fixnum(y) == 0) { - return mrb_assoc_new(mrb, mrb_float_value(strtod("inf", NULL)), - mrb_float_value(strtod("nan", NULL))); + return mrb_assoc_new(mrb, mrb_float_value(str_to_mrb_float("inf")), + mrb_float_value(str_to_mrb_float("nan"))); } fixdivmod(mrb, mrb_fixnum(x), mrb_fixnum(y), &div, &mod); return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod)); diff --git a/src/variable.c b/src/variable.c index e3e4c4fb5..7f9274ee4 100644 --- a/src/variable.c +++ b/src/variable.c @@ -420,6 +420,7 @@ obj_iv_p(mrb_value obj) case MRB_TT_OBJECT: case MRB_TT_CLASS: case MRB_TT_MODULE: + case MRB_TT_SCLASS: case MRB_TT_HASH: case MRB_TT_DATA: return TRUE; diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 3c6352f80..2ecb00623 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -70,6 +70,7 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) char *infile = NULL; char *outfile = NULL; char **origargv = argv; + int result = 0; static const struct _args args_zero = { 0 }; *args = args_zero; @@ -93,7 +94,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) args->initname = (*argv) + 2; if (*args->initname == '\0') { printf("%s: Function name is not specified.\n", *origargv); - return -2; + result = -2; + goto exit; } args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE; break; @@ -117,8 +119,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) mrb_show_copyright(mrb); exit(0); } - else return -3; - return 0; + result = -3; + goto exit; default: break; } @@ -127,33 +129,36 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) args->filename = infile = *argv; if ((args->rfp = fopen(infile, "r")) == NULL) { printf("%s: Cannot open program file. (%s)\n", *origargv, infile); - return 0; + goto exit; } } } - if (infile == NULL) - return -4; - if (args->check_syntax) - return 0; - - if (outfile == NULL) { - if (strcmp("-", infile) == 0) { - outfile = infile; + if (infile == NULL) { + result = -4; + goto exit; + } + if (!args->check_syntax) { + if (outfile == NULL) { + if (strcmp("-", infile) == 0) { + outfile = infile; + } + else { + outfile = get_outfilename(infile, args->ext); + } } - else { - outfile = get_outfilename(infile, args->ext); + if (strcmp("-", outfile) == 0) { + args->wfp = stdout; + } + else if ((args->wfp = fopen(outfile, "wb")) == NULL) { + printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); + result = -1; + goto exit; } } - if (strcmp("-", outfile) == 0) { - args->wfp = stdout; - } - else if ((args->wfp = fopen(outfile, "wb")) == NULL) { - printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); - return 0; - } - - return 0; + exit: + if (outfile && infile != outfile) free(outfile); + return result; } static void |
