Index: EcnlProtoTool/trunk/mruby-2.1.1/src/dump.c
===================================================================
--- EcnlProtoTool/trunk/mruby-1.3.0/src/dump.c	(revision 331)
+++ EcnlProtoTool/trunk/mruby-2.1.1/src/dump.c	(revision 439)
@@ -7,4 +7,5 @@
 #include <string.h>
 #include <limits.h>
+#include <math.h>
 #include <mruby/dump.h>
 #include <mruby/string.h>
@@ -16,8 +17,10 @@
 #define FLAG_BYTEORDER_NONATIVE 0
 
+#ifndef MRB_WITHOUT_FLOAT
 #ifdef MRB_USE_FLOAT
-#define MRB_FLOAT_FMT "%.8e"
+#define MRB_FLOAT_FMT "%.9g"
 #else
-#define MRB_FLOAT_FMT "%.16e"
+#define MRB_FLOAT_FMT "%.17g"
+#endif
 #endif
 
@@ -80,38 +83,31 @@
 {
   uint8_t *cur = buf;
-  uint32_t iseq_no;
 
   cur += uint32_to_bin(irep->ilen, cur); /* number of opcode */
   cur += write_padding(cur);
-  switch (flags & DUMP_ENDIAN_NAT) {
-  case DUMP_ENDIAN_BIG:
-    if (bigendian_p()) goto native;
-    for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
-      cur += uint32_to_bin(irep->iseq[iseq_no], cur); /* opcode */
-    }
-    break;
-  case DUMP_ENDIAN_LIL:
-    if (!bigendian_p()) goto native;
-    for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
-      cur += uint32l_to_bin(irep->iseq[iseq_no], cur); /* opcode */
-    }
-    break;
-
-  native:
-  case DUMP_ENDIAN_NAT:
-    memcpy(cur, irep->iseq, irep->ilen * sizeof(mrb_code));
-    cur += irep->ilen * sizeof(mrb_code);
-    break;
-  }
+  memcpy(cur, irep->iseq, irep->ilen * sizeof(mrb_code));
+  cur += irep->ilen * sizeof(mrb_code);
 
   return cur - buf;
 }
 
+#ifndef MRB_WITHOUT_FLOAT
+static mrb_value
+float_to_str(mrb_state *mrb, mrb_value flt)
+{
+  mrb_float f = mrb_float(flt);
+
+  if (isinf(f)) {
+    return f < 0 ? mrb_str_new_lit(mrb, "I") : mrb_str_new_lit(mrb, "i");
+  }
+  return  mrb_float_to_str(mrb, flt, MRB_FLOAT_FMT);
+}
+#endif
 
 static size_t
 get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
 {
+  int pool_no;
   size_t size = 0;
-  size_t pool_no;
   mrb_value str;
 
@@ -132,6 +128,7 @@
       break;
 
+#ifndef MRB_WITHOUT_FLOAT
     case MRB_TT_FLOAT:
-      str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT);
+      str = float_to_str(mrb, irep->pool[pool_no]);
       {
         mrb_int len = RSTRING_LEN(str);
@@ -140,4 +137,5 @@
       }
       break;
+#endif
 
     case MRB_TT_STRING:
@@ -161,5 +159,5 @@
 write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
 {
-  size_t pool_no;
+  int pool_no;
   uint8_t *cur = buf;
   uint16_t len;
@@ -178,8 +176,10 @@
       break;
 
+#ifndef MRB_WITHOUT_FLOAT
     case MRB_TT_FLOAT:
       cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */
-      str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT);
+      str = float_to_str(mrb, irep->pool[pool_no]);
       break;
+#endif
 
     case MRB_TT_STRING:
@@ -214,5 +214,5 @@
 {
   size_t size = 0;
-  uint32_t sym_no;
+  int sym_no;
   mrb_int len;
 
@@ -221,5 +221,5 @@
     size += sizeof(uint16_t); /* snl(n) */
     if (irep->syms[sym_no] != 0) {
-      mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
+      mrb_sym_name_len(mrb, irep->syms[sym_no], &len);
       size += len + 1; /* sn(n) + null char */
     }
@@ -232,5 +232,5 @@
 write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
 {
-  uint32_t sym_no;
+  int sym_no;
   uint8_t *cur = buf;
   const char *name;
@@ -242,5 +242,5 @@
       mrb_int len;
 
-      name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
+      name = mrb_sym_name_len(mrb, irep->syms[sym_no], &len);
 
       mrb_assert_int_fit(mrb_int, len, uint16_t, UINT16_MAX);
@@ -274,5 +274,5 @@
 {
   size_t size = 0;
-  size_t irep_no;
+  int irep_no;
 
   size = get_irep_record_size_1(mrb, irep);
@@ -286,5 +286,5 @@
 write_irep_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
 {
-  uint32_t i;
+  int i;
   uint8_t *src = bin;
 
@@ -367,116 +367,4 @@
 }
 
-static int
-write_section_lineno_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
-{
-  struct rite_section_lineno_header *header = (struct rite_section_lineno_header*)bin;
-
-  memcpy(header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(header->section_ident));
-  uint32_to_bin((uint32_t)section_size, header->section_size);
-
-  return MRB_DUMP_OK;
-}
-
-static size_t
-get_lineno_record_size(mrb_state *mrb, mrb_irep *irep)
-{
-  size_t size = 0;
-
-  size += sizeof(uint32_t); /* record size */
-  size += sizeof(uint16_t); /* filename size */
-  if (irep->filename) {
-    size += strlen(irep->filename); /* filename */
-  }
-  size += sizeof(uint32_t); /* niseq */
-  if (irep->lines) {
-    size += sizeof(uint16_t) * irep->ilen; /* lineno */
-  }
-
-  return size;
-}
-
-static size_t
-write_lineno_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
-{
-  uint8_t *cur = bin;
-  size_t iseq_no;
-  size_t filename_len;
-  ptrdiff_t diff;
-
-  cur += sizeof(uint32_t); /* record size */
-
-  if (irep->filename) {
-    filename_len = strlen(irep->filename);
-  }
-  else {
-    filename_len = 0;
-  }
-  mrb_assert_int_fit(size_t, filename_len, uint16_t, UINT16_MAX);
-  cur += uint16_to_bin((uint16_t)filename_len, cur); /* filename size */
-
-  if (filename_len) {
-    memcpy(cur, irep->filename, filename_len);
-    cur += filename_len; /* filename */
-  }
-
-  if (irep->lines) {
-    mrb_assert_int_fit(size_t, irep->ilen, uint32_t, UINT32_MAX);
-    cur += uint32_to_bin((uint32_t)(irep->ilen), cur); /* niseq */
-    for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
-      cur += uint16_to_bin(irep->lines[iseq_no], cur); /* opcode */
-    }
-  }
-  else {
-    cur += uint32_to_bin(0, cur); /* niseq */
-  }
-
-  diff = cur - bin;
-  mrb_assert_int_fit(ptrdiff_t, diff, uint32_t, UINT32_MAX);
-
-  uint32_to_bin((uint32_t)diff, bin); /* record size */
-
-  mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
-  return (size_t)diff;
-}
-
-static size_t
-write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
-{
-  size_t i;
-  size_t rlen, size = 0;
-
-  rlen = write_lineno_record_1(mrb, irep, bin);
-  bin += rlen;
-  size += rlen;
-  for (i=0; i<irep->rlen; i++) {
-    rlen = write_lineno_record(mrb, irep, bin);
-    bin += rlen;
-    size += rlen;
-  }
-  return size;
-}
-
-static int
-write_section_lineno(mrb_state *mrb, mrb_irep *irep, uint8_t *bin)
-{
-  size_t section_size = 0;
-  size_t rlen = 0; /* size of irep record */
-  uint8_t *cur = bin;
-
-  if (mrb == NULL || bin == NULL) {
-    return MRB_DUMP_INVALID_ARGUMENT;
-  }
-
-  cur += sizeof(struct rite_section_lineno_header);
-  section_size += sizeof(struct rite_section_lineno_header);
-
-  rlen = write_lineno_record(mrb, irep, cur);
-  section_size += rlen;
-
-  write_section_lineno_header(mrb, section_size, bin);
-
-  return MRB_DUMP_OK;
-}
-
 static size_t
 get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
@@ -484,5 +372,5 @@
   size_t ret = 0;
   uint16_t f_idx;
-  size_t i;
+  int i;
 
   ret += sizeof(uint32_t); /* record size */
@@ -532,6 +420,7 @@
 {
   mrb_sym *filenames = *fp;
-  size_t i, size = 0;
+  size_t size = 0;
   mrb_irep_debug_info *di = irep->debug_info;
+  int i;
 
   mrb_assert(lp);
@@ -548,5 +437,5 @@
 
       /* filename */
-      mrb_sym2name_len(mrb, file->filename_sym, &filename_len);
+      mrb_sym_name_len(mrb, file->filename_sym, &filename_len);
       size += sizeof(uint16_t) + (size_t)filename_len;
     }
@@ -606,5 +495,5 @@
   ret = cur - bin;
   mrb_assert_int_fit(ptrdiff_t, ret, uint32_t, UINT32_MAX);
-  uint32_to_bin(ret, bin);
+  uint32_to_bin((uint32_t)ret, bin);
 
   mrb_assert_int_fit(ptrdiff_t, ret, size_t, SIZE_MAX);
@@ -616,5 +505,5 @@
 {
   size_t size, len;
-  size_t irep_no;
+  int irep_no;
 
   size = len = write_debug_record_1(mrb, irep, bin, filenames, filenames_len);
@@ -652,7 +541,7 @@
   section_size += sizeof(uint16_t);
   for (i = 0; i < filenames_len; ++i) {
-    sym = mrb_sym2name_len(mrb, filenames[i], &sym_len);
+    sym = mrb_sym_name_len(mrb, filenames[i], &sym_len);
     mrb_assert(sym);
-    cur += uint16_to_bin(sym_len, cur);
+    cur += uint16_to_bin((uint16_t)sym_len, cur);
     memcpy(cur, sym, sym_len);
     cur += sym_len;
@@ -666,5 +555,5 @@
   memcpy(header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(header->section_ident));
   mrb_assert(section_size <= INT32_MAX);
-  uint32_to_bin(section_size, header->section_size);
+  uint32_to_bin((uint32_t)section_size, header->section_size);
 
   return MRB_DUMP_OK;
@@ -674,5 +563,5 @@
 create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
 {
-  size_t i;
+  int i;
 
   if (*syms == NULL) {
@@ -706,6 +595,6 @@
 
   for (i = 0; i < syms_len; ++i) {
-    str = mrb_sym2name_len(mrb, syms[i], &str_len);
-    cur += uint16_to_bin(str_len, cur);
+    str = mrb_sym_name_len(mrb, syms[i], &str_len);
+    cur += uint16_to_bin((uint16_t)str_len, cur);
     memcpy(cur, str, str_len);
     cur += str_len;
@@ -721,5 +610,5 @@
 {
   uint8_t *cur = *start;
-  size_t i;
+  int i;
 
   for (i = 0; i + 1 < irep->nlocals; ++i) {
@@ -749,5 +638,6 @@
 get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
 {
-  size_t ret = 0, i;
+  size_t ret = 0;
+  int i;
 
   ret += (sizeof(uint16_t) + sizeof(uint16_t)) * (irep->nlocals - 1);
@@ -769,5 +659,5 @@
   for (i = 0; i < syms_len; ++i) {
     mrb_int str_len;
-    mrb_sym2name_len(mrb, syms[i], &str_len);
+    mrb_sym_name_len(mrb, syms[i], &str_len);
     ret += str_len;
   }
@@ -807,5 +697,5 @@
   diff = cur - start;
   mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
-  uint32_to_bin(diff, header->section_size);
+  uint32_to_bin((uint32_t)diff, header->section_size);
 
 lv_section_exit:
@@ -842,5 +732,5 @@
   uint32_to_bin((uint32_t)binary_size, header->binary_size);
 
-  offset = (&(header->binary_crc[0]) - bin) + sizeof(uint16_t);
+  offset = (uint32_t)((&(header->binary_crc[0]) - bin) + sizeof(uint16_t));
   crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
   uint16_to_bin(crc, header->binary_crc);
@@ -850,11 +740,11 @@
 
 static mrb_bool
-is_debug_info_defined(mrb_irep *irep)
-{
-  size_t i;
+debug_info_defined_p(mrb_irep *irep)
+{
+  int i;
 
   if (!irep->debug_info) return FALSE;
   for (i=0; i<irep->rlen; i++) {
-    if (!is_debug_info_defined(irep->reps[i])) return FALSE;
+    if (!debug_info_defined_p(irep->reps[i])) return FALSE;
   }
   return TRUE;
@@ -862,12 +752,12 @@
 
 static mrb_bool
-is_lv_defined(mrb_irep *irep)
-{
-  size_t i;
+lv_defined_p(mrb_irep *irep)
+{
+  int i;
 
   if (irep->lv) { return TRUE; }
 
   for (i = 0; i < irep->rlen; ++i) {
-    if (is_lv_defined(irep->reps[i])) { return TRUE; }
+    if (lv_defined_p(irep->reps[i])) { return TRUE; }
   }
 
@@ -898,5 +788,5 @@
   size_t section_lineno_size = 0, section_lv_size = 0;
   uint8_t *cur = NULL;
-  mrb_bool const debug_info_defined = is_debug_info_defined(irep), lv_defined = is_lv_defined(irep);
+  mrb_bool const debug_info_defined = debug_info_defined_p(irep), lv_defined = lv_defined_p(irep);
   mrb_sym *lv_syms = NULL; uint32_t lv_syms_len = 0;
   mrb_sym *filenames = NULL; uint16_t filenames_len = 0;
@@ -923,8 +813,4 @@
       section_lineno_size += get_debug_record_size(mrb, irep);
     }
-    else {
-      section_lineno_size += sizeof(struct rite_section_lineno_header);
-      section_lineno_size += get_lineno_record_size(mrb, irep);
-    }
   }
 
@@ -954,10 +840,7 @@
     if (debug_info_defined) {
       result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
-    }
-    else {
-      result = write_section_lineno(mrb, irep, cur);
-    }
-    if (result != MRB_DUMP_OK) {
-      goto error_exit;
+      if (result != MRB_DUMP_OK) {
+        goto error_exit;
+      }
     }
     cur += section_lineno_size;
@@ -1061,5 +944,7 @@
     }
     if (fprintf(fp,
+          "#ifdef __cplusplus\n"
           "extern const uint8_t %s[];\n"
+          "#endif\n"
           "const uint8_t\n"
           "#if defined __GNUC__\n"
