From 94fb04a51e290d4f4a6bf64dc97996ecfdc3373a Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Wed, 9 Jan 2013 11:22:39 +0900 Subject: Remove redundant null checks. --- src/cdump.c | 3 +-- src/dump.c | 6 ++---- src/load.c | 9 +++------ 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/cdump.c b/src/cdump.c index 89ae0bbff..7cb159046 100644 --- a/src/cdump.c +++ b/src/cdump.c @@ -167,8 +167,7 @@ make_cdump_irep(mrb_state *mrb, int irep_no, FILE *f) else SOURCE_CODE0(""); - if (buf) - mrb_free(mrb, buf); + mrb_free(mrb, buf); return MRB_CDUMP_OK; } diff --git a/src/dump.c b/src/dump.c index c32703a17..374f709ee 100644 --- a/src/dump.c +++ b/src/dump.c @@ -409,8 +409,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) } error_exit: - if (char_buf) - mrb_free(mrb, char_buf); + mrb_free(mrb, char_buf); return (int)(buf - buf_top); } @@ -455,8 +454,7 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) } error_exit: - if (char_buf) - mrb_free(mrb, char_buf); + mrb_free(mrb, char_buf); return (int)(buf - buf_top); } diff --git a/src/load.c b/src/load.c index 2b5f58001..1b7059a34 100644 --- a/src/load.c +++ b/src/load.c @@ -235,8 +235,7 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ *len = dst - pStart; error_exit: - if (char_buf) - mrb_free(mrb, char_buf); + mrb_free(mrb, char_buf); return MRB_DUMP_OK; } @@ -292,8 +291,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ret = mrb_read_irep(mrb, (char*)rite_dst); error_exit: - if (rite_dst) - mrb_free(mrb, rite_dst); + mrb_free(mrb, rite_dst); return ret; } @@ -489,8 +487,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) *len = src - recordStart; error_exit: - if (buf) - mrb_free(mrb, buf); + mrb_free(mrb, buf); return ret; } -- cgit v1.2.3 From 0b337accad67e8855a574e8524b5a1ceed162bd3 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Wed, 9 Jan 2013 12:03:16 +0900 Subject: Extract each expresion in "if" statements. It is for maintainability. --- src/dump.c | 36 ++++++++++++++++++++++++------------ src/load.c | 36 ++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/dump.c b/src/dump.c index c32703a17..d26372ee3 100644 --- a/src/dump.c +++ b/src/dump.c @@ -353,7 +353,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t len =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ @@ -376,7 +377,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -389,7 +391,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -423,7 +426,8 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->slen, buf, type); /* number of symbol */ @@ -439,7 +443,8 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) nlen = str_dump_len((char*)name, len, type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, 0, buf_size); @@ -475,7 +480,8 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) default: return MRB_DUMP_GENERAL_FAILURE; } - if ((buf = (char *)mrb_calloc(mrb, 1, buf_size)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; buf_top = buf; @@ -599,7 +605,8 @@ write_irep_record(mrb_state *mrb, int irep_no, char* bin, uint32_t *rlen, int ty default: break; } - if ((rc = calc_crc_section(mrb, irep, &crc, section)) != 0) + rc = calc_crc_section(mrb, irep, &crc, section); + if (rc != 0) return rc; bin += uint16_dump(crc, bin, type); /* crc */ @@ -624,10 +631,12 @@ dump_irep_record(mrb_state *mrb, int irep_no, FILE* fp, uint32_t *rlen) if (irep_record_size == 0) return MRB_DUMP_GENERAL_FAILURE; - if ((buf = (char *)mrb_calloc(mrb, 1, irep_record_size)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, irep_record_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; - if ((rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX)) != MRB_DUMP_OK) { + rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX); + if (rc != MRB_DUMP_OK) { rc = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -657,7 +666,8 @@ mrb_write_irep(mrb_state *mrb, int top, char *bin) bin += sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; for (irep_no=top; irep_noirep_len; irep_no++) { - if ((rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN)) != 0) + rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN); + if (rc != 0) return rc; bin += (rlen + DUMP_SIZE(MRB_DUMP_SIZE_OF_LONG, DUMP_TYPE_BIN)); @@ -685,7 +695,8 @@ mrb_dump_irep(mrb_state *mrb, int top, FILE* fp) return MRB_DUMP_WRITE_FAULT; for (irep_no=top; irep_noirep_len; irep_no++) { - if ((rc = dump_irep_record(mrb, irep_no, fp, &rlen)) != 0) + rc = dump_irep_record(mrb, irep_no, fp, &rlen); + if (rc != 0) return rc; rbds += rlen; @@ -716,7 +727,8 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) buf_size += get_irep_record_size(mrb, irep_no, DUMP_TYPE_BIN); buf_size += MRB_DUMP_SIZE_OF_LONG; /* end of file */ - if ((buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + buf = (char *)mrb_malloc(mrb, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; rc = mrb_write_irep(mrb, n, buf); diff --git a/src/load.c b/src/load.c index 2b5f58001..f434fdd74 100644 --- a/src/load.c +++ b/src/load.c @@ -144,7 +144,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; pStart = dst; @@ -193,7 +194,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( pdl > buf_size - 1) { buf_size = pdl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); @@ -220,7 +222,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( snl > buf_size - 1) { buf_size = snl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); @@ -259,11 +262,13 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) rfp = &ritefp; //Read File Header Section - if ((ret = load_rite_header(fp, &bin_header, hcrc)) != MRB_DUMP_OK) + ret = load_rite_header(fp, &bin_header, hcrc); + if (ret != MRB_DUMP_OK) return ret; len = sizeof(rite_binary_header) + bin_to_uint32(bin_header.rbds); - if ((rite_dst = (unsigned char *)mrb_malloc(mrb, len)) == NULL) + rite_dst = (unsigned char *)mrb_malloc(mrb, len); + if (rite_dst == NULL) return MRB_DUMP_GENERAL_FAILURE; dst = rite_dst; @@ -277,7 +282,8 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) for (i=0; iilen = bin_to_uint32(src); //iseq length src += MRB_DUMP_SIZE_OF_LONG; if (irep->ilen > 0) { - if ((irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen)) == NULL) { + irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); + if (irep->iseq == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -397,7 +404,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (pdl > bufsize - 1) { mrb_free(mrb, buf); bufsize = pdl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -448,7 +456,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) irep->slen = bin_to_uint32(src); //syms length src += MRB_DUMP_SIZE_OF_LONG; if (irep->slen > 0) { - if ((irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen)) == NULL) { + irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); + if (irep->syms == NULL) { ret = MRB_DUMP_INVALID_IREP; goto error_exit; } @@ -469,7 +478,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (snl > bufsize - 1) { mrb_free(mrb, buf); bufsize = snl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -510,7 +520,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) sirep = mrb->irep_len; //Read File Header Section - if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0) + nirep = read_rite_header(mrb, src, &bin_header); + if (nirep < 0) return nirep; src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc @@ -518,7 +529,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) //Read Binary Data Section for (n=0,i=sirep; n Date: Wed, 9 Jan 2013 13:33:00 +0900 Subject: Fix error handlings. --- src/dump.c | 48 ++++++++++++++++++++++++++++++++++++------------ src/load.c | 22 +++++++++++++++------- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/dump.c b/src/dump.c index 71fe12bf0..2885c225a 100644 --- a/src/dump.c +++ b/src/dump.c @@ -351,11 +351,14 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) char *char_buf; uint16_t buf_size =0; uint16_t len =0; + int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ @@ -378,8 +381,10 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) if (len > buf_size - 1) { buf_size = len + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); @@ -392,8 +397,10 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) if ( len > buf_size - 1) { buf_size = len + 1; char_buf = mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); @@ -411,9 +418,10 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) buf += len; } + result = (int)(buf - buf_top); error_exit: mrb_free(mrb, char_buf); - return (int)(buf - buf_top); + return result; } static int @@ -469,6 +477,7 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) char *buf, *buf_top; uint32_t buf_size; int type = DUMP_TYPE_BIN; + int result; switch (section) { case DUMP_IREP_HEADER: buf_size = get_irep_header_size(mrb, irep, type); break; @@ -485,18 +494,33 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) buf_top = buf; switch (section) { - case DUMP_IREP_HEADER: buf += write_irep_header(mrb, irep, buf, type); break; - case DUMP_ISEQ_BLOCK: buf += write_iseq_block(mrb, irep, buf, type); break; - case DUMP_POOL_BLOCK: buf += write_pool_block(mrb, irep, buf, type); break; - case DUMP_SYMS_BLOCK: buf += write_syms_block(mrb, irep, buf, type); break; - default: break; + case DUMP_IREP_HEADER: + result = write_irep_header(mrb, irep, buf, type); + break; + case DUMP_ISEQ_BLOCK: + result = write_iseq_block(mrb, irep, buf, type); + break; + case DUMP_POOL_BLOCK: + result = write_pool_block(mrb, irep, buf, type); + break; + case DUMP_SYMS_BLOCK: + result = write_syms_block(mrb, irep, buf, type); + break; + default: + break; /* Already checked above. */ + } + if (result < 0) { + goto error_exit; } + buf += result; *crc = calc_crc_16_ccitt((unsigned char*)buf_top, (int)(buf - buf_top)); mrb_free(mrb, buf_top); - return MRB_DUMP_OK; + result = MRB_DUMP_OK; + error_exit: + return result; } static uint16_t @@ -604,7 +628,7 @@ write_irep_record(mrb_state *mrb, int irep_no, char* bin, uint32_t *rlen, int ty } rc = calc_crc_section(mrb, irep, &crc, section); - if (rc != 0) + if (rc != MRB_DUMP_OK) return rc; bin += uint16_dump(crc, bin, type); /* crc */ @@ -718,7 +742,7 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) int buf_idx = 0; if (mrb == NULL || n < 0 || n >= mrb->irep_len || f == NULL || initname == NULL) - return -1; + return MRB_DUMP_INVALID_ARGUMENT; buf_size = sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; for (irep_no=n; irep_noirep_len; irep_no++) diff --git a/src/load.c b/src/load.c index 44bf32ae7..504246799 100644 --- a/src/load.c +++ b/src/load.c @@ -142,11 +142,14 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ unsigned char *pStart; char *char_buf; uint16_t buf_size =0; + int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } pStart = dst; @@ -195,8 +198,10 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( pdl > buf_size - 1) { buf_size = pdl + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } } memset(char_buf, '\0', buf_size); rite_fgets(rfp, (unsigned char*)char_buf, pdl, FALSE); //pool @@ -223,8 +228,10 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( snl > buf_size - 1) { buf_size = snl + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } } memset(char_buf, '\0', buf_size); rite_fgets(rfp, (unsigned char*)char_buf, snl, FALSE); //symbol name @@ -237,10 +244,11 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ *len = dst - pStart; + result = MRB_DUMP_OK; error_exit: mrb_free(mrb, char_buf); - return MRB_DUMP_OK; + return result; } int @@ -340,7 +348,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) recordStart = src; buf = (char *)mrb_malloc(mrb, bufsize); if (buf == NULL) { - ret = MRB_DUMP_INVALID_IREP; + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -390,7 +398,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (plen > 0) { irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen); if (irep->pool == NULL) { - ret = MRB_DUMP_INVALID_IREP; + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -456,7 +464,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (irep->slen > 0) { irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); if (irep->syms == NULL) { - ret = MRB_DUMP_INVALID_IREP; + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } -- cgit v1.2.3 From d5b8dc54a69a90b5f0e304fefcd0d62ca10ec64a Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Wed, 9 Jan 2013 13:33:51 +0900 Subject: Add a comment: the usage of MRB_DUMP_GENERAL_FAILURE. --- include/mruby/dump.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/mruby/dump.h b/include/mruby/dump.h index 1af24ab64..650651a7e 100644 --- a/include/mruby/dump.h +++ b/include/mruby/dump.h @@ -28,7 +28,11 @@ int mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname); #define DUMP_TYPE_BIN 1 #define DUMP_TYPE_HEX 2 -/* dump/load error code */ +/* dump/load error code + * + * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by + * unspecified issues like malloc failed. + */ #define MRB_DUMP_OK 0 #define MRB_DUMP_GENERAL_FAILURE -1 #define MRB_DUMP_WRITE_FAULT -2 -- cgit v1.2.3 From 3479d8474a8925f42983f7f490c8665083da93a6 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Wed, 9 Jan 2013 20:15:49 +0900 Subject: Added s flag to ar command --- src/mruby_core.rake | 2 +- tasks/libmruby.rake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mruby_core.rake b/src/mruby_core.rake index bc84ed6d9..7db1dbf77 100644 --- a/src/mruby_core.rake +++ b/src/mruby_core.rake @@ -6,7 +6,7 @@ MRuby.each_target do self.libmruby << objs file "#{build_dir}/lib/libmruby_core.a" => objs do |t| - archive t.name, 'r', t.prerequisites + archive t.name, 'rs', t.prerequisites end # Parser diff --git a/tasks/libmruby.rake b/tasks/libmruby.rake index 1c68aa5e4..9f5cc555a 100644 --- a/tasks/libmruby.rake +++ b/tasks/libmruby.rake @@ -1,5 +1,5 @@ MRuby.each_target do file "#{build_dir}/lib/libmruby.a" => libmruby.flatten do |t| - archive t.name, 'r', t.prerequisites + archive t.name, 'rs', t.prerequisites end end -- cgit v1.2.3 From 33350251ae3d8d683ac4b4c462e7f8aa690c25da Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Wed, 9 Jan 2013 22:40:08 +0900 Subject: Added conf.bins for defining bulding binaries --- Rakefile | 26 +++++++++++--------------- build_config.rb | 2 ++ doc/compile/README.md | 1 + tasks/mruby_build.rake | 9 ++++++--- tools/mirb/mirb.rake | 10 ++++++---- tools/mrbc/mrbc.rake | 10 ++++++---- tools/mruby/mruby.rake | 10 ++++++---- 7 files changed, 38 insertions(+), 30 deletions(-) diff --git a/Rakefile b/Rakefile index d31af964a..d067a2279 100644 --- a/Rakefile +++ b/Rakefile @@ -27,25 +27,21 @@ load 'test/mrbtest.rake' # generic build targets, rules task :default => :all -binfiles = [exefile('bin/mruby'), exefile('bin/mirb'), exefile('bin/mrbc')] - -desc "build all targets, install (locally) in-repo" -task :all => binfiles + MRuby.targets.map { |t| [exefile("#{t.build_dir}/bin/mruby"), exefile("#{t.build_dir}/bin/mirb"), exefile("#{t.build_dir}/bin/mrbc")] }.flatten - -file exefile('bin/mruby') => exefile('build/host/bin/mruby') do |t| - FileUtils.cp t.prerequisites.first, t.name -end +binfiles = MRuby.targets['host'].bins.map do |bin| + install_path = exefile("bin/#{bin}") + + file install_path => exefile("build/host/bin/#{bin}") do |t| + FileUtils.cp t.prerequisites.first, t.name + end -file exefile('bin/mirb') => exefile('build/host/bin/mirb') do |t| - FileUtils.cp t.prerequisites.first, t.name + install_path end -file exefile('bin/mrbc') => exefile('build/host/bin/mrbc') do |t| - FileUtils.cp t.prerequisites.first, t.name -end +desc "build all targets, install (locally) in-repo" +task :all => binfiles + MRuby.targets.values.map { |t| [exefile("#{t.build_dir}/bin/mruby"), exefile("#{t.build_dir}/bin/mirb"), exefile("#{t.build_dir}/bin/mrbc")] }.flatten desc "run all mruby tests" -task :test => MRuby.targets.map { |t| exefile("#{t.build_dir}/test/mrbtest") } do +task :test => MRuby.targets.values.map { |t| exefile("#{t.build_dir}/test/mrbtest") } do sh "#{filename exefile('build/host/test/mrbtest')}" if MRuby.targets.count > 1 puts "\nYou should run #{MRuby.targets.map{ |t| t.name == 'host' ? nil : "#{t.build_dir}/test/mrbtest" }.compact.join(', ')} on target device." @@ -54,7 +50,7 @@ end desc "clean all built and in-repo installed artifacts" task :clean do - MRuby.targets.each do |t| + MRuby.each_target do |t| FileUtils.rm_rf t.build_dir end FileUtils.rm_f binfiles diff --git a/build_config.rb b/build_config.rb index 93ccc1a98..4e80a2596 100644 --- a/build_config.rb +++ b/build_config.rb @@ -2,6 +2,7 @@ MRuby::Build.new do |conf| conf.cc = ENV['CC'] || 'gcc' conf.ld = ENV['LD'] || 'gcc' conf.ar = ENV['AR'] || 'ar' + # conf.bins = %w(mrbc mruby mirb) # conf.cxx = conf.cc # conf.objcc = conf.cc # conf.asm = conf.cc @@ -27,6 +28,7 @@ MRuby::CrossBuild.new('i386') do |conf| conf.cc = ENV['CC'] || 'gcc' conf.ld = ENV['LD'] || 'gcc' conf.ar = ENV['AR'] || 'ar' + # conf.bins = %w(mrbc mruby mirb) # conf.cxx = 'gcc' # conf.objcc = 'gcc' # conf.asm = 'gcc' diff --git a/doc/compile/README.md b/doc/compile/README.md index e68c0d6f1..cec70e68b 100644 --- a/doc/compile/README.md +++ b/doc/compile/README.md @@ -53,6 +53,7 @@ The following options can be configurated: * conf.objccflags (Object compiler flags) * conf.asmflags (Assembler flags) * conf.gem (A GEM which should be integrated - can be set several times) +* conf.bins (Build binaries) To compile just call ```./minirake``` inside of the mruby source root. To generate the test tool environment call ```./minirake test```. To clean diff --git a/tasks/mruby_build.rake b/tasks/mruby_build.rake index d5dc075c3..de9e556b4 100644 --- a/tasks/mruby_build.rake +++ b/tasks/mruby_build.rake @@ -3,11 +3,11 @@ module MRuby attr_accessor :build def targets - @targets ||= [] + @targets ||= {} end def each_target(&block) - @targets.each do |target| + @targets.each do |key, target| target.instance_eval(&block) end end @@ -22,6 +22,7 @@ module MRuby attr_writer :cxx, :cxxflags attr_writer :objcc, :objcflags attr_writer :asm, :asmflags + attr_accessor :bins attr_accessor :gperf, :yacc attr_accessor :cat, :git attr_reader :root, :gems @@ -38,9 +39,11 @@ module MRuby @yacc, @gperf = 'bison', 'gperf' @cat, @git = 'cat', 'git' + @bins = %w(mruby mrbc mirb) + @gems, @libmruby = [], [] - MRuby.targets << self + MRuby.targets[name.to_s] = self MRuby.build = self instance_eval(&block) end diff --git a/tools/mirb/mirb.rake b/tools/mirb/mirb.rake index 52f334420..958ebe79a 100644 --- a/tools/mirb/mirb.rake +++ b/tools/mirb/mirb.rake @@ -1,10 +1,12 @@ dir = File.dirname(__FILE__).sub(%r|^\./|, '') MRuby.each_target do - exec = exefile("#{build_dir}/bin/mirb") - objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } + if bins.select { |s| s.to_s == 'mirb' } + exec = exefile("#{build_dir}/bin/mirb") + objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } - file exec => objs + ["#{build_dir}/lib/libmruby.a"] do |t| - link t.name, t.prerequisites, gems.map { |g| g.mruby_ldflags }, gems.map { |g| g.mruby_libs } + file exec => objs + ["#{build_dir}/lib/libmruby.a"] do |t| + link t.name, t.prerequisites, gems.map { |g| g.mruby_ldflags }, gems.map { |g| g.mruby_libs } + end end end diff --git a/tools/mrbc/mrbc.rake b/tools/mrbc/mrbc.rake index cf356ba4f..bff88312a 100644 --- a/tools/mrbc/mrbc.rake +++ b/tools/mrbc/mrbc.rake @@ -1,10 +1,12 @@ dir = File.dirname(__FILE__).sub(%r|^\./|, '') MRuby.each_target do - exec = exefile("#{build_dir}/bin/mrbc") - objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } + if bins.select { |s| s.to_s == 'mrbc' } + exec = exefile("#{build_dir}/bin/mrbc") + objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } - file exec => objs + ["#{build_dir}/lib/libmruby_core.a"] do |t| - link t.name, t.prerequisites, [], gems.map { |g| g.mruby_libs } + file exec => objs + ["#{build_dir}/lib/libmruby_core.a"] do |t| + link t.name, t.prerequisites, [], gems.map { |g| g.mruby_libs } + end end end diff --git a/tools/mruby/mruby.rake b/tools/mruby/mruby.rake index 162e8d1ba..7842c4266 100644 --- a/tools/mruby/mruby.rake +++ b/tools/mruby/mruby.rake @@ -1,10 +1,12 @@ dir = File.dirname(__FILE__).sub(%r|^\./|, '') MRuby.each_target do - exec = exefile("#{build_dir}/bin/mruby") - objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } + if bins.select { |s| s.to_s == 'mruby' } + exec = exefile("#{build_dir}/bin/mruby") + objs = Dir.glob("#{dir}/*.{c}").map { |f| f.pathmap("#{build_dir}/%X.o") } - file exec => objs + ["#{build_dir}/lib/libmruby.a"] do |t| - link t.name, t.prerequisites, gems.map { |g| g.mruby_ldflags }, gems.map { |g| g.mruby_libs } + file exec => objs + ["#{build_dir}/lib/libmruby.a"] do |t| + link t.name, t.prerequisites, gems.map { |g| g.mruby_ldflags }, gems.map { |g| g.mruby_libs } + end end end -- cgit v1.2.3 From f134e8e1bf1b3a4885f33cc954a39b4250b776e1 Mon Sep 17 00:00:00 2001 From: Yukihiro Matz Matsumoto Date: Thu, 10 Jan 2013 01:06:51 +0900 Subject: jump address error when value taken from if statement without else clause; close #712 --- src/codegen.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index c1a62320d..246101f24 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1106,18 +1106,24 @@ codegen(codegen_scope *s, node *tree, int val) if (e) { if (val) pop(); pos2 = new_label(s); - genop(s, MKOP_sBx(OP_JMP, 0)); - dispatch(s, pos1); + genop(s, MKOP_sBx(OP_JMP, 0)); + dispatch(s, pos1); codegen(s, e, val); dispatch(s, pos2); } else { if (val) { pop(); + pos2 = new_label(s); + genop(s, MKOP_sBx(OP_JMP, 0)); + dispatch(s, pos1); genop(s, MKOP_A(OP_LOADNIL, cursp())); + dispatch(s, pos2); push(); } - dispatch(s, pos1); + else { + dispatch(s, pos1); + } } } break; -- cgit v1.2.3