feat: add foreign class support with allocation, construct opcodes, and CLI callback wiring

Implement the initial infrastructure for foreign classes in the Wren VM, including new opcodes (FOREIGN_CONSTRUCT, FOREIGN_CLASS), a WrenBindForeignClassFn callback type in the public API, and updated CLI vm.c to store and forward foreign binding callbacks via setForeignCallbacks(). The compiler now tracks whether a class is foreign via a new isForeign flag in ClassCompiler, emits CODE_FOREIGN_CONSTRUCT instead of CODE_CONSTRUCT for foreign class constructors, and errors on field definitions inside foreign classes. The debug dump and opcode header gain entries for the new opcodes, and wrenBindSuperclass handles the -1 numFields sentinel for foreign classes to prevent field inheritance from superclasses.
This commit is contained in:
Bob Nystrom
2015-08-15 19:07:53 +00:00
parent 7551fa8c9b
commit 2dc209e585
33 changed files with 720 additions and 119 deletions
+48
View File
@@ -0,0 +1,48 @@
// Class with a default constructor.
foreign class Counter {
foreign increment(amount)
foreign value
}
var counter = Counter.new()
IO.print(counter.value) // expect: 0
counter.increment(3.1)
IO.print(counter.value) // expect: 3.1
counter.increment(1.2)
IO.print(counter.value) // expect: 4.3
// Foreign classes can inherit a class as long as it has no fields.
class PointBase {
inherited() {
IO.print("inherited method")
}
}
// Class with non-default constructor.
foreign class Point is PointBase {
construct new() {
IO.print("default")
}
construct new(x, y, z) {
IO.print(x, ", ", y, ", ", z)
}
foreign translate(x, y, z)
foreign toString
}
var p = Point.new(1, 2, 3) // expect: 1, 2, 3
IO.print(p) // expect: (1, 2, 3)
p.translate(3, 4, 5)
IO.print(p) // expect: (4, 6, 8)
p = Point.new() // expect: default
IO.print(p) // expect: (0, 0, 0)
p.inherited() // expect: inherited method
var error = Fiber.new {
class Subclass is Point {}
}.try()
IO.print(error) // expect: Class 'Subclass' cannot inherit from foreign class 'Point'.