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