From dbc37b36c4ed71177c20c43fe02df809fd2d2ca8 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 2 Oct 2020 20:18:04 -0300 Subject: [PATCH] Make argument on io.flush optional --- docs/pages/libraries.md | 2 +- lib/allocators/gc.nelua | 10 +++++----- lib/io.nelua | 4 ++-- nelua/utils/errorer.lua | 2 +- tests/io_test.nelua | 3 +-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/pages/libraries.md b/docs/pages/libraries.md index 296bed18..5799a61d 100644 --- a/docs/pages/libraries.md +++ b/docs/pages/libraries.md @@ -83,7 +83,7 @@ This page is under construction and very incomplete. | `global io.stdout: filestream`{:.language-nelua} | Output file used for io.write. | | `global io.stdin: filestream`{:.language-nelua} | Input file used for io.read. | | `io.open(filename: stringview[, mode: stringview]) : (filestream, stringview, integer)`{:.language-nelua} | Opens a file. Alias of `filestream.open`. | -| `io.flush(file: filestream): boolean`{:.language-nelua} | Flushes the `file` | +| `io.flush(): boolean`{:.language-nelua} | Flushes stdout. | | `io.close([file])`{:.language-nelua} | Alias of `file:close`. Closes `io.stdout` if no file was given. | | `io.input(file: [stringview, filestream, niltype]): filestream`{:.language-nelua} | Sets, opens or returns the input file. | | `io.output(file: [stringview, filestream, niltype]): filestream`{:.language-nelua} | Sets, opens or returns the output file. | diff --git a/lib/allocators/gc.nelua b/lib/allocators/gc.nelua index f23bb06b..db2ba431 100644 --- a/lib/allocators/gc.nelua +++ b/lib/allocators/gc.nelua @@ -491,12 +491,12 @@ function GC:alloc0_opt(size: usize, flags: usize, finalizer: FinalizerCallback): return self:_alloc(ptr, size, flags, finalizer) end -function GC:alloc(size: usize): pointer +function GC:alloc(size: usize): pointer check(size > 0, 'GCAllocator.alloc: size cannot be zero') return self:alloc_opt(size, 0, nilptr) end -function GC:alloc0(size: usize): pointer +function GC:alloc0(size: usize): pointer check(size > 0, 'GCAllocator.alloc0: size must be greater than 0') return self:alloc0_opt(size, 0, nilptr) end @@ -530,19 +530,19 @@ function GC:_realloc(qtr: pointer, ptr: pointer, size: usize): pointer return nilptr end -function GC:realloc(ptr: pointer, size: usize, oldsize: usize): pointer +function GC:realloc(ptr: pointer, size: usize, oldsize: usize): pointer local qtr: pointer = allocator:realloc(ptr, size, oldsize) if not qtr then return nilptr end return self:_realloc(qtr, ptr, size) end -function GC:realloc0(ptr: pointer, size: usize, oldsize: usize): pointer +function GC:realloc0(ptr: pointer, size: usize, oldsize: usize): pointer local qtr: pointer = allocator:realloc0(ptr, size, oldsize) if not qtr then return nilptr end return self:_realloc(qtr, ptr, size) end -function GC:dealloc(ptr: pointer) +function GC:dealloc(ptr: pointer) local p: *GCItem = self:_get_ptr(ptr) if p then if p.finalizer then diff --git a/lib/io.nelua b/lib/io.nelua index 9a439e05..811e2ff3 100644 --- a/lib/io.nelua +++ b/lib/io.nelua @@ -25,8 +25,8 @@ function io.open(filename: stringview, return filestream.open(filename, mode) end -function io.flush(file: filestream): boolean - return file:flush() +function io.flush(): boolean + return io.stdout:flush() end function io.close(file: #[concept(function(x) diff --git a/nelua/utils/errorer.lua b/nelua/utils/errorer.lua index b65079af..65d80c39 100644 --- a/nelua/utils/errorer.lua +++ b/nelua/utils/errorer.lua @@ -60,7 +60,7 @@ local function get_pretty_source_pos_errmsg(src, lineno, colno, errmsg, errname, local errmsgcolor = colreset..colbright -- generate the error message - return string.format("%s:%d:%d: %s: %s\n%s\n%s%s", + return string.format("%s:%d:%d: %s: %s\n%s\n%s%s\n", srcname..colbright, lineno, colno, errcolor..errname, errmsgcolor..errmsg..colreset, line, linehelper, errtraceback) end diff --git a/tests/io_test.nelua b/tests/io_test.nelua index 4a03a181..a7e68b4d 100644 --- a/tests/io_test.nelua +++ b/tests/io_test.nelua @@ -24,8 +24,6 @@ do -- basic io.open/io.isopen/io.close file = io.open('LICENSE', 'r') assert(io.isopen(file) == true) - assert(io.flush(file) == true) - assert(io.flush(file) == true) assert(io.close(file) == true) assert(not io.isopen(file)) end @@ -163,6 +161,7 @@ do -- io.output/io.write assert(io.stdout ~= stdout) assert(io.write('test1') == true) assert(io.stdout:write('!!\n') == true) + assert(io.flush() == true) assert(io.output(stdout) == stdout) assert(io.stdout == stdout) assert(io.stdout:isopen())