mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
input and output can now be closed
This commit is contained in:
@@ -138,7 +138,11 @@ static int nativeClose(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments
|
||||
Toy_File* file = (Toy_File*)TOY_AS_OPAQUE(selfLiteral);
|
||||
|
||||
int result = 0;
|
||||
if (file->fp != NULL) {
|
||||
if (
|
||||
file->fp != stdout &&
|
||||
file->fp != stdin &&
|
||||
file->fp != NULL
|
||||
) {
|
||||
result = fclose(file->fp);
|
||||
file->fp = NULL;
|
||||
}
|
||||
@@ -811,18 +815,30 @@ int Toy_hookFileIO(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit
|
||||
createToyVariableInt(&variables[1], "MAX_FILES_OPEN", FOPEN_MAX);
|
||||
createToyVariableInt(&variables[2], "END_OF_FILE", EOF);
|
||||
|
||||
Toy_RefString* outMode = Toy_createRefString("w");
|
||||
Toy_RefString* outName = Toy_createRefString("output");
|
||||
|
||||
static Toy_File* outFile;
|
||||
outFile = createToyFile(Toy_createRefString("w"), Toy_createRefString("output"));
|
||||
outFile = createToyFile(outMode, outName);
|
||||
outFile->fp = stdout;
|
||||
|
||||
createToyVariableFile(&variables[3], "output", outFile);
|
||||
|
||||
Toy_deleteRefString(outMode);
|
||||
Toy_deleteRefString(outName);
|
||||
|
||||
Toy_RefString* inMode = Toy_createRefString("r");
|
||||
Toy_RefString* inName = Toy_createRefString("input");
|
||||
|
||||
static Toy_File* inFile;
|
||||
inFile = createToyFile(Toy_createRefString("r"), Toy_createRefString("input"));
|
||||
inFile = createToyFile(inMode, inName);
|
||||
inFile->fp = stdin;
|
||||
|
||||
createToyVariableFile(&variables[4], "input", inFile);
|
||||
|
||||
Toy_deleteRefString(inMode);
|
||||
Toy_deleteRefString(inName);
|
||||
|
||||
// store the library in an aliased dictionary
|
||||
if (!TOY_IS_NULL(alias)) {
|
||||
// make sure the name isn't taken
|
||||
|
||||
@@ -27,8 +27,8 @@ fn reset() {
|
||||
assert reader.read(string) == "\ntest", "read string failed";
|
||||
|
||||
// invaild types
|
||||
assert input.read(type) == null, "read type failed";
|
||||
assert input.read(any) == null, "read any failed";
|
||||
assert reader.read(type) == null, "read type failed";
|
||||
assert reader.read(any) == null, "read any failed";
|
||||
|
||||
reader.close();
|
||||
}
|
||||
@@ -76,6 +76,7 @@ fn reset() {
|
||||
|
||||
var result = reader.read(string);
|
||||
assert (result == "d!\n" || result == "d!\\r\n"), "read in read extended failed";
|
||||
print result;
|
||||
|
||||
reader.close();
|
||||
reset();
|
||||
@@ -137,4 +138,7 @@ fn reset() {
|
||||
reader.close();
|
||||
}
|
||||
|
||||
input.close();
|
||||
output.close();
|
||||
|
||||
print "All good";
|
||||
Reference in New Issue
Block a user