Fix bug with resizing local table when assigning names to exceptions

This commit is contained in:
K. Lange 2021-02-26 15:28:03 +09:00
parent c50352d5a2
commit adfd8a6492

View File

@ -193,7 +193,7 @@ static void and_(int canAssign);
static KrkToken classDeclaration();
static void declareVariable();
static void namedVariable(KrkToken name, int canAssign);
static Local * addLocal(KrkToken name);
static size_t addLocal(KrkToken name);
static void string(int canAssign);
static KrkToken decorator(size_t level, FunctionType type);
static void call(int canAssign);
@ -1595,7 +1595,7 @@ static void tryStatement() {
int tryJump = emitJump(OP_PUSH_TRY);
/* We'll rename this later, but it needs to be on the stack now as it represents the exception handler */
size_t localNameCount = current->function->localNameCount;
Local * exceptionObject = addLocal(syntheticToken(""));
size_t exceptionObject = addLocal(syntheticToken(""));
defineVariable(0);
beginScope();
@ -1621,13 +1621,13 @@ static void tryStatement() {
/* Match 'as' to rename exception */
if (match(TOKEN_AS)) {
consume(TOKEN_IDENTIFIER, "Expected identifier after 'as'");
exceptionObject->name = parser.previous;
current->locals[exceptionObject].name = parser.previous;
} else {
/* XXX Should we remove this now? */
exceptionObject->name = syntheticToken("exception");
current->locals[exceptionObject].name = syntheticToken("exception");
}
/* Make sure we update the local name for debugging */
current->function->localNames[localNameCount].name = krk_copyString(exceptionObject->name.start, exceptionObject->name.length);
current->function->localNames[localNameCount].name = krk_copyString(current->locals[exceptionObject].name.start, current->locals[exceptionObject].name.length);
consume(TOKEN_COLON, "Expect ':' after except.");
beginScope();
@ -2501,12 +2501,13 @@ static ssize_t resolveLocal(Compiler * compiler, KrkToken * name) {
return -1;
}
static Local * addLocal(KrkToken name) {
static size_t addLocal(KrkToken name) {
if (current->localCount + 1 > current->localsSpace) {
size_t old = current->localsSpace;
current->localsSpace = GROW_CAPACITY(old);
current->locals = GROW_ARRAY(Local,current->locals,old,current->localsSpace);
}
size_t out = current->localCount;
Local * local = &current->locals[current->localCount++];
local->name = name;
local->depth = -1;
@ -2522,7 +2523,7 @@ static Local * addLocal(KrkToken name) {
current->function->localNames[current->function->localNameCount].deathday = 0;
current->function->localNames[current->function->localNameCount].name = krk_copyString(name.start, name.length);
current->function->localNameCount++;
return local;
return out;
}
static void declareVariable() {