feat: add wrenBindMethod to patch field indices and superclass dispatch in inherited methods

When a class is defined, its superclass is not known until runtime since class definitions are imperative statements. This adds a new wrenBindMethod function that walks the bytecode of a method after it is bound to a class, adjusting LOAD_FIELD/STORE_FIELD indices to account for inherited fields and patching superclass dispatch targets. The newClass function now correctly accumulates numFields from the superclass chain, and the VM calls wrenBindMethod when binding methods to a class. Includes new test files for inherited field access and the is operator.
This commit is contained in:
Bob Nystrom
2013-12-11 00:13:25 +00:00
parent 6695580df4
commit d8c31a4391
10 changed files with 174 additions and 22 deletions
-27
View File
@@ -1,27 +0,0 @@
class Foo {
methodOnFoo { io.write("foo") }
method(a) { io.write("foo") }
method(a, b, c) { io.write("foo") }
}
class Bar is Foo {
methodOnBar { io.write("bar") }
method(a, b) { io.write("bar") }
method(a, b, c, d) { io.write("bar") }
}
var bar = Bar.new
bar.methodOnFoo // expect: foo
bar.methodOnBar // expect: bar
// Methods with different arity do not shadow each other.
bar.method(1) // expect: foo
bar.method(1, 2) // expect: bar
bar.method(1, 2, 3) // expect: foo
bar.method(1, 2, 3, 4) // expect: bar
// TODO(bob): Overriding (or BETA-style refining?).
// TODO(bob): Private fields.
// TODO(bob): Super (or inner) calls.
// TODO(bob): Grammar for what expressions can follow "is".
// TODO(bob): Prevent extending built-in types.