Index: EcnlProtoTool/trunk/mruby-2.1.1/src/backtrace.c
===================================================================
--- EcnlProtoTool/trunk/mruby-1.3.0/src/backtrace.c	(revision 331)
+++ EcnlProtoTool/trunk/mruby-2.1.1/src/backtrace.c	(revision 439)
@@ -17,26 +17,29 @@
 
 struct backtrace_location {
-  int lineno;
+  int32_t lineno;
+  mrb_sym method_id;
   const char *filename;
-  mrb_sym method_id;
 };
 
-typedef void (*each_backtrace_func)(mrb_state*, int i, struct backtrace_location*, void*);
+typedef void (*each_backtrace_func)(mrb_state*, const struct backtrace_location*, void*);
 
 static const mrb_data_type bt_type = { "Backtrace", mrb_free };
 
-static void
-each_backtrace(mrb_state *mrb, mrb_int ciidx, mrb_code *pc0, each_backtrace_func func, void *data)
-{
-  int i, j;
+mrb_value mrb_exc_inspect(mrb_state *mrb, mrb_value exc);
+mrb_value mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace);
+
+static void
+each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, const mrb_code *pc0, each_backtrace_func func, void *data)
+{
+  ptrdiff_t i;
 
   if (ciidx >= mrb->c->ciend - mrb->c->cibase)
     ciidx = 10; /* ciidx is broken... */
 
-  for (i=ciidx, j=0; i >= 0; i--,j++) {
+  for (i=ciidx; i >= 0; i--) {
     struct backtrace_location loc;
     mrb_callinfo *ci;
     mrb_irep *irep;
-    mrb_code *pc;
+    const mrb_code *pc;
 
     ci = &mrb->c->cibase[i];
@@ -52,14 +55,15 @@
     }
     else if (i+1 <= ciidx) {
-      pc = mrb->c->cibase[i+1].pc - 1;
+      if (!mrb->c->cibase[i + 1].pc) continue;
+      pc = &mrb->c->cibase[i+1].pc[-1];
     }
     else {
       pc = pc0;
     }
-    loc.filename = mrb_debug_get_filename(irep, (uint32_t)(pc - irep->iseq));
-    loc.lineno = mrb_debug_get_line(irep, (uint32_t)(pc - irep->iseq));
-
+
+    loc.lineno = mrb_debug_get_line(mrb, irep, pc - irep->iseq);
     if (loc.lineno == -1) continue;
 
+    loc.filename = mrb_debug_get_filename(mrb, irep, pc - irep->iseq);
     if (!loc.filename) {
       loc.filename = "(unknown)";
@@ -67,5 +71,5 @@
 
     loc.method_id = ci->mid;
-    func(mrb, j, &loc, data);
+    func(mrb, &loc, data);
   }
 }
@@ -74,50 +78,25 @@
 
 static void
-print_backtrace(mrb_state *mrb, mrb_value backtrace)
-{
-  int i, n;
+print_backtrace(mrb_state *mrb, struct RObject *exc, mrb_value backtrace)
+{
+  mrb_int i;
+  mrb_int n = RARRAY_LEN(backtrace);
+  mrb_value *loc, mesg;
   FILE *stream = stderr;
 
-  if (!mrb_array_p(backtrace)) return;
-  fprintf(stream, "trace:\n");
-
-  n = RARRAY_LEN(backtrace);
-  for (i=0; n--; i++) {
-    mrb_value entry = RARRAY_PTR(backtrace)[n];
-
-    if (mrb_string_p(entry)) {
-      fprintf(stream, "\t[%d] %.*s\n", i, (int)RSTRING_LEN(entry), RSTRING_PTR(entry));
-    }
-  }
-}
-
-static void
-print_packed_backtrace(mrb_state *mrb, mrb_value packed)
-{
-  FILE *stream = stderr;
-  struct backtrace_location *bt;
-  int n, i;
-
-  bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, packed, &bt_type);
-  if (bt == NULL) {
-    mrb_raise(mrb, E_RUNTIME_ERROR, "broken backtrace");
-  }
-  n = (mrb_int)RDATA(packed)->flags;
-
-  fprintf(stream, "trace:\n");
-  for (i = 0; n--; i++) {
-    int ai = mrb_gc_arena_save(mrb);
-    struct backtrace_location *entry = &bt[n];
-    if (entry->filename == NULL) continue;
-    fprintf(stream, "\t[%d] %s:%d", (int)i, entry->filename, entry->lineno);
-    if (entry->method_id != 0) {
-      const char *method_name;
-
-      method_name = mrb_sym2name(mrb, entry->method_id);
-      fprintf(stream, ":in %s", method_name);
-      mrb_gc_arena_restore(mrb, ai);
-    }
-    fprintf(stream, "\n");
-  }
+  if (n != 0) {
+    fprintf(stream, "trace (most recent call last):\n");
+    for (i=n-1,loc=&RARRAY_PTR(backtrace)[i]; i>0; i--,loc--) {
+      if (mrb_string_p(*loc)) {
+        fprintf(stream, "\t[%d] %.*s\n",
+                (int)i, (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
+      }
+    }
+    if (mrb_string_p(*loc)) {
+      fprintf(stream, "%.*s: ", (int)RSTRING_LEN(*loc), RSTRING_PTR(*loc));
+    }
+  }
+  mesg = mrb_exc_inspect(mrb, mrb_obj_value(exc));
+  fprintf(stream, "%.*s\n", (int)RSTRING_LEN(mesg), RSTRING_PTR(mesg));
 }
 
@@ -138,10 +117,6 @@
   backtrace = mrb_obj_iv_get(mrb, mrb->exc, mrb_intern_lit(mrb, "backtrace"));
   if (mrb_nil_p(backtrace)) return;
-  if (mrb_array_p(backtrace)) {
-    print_backtrace(mrb, backtrace);
-  }
-  else {
-    print_packed_backtrace(mrb, backtrace);
-  }
+  if (!mrb_array_p(backtrace)) backtrace = mrb_unpack_backtrace(mrb, backtrace);
+  print_backtrace(mrb, mrb->exc, backtrace);
 }
 #else
@@ -155,12 +130,23 @@
 
 static void
+count_backtrace_i(mrb_state *mrb,
+                 const struct backtrace_location *loc,
+                 void *data)
+{
+  int *lenp = (int*)data;
+
+  (*lenp)++;
+}
+
+static void
 pack_backtrace_i(mrb_state *mrb,
-                 int i,
-                 struct backtrace_location *loc,
+                 const struct backtrace_location *loc,
                  void *data)
 {
-  struct backtrace_location *entry = (struct backtrace_location*)data;
-
-  entry[i] = *loc;
+  struct backtrace_location **pptr = (struct backtrace_location**)data;
+  struct backtrace_location *ptr = *pptr;
+
+  *ptr = *loc;
+  *pptr = ptr+1;
 }
 
@@ -170,12 +156,14 @@
   struct RData *backtrace;
   ptrdiff_t ciidx = mrb->c->ci - mrb->c->cibase;
-  mrb_int len = (ciidx+1)*sizeof(struct backtrace_location);
+  int len = 0;
+  int size;
   void *ptr;
 
-  ptr = mrb_malloc(mrb, len);
-  memset(ptr, 0, len);
+  each_backtrace(mrb, ciidx, mrb->c->ci->pc, count_backtrace_i, &len);
+  size = len * sizeof(struct backtrace_location);
+  ptr = mrb_malloc(mrb, size);
   backtrace = mrb_data_object_alloc(mrb, NULL, ptr, &bt_type);
-  backtrace->flags = (unsigned int)ciidx+1;
-  each_backtrace(mrb, ciidx, mrb->c->ci->pc, pack_backtrace_i, ptr);
+  backtrace->flags = (uint32_t)len;
+  each_backtrace(mrb, ciidx, mrb->c->ci->pc, pack_backtrace_i, &ptr);
   return mrb_obj_value(backtrace);
 }
@@ -184,9 +172,12 @@
 mrb_keep_backtrace(mrb_state *mrb, mrb_value exc)
 {
+  mrb_sym sym = mrb_intern_lit(mrb, "backtrace");
   mrb_value backtrace;
-  int ai = mrb_gc_arena_save(mrb);
-
+  int ai;
+
+  if (mrb_iv_defined(mrb, exc, sym)) return;
+  ai = mrb_gc_arena_save(mrb);
   backtrace = packed_backtrace(mrb);
-  mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "backtrace"), backtrace);
+  mrb_iv_set(mrb, exc, sym, backtrace);
   mrb_gc_arena_restore(mrb, ai);
 }
@@ -195,27 +186,26 @@
 mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace)
 {
-  struct backtrace_location *bt;
+  const struct backtrace_location *bt;
   mrb_int n, i;
-
-  if (mrb_nil_p(backtrace)) return mrb_ary_new_capa(mrb, 0);
+  int ai;
+
+  if (mrb_nil_p(backtrace)) {
+  empty_backtrace:
+    return mrb_ary_new_capa(mrb, 0);
+  }
   if (mrb_array_p(backtrace)) return backtrace;
   bt = (struct backtrace_location*)mrb_data_check_get_ptr(mrb, backtrace, &bt_type);
-  if (bt == NULL) {
-    mrb_raise(mrb, E_RUNTIME_ERROR, "broken backtrace");
-  }
+  if (bt == NULL) goto empty_backtrace;
   n = (mrb_int)RDATA(backtrace)->flags;
   backtrace = mrb_ary_new_capa(mrb, n);
+  ai = mrb_gc_arena_save(mrb);
   for (i = 0; i < n; i++) {
-    int ai = mrb_gc_arena_save(mrb);
-    struct backtrace_location *entry = &bt[i];
+    const struct backtrace_location *entry = &bt[i];
     mrb_value btline;
 
-    if (entry->filename == NULL) continue;
-    btline = mrb_format(mrb, "%S:%S",
-                              mrb_str_new_cstr(mrb, entry->filename),
-                              mrb_fixnum_value(entry->lineno));
+    btline = mrb_format(mrb, "%s:%d", entry->filename, (int)entry->lineno);
     if (entry->method_id != 0) {
       mrb_str_cat_lit(mrb, btline, ":in ");
-      mrb_str_cat_cstr(mrb, btline, mrb_sym2name(mrb, entry->method_id));
+      mrb_str_cat_cstr(mrb, btline, mrb_sym_name(mrb, entry->method_id));
     }
     mrb_ary_push(mrb, backtrace, btline);
