1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

iseq.c: rb_id2str

* iseq.c (iseq_load, insn_operand_intern, rb_iseq_disasm)
  (rb_iseq_parameters): use rb_id2str() instead of rb_id2name() to
  keep encoding.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-06-09 14:36:56 +00:00
parent f64e7c834f
commit a1d6532ef6
3 changed files with 47 additions and 29 deletions

View file

@ -1,4 +1,8 @@
Sat Jun 9 23:36:15 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Jun 9 23:36:53 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* iseq.c (iseq_load, insn_operand_intern, rb_iseq_disasm)
(rb_iseq_parameters): use rb_id2str() instead of rb_id2name() to
keep encoding.
* string.c (rb_str_symname_p): new function that checks if the string * string.c (rb_str_symname_p): new function that checks if the string
is valid as a symbol name. split from sym_inspect(). is valid as a symbol name. split from sym_inspect().

62
iseq.c
View file

@ -517,9 +517,9 @@ iseq_load(VALUE self, VALUE data, VALUE parent, VALUE opt)
if (st_lookup(type_map, type, &iseq_type) == 0) { if (st_lookup(type_map, type, &iseq_type) == 0) {
ID typeid = SYM2ID(type); ID typeid = SYM2ID(type);
const char *typename = rb_id2name(typeid); VALUE typename = rb_id2str(typeid);
if (typename) if (typename)
rb_raise(rb_eTypeError, "unsupport type: :%s", typename); rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, typename);
else else
rb_raise(rb_eTypeError, "unsupport type: %p", (void *)typeid); rb_raise(rb_eTypeError, "unsupport type: %p", (void *)typeid);
} }
@ -757,6 +757,19 @@ rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
} }
} }
static VALUE
id_to_name(ID id, VALUE default_value)
{
VALUE str = rb_id2str(id);
if (!str) {
str = default_value;
}
else if (!rb_str_symname_p(str)) {
str = rb_str_inspect(str);
}
return str;
}
static VALUE static VALUE
insn_operand_intern(rb_iseq_t *iseq, insn_operand_intern(rb_iseq_t *iseq,
VALUE insn, int op_no, VALUE op, VALUE insn, int op_no, VALUE op,
@ -779,30 +792,19 @@ insn_operand_intern(rb_iseq_t *iseq,
{ {
rb_iseq_t *liseq = iseq->local_iseq; rb_iseq_t *liseq = iseq->local_iseq;
int lidx = liseq->local_size - (int)op; int lidx = liseq->local_size - (int)op;
const char *name = rb_id2name(liseq->local_table[lidx]);
if (name) { ret = id_to_name(liseq->local_table[lidx], INT2FIX('*'));
ret = rb_str_new2(name);
}
else {
ret = rb_str_new2("*");
}
break; break;
} }
case TS_DINDEX:{ case TS_DINDEX:{
if (insn == BIN(getdynamic) || insn == BIN(setdynamic)) { if (insn == BIN(getdynamic) || insn == BIN(setdynamic)) {
rb_iseq_t *diseq = iseq; rb_iseq_t *diseq = iseq;
VALUE level = *pnop, i; VALUE level = *pnop, i;
const char *name;
for (i = 0; i < level; i++) { for (i = 0; i < level; i++) {
diseq = diseq->parent_iseq; diseq = diseq->parent_iseq;
} }
name = rb_id2name(diseq->local_table[diseq->local_size - op]); ret = id_to_name(diseq->local_table[diseq->local_size - op], INT2FIX('*'));
if (!name) {
name = "*";
}
ret = rb_str_new2(name);
} }
else { else {
ret = rb_inspect(INT2FIX(op)); ret = rb_inspect(INT2FIX(op));
@ -997,8 +999,8 @@ rb_iseq_disasm(VALUE self)
iseqdat->arg_simple); iseqdat->arg_simple);
for (i = 0; i < iseqdat->local_table_size; i++) { for (i = 0; i < iseqdat->local_table_size; i++) {
const char *name = rb_id2name(tbl[i]); long width;
char info[0x100]; VALUE name = id_to_name(tbl[i], 0);
char argi[0x100] = ""; char argi[0x100] = "";
char opti[0x100] = ""; char opti[0x100] = "";
@ -1019,10 +1021,14 @@ rb_iseq_disasm(VALUE self)
i < iseqdat->arg_post_start + iseqdat->arg_post_len) ? "Post" : "", i < iseqdat->arg_post_start + iseqdat->arg_post_len) ? "Post" : "",
iseqdat->arg_block == i ? "Block" : ""); iseqdat->arg_block == i ? "Block" : "");
snprintf(info, sizeof(info), "%s%s%s%s", name ? name : "?", rb_str_catf(str, "[%2d] ", iseqdat->local_size - i);
*argi ? "<" : "", argi, *argi ? ">" : ""); width = RSTRING_LEN(str) + 11;
if (name)
rb_str_catf(str, "[%2d] %-11s", iseqdat->local_size - i, info); rb_str_append(str, name);
else
rb_str_cat2(str, "?");
if (*argi) rb_str_catf(str, "<%s>", argi);
if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
} }
rb_str_cat2(str, "\n"); rb_str_cat2(str, "\n");
} }
@ -1409,7 +1415,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
#define PARAM_ID(i) iseq->local_table[(i)] #define PARAM_ID(i) iseq->local_table[(i)]
#define PARAM(i, type) ( \ #define PARAM(i, type) ( \
PARAM_TYPE(type), \ PARAM_TYPE(type), \
rb_id2name(PARAM_ID(i)) ? \ rb_id2str(PARAM_ID(i)) ? \
rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \ rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
a) a)
@ -1418,7 +1424,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
if (is_proc) { if (is_proc) {
for (i = 0; i < iseq->argc; i++) { for (i = 0; i < iseq->argc; i++) {
PARAM_TYPE(opt); PARAM_TYPE(opt);
rb_ary_push(a, rb_id2name(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil); rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
rb_ary_push(args, a); rb_ary_push(args, a);
} }
} }
@ -1435,7 +1441,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
if (iseq->arg_keyword != -1) r -= iseq->arg_keywords; if (iseq->arg_keyword != -1) r -= iseq->arg_keywords;
for (; i < r; i++) { for (; i < r; i++) {
PARAM_TYPE(opt); PARAM_TYPE(opt);
if (rb_id2name(PARAM_ID(i))) { if (rb_id2str(PARAM_ID(i))) {
rb_ary_push(a, ID2SYM(PARAM_ID(i))); rb_ary_push(a, ID2SYM(PARAM_ID(i)));
} }
rb_ary_push(args, a); rb_ary_push(args, a);
@ -1448,7 +1454,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
if (is_proc) { if (is_proc) {
for (i = iseq->arg_post_start; i < r; i++) { for (i = iseq->arg_post_start; i < r; i++) {
PARAM_TYPE(opt); PARAM_TYPE(opt);
rb_ary_push(a, rb_id2name(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil); rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
rb_ary_push(args, a); rb_ary_push(args, a);
} }
} }
@ -1461,12 +1467,12 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
CONST_ID(key, "key"); CONST_ID(key, "key");
for (i = 0; i < iseq->arg_keywords; i++) { for (i = 0; i < iseq->arg_keywords; i++) {
PARAM_TYPE(key); PARAM_TYPE(key);
if (rb_id2name(iseq->arg_keyword_table[i])) { if (rb_id2str(iseq->arg_keyword_table[i])) {
rb_ary_push(a, ID2SYM(iseq->arg_keyword_table[i])); rb_ary_push(a, ID2SYM(iseq->arg_keyword_table[i]));
} }
rb_ary_push(args, a); rb_ary_push(args, a);
} }
if (rb_id2name(iseq->local_table[iseq->arg_keyword])) { if (rb_id2str(iseq->local_table[iseq->arg_keyword])) {
CONST_ID(keyrest, "keyrest"); CONST_ID(keyrest, "keyrest");
rb_ary_push(args, PARAM(iseq->arg_keyword, keyrest)); rb_ary_push(args, PARAM(iseq->arg_keyword, keyrest));
} }

View file

@ -15,4 +15,12 @@ class TestISeq < Test::Unit::TestCase
e = assert_raise(TypeError) {RubyVM::InstructionSequence.load(ary)} e = assert_raise(TypeError) {RubyVM::InstructionSequence.load(ary)}
assert_match(/:foobar/, e.message) assert_match(/:foobar/, e.message)
end if defined?(RubyVM::InstructionSequence.load) end if defined?(RubyVM::InstructionSequence.load)
def test_disasm_encoding
src = "\u{3042} = 1"
enc = Encoding.default_internal || Encoding.default_external
assert_equal(enc, RubyVM::InstructionSequence.compile(src.encode(enc)).disasm.encoding)
enc = enc == Encoding::UTF_8 ? Encoding::Shift_JIS : Encoding::UTF_8
assert_equal(true, RubyVM::InstructionSequence.compile(src.encode(enc)).disasm.ascii_only?)
end
end end