From 23b55fc3607211e3e2e2593825e504de07101f5a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 14 Aug 2023 10:47:10 +1000 Subject: [PATCH] Fixed execFnDecl accidentally modifying the literalCache for a moment, resolved #105 --- source/toy_interpreter.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index db476f7..bb2647b 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -655,8 +655,15 @@ static bool execFnDecl(Toy_Interpreter* interpreter, bool lng) { functionIndex = (int)readByte(interpreter->bytecode, &interpreter->count); } - Toy_Literal identifier = interpreter->literalCache.literals[identifierIndex]; - Toy_Literal function = interpreter->literalCache.literals[functionIndex]; + Toy_Literal identifier = Toy_copyLiteral(interpreter->literalCache.literals[identifierIndex]); + Toy_Literal function = Toy_copyLiteral(interpreter->literalCache.literals[functionIndex]); + + if (!TOY_IS_IDENTIFIER(identifier) || !TOY_IS_FUNCTION(function)) { + interpreter->errorOutput("Failed to declare a function, unknown literal error\n"); + Toy_freeLiteral(identifier); + Toy_freeLiteral(function); + return false; + } TOY_AS_FUNCTION(function).scope = Toy_pushScope(interpreter->scope); //hacked in (needed for closure persistance) @@ -666,6 +673,10 @@ static bool execFnDecl(Toy_Interpreter* interpreter, bool lng) { interpreter->errorOutput("Can't redefine the function \""); Toy_printLiteralCustom(identifier, interpreter->errorOutput); interpreter->errorOutput("\"\n"); + + Toy_freeLiteral(identifier); + Toy_freeLiteral(function); + return false; } @@ -673,6 +684,10 @@ static bool execFnDecl(Toy_Interpreter* interpreter, bool lng) { interpreter->errorOutput("Incorrect type assigned to variable \""); Toy_printLiteralCustom(identifier, interpreter->errorOutput); interpreter->errorOutput("\"\n"); + + Toy_freeLiteral(identifier); + Toy_freeLiteral(function); + return false; } @@ -680,6 +695,8 @@ static bool execFnDecl(Toy_Interpreter* interpreter, bool lng) { TOY_AS_FUNCTION(function).scope = NULL; Toy_freeLiteral(type); + Toy_freeLiteral(identifier); + Toy_freeLiteral(function); return true; }