Skip to content

Commit 21994d0

Browse files
committed
Instance variables were treated as local to a given sub-class, which is blatantly wrong
1 parent 10d1872 commit 21994d0

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

classcope.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def initialize(next_scope, name, offsets, superclass = nil)
3131
@name = name
3232
@vtable = {}
3333
@vtableoffsets = offsets
34+
35+
# FIXME: This can only safely be determined after we've parsed everything, and
36+
# even then only if we add any additional ones that are defined dynamically to a hash
37+
# but this works ok for now as long as we don't reopen a superclass and add new ivars.
3438
@ivaroff = @superclass ? @superclass.instance_size : 0
39+
3540
@instance_vars = !@superclass ? [:@__class__] : [] # FIXME: Do this properly
3641
@class_vars = {}
3742

@@ -74,8 +79,19 @@ def rest?
7479
false
7580
end
7681

82+
def find_ivar_offset(a)
83+
a = a.to_sym
84+
offset = @instance_vars.index(a)
85+
return @ivaroff + offset if offset
86+
return nil if !@superclass
87+
return @superclass.find_ivar_offset(a)
88+
end
89+
7790
def add_ivar(a)
78-
@instance_vars << a.to_sym if !@instance_vars.include?(a.to_sym)
91+
a = a.to_sym
92+
if !find_ivar_offset(a)
93+
@instance_vars << a
94+
end
7995
end
8096

8197
def add_constant(c, v = true)
@@ -120,17 +136,20 @@ def get_class_var(a)
120136
end
121137

122138
def get_instance_var(a)
123-
offset = @instance_vars.index(a)
124-
add_ivar(a) if !offset
125-
offset = @instance_vars.index(a)
139+
a = a.to_sym
140+
offset = find_ivar_offset(a)
141+
if !offset
142+
add_ivar(a)
143+
end
144+
offset = find_ivar_offset(a)
126145

127146
# This will show the name of the current class, the superclass name, the instance variable
128147
# name, the offset of the instance variable relative to the current class "base", and the
129148
# instance variable offset for the current class which comes in quite handy when debugging
130149
# object layout:
131150
#
132151
# STDERR.puts [:ivar, @name, @superclass ? @superclass.name : "",a, offset, @ivaroff].inspect
133-
return [:ivar, offset + @ivaroff]
152+
return [:ivar, offset]
134153
end
135154

136155
# Returns an argument within a class scope.

0 commit comments

Comments
 (0)