diff options
| -rw-r--r-- | Rakefile | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 27 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 2 | ||||
| -rw-r--r-- | src/backtrace.c | 82 | ||||
| -rw-r--r-- | src/class.c | 8 | ||||
| -rw-r--r-- | src/error.c | 3 | ||||
| -rw-r--r-- | src/string.c | 1 | ||||
| -rw-r--r-- | tasks/mrbgem_spec.rake | 8 |
8 files changed, 106 insertions, 29 deletions
@@ -41,7 +41,7 @@ depfiles = MRuby.targets['host'].bins.map do |bin| FileUtils.rm_f t.name, { :verbose => $verbose } FileUtils.cp t.prerequisites.first, t.name, { :verbose => $verbose } end - + install_path end @@ -53,7 +53,7 @@ MRuby.each_target do |target| gem.bins.each do |bin| exec = exefile("#{build_dir}/bin/#{bin}") - objs = Dir.glob("#{current_dir}/tools/#{bin}/*.c").map { |f| objfile(f.pathmap("#{current_build_dir}/tools/#{bin}/%n")) } + objs = Dir.glob("#{current_dir}/tools/#{bin}/*.{c,cpp,cxx}").map { |f| objfile(f.pathmap("#{current_build_dir}/tools/#{bin}/%n")) } file exec => objs + [libfile("#{build_dir}/lib/libmruby")] do |t| gem_flags = gems.map { |g| g.linker.flags } diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 4bb9b9621..6718e734a 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -107,6 +107,7 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self) * # returns true if one of the prefixes matches. * "hello".start_with?("heaven", "hell") #=> true * "hello".start_with?("heaven", "paradise") #=> false + * "h".start_with?("heaven", "hell") #=> false */ static mrb_value mrb_str_start_with(mrb_state *mrb, mrb_value self) @@ -116,16 +117,17 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { - size_t len_l, len_r, len_cmp; + size_t len_l, len_r; int ai = mrb_gc_arena_save(mrb); sub = mrb_string_type(mrb, argv[i]); mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); len_r = RSTRING_LEN(sub); - len_cmp = (len_l > len_r) ? len_r : len_l; - if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_cmp) == 0) { - return mrb_true_value(); - } + if (len_l >= len_r) { + if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) { + return mrb_true_value(); + } + } } return mrb_false_value(); } @@ -144,18 +146,19 @@ mrb_str_end_with(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { - size_t len_l, len_r, len_cmp; + size_t len_l, len_r; int ai = mrb_gc_arena_save(mrb); sub = mrb_string_type(mrb, argv[i]); mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); len_r = RSTRING_LEN(sub); - len_cmp = (len_l > len_r) ? len_r : len_l; - if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp), - RSTRING_PTR(sub) + (len_r - len_cmp), - len_cmp) == 0) { - return mrb_true_value(); - } + if (len_l >= len_r) { + if (memcmp(RSTRING_PTR(self) + (len_l - len_r), + RSTRING_PTR(sub), + len_r) == 0) { + return mrb_true_value(); + } + } } return mrb_false_value(); } diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 496730a0e..3ab959437 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -104,11 +104,13 @@ end assert('String#start_with?') do assert_true "hello".start_with?("heaven", "hell") assert_true !"hello".start_with?("heaven", "paradise") + assert_true !"h".start_with?("heaven", "hell") assert_raise TypeError do "hello".start_with?(true) end end assert('String#end_with?') do assert_true "string".end_with?("ing", "mng") assert_true !"string".end_with?("str", "tri") + assert_true !"ng".end_with?("ing", "mng") assert_raise TypeError do "hello".end_with?(true) end end diff --git a/src/backtrace.c b/src/backtrace.c index e05ad4326..77906e843 100644 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -7,18 +7,58 @@ #include "mruby.h" #include "mruby/variable.h" #include "mruby/proc.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include <stdarg.h> + +typedef void (*output_stream_func)(mrb_state*, void*, int, const char*, ...); -void -mrb_print_backtrace(mrb_state *mrb) -{ #ifdef ENABLE_STDIO +static void +print_backtrace_i(mrb_state *mrb, void *stream, int level, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vfprintf((FILE*)stream, format, ap); + va_end(ap); +} +#endif + +#define MIN_BUFSIZE 127 + +static void +get_backtrace_i(mrb_state *mrb, void *stream, int level, const char *format, ...) +{ + va_list ap; + mrb_value ary, str; + int len, ai; + + if (level > 0) { + return; + } + + ai = mrb_gc_arena_save(mrb); + ary = mrb_obj_value((struct RArray*)stream); + va_start(ap, format); + len = vsnprintf(NULL, 0, format, ap); + str = mrb_str_new(mrb, 0, len); + vsnprintf(RSTRING_PTR(str), len, format, ap); + mrb_ary_push(mrb, ary, str); + va_end(ap); + mrb_gc_arena_restore(mrb, ai); +} + +static void +mrb_output_backtrace(mrb_state *mrb, struct RObject *exc, output_stream_func func, void *stream) +{ mrb_callinfo *ci; mrb_int ciidx; const char *filename, *method, *sep; int i, line; - fputs("trace:\n", stderr); - ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern2(mrb, "ciidx", 5))); + func(mrb, stream, 1, "trace:\n"); + ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, exc, mrb_intern2(mrb, "ciidx", 5))); if (ciidx >= mrb->c->ciend - mrb->c->cibase) ciidx = 10; /* ciidx is broken... */ @@ -41,7 +81,7 @@ mrb_print_backtrace(mrb_state *mrb) pc = mrb->c->cibase[i+1].pc; } else { - pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern2(mrb, "lastpc", 6))); + pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, exc, mrb_intern2(mrb, "lastpc", 6))); } if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) { line = irep->lines[pc - irep->iseq - 1]; @@ -59,15 +99,39 @@ mrb_print_backtrace(mrb_state *mrb) const char *cn = mrb_class_name(mrb, ci->proc->target_class); if (cn) { - fprintf(stderr, "\t[%d] %s:%d:in %s%s%s\n", i, filename, line, cn, sep, method); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d:in %s%s%s", filename, line, cn, sep, method); + func(mrb, stream, 1, "\n"); } else { - fprintf(stderr, "\t[%d] %s:%d:in %s\n", i, filename, line, method); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d:in %s", filename, line, method); + func(mrb, stream, 1, "\n"); } } else { - fprintf(stderr, "\t[%d] %s:%d\n", i, filename, line); + func(mrb, stream, 1, "\t[%d] ", i); + func(mrb, stream, 0, "%s:%d", filename, line); + func(mrb, stream, 1, "\n"); } } +} + +void +mrb_print_backtrace(mrb_state *mrb) +{ +#ifdef ENABLE_STDIO + mrb_output_backtrace(mrb, mrb->exc, print_backtrace_i, (void*)stderr); #endif } + +mrb_value +mrb_get_backtrace(mrb_state *mrb, mrb_value self) +{ + mrb_value ary; + + ary = mrb_ary_new(mrb); + mrb_output_backtrace(mrb, mrb_obj_ptr(self), get_backtrace_i, (void*)mrb_ary_ptr(ary)); + + return ary; +} diff --git a/src/class.c b/src/class.c index b5c561d06..1870b94ba 100644 --- a/src/class.c +++ b/src/class.c @@ -489,14 +489,19 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) mrb_value ss; struct RString *s; char **ps; + mrb_int len; ps = va_arg(ap, char**); if (i < argc) { ss = to_str(mrb, *sp++); s = mrb_str_ptr(ss); - if ((mrb_int)strlen(s->ptr) < s->len) { + len = (mrb_int)strlen(s->ptr); + if (len < s->len) { mrb_raise(mrb, E_ARGUMENT_ERROR, "String contains NUL"); } + else if (len > s->len) { + mrb_str_modify(mrb, s); + } *ps = s->ptr; i++; } @@ -1886,7 +1891,6 @@ mrb_init_class(mrb_state *mrb) mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, MRB_ARGS_ANY()); /* 15.3.1.3.30 */ mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, MRB_ARGS_ANY()); - mrb_define_method(mrb, cls, "alloc", mrb_instance_alloc, MRB_ARGS_NONE()); mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, MRB_ARGS_NONE()); /* 15.2.3.3.4 */ mrb_define_method(mrb, cls, "new", mrb_instance_new, MRB_ARGS_ANY()); /* 15.2.3.3.3 */ mrb_define_method(mrb, cls, "inherited", mrb_bob_init, MRB_ARGS_REQ(1)); diff --git a/src/error.c b/src/error.c index 98b49ad82..03f587a38 100644 --- a/src/error.c +++ b/src/error.c @@ -435,6 +435,8 @@ mrb_sys_fail(mrb_state *mrb, const char *mesg) } } +mrb_value mrb_get_backtrace(mrb_state*, mrb_value); + void mrb_init_exception(mrb_state *mrb) { @@ -448,6 +450,7 @@ mrb_init_exception(mrb_state *mrb) mrb_define_method(mrb, e, "to_s", exc_to_s, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "message", exc_message, MRB_ARGS_NONE()); mrb_define_method(mrb, e, "inspect", exc_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, e, "backtrace", mrb_get_backtrace, MRB_ARGS_NONE()); mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */ mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */ diff --git a/src/string.c b/src/string.c index 088f8efc4..fa581f025 100644 --- a/src/string.c +++ b/src/string.c @@ -62,6 +62,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) if (shared->refcnt == 1 && s->ptr == shared->ptr) { s->ptr = shared->ptr; s->aux.capa = shared->len; + s->ptr[s->len] = '\0'; mrb_free(mrb, shared); } else { diff --git a/tasks/mrbgem_spec.rake b/tasks/mrbgem_spec.rake index df55e749f..94f24af26 100644 --- a/tasks/mrbgem_spec.rake +++ b/tasks/mrbgem_spec.rake @@ -6,7 +6,7 @@ module MRuby class << self attr_accessor :current end - LinkerConfig = Struct.new(:libraries, :library_paths, :flags, :flags_before_libraries, :flags_after_libraries) + LinkerConfig = Struct.new(:libraries, :library_paths, :flags, :flags_before_libraries, :flags_after_libraries) class Specification include Rake::DSL @@ -53,13 +53,13 @@ module MRuby @linker = LinkerConfig.new([], [], [], []) @rbfiles = Dir.glob("#{dir}/mrblib/*.rb").sort - @objs = Dir.glob("#{dir}/src/*.{c,cpp,m,asm,S}").map do |f| + @objs = Dir.glob("#{dir}/src/*.{c,cpp,cxx,m,asm,S}").map do |f| objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X")) end @objs << objfile("#{build_dir}/gem_init") @test_rbfiles = Dir.glob("#{dir}/test/*.rb") - @test_objs = Dir.glob("#{dir}/test/*.{c,cpp,m,asm,S}").map do |f| + @test_objs = Dir.glob("#{dir}/test/*.{c,cpp,cxx,m,asm,S}").map do |f| objfile(f.relative_path_from(dir).to_s.pathmap("#{build_dir}/%X")) end @test_preload = 'test/assert.rb' @@ -211,7 +211,7 @@ module MRuby end # ~> compare algorithm - # + # # Example: # ~> 2.2 means >= 2.2.0 and < 3.0.0 # ~> 2.2.0 means >= 2.2.0 and < 2.3.0 |
