diff options
Diffstat (limited to 'std')
| -rw-r--r-- | std/array_list.zig | 2 | ||||
| -rw-r--r-- | std/buffer.zig | 2 | ||||
| -rw-r--r-- | std/c/darwin.zig | 26 | ||||
| -rw-r--r-- | std/c/index.zig | 1 | ||||
| -rw-r--r-- | std/debug.zig | 7 | ||||
| -rw-r--r-- | std/hash_map.zig | 2 | ||||
| -rw-r--r-- | std/io.zig | 39 | ||||
| -rw-r--r-- | std/io_test.zig | 45 | ||||
| -rw-r--r-- | std/linked_list.zig | 2 | ||||
| -rw-r--r-- | std/mem.zig | 4 | ||||
| -rw-r--r-- | std/os/darwin.zig | 2 | ||||
| -rw-r--r-- | std/os/index.zig | 7 | ||||
| -rw-r--r-- | std/os/path.zig | 32 | ||||
| -rw-r--r-- | std/os/windows/index.zig | 3 |
14 files changed, 123 insertions, 51 deletions
diff --git a/std/array_list.zig b/std/array_list.zig index 842cc048cf..65b7e023e2 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -101,7 +101,7 @@ pub fn ArrayList(comptime T: type) -> type{ } test "basic ArrayList test" { - var list = ArrayList(i32).init(&debug.global_allocator); + var list = ArrayList(i32).init(debug.global_allocator); defer list.deinit(); {var i: usize = 0; while (i < 10) : (i += 1) { diff --git a/std/buffer.zig b/std/buffer.zig index 5eb67beef3..a1aa8faf9d 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -135,7 +135,7 @@ pub const Buffer = struct { test "simple Buffer" { const cstr = @import("cstr.zig"); - var buf = %%Buffer.init(&debug.global_allocator, ""); + var buf = %%Buffer.init(debug.global_allocator, ""); assert(buf.len() == 0); %%buf.append("hello"); %%buf.appendByte(' '); diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 7ce29b5076..433463fde9 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -6,26 +6,28 @@ pub const _errno = __error; /// Renamed to Stat to not conflict with the stat function. pub const Stat = extern struct { - dev: u32, + dev: i32, mode: u16, nlink: u16, ino: u64, uid: u32, gid: u32, - rdev: u64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - - size: u64, - blocks: u64, - blksize: u32, + rdev: i32, + atime: usize, + atimensec: usize, + mtime: usize, + mtimensec: usize, + ctime: usize, + ctimensec: usize, + birthtime: usize, + birthtimensec: usize, + size: i64, + blocks: i64, + blksize: i32, flags: u32, gen: u32, lspare: i32, - qspare: [2]u64, - + qspare: [2]i64, }; pub const timespec = extern struct { diff --git a/std/c/index.zig b/std/c/index.zig index 315ccc5292..2ac867ee71 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -14,6 +14,7 @@ pub extern "c" fn exit(code: c_int) -> noreturn; pub extern "c" fn isatty(fd: c_int) -> c_int; pub extern "c" fn close(fd: c_int) -> c_int; pub extern "c" fn fstat(fd: c_int, buf: &Stat) -> c_int; +pub extern "c" fn @"fstat$INODE64"(fd: c_int, buf: &Stat) -> c_int; pub extern "c" fn lseek(fd: c_int, offset: isize, whence: c_int) -> isize; pub extern "c" fn open(path: &const u8, oflag: c_int, ...) -> c_int; pub extern "c" fn raise(sig: c_int) -> c_int; diff --git a/std/debug.zig b/std/debug.zig index 23e951219a..50322024c3 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -38,7 +38,7 @@ fn getStderrStream() -> %&io.OutStream { /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. pub fn dumpStackTrace() { const stderr = getStderrStream() %% return; - writeStackTrace(stderr, &global_allocator, stderr_file.isTty(), 1) %% return; + writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) %% return; } /// This function invokes undefined behavior when `ok` is `false`. @@ -86,7 +86,7 @@ pub fn panic(comptime format: []const u8, args: ...) -> noreturn { const stderr = getStderrStream() %% os.abort(); stderr.print(format ++ "\n", args) %% os.abort(); - writeStackTrace(stderr, &global_allocator, stderr_file.isTty(), 1) %% os.abort(); + writeStackTrace(stderr, global_allocator, stderr_file.isTty(), 1) %% os.abort(); os.abort(); } @@ -967,7 +967,8 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { } } -pub var global_allocator = mem.Allocator { +pub const global_allocator = &global_allocator_state; +var global_allocator_state = mem.Allocator { .allocFn = globalAlloc, .reallocFn = globalRealloc, .freeFn = globalFree, diff --git a/std/hash_map.zig b/std/hash_map.zig index 582aa7ccaf..d124d7b573 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -232,7 +232,7 @@ pub fn HashMap(comptime K: type, comptime V: type, } test "basicHashMapTest" { - var map = HashMap(i32, i32, hash_i32, eql_i32).init(&debug.global_allocator); + var map = HashMap(i32, i32, hash_i32, eql_i32).init(debug.global_allocator); defer map.deinit(); assert(%%map.put(1, 11) == null); diff --git a/std/io.zig b/std/io.zig index dea327cf16..d570927488 100644 --- a/std/io.zig +++ b/std/io.zig @@ -20,6 +20,12 @@ const fmt = std.fmt; const is_posix = builtin.os != builtin.Os.windows; const is_windows = builtin.os == builtin.Os.windows; +test "import io tests" { + comptime { + _ = @import("io_test.zig"); + } +} + /// The function received invalid input at runtime. An Invalid error means a /// bug in the program that called the function. error Invalid; @@ -247,17 +253,32 @@ pub const File = struct { } pub fn getEndPos(self: &File) -> %usize { - var stat: system.Stat = undefined; - const err = system.getErrno(system.fstat(self.handle, &stat)); - if (err > 0) { - return switch (err) { - system.EBADF => error.BadFd, - system.ENOMEM => error.OutOfMemory, - else => os.unexpectedErrorPosix(err), + if (is_posix) { + var stat: system.Stat = undefined; + const err = system.getErrno(system.fstat(self.handle, &stat)); + if (err > 0) { + return switch (err) { + system.EBADF => error.BadFd, + system.ENOMEM => error.OutOfMemory, + else => os.unexpectedErrorPosix(err), + } } - } - return usize(stat.size); + return usize(stat.size); + } else if (is_windows) { + var file_size: system.LARGE_INTEGER = undefined; + if (system.GetFileSizeEx(self.handle, &file_size) == 0) { + const err = system.GetLastError(); + return switch (err) { + else => os.unexpectedErrorWindows(err), + }; + } + if (file_size < 0) + return error.Overflow; + return math.cast(usize, u64(file_size)); + } else { + unreachable; + } } pub fn read(self: &File, buffer: []u8) -> %usize { diff --git a/std/io_test.zig b/std/io_test.zig new file mode 100644 index 0000000000..14e98bc74e --- /dev/null +++ b/std/io_test.zig @@ -0,0 +1,45 @@ +const std = @import("index.zig"); +const io = std.io; +const allocator = std.debug.global_allocator; +const Rand = std.rand.Rand; +const assert = std.debug.assert; +const mem = std.mem; +const os = std.os; + +test "write a file, read it, then delete it" { + var data: [1024]u8 = undefined; + var rng = Rand.init(1234); + rng.fillBytes(data[0..]); + const tmp_file_name = "temp_test_file.txt"; + { + var file = %%io.File.openWrite(tmp_file_name, allocator); + defer file.close(); + + var file_out_stream = io.FileOutStream.init(&file); + var buf_stream = io.BufferedOutStream.init(&file_out_stream.stream); + const st = &buf_stream.stream; + %%st.print("begin"); + %%st.write(data[0..]); + %%st.print("end"); + %%buf_stream.flush(); + } + { + var file = %%io.File.openRead(tmp_file_name, allocator); + defer file.close(); + + const file_size = %%file.getEndPos(); + const expected_file_size = "begin".len + data.len + "end".len; + assert(file_size == expected_file_size); + + var file_in_stream = io.FileInStream.init(&file); + var buf_stream = io.BufferedInStream.init(&file_in_stream.stream); + const st = &buf_stream.stream; + const contents = %%st.readAllAlloc(allocator, 2 * 1024); + defer allocator.free(contents); + + assert(mem.eql(u8, contents[0.."begin".len], "begin")); + assert(mem.eql(u8, contents["begin".len..contents.len - "end".len], data)); + assert(mem.eql(u8, contents[contents.len - "end".len ..], "end")); + } + %%os.deleteFile(allocator, tmp_file_name); +} diff --git a/std/linked_list.zig b/std/linked_list.zig index f1cd60303e..cbcef793de 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -195,7 +195,7 @@ pub fn LinkedList(comptime T: type) -> type { } test "basic linked list test" { - const allocator = &debug.global_allocator; + const allocator = debug.global_allocator; var list = LinkedList(u32).init(); var one = %%list.createNode(1, allocator); diff --git a/std/mem.zig b/std/mem.zig index e37ccf011d..3cfdb25b35 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -331,8 +331,8 @@ pub fn join(allocator: &Allocator, sep: u8, strings: ...) -> %[]u8 { } test "mem.join" { - assert(eql(u8, %%join(&debug.global_allocator, ',', "a", "b", "c"), "a,b,c")); - assert(eql(u8, %%join(&debug.global_allocator, ',', "a"), "a")); + assert(eql(u8, %%join(debug.global_allocator, ',', "a", "b", "c"), "a,b,c")); + assert(eql(u8, %%join(debug.global_allocator, ',', "a"), "a")); } test "testStringEquality" { diff --git a/std/os/darwin.zig b/std/os/darwin.zig index 04c72f71c4..9d80c64006 100644 --- a/std/os/darwin.zig +++ b/std/os/darwin.zig @@ -129,7 +129,7 @@ pub fn isatty(fd: i32) -> bool { } pub fn fstat(fd: i32, buf: &c.Stat) -> usize { - errnoWrap(c.fstat(fd, buf)) + errnoWrap(c.@"fstat$INODE64"(fd, buf)) } pub fn lseek(fd: i32, offset: isize, whence: c_int) -> usize { diff --git a/std/os/index.zig b/std/os/index.zig index 58bfe611cc..c3f33c4ccd 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -562,7 +562,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 { test "os.getCwd" { // at least call it so it gets compiled - _ = getCwd(&debug.global_allocator); + _ = getCwd(debug.global_allocator); } pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void { @@ -1432,10 +1432,10 @@ test "windows arg parsing" { fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const u8) { var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line); for (expected_args) |expected_arg| { - const arg = %%??it.next(&debug.global_allocator); + const arg = %%??it.next(debug.global_allocator); assert(mem.eql(u8, arg, expected_arg)); } - assert(it.next(&debug.global_allocator) == null); + assert(it.next(debug.global_allocator) == null); } test "std.os" { @@ -1500,4 +1500,3 @@ pub fn isTty(handle: FileHandle) -> bool { } } } - diff --git a/std/os/path.zig b/std/os/path.zig index 9a7b6b139e..a372b5b077 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -41,23 +41,23 @@ pub fn joinPosix(allocator: &Allocator, paths: ...) -> %[]u8 { } test "os.path.join" { - assert(mem.eql(u8, %%joinWindows(&debug.global_allocator, "c:\\a\\b", "c"), "c:\\a\\b\\c")); - assert(mem.eql(u8, %%joinWindows(&debug.global_allocator, "c:\\a\\b\\", "c"), "c:\\a\\b\\c")); + assert(mem.eql(u8, %%joinWindows(debug.global_allocator, "c:\\a\\b", "c"), "c:\\a\\b\\c")); + assert(mem.eql(u8, %%joinWindows(debug.global_allocator, "c:\\a\\b\\", "c"), "c:\\a\\b\\c")); - assert(mem.eql(u8, %%joinWindows(&debug.global_allocator, "c:\\", "a", "b\\", "c"), "c:\\a\\b\\c")); - assert(mem.eql(u8, %%joinWindows(&debug.global_allocator, "c:\\a\\", "b\\", "c"), "c:\\a\\b\\c")); + assert(mem.eql(u8, %%joinWindows(debug.global_allocator, "c:\\", "a", "b\\", "c"), "c:\\a\\b\\c")); + assert(mem.eql(u8, %%joinWindows(debug.global_allocator, "c:\\a\\", "b\\", "c"), "c:\\a\\b\\c")); - assert(mem.eql(u8, %%joinWindows(&debug.global_allocator, + assert(mem.eql(u8, %%joinWindows(debug.global_allocator, "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std", "io.zig"), "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std\\io.zig")); - assert(mem.eql(u8, %%joinPosix(&debug.global_allocator, "/a/b", "c"), "/a/b/c")); - assert(mem.eql(u8, %%joinPosix(&debug.global_allocator, "/a/b/", "c"), "/a/b/c")); + assert(mem.eql(u8, %%joinPosix(debug.global_allocator, "/a/b", "c"), "/a/b/c")); + assert(mem.eql(u8, %%joinPosix(debug.global_allocator, "/a/b/", "c"), "/a/b/c")); - assert(mem.eql(u8, %%joinPosix(&debug.global_allocator, "/", "a", "b/", "c"), "/a/b/c")); - assert(mem.eql(u8, %%joinPosix(&debug.global_allocator, "/a/", "b/", "c"), "/a/b/c")); + assert(mem.eql(u8, %%joinPosix(debug.global_allocator, "/", "a", "b/", "c"), "/a/b/c")); + assert(mem.eql(u8, %%joinPosix(debug.global_allocator, "/a/", "b/", "c"), "/a/b/c")); - assert(mem.eql(u8, %%joinPosix(&debug.global_allocator, "/home/andy/dev/zig/build/lib/zig/std", "io.zig"), + assert(mem.eql(u8, %%joinPosix(debug.global_allocator, "/home/andy/dev/zig/build/lib/zig/std", "io.zig"), "/home/andy/dev/zig/build/lib/zig/std/io.zig")); } @@ -453,7 +453,7 @@ pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { } test "os.path.resolve" { - const cwd = %%os.getCwd(&debug.global_allocator); + const cwd = %%os.getCwd(debug.global_allocator); if (is_windows) { assert(mem.eql(u8, testResolveWindows([][]const u8{"."}), cwd)); } else { @@ -492,11 +492,11 @@ test "os.path.resolvePosix" { } fn testResolveWindows(paths: []const []const u8) -> []u8 { - return %%resolveWindows(&debug.global_allocator, paths); + return %%resolveWindows(debug.global_allocator, paths); } fn testResolvePosix(paths: []const []const u8) -> []u8 { - return %%resolvePosix(&debug.global_allocator, paths); + return %%resolvePosix(debug.global_allocator, paths); } pub fn dirname(path: []const u8) -> []const u8 { @@ -899,12 +899,12 @@ test "os.path.relative" { } fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) { - const result = %%relativePosix(&debug.global_allocator, from, to); + const result = %%relativePosix(debug.global_allocator, from, to); assert(mem.eql(u8, result, expected_output)); } fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) { - const result = %%relativeWindows(&debug.global_allocator, from, to); + const result = %%relativeWindows(debug.global_allocator, from, to); assert(mem.eql(u8, result, expected_output)); } @@ -1022,5 +1022,5 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { test "os.path.real" { // at least call it so it gets compiled - _ = real(&debug.global_allocator, "some_path"); + _ = real(debug.global_allocator, "some_path"); } diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig index a398deb0b3..913cc79801 100644 --- a/std/os/windows/index.zig +++ b/std/os/windows/index.zig @@ -46,6 +46,8 @@ pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuf pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: &DWORD) -> BOOL; +pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: &LARGE_INTEGER) -> BOOL; + pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD; pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(in_hFile: HANDLE, @@ -115,6 +117,7 @@ pub const ULONG_PTR = usize; pub const UNICODE = false; pub const WCHAR = u16; pub const WORD = u16; +pub const LARGE_INTEGER = i64; pub const TRUE = 1; pub const FALSE = 0; |
