Skip to content

Commit

Permalink
* compile.c (compile_cpath, iseq_compile_each): reverted
Browse files Browse the repository at this point in the history
  constant/class variable lookup in instance_eval etc. to the
  behavior of 1.8.
* eval.c (rb_mod_nesting): ditto.
* insns.def (putspecialobject, defineclass): ditto.
* node.h (NODE_FL_CREF_PUSHED_BY_EVAL): ditto.
* vm_core.h (VM_SPECIAL_OBJECT_CONST_BASE): ditto.
* vm_eval.c (yield_under, eval_under): ditto.
* vm_insnhelper.c (vm_cref_push, vm_get_const_base,
  vm_get_ev_const, vm_get_cvar_base): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
shugo committed Dec 3, 2009
1 parent 9998481 commit 7dbfe89
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 22 deletions.
19 changes: 19 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
Fri Dec 4 03:10:38 2009 Shugo Maeda <[email protected]>

* compile.c (compile_cpath, iseq_compile_each): reverted
constant/class variable lookup in instance_eval etc. to the
behavior of 1.8.

* eval.c (rb_mod_nesting): ditto.

* insns.def (putspecialobject, defineclass): ditto.

* node.h (NODE_FL_CREF_PUSHED_BY_EVAL): ditto.

* vm_core.h (VM_SPECIAL_OBJECT_CONST_BASE): ditto.

* vm_eval.c (yield_under, eval_under): ditto.

* vm_insnhelper.c (vm_cref_push, vm_get_const_base,
vm_get_ev_const, vm_get_cvar_base): ditto.

Thu Dec 3 20:27:27 2009 Martin Duerst <[email protected]>

* enc/trans/gb18030-tbl.rb: Fix omission of C1 region in code table
Expand Down
6 changes: 4 additions & 2 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,8 @@ compile_cpath(LINK_ANCHOR *ret, rb_iseq_t *iseq, NODE *cpath)
}
else {
/* class at cbase Foo */
ADD_INSN1(ret, nd_line(cpath), putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
ADD_INSN1(ret, nd_line(cpath), putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
return Qtrue;
}
}
Expand Down Expand Up @@ -3710,7 +3711,8 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
}

if (node->nd_vid) {
ADD_INSN1(ret, nd_line(node), putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
ADD_INSN1(ret, nd_line(node), putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
ADD_INSN1(ret, nd_line(node), setconstant, ID2SYM(node->nd_vid));
}
else {
Expand Down
3 changes: 2 additions & 1 deletion eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ rb_mod_nesting(void)

while (cref && cref->nd_next) {
VALUE klass = cref->nd_clss;
if (!NIL_P(klass)) {
if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
!NIL_P(klass)) {
rb_ary_push(ary, klass);
}
cref = cref->nd_next;
Expand Down
5 changes: 4 additions & 1 deletion insns.def
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ putspecialobject
case VM_SPECIAL_OBJECT_CBASE:
val = vm_get_cbase(GET_ISEQ(), GET_LFP(), GET_DFP());
break;
case VM_SPECIAL_OBJECT_CONST_BASE:
val = vm_get_const_base(GET_ISEQ(), GET_LFP(), GET_DFP());
break;
default:
rb_bug("putspecialobject insn: unknown value_type");
}
Expand Down Expand Up @@ -944,7 +947,7 @@ defineclass
rb_bug("unknown defineclass type: %d", (int)define_type);
}

COPY_CREF(class_iseq->cref_stack, vm_cref_push(th, klass, NOEX_PUBLIC));
COPY_CREF(class_iseq->cref_stack, vm_cref_push(th, klass, NOEX_PUBLIC, NULL));

/* enter scope */
vm_push_frame(th, class_iseq,
Expand Down
1 change: 1 addition & 0 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ typedef struct RNode {

/* 0..4:T_TYPES, 5:FL_MARK, 6:reserved, 7:NODE_FL_NEWLINE */
#define NODE_FL_NEWLINE (((VALUE)1)<<7)
#define NODE_FL_CREF_PUSHED_BY_EVAL NODE_FL_NEWLINE

#define NODE_TYPESHIFT 8
#define NODE_TYPEMASK (((VALUE)0x7f)<<NODE_TYPESHIFT)
Expand Down
27 changes: 23 additions & 4 deletions test/ruby/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,29 @@ def test_ancestors
remove_rake_mixins(remove_json_mixins(remove_pp_mixins(String.ancestors))))
end

CLASS_EVAL = 2
@@class_eval = 'b'

def test_class_eval
Other.class_eval("CLASS_EVAL = 1")
assert_equal(1, Other::CLASS_EVAL)
assert(Other.constants.include?(:CLASS_EVAL))
assert_equal(2, Other.class_eval { CLASS_EVAL })

Other.class_eval("@@class_eval = 'a'")
assert_equal('a', Other.class_variable_get(:@@class_eval))
assert_equal('b', Other.class_eval { @@class_eval })

Other.class_eval do
module_function

def class_eval_test
"foo"
end
end
assert("foo", Other.class_eval_test)

assert_equal([Other], Other.class_eval { |*args| args })
end

def test_const_defined?
Expand Down Expand Up @@ -451,7 +470,7 @@ def test_const_get3

def test_class_variable_get
c = Class.new
c.class_eval { @@foo = :foo }
c.class_eval('@@foo = :foo')
assert_equal(:foo, c.class_variable_get(:@@foo))
assert_raise(NameError) { c.class_variable_get(:@@bar) } # c.f. instance_variable_get
assert_raise(NameError) { c.class_variable_get(:foo) }
Expand All @@ -460,21 +479,21 @@ def test_class_variable_get
def test_class_variable_set
c = Class.new
c.class_variable_set(:@@foo, :foo)
assert_equal(:foo, c.class_eval { @@foo })
assert_equal(:foo, c.class_eval('@@foo'))
assert_raise(NameError) { c.class_variable_set(:foo, 1) }
end

def test_class_variable_defined
c = Class.new
c.class_eval { @@foo = :foo }
c.class_eval('@@foo = :foo')
assert_equal(true, c.class_variable_defined?(:@@foo))
assert_equal(false, c.class_variable_defined?(:@@bar))
assert_raise(NameError) { c.class_variable_defined?(:foo) }
end

def test_remove_class_variable
c = Class.new
c.class_eval { @@foo = :foo }
c.class_eval('@@foo = :foo')
c.class_eval { remove_class_variable(:@@foo) }
assert_equal(false, c.class_variable_defined?(:@@foo))
end
Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,20 @@ def test_specific_eval_with_wrong_arguments
end
end

class InstanceExec
INSTANCE_EXEC = 123
end

def test_instance_exec
x = 1.instance_exec(42) {|a| self + a }
assert_equal(43, x)

x = "foo".instance_exec("bar") {|a| self + a }
assert_equal("foobar", x)

assert_raise(NameError) do
InstanceExec.new.instance_exec { INSTANCE_EXEC }
end
end

def test_extend
Expand Down
5 changes: 3 additions & 2 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,9 @@ typedef struct {
#define VM_CALL_SUPER_BIT (0x01 << 7)
#define VM_CALL_OPT_SEND_BIT (0x01 << 8)

#define VM_SPECIAL_OBJECT_VMCORE 0x01
#define VM_SPECIAL_OBJECT_CBASE 0x02
#define VM_SPECIAL_OBJECT_VMCORE 0x01
#define VM_SPECIAL_OBJECT_CBASE 0x02
#define VM_SPECIAL_OBJECT_CONST_BASE 0x03

#define VM_FRAME_MAGIC_METHOD 0x11
#define VM_FRAME_MAGIC_BLOCK 0x21
Expand Down
10 changes: 6 additions & 4 deletions vm_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static inline VALUE vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *a
static inline VALUE vm_yield(rb_thread_t *th, int argc, const VALUE *argv);
static inline VALUE vm_backtrace(rb_thread_t *th, int lev);
static int vm_backtrace_each(rb_thread_t *th, int lev, rb_backtrace_iter_func *iter, void *arg);
static NODE *vm_cref_push(rb_thread_t *th, VALUE klass, int noex);
static NODE *vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr);
static VALUE vm_exec(rb_thread_t *th);
static void vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const NODE *cref);
static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, VALUE ary);
Expand Down Expand Up @@ -1161,16 +1161,18 @@ yield_under(VALUE under, VALUE self, VALUE values)
{
rb_thread_t *th = GET_THREAD();
rb_block_t block, *blockptr;
NODE *cref = vm_cref_push(th, under, NOEX_PUBLIC);
NODE *cref;

if ((blockptr = GC_GUARDED_PTR_REF(th->cfp->lfp[0])) != 0) {
block = *blockptr;
block.self = self;
th->cfp->lfp[0] = GC_GUARDED_PTR(&block);
}
cref = vm_cref_push(th, under, NOEX_PUBLIC, &block);
cref->flags |= NODE_FL_CREF_PUSHED_BY_EVAL;

if (values == Qundef) {
return vm_yield_with_cref(th, 0, 0, cref);
return vm_yield_with_cref(th, 1, &self, cref);
}
else {
return vm_yield_with_cref(th, RARRAY_LENINT(values), RARRAY_PTR(values), cref);
Expand All @@ -1181,7 +1183,7 @@ yield_under(VALUE under, VALUE self, VALUE values)
static VALUE
eval_under(VALUE under, VALUE self, VALUE src, const char *file, int line)
{
NODE *cref = vm_cref_push(GET_THREAD(), under, NOEX_PUBLIC);
NODE *cref = vm_cref_push(GET_THREAD(), under, NOEX_PUBLIC, NULL);

if (rb_safe_level() >= 4) {
StringValue(src);
Expand Down
43 changes: 35 additions & 8 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,14 +1066,17 @@ vm_get_cref(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
}

static NODE *
vm_cref_push(rb_thread_t *th, VALUE klass, int noex)
vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr)
{
rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(th, th->cfp);
NODE *cref = NEW_BLOCK(klass);
cref->nd_file = 0;
cref->nd_visi = noex;

if (cfp) {
if (blockptr) {
cref->nd_next = vm_get_cref(blockptr->iseq, blockptr->lfp, blockptr->dfp);
}
else if (cfp) {
cref->nd_next = vm_get_cref(cfp->iseq, cfp->lfp, cfp->dfp);
}

Expand All @@ -1096,6 +1099,23 @@ vm_get_cbase(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
return klass;
}

static inline VALUE
vm_get_const_base(const rb_iseq_t *iseq, const VALUE *lfp, const VALUE *dfp)
{
NODE *cref = vm_get_cref(iseq, lfp, dfp);
VALUE klass = Qundef;

while (cref) {
if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
(klass = cref->nd_clss) != 0) {
break;
}
cref = cref->nd_next;
}

return klass;
}

static inline void
vm_check_if_namespace(VALUE klass)
{
Expand All @@ -1117,12 +1137,16 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,

if (orig_klass == Qnil) {
/* in current lexical scope */
const NODE *root_cref = vm_get_cref(iseq, th->cfp->lfp, th->cfp->dfp);
const NODE *cref = root_cref;
const NODE *cref = vm_get_cref(iseq, th->cfp->lfp, th->cfp->dfp);
const NODE *root_cref = NULL;
VALUE klass = orig_klass;

while (cref && cref->nd_next) {
klass = cref->nd_clss;
if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL)) {
klass = cref->nd_clss;
if (root_cref == NULL)
root_cref = cref;
}
cref = cref->nd_next;

if (!NIL_P(klass)) {
Expand All @@ -1149,8 +1173,10 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
}

/* search self */
klass = root_cref->nd_clss;
if (NIL_P(klass)) {
if (root_cref && !NIL_P(root_cref->nd_clss)) {
klass = root_cref->nd_clss;
}
else {
klass = CLASS_OF(th->cfp->self);
}

Expand Down Expand Up @@ -1178,7 +1204,8 @@ vm_get_cvar_base(NODE *cref)
VALUE klass;

while (cref && cref->nd_next &&
(NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON))) {
(NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON) ||
(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL))) {
cref = cref->nd_next;

if (!cref->nd_next) {
Expand Down

0 comments on commit 7dbfe89

Please sign in to comment.