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:
@@ -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
|
||||
Reference in New Issue
Block a user