Fixed a segfault

This commit is contained in:
2023-02-09 08:16:56 +00:00
parent 60908c8bf3
commit fc67d6a18b
9 changed files with 60 additions and 23 deletions

View File

@@ -1,14 +1,50 @@
import compound;
//test the standard library, under a number of different circumstances
fn p(x) {
print x;
};
//test basic import
{
import standard;
fn f(acc, k, v) {
acc(v);
return acc;
//this depends on external factors, so only check the length
assert clock().length() == 24, "import library failed";
}
var a = [1, 2, 3, 4];
a.reduce(p, f); //prints "10"
//test import within a function
{
fn f() {
import standard;
assert clock != null, "import library within function failed";
return clock;
}
//invoke
assert f()().length() == 24, "import library within function and return failed";
}
//test closing over standard library element
{
import standard;
fn f() {
assert clock != null, "import library outside function failed";
return clock;
}
//invoke
assert f()().length() == 24, "import library outside function and return failed";
}
//test importing as an alias
{
import standard as std;
assert std["clock"]().length() == 24, "import library as alias failed";
}
print "All good";

View File

@@ -1123,6 +1123,14 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
//let's screw with the fn name, too
if (looseFirstArgument) {
if (!TOY_IS_IDENTIFIER(identifier)) {
interpreter->errorOutput("Bad literal passed as a function identifier\n");
Toy_freeLiteral(identifier);
Toy_freeLiteral(stackSize);
Toy_freeLiteralArray(&arguments);
return false;
}
int length = TOY_AS_IDENTIFIER(identifier)->length + 1;
char buffer[TOY_MAX_STRING_LENGTH];
snprintf(buffer, TOY_MAX_STRING_LENGTH, "_%s", Toy_toCString(TOY_AS_IDENTIFIER(identifier))); //prepend an underscore
@@ -1135,6 +1143,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
if (!Toy_parseIdentifierToValue(interpreter, &func)) {
Toy_freeLiteralArray(&arguments);
Toy_freeLiteral(stackSize);
Toy_freeLiteral(identifier);
return false;
}
@@ -1157,6 +1166,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
TOY_AS_FUNCTION_NATIVE(func)(interpreter, &correct);
Toy_freeLiteralArray(&correct);
Toy_freeLiteral(stackSize);
Toy_freeLiteral(identifier);
return true;
}
@@ -1167,6 +1177,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
interpreter->errorOutput("\n");
Toy_freeLiteral(identifier);
Toy_freeLiteral(stackSize);
Toy_freeLiteralArray(&arguments);
return false;
}
@@ -1181,6 +1192,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
Toy_freeLiteralArray(&arguments);
Toy_freeLiteral(func);
Toy_freeLiteral(stackSize);
Toy_freeLiteral(identifier);
return ret;

View File

@@ -0,0 +1,2 @@
1.type();

View File

@@ -2,6 +2,3 @@
var t = astype [int];
var arr: t = [1, 2, 3.14];
}
print "All good";

View File

@@ -2,6 +2,3 @@
var t = astype [string:int];
var dict: t = ["one": 1, "two": 2, 3:4];
}
print "All good";

View File

@@ -2,6 +2,3 @@
var t = astype [string:int];
var dict: t = ["one": 1, "two": 2, "pi": 3.14];
}
print "All good";

View File

@@ -2,6 +2,3 @@
var a: [int] = [1, 2, 3];
print a[a];
}
print "All good";

View File

@@ -1,5 +1,3 @@
{
var s = "foo" - "bar";
}
print "All good";

View File

@@ -96,6 +96,7 @@ int main() {
//run each file in tests/scripts/
char* filenames[] = {
"access-parent-directory.toy",
"bad-function-identifier.toy",
"declare-types-array.toy",
"declare-types-dictionary-key.toy",
"declare-types-dictionary-value.toy",