From 1dde9d8f29304cde61e02ff864b608aa94c750f7 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 20 Feb 2023 12:55:29 +0000 Subject: [PATCH] Improved error message in set() and push() The actual issue was that the type check wasn't catching the issue, so it reached the scope before it was caught. Fixed it, anyway. --- source/toy_builtin.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/source/toy_builtin.c b/source/toy_builtin.c index 1b175eb..f2354e7 100644 --- a/source/toy_builtin.c +++ b/source/toy_builtin.c @@ -1174,23 +1174,27 @@ int Toy_private_set(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { switch(obj.type) { case TOY_LITERAL_ARRAY: { - Toy_Literal typeLiteral = Toy_getScopeType(interpreter->scope, key); + //check the subtype of the array, if there is one, against the given argument + Toy_Literal typeLiteral = Toy_getScopeType(interpreter->scope, idn); if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_ARRAY) { Toy_Literal subtypeLiteral = ((Toy_Literal*)(TOY_AS_TYPE(typeLiteral).subtypes))[0]; if (TOY_AS_TYPE(subtypeLiteral).typeOf != TOY_LITERAL_ANY && TOY_AS_TYPE(subtypeLiteral).typeOf != val.type) { interpreter->errorOutput("Bad argument type in set\n"); + Toy_freeLiteral(typeLiteral); return -1; } } + Toy_freeLiteral(typeLiteral); + if (!TOY_IS_INTEGER(key)) { interpreter->errorOutput("Expected integer index in set\n"); return -1; } - if (TOY_AS_ARRAY(obj)->count <= TOY_AS_INTEGER(key) || TOY_AS_INTEGER(key) < 0) { + if (TOY_AS_INTEGER(key) >= TOY_AS_ARRAY(obj)->count || TOY_AS_INTEGER(key) < 0) { interpreter->errorOutput("Index out of bounds in set\n"); return -1; } @@ -1298,7 +1302,7 @@ int Toy_private_get(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { return -1; } - if (TOY_AS_ARRAY(obj)->count <= TOY_AS_INTEGER(key) || TOY_AS_INTEGER(key) < 0) { + if (TOY_AS_INTEGER(key) >= TOY_AS_ARRAY(obj)->count || TOY_AS_INTEGER(key) < 0) { interpreter->errorOutput("Index out of bounds in get\n"); return -1; } @@ -1374,17 +1378,21 @@ int Toy_private_push(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) switch(obj.type) { case TOY_LITERAL_ARRAY: { - Toy_Literal typeLiteral = Toy_getScopeType(interpreter->scope, val); + //check the subtype of the array, if there is one, against the given argument + Toy_Literal typeLiteral = Toy_getScopeType(interpreter->scope, idn); if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_ARRAY) { Toy_Literal subtypeLiteral = ((Toy_Literal*)(TOY_AS_TYPE(typeLiteral).subtypes))[0]; if (TOY_AS_TYPE(subtypeLiteral).typeOf != TOY_LITERAL_ANY && TOY_AS_TYPE(subtypeLiteral).typeOf != val.type) { interpreter->errorOutput("Bad argument type in push"); + Toy_freeLiteral(typeLiteral); return -1; } } + Toy_freeLiteral(typeLiteral); + Toy_pushLiteralArray(TOY_AS_ARRAY(obj), val); if (!Toy_setScopeVariable(interpreter->scope, idn, obj, true)) { //TODO: could definitely be more efficient than overwriting the whole original object