refactor: replace Foo.new constructor syntax with new Foo and simplify superclass constructor calls

Remove the `this new` constructor declaration syntax in favor of a `new` keyword that creates instances directly. Superclass constructors are now invoked via bare `super(args)` instead of `super.new(args)`, eliminating the semantic weirdness of calling a constructor on a partially-constructed instance. Update the compiler to treat `new` as a keyword token, replace the `isMethod` flag with `methodName`/`methodLength` fields, and change the VM's `NEW` opcode to pop the class from the stack before creating the instance. Add a default `new` native method on Object that returns `this`. Update all benchmarks and tests to use the new syntax.
This commit is contained in:
Bob Nystrom
2013-12-19 15:02:27 +00:00
parent f5a61e4241
commit 0e70d98169
30 changed files with 171 additions and 190 deletions
+8
View File
@@ -320,6 +320,13 @@ DEF_NATIVE(object_bangeq)
return BOOL_VAL(!wrenValuesEqual(args[0], args[1]));
}
DEF_NATIVE(object_new)
{
// This is the default argument-less constructor that all objects inherit.
// It just returns "this".
return args[0];
}
DEF_NATIVE(object_type)
{
return OBJ_VAL(wrenGetClass(vm, args[0]));
@@ -440,6 +447,7 @@ void wrenInitializeCore(WrenVM* vm)
vm->objectClass = defineClass(vm, "Object", NULL);
NATIVE(vm->objectClass, "== ", object_eqeq);
NATIVE(vm->objectClass, "!= ", object_bangeq);
NATIVE(vm->objectClass, "new", object_new);
NATIVE(vm->objectClass, "type", object_type);
// The "Class" class is the superclass of all metaclasses.