commit e616ea4612c2356896c4e077af975e9092a99629
parent 4298b6824c2b42026fbf7147eb267cb3c312756f
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date: Tue, 13 Apr 2021 00:28:53 +0200
bufio::fixed: implement closer
Diffstat:
8 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/bufio/dynamic.ha b/bufio/dynamic.ha
@@ -189,4 +189,5 @@ export fn truncate(s: *io::stream) (void | errors::unsupported) = {
assert(io::read(s, buf[..]) is io::EOF);
// TODO: this should check for errors::unsupported (harec bug prevents that)
assert(io::write(s, [1, 2]) is io::error);
+ io::close(s);
};
diff --git a/bufio/fixed.ha b/bufio/fixed.ha
@@ -58,6 +58,7 @@ fn fixed_close(s: *io::stream) void = free(s);
// TODO: add a read test too
static let buf: [1024]u8 = [0...];
let stream = fixed(buf, io::mode::WRITE);
+ defer io::close(stream);
let n = 0z;
n += io::write(stream, strings::toutf8("hello ")) as size;
n += io::write(stream, strings::toutf8("world")) as size;
diff --git a/bufio/scanner.ha b/bufio/scanner.ha
@@ -76,6 +76,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
@test fn scanbyte() void = {
let buf = fixed([1, 3, 3, 7], io::mode::READ);
+ defer io::close(buf);
assert(scanbyte(buf) as u8 == 1);
assert(scanbyte(buf) as u8 == 3);
@@ -86,6 +87,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
@test fn scantok() void = {
let buf = fixed([1, 3, 4, 5, 3, 7], io::mode::READ);
+ defer io::close(buf);
let tok = scantok(buf, 4) as []u8;
defer free(tok);
@@ -101,6 +103,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
@test fn scanline() void = {
let helloworld = strings::toutf8("hello\nworld");
let buf = fixed(helloworld, io::mode::READ);
+ defer io::close(buf);
let line = scanline(buf) as []u8;
defer free(line);
@@ -118,6 +121,7 @@ export fn scanrune(stream: *io::stream) (rune | utf8::invalid | io::EOF | io::er
0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81,
0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00,
], io::mode::READ);
+ defer io::close(in);
const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [
'こ', 'ん', 'に', 'ち', 'は', '\0', io::EOF,
diff --git a/fmt/fmt.ha b/fmt/fmt.ha
@@ -79,6 +79,7 @@ export fn asprintf(fmt: str, args: formattable...) str = {
// returned string is borrowed from this buffer.
export fn bsprintf(buf: []u8, fmt: str, args: formattable...) str = {
let sink = bufio::fixed(buf, io::mode::WRITE);
+ defer io::close(sink);
let l = fprintf(sink, fmt, args...) as size;
return strings::fromutf8_unsafe(buf[..l]);
};
@@ -134,6 +135,7 @@ export fn asprint(args: formattable...) str = {
// is borrowed from this buffer.
export fn bsprint(buf: []u8, args: formattable...) str = {
let sink = bufio::fixed(buf, io::mode::WRITE);
+ defer io::close(sink);
assert(fprint(sink, args...) is size);
return strings::fromutf8_unsafe(buf);
};
@@ -145,7 +147,7 @@ export fn fprintln(s: *io::stream, args: formattable...) (io::error | size) = {
};
// Formats values for printing using the default format modifiers and writes
-// them to an [io::stream] separated by spaces
+// them to an [io::stream] separated by spaces
export fn fprint(s: *io::stream, args: formattable...) (io::error | size) = {
let mod = modifiers { base = strconv::base::DEC, ... };
let n = 0z;
diff --git a/hare/lex/+test.ha b/hare/lex/+test.ha
@@ -6,6 +6,7 @@ use strings;
@test fn unget() void = {
let buf = bufio::fixed(strings::toutf8("z"), mode::READ);
+ defer io::close(buf);
let lexer = init(buf, "<test>");
unget(&lexer, 'x');
unget(&lexer, 'y');
@@ -58,6 +59,7 @@ fn vassert(expected: value, actual: value) bool = {
fn lextest(in: str, expected: []token) void = {
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = init(buf, "<test>");
for (let i = 0z; i < len(expected); i += 1) {
let etok = expected[i];
@@ -171,9 +173,9 @@ fn loc(line: uint, col: uint) location = location {
@test fn keywords() void = {
let keywords = bmap[..ltok::LAST_KEYWORD+1];
for (let i = 0z; i < len(keywords); i += 1) {
- let lexer = init(bufio::fixed(
- strings::toutf8(keywords[i]), mode::READ),
- "<test>");
+ let buf = bufio::fixed(strings::toutf8(keywords[i]), mode::READ);
+ defer io::close(buf);
+ let lexer = init(buf, "<test>");
let tok = lex(&lexer) as token;
assert(tok.0 == i: ltok);
};
diff --git a/hare/parse/+test.ha b/hare/parse/+test.ha
@@ -3,6 +3,7 @@ use fmt;
use hare::ast;
use hare::lex;
use hare::unparse;
+use io;
use io::{mode};
use strings;
use strio;
@@ -11,6 +12,7 @@ use strio;
{
const in = "foo";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let ident = ident(&lexer) as ast::ident;
defer ast::ident_free(ident);
@@ -23,6 +25,7 @@ use strio;
{
const in = "foo::bar";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let ident = ident(&lexer) as ast::ident;
defer ast::ident_free(ident);
@@ -35,6 +38,7 @@ use strio;
{
const in = "foo::bar::baz";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let ident = ident(&lexer) as ast::ident;
defer ast::ident_free(ident);
@@ -48,6 +52,7 @@ use strio;
{
const in = "foo::bar;";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let ident = ident(&lexer) as ast::ident;
defer ast::ident_free(ident);
@@ -62,6 +67,7 @@ use strio;
{
const in = "use foo;";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let mods = imports(&lexer) as []ast::import;
defer for (let i = 0z; i < len(mods); i += 1) {
@@ -84,6 +90,7 @@ use strio;
"use baz::bat;\n\n"
"export fn main() void = void;";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let mods = imports(&lexer) as []ast::import;
defer for (let i = 0z; i < len(mods); i += 1) {
@@ -113,6 +120,7 @@ use strio;
"use qux = quux::corge;\n"
"export fn main() void = void;";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let mods = imports(&lexer) as []ast::import;
defer for (let i = 0z; i < len(mods); i += 1) {
@@ -144,6 +152,7 @@ use strio;
"use quux::corge::{grault, garply,};\n"
"export fn main() void = void;";
let buf = bufio::fixed(strings::toutf8(in), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let mods = imports(&lexer) as []ast::import;
defer for (let i = 0z; i < len(mods); i += 1) {
@@ -174,6 +183,7 @@ use strio;
fn roundtrip(src: str) void = {
let buf = bufio::fixed(strings::toutf8(src), mode::READ);
+ defer io::close(buf);
let lexer = lex::init(buf, "<test>");
let u = ast::subunit {
imports = [],
diff --git a/strio/fixed.ha b/strio/fixed.ha
@@ -57,6 +57,7 @@ fn fixed_close(s: *io::stream) void = {
@test fn fixed() void = {
static let buf: [1024]u8 = [0...];
let stream = fixed(buf);
+ defer io::close(stream);
io::write(stream, strings::toutf8("hello ")) as size;
io::write(stream, strings::toutf8("world")) as size;
assert(string(stream) == "hello world");
diff --git a/unix/passwd/passwd.ha b/unix/passwd/passwd.ha
@@ -103,7 +103,7 @@ export fn getuser(username: str) (pwent | void) = {
let buf = bufio::fixed(strings::toutf8(
"sircmpwn:x:1000:1000:sircmpwn's comment:/home/sircmpwn:/bin/mrsh\n"
"alex:x:1001:1001::/home/alex:/bin/zsh"), io::mode::READ);
- defer free(buf);
+ defer io::close(buf);
let ent = nextpw(buf) as pwent;
defer pwent_finish(ent);