feat: add static method support to class definitions with metaclass dispatch

Add TOKEN_STATIC keyword to the lexer, replacing the unused TOKEN_META, and extend the method() compiler function with an isStatic parameter. When isStatic is true, emit a new CODE_METACLASS instruction before CODE_METHOD to push the class's metaclass onto the stack, enabling static method dispatch. Also fix a buffer overflow bug in string_plus by allocating one extra byte for the null terminator, and refactor object initialization across vm.c by introducing a shared initObj helper to eliminate repeated type/flag assignments.
This commit is contained in:
Bob Nystrom
2013-11-10 19:46:13 +00:00
parent d74b4deff3
commit d0e0a776f4
5 changed files with 101 additions and 86 deletions
+13
View File
@@ -0,0 +1,13 @@
class Foo {
bar { "on instance" }
static bar { "on metaclass" }
bar(arg) { "on instance " + arg }
static bar(arg) { "on metaclass " + arg }
}
io.write("on metaclass " + "arg") // expect: on metaclass arg
io.write(Foo.new.bar) // expect: on instance
io.write(Foo.bar) // expect: on metaclass
io.write(Foo.new.bar("arg")) // expect: on instance arg
io.write(Foo.bar("arg")) // expect: on metaclass arg