feat: prevent subclassing of built-in types with runtime error

Add a check in the interpreter's class creation path that rejects attempts to subclass any of the seven core built-in types (Class, Fiber, Fn, List, Map, Range, String). When such a subclass is declared, a runtime error is raised with a message like "Class 'SubName' may not subclass a built-in". Also add corresponding test files for each forbidden subclass scenario.
This commit is contained in:
Bob Nystrom
2015-02-27 15:41:25 +00:00
9 changed files with 41 additions and 0 deletions
+13
View File
@@ -1094,6 +1094,19 @@ static bool runInterpreter(WrenVM* vm)
{
// TODO: Handle the superclass not being a class object!
superclass = AS_CLASS(PEEK());
if (superclass == vm->classClass ||
superclass == vm->fiberClass ||
superclass == vm->fnClass ||
superclass == vm->listClass || /* This covers also OBJ_CLOSURE */
superclass == vm->mapClass ||
superclass == vm->rangeClass ||
superclass == vm->stringClass )
{
char message[70 + MAX_VARIABLE_NAME];
sprintf(message,
"Class '%s' may not subclass a built-in", name->value);
RUNTIME_ERROR(AS_STRING(wrenNewString(vm, message, strlen(message))));
}
}
int numFields = READ_BYTE();