diff options
| -rw-r--r-- | src/codegen.c | 17 | ||||
| -rw-r--r-- | src/parse.y | 25 | ||||
| -rw-r--r-- | test/t/class.rb | 75 |
3 files changed, 99 insertions, 18 deletions
diff --git a/src/codegen.c b/src/codegen.c index e728da4ca..5c3614814 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1938,16 +1938,21 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_UNDEF: { - int sym = new_msym(s, sym(tree)); int undef = new_msym(s, mrb_intern(s->mrb, "undef_method")); + int num = 0; + node *t = tree; genop(s, MKOP_A(OP_TCLASS, cursp())); push(); - genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym)); - push(); - genop(s, MKOP_A(OP_LOADNIL, cursp())); - pop_n(2); - genop(s, MKOP_ABC(OP_SEND, cursp(), undef, 2)); + while (t) { + int symbol = new_msym(s, sym(t->car)); + genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol)); + push(); + t = t->cdr; + num++; + } + pop_n(num + 1); + genop(s, MKOP_ABC(OP_SEND, cursp(), undef, num)); if (val) { push(); } diff --git a/src/parse.y b/src/parse.y index 1cb8741f2..1ca3385f5 100644 --- a/src/parse.y +++ b/src/parse.y @@ -570,7 +570,7 @@ new_const(parser_state *p, mrb_sym sym) static node* new_undef(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_UNDEF, nsym(sym)); + return list2((node*)NODE_UNDEF, nsym(sym)); } // (:class class super body) @@ -917,7 +917,7 @@ var_reference(parser_state *p, node *lhs) %type <nd> assoc_list assocs assoc undef_list backref for_var %type <nd> block_param opt_block_param block_param_def f_opt %type <nd> bv_decls opt_bv_decl bvar f_larglist lambda_body -%type <nd> brace_block cmd_brace_block do_block lhs none fitem f_bad_arg +%type <nd> brace_block cmd_brace_block do_block lhs none f_bad_arg %type <nd> mlhs mlhs_list mlhs_post mlhs_basic mlhs_item mlhs_node mlhs_inner %type <id> fsym sym basic_symbol operation operation2 operation3 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg @@ -1485,19 +1485,13 @@ fsym : fname | basic_symbol ; -fitem : fsym - { - $$ = new_sym(p, $1); - } - ; - undef_list : fsym { $$ = new_undef(p, $1); } - | undef_list ',' {p->lstate = EXPR_FNAME;} fitem + | undef_list ',' {p->lstate = EXPR_FNAME;} fsym { - $$ = push($1, (node*)$4); + $$ = push($1, nsym($4)); } ; @@ -5462,8 +5456,15 @@ parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_UNDEF: - printf("NODE_UNDEF %s:\n", - mrb_sym2name(mrb, sym(tree))); + printf("NODE_UNDEF"); + { + node *t = tree; + while (t) { + printf(" %s", mrb_sym2name(mrb, sym(t->car))); + t = t->cdr; + } + } + printf(":\n"); break; case NODE_CLASS: diff --git a/test/t/class.rb b/test/t/class.rb index d6c4715dd..1c50853dc 100644 --- a/test/t/class.rb +++ b/test/t/class.rb @@ -209,3 +209,78 @@ end assert('Class Dup 2') do module M; end; M.dup.class == Module end + +assert('Class Alias 1') do + class A + def test; 1; end + + alias test2 test + alias :test3 :test + end + + A.new.test2 == 1 and A.new.test3 == 1 +end + +assert('Class Alias 2') do + class A + def test; 1; end + + alias test2 test + + def test; 2; end + end + + A.new.test == 2 and A.new.test2 == 1 +end + +assert('Class Undef 1') do + class A + def test1; 1; end + def test2; 2; end + + undef test1 + undef :test2 + end + + result1 = false + begin + A.new.test1 + rescue NoMethodError + result1 = true + end + + result2 = false + begin + A.new.test2 + rescue NoMethodError + result2 = true + end + + result1 == true and result2 == true +end + +assert('Class Undef 2') do + class A + def test1; 1; end + def test2; 2; end + + undef test1, test2 + end + + result1 = false + begin + A.new.test1 + rescue NoMethodError + result1 = true + end + + result2 = false + begin + A.new.test2 + rescue NoMethodError + result2 = true + end + + result1 == true and result2 == true +end + |
