mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Fixed a segfault
This commit is contained in:
@@ -1,14 +1,50 @@
|
|||||||
import compound;
|
//test the standard library, under a number of different circumstances
|
||||||
|
|
||||||
fn p(x) {
|
//test basic import
|
||||||
print x;
|
{
|
||||||
};
|
import standard;
|
||||||
|
|
||||||
fn f(acc, k, v) {
|
//this depends on external factors, so only check the length
|
||||||
acc(v);
|
assert clock().length() == 24, "import library failed";
|
||||||
return acc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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";
|
||||||
|
|||||||
@@ -1123,6 +1123,14 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
|
|
||||||
//let's screw with the fn name, too
|
//let's screw with the fn name, too
|
||||||
if (looseFirstArgument) {
|
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;
|
int length = TOY_AS_IDENTIFIER(identifier)->length + 1;
|
||||||
char buffer[TOY_MAX_STRING_LENGTH];
|
char buffer[TOY_MAX_STRING_LENGTH];
|
||||||
snprintf(buffer, TOY_MAX_STRING_LENGTH, "_%s", Toy_toCString(TOY_AS_IDENTIFIER(identifier))); //prepend an underscore
|
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)) {
|
if (!Toy_parseIdentifierToValue(interpreter, &func)) {
|
||||||
Toy_freeLiteralArray(&arguments);
|
Toy_freeLiteralArray(&arguments);
|
||||||
|
Toy_freeLiteral(stackSize);
|
||||||
Toy_freeLiteral(identifier);
|
Toy_freeLiteral(identifier);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1157,6 +1166,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
TOY_AS_FUNCTION_NATIVE(func)(interpreter, &correct);
|
TOY_AS_FUNCTION_NATIVE(func)(interpreter, &correct);
|
||||||
|
|
||||||
Toy_freeLiteralArray(&correct);
|
Toy_freeLiteralArray(&correct);
|
||||||
|
Toy_freeLiteral(stackSize);
|
||||||
Toy_freeLiteral(identifier);
|
Toy_freeLiteral(identifier);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1167,6 +1177,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
interpreter->errorOutput("\n");
|
interpreter->errorOutput("\n");
|
||||||
|
|
||||||
Toy_freeLiteral(identifier);
|
Toy_freeLiteral(identifier);
|
||||||
|
Toy_freeLiteral(stackSize);
|
||||||
Toy_freeLiteralArray(&arguments);
|
Toy_freeLiteralArray(&arguments);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1181,6 +1192,7 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) {
|
|||||||
|
|
||||||
Toy_freeLiteralArray(&arguments);
|
Toy_freeLiteralArray(&arguments);
|
||||||
Toy_freeLiteral(func);
|
Toy_freeLiteral(func);
|
||||||
|
Toy_freeLiteral(stackSize);
|
||||||
Toy_freeLiteral(identifier);
|
Toy_freeLiteral(identifier);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
2
test/scripts/mustfail/bad-function-identifier.toy
Normal file
2
test/scripts/mustfail/bad-function-identifier.toy
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
1.type();
|
||||||
|
|
||||||
@@ -2,6 +2,3 @@
|
|||||||
var t = astype [int];
|
var t = astype [int];
|
||||||
var arr: t = [1, 2, 3.14];
|
var arr: t = [1, 2, 3.14];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
|
||||||
|
|||||||
@@ -2,6 +2,3 @@
|
|||||||
var t = astype [string:int];
|
var t = astype [string:int];
|
||||||
var dict: t = ["one": 1, "two": 2, 3:4];
|
var dict: t = ["one": 1, "two": 2, 3:4];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
|
||||||
|
|||||||
@@ -2,6 +2,3 @@
|
|||||||
var t = astype [string:int];
|
var t = astype [string:int];
|
||||||
var dict: t = ["one": 1, "two": 2, "pi": 3.14];
|
var dict: t = ["one": 1, "two": 2, "pi": 3.14];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
|
||||||
|
|||||||
@@ -2,6 +2,3 @@
|
|||||||
var a: [int] = [1, 2, 3];
|
var a: [int] = [1, 2, 3];
|
||||||
print a[a];
|
print a[a];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
print "All good";
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
{
|
{
|
||||||
var s = "foo" - "bar";
|
var s = "foo" - "bar";
|
||||||
}
|
}
|
||||||
|
|
||||||
print "All good";
|
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ int main() {
|
|||||||
//run each file in tests/scripts/
|
//run each file in tests/scripts/
|
||||||
char* filenames[] = {
|
char* filenames[] = {
|
||||||
"access-parent-directory.toy",
|
"access-parent-directory.toy",
|
||||||
|
"bad-function-identifier.toy",
|
||||||
"declare-types-array.toy",
|
"declare-types-array.toy",
|
||||||
"declare-types-dictionary-key.toy",
|
"declare-types-dictionary-key.toy",
|
||||||
"declare-types-dictionary-value.toy",
|
"declare-types-dictionary-value.toy",
|
||||||
|
|||||||
Reference in New Issue
Block a user