diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-03-29 01:55:58 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-03-29 01:55:58 -0700 |
| commit | 6b67f08361c7fec36c59dc46d01cc9dd599bf73c (patch) | |
| tree | 2c86e2bcd1c5f49f8041cf9c9880df8a3a29dab1 /src/load.c | |
| parent | cb946da78710dbd5760510ffdbe8feec03ab99fe (diff) | |
| parent | 24eb120148921871b3e4e6ea812272782d2e0757 (diff) | |
| download | mruby-6b67f08361c7fec36c59dc46d01cc9dd599bf73c.tar.gz mruby-6b67f08361c7fec36c59dc46d01cc9dd599bf73c.zip | |
Merge pull request #1104 from monaka/pr-simplify-dump-load
Simplify dump/load
Diffstat (limited to 'src/load.c')
| -rw-r--r-- | src/load.c | 70 |
1 files changed, 15 insertions, 55 deletions
diff --git a/src/load.c b/src/load.c index 4a9947e75..a2f685742 100644 --- a/src/load.c +++ b/src/load.c @@ -17,22 +17,16 @@ #include "mruby/proc.h" #include "mruby/string.h" -#ifdef ENABLE_STDIO -typedef struct _RiteFILE -{ - FILE* fp; - unsigned char buf[256]; - int cnt; - int readlen; -} RiteFILE; -#endif - #ifndef _WIN32 # if SIZE_MAX < UINT32_MAX # error "It can't be run this code on this environment (SIZE_MAX < UINT32_MAX)" # endif #endif +#if CHAR_BIT != 8 +# error This code assumes CHAR_BIT == 8 +#endif + static size_t offset_crc_body() { @@ -45,21 +39,12 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) { int ret; size_t i; - char *char_buf; const uint8_t *src = bin; - uint16_t tt, pool_data_len, snl, buf_size = MRB_DUMP_DEFAULT_STR_LEN; - mrb_int fix_num; - mrb_float f; + uint16_t tt, pool_data_len, snl; size_t plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - // skip record size src += sizeof(uint32_t); @@ -98,34 +83,23 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) } for (i = 0; i < plen; i++) { + mrb_value s; tt = *src++; //pool TT pool_data_len = bin_to_uint16(src); //pool data length src += sizeof(uint16_t); - if (pool_data_len > buf_size - 1) { - mrb_free(mrb, char_buf); - buf_size = pool_data_len + 1; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - } - memcpy(char_buf, src, pool_data_len); + s = mrb_str_new(mrb, (char *)src, pool_data_len); src += pool_data_len; - char_buf[pool_data_len] = '\0'; switch (tt) { //pool data case MRB_TT_FIXNUM: - fix_num = str_to_mrb_int(char_buf); - irep->pool[i] = mrb_fixnum_value(fix_num); + irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; case MRB_TT_FLOAT: - f = str_to_mrb_float(char_buf); - irep->pool[i] = mrb_float_value(f); + irep->pool[i] = mrb_float_value(mrb_str_to_dbl(mrb, s, FALSE)); break; case MRB_TT_STRING: - irep->pool[i] = mrb_str_new(mrb, char_buf, pool_data_len); + irep->pool[i] = s; break; default: @@ -148,10 +122,6 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) } for (i = 0; i < irep->slen; i++) { - static const mrb_sym mrb_sym_zero = { 0 }; - *irep->syms = mrb_sym_zero; - } - for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); //symbol name length src += sizeof(uint16_t); @@ -160,26 +130,16 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) continue; } - if (snl > buf_size - 1) { - mrb_free(mrb, char_buf); - buf_size = snl + 1; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - } - memcpy(char_buf, src, snl); //symbol name + irep->syms[i] = mrb_intern2(mrb, (char *)src, snl); src += snl; - char_buf[snl] = '\0'; - irep->syms[i] = mrb_intern2(mrb, char_buf, snl); + + mrb_gc_arena_restore(mrb, ai); } } *len = src - bin; ret = MRB_DUMP_OK; error_exit: - mrb_free(mrb, char_buf); return ret; } @@ -236,7 +196,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 int ret; size_t i, fname_len, niseq; char *fname; - short *lines; + uint16_t *lines; ret = MRB_DUMP_OK; *len = 0; @@ -259,7 +219,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 bin += sizeof(uint32_t); // niseq *len += sizeof(uint32_t); - lines = (short *)mrb_malloc(mrb, niseq * sizeof(short)); + lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t)); for (i = 0; i < niseq; i++) { lines[i] = bin_to_uint16(bin); bin += sizeof(uint16_t); // niseq |
