feat: add explicit Stdout.flush() and remove automatic flush on System.write()

Remove the automatic fflush(stdout) call from the write() function in vm.c, which was causing gratuitous performance overhead on every System.write() invocation. Introduce a new Stdout class with a foreign static flush() method that delegates to fflush(stdout), giving users explicit control over when output is flushed. Update the io module registration in modules.c to include the Stdout class and bump MAX_CLASSES_PER_MODULE from 5 to 6. Add documentation for Stdout in doc/site/modules/io/stdout.markdown, update the io index and template to list the new class, and add flush() usage notes to the Stdin readByte() and readLine() docs. Implement the stdoutFlush() foreign function in io.c.
This commit is contained in:
Bob Nystrom
2017-10-09 14:16:05 +00:00
parent 8b2994b6e4
commit 5098c37491
9 changed files with 41 additions and 2 deletions
+5 -1
View File
@@ -43,6 +43,7 @@ extern void stdinIsRawSet(WrenVM* vm);
extern void stdinIsTerminal(WrenVM* vm);
extern void stdinReadStart(WrenVM* vm);
extern void stdinReadStop(WrenVM* vm);
extern void stdoutFlush(WrenVM* vm);
extern void schedulerCaptureMethods(WrenVM* vm);
extern void timerStartTimer(WrenVM* vm);
@@ -61,7 +62,7 @@ extern void timerStartTimer(WrenVM* vm);
// If you add a new class to the largest module below, make sure to bump this.
// Note that it also includes an extra slot for the sentinel value indicating
// the end of the list.
#define MAX_CLASSES_PER_MODULE 5
#define MAX_CLASSES_PER_MODULE 6
// Describes one foreign method in a class.
typedef struct
@@ -153,6 +154,9 @@ static ModuleRegistry modules[] =
STATIC_METHOD("readStart_()", stdinReadStart)
STATIC_METHOD("readStop_()", stdinReadStop)
END_CLASS
CLASS(Stdout)
STATIC_METHOD("flush()", stdoutFlush)
END_CLASS
END_MODULE
MODULE(os)
CLASS(Platform)
-1
View File
@@ -151,7 +151,6 @@ static WrenForeignClassMethods bindForeignClass(
static void write(WrenVM* vm, const char* text)
{
printf("%s", text);
fflush(stdout);
}
static void reportError(WrenVM* vm, WrenErrorType type,