authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-25 23:34:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-30 13:44:36-04:00
loga870228ab467906ecc663bf89ecd042b49e116f5
tree9a584d415178434c7eecc38f7b1b3161774d351f
parentcc4552733351390aecab9ae900beb822237d6041

self-hosted: use std.event.fs.readFile


2 files changed, 12 insertions(+), 2 deletions(-)

src-self-hosted/compilation.zig+8-2
...@@ -30,6 +30,9 @@ const Package = @import("package.zig").Package;...@@ -30,6 +30,9 @@ const Package = @import("package.zig").Package;
30const link = @import("link.zig").link;30const link = @import("link.zig").link;
31const LibCInstallation = @import("libc_installation.zig").LibCInstallation;31const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
32const CInt = @import("c_int.zig").CInt;32const CInt = @import("c_int.zig").CInt;
33const fs = event.fs;
34
35const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3336
34/// Data that is local to the event loop.37/// Data that is local to the event loop.
35pub const EventLoopLocal = struct {38pub const EventLoopLocal = struct {
...@@ -757,8 +760,11 @@ pub const Compilation = struct {...@@ -757,8 +760,11 @@ pub const Compilation = struct {
757 const root_scope = blk: {760 const root_scope = blk: {
758 errdefer self.gpa().free(root_src_real_path);761 errdefer self.gpa().free(root_src_real_path);
759762
760 // TODO async/await readFileAlloc()763 const source_code = (await (async fs.readFile(
761 const source_code = io.readFileAlloc(self.gpa(), root_src_real_path) catch |err| {764 self.loop,
765 root_src_real_path,
766 max_src_size,
767 ) catch unreachable)) catch |err| {
762 try printError("unable to open '{}': {}", root_src_real_path, err);768 try printError("unable to open '{}': {}", root_src_real_path, err);
763 return err;769 return err;
764 };770 };
std/event/fs.zig+4
...@@ -273,6 +273,7 @@ pub async fn writeFileMode(loop: *event.Loop, path: []const u8, contents: []cons...@@ -273,6 +273,7 @@ pub async fn writeFileMode(loop: *event.Loop, path: []const u8, contents: []cons
273273
274/// The promise resumes when the last data has been confirmed written, but before the file handle274/// The promise resumes when the last data has been confirmed written, but before the file handle
275/// is closed.275/// is closed.
276/// Caller owns returned memory.
276pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize) ![]u8 {277pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize) ![]u8 {
277 var close_op = try CloseOperation.create(loop);278 var close_op = try CloseOperation.create(loop);
278 defer close_op.deinit();279 defer close_op.deinit();
...@@ -292,6 +293,9 @@ pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize)...@@ -292,6 +293,9 @@ pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize)
292 const buf_array = [][]u8{buf};293 const buf_array = [][]u8{buf};
293 const amt = try await (async preadv(loop, fd, list.len, buf_array) catch unreachable);294 const amt = try await (async preadv(loop, fd, list.len, buf_array) catch unreachable);
294 list.len += amt;295 list.len += amt;
296 if (list.len > max_size) {
297 return error.FileTooBig;
298 }
295 if (amt < buf.len) {299 if (amt < buf.len) {
296 return list.toOwnedSlice();300 return list.toOwnedSlice();
297 }301 }