mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
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.
This commit is contained in:
@@ -1174,23 +1174,27 @@ int Toy_private_set(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
|||||||
|
|
||||||
switch(obj.type) {
|
switch(obj.type) {
|
||||||
case TOY_LITERAL_ARRAY: {
|
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) {
|
if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_ARRAY) {
|
||||||
Toy_Literal subtypeLiteral = ((Toy_Literal*)(TOY_AS_TYPE(typeLiteral).subtypes))[0];
|
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) {
|
if (TOY_AS_TYPE(subtypeLiteral).typeOf != TOY_LITERAL_ANY && TOY_AS_TYPE(subtypeLiteral).typeOf != val.type) {
|
||||||
interpreter->errorOutput("Bad argument type in set\n");
|
interpreter->errorOutput("Bad argument type in set\n");
|
||||||
|
Toy_freeLiteral(typeLiteral);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Toy_freeLiteral(typeLiteral);
|
||||||
|
|
||||||
if (!TOY_IS_INTEGER(key)) {
|
if (!TOY_IS_INTEGER(key)) {
|
||||||
interpreter->errorOutput("Expected integer index in set\n");
|
interpreter->errorOutput("Expected integer index in set\n");
|
||||||
return -1;
|
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");
|
interpreter->errorOutput("Index out of bounds in set\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1298,7 +1302,7 @@ int Toy_private_get(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
|||||||
return -1;
|
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");
|
interpreter->errorOutput("Index out of bounds in get\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1374,17 +1378,21 @@ int Toy_private_push(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments)
|
|||||||
|
|
||||||
switch(obj.type) {
|
switch(obj.type) {
|
||||||
case TOY_LITERAL_ARRAY: {
|
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) {
|
if (TOY_AS_TYPE(typeLiteral).typeOf == TOY_LITERAL_ARRAY) {
|
||||||
Toy_Literal subtypeLiteral = ((Toy_Literal*)(TOY_AS_TYPE(typeLiteral).subtypes))[0];
|
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) {
|
if (TOY_AS_TYPE(subtypeLiteral).typeOf != TOY_LITERAL_ANY && TOY_AS_TYPE(subtypeLiteral).typeOf != val.type) {
|
||||||
interpreter->errorOutput("Bad argument type in push");
|
interpreter->errorOutput("Bad argument type in push");
|
||||||
|
Toy_freeLiteral(typeLiteral);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Toy_freeLiteral(typeLiteral);
|
||||||
|
|
||||||
Toy_pushLiteralArray(TOY_AS_ARRAY(obj), val);
|
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
|
if (!Toy_setScopeVariable(interpreter->scope, idn, obj, true)) { //TODO: could definitely be more efficient than overwriting the whole original object
|
||||||
|
|||||||
Reference in New Issue
Block a user