forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.zig
324 lines (285 loc) · 11.5 KB
/
cache.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const FeatureFlags = bun.FeatureFlags;
const default_allocator = bun.default_allocator;
const C = bun.C;
const js_ast = bun.JSAst;
const logger = bun.logger;
const js_parser = bun.js_parser;
const json_parser = bun.JSON;
const options = @import("./options.zig");
const Define = @import("./defines.zig").Define;
const std = @import("std");
const fs = @import("./fs.zig");
const sync = @import("sync.zig");
const Mutex = @import("./lock.zig").Lock;
const import_record = @import("./import_record.zig");
const ImportRecord = import_record.ImportRecord;
pub const Set = struct {
js: JavaScript,
fs: Fs,
json: Json,
pub fn init(allocator: std.mem.Allocator) Set {
return Set{
.js = JavaScript.init(allocator),
.fs = Fs{
.shared_buffer = MutableString.init(allocator, 0) catch unreachable,
.macro_shared_buffer = MutableString.init(allocator, 0) catch unreachable,
},
.json = Json{},
};
}
};
const debug = Output.scoped(.fs, false);
pub const Fs = struct {
pub const Entry = struct {
contents: string,
fd: StoredFileDescriptorType = bun.invalid_fd,
pub fn deinit(entry: *Entry, allocator: std.mem.Allocator) void {
if (entry.contents.len > 0) {
allocator.free(entry.contents);
entry.contents = "";
}
}
pub fn closeFD(entry: *Entry) ?bun.sys.Error {
if (entry.fd != bun.invalid_fd) {
defer {
entry.fd = bun.invalid_fd;
}
return bun.sys.close(entry.fd);
}
return null;
}
};
shared_buffer: MutableString,
macro_shared_buffer: MutableString,
use_alternate_source_cache: bool = false,
stream: bool = false,
// When we are in a macro, the shared buffer may be in use by the in-progress macro.
// so we have to dynamically switch it out.
pub inline fn sharedBuffer(this: *Fs) *MutableString {
return if (!this.use_alternate_source_cache)
&this.shared_buffer
else
&this.macro_shared_buffer;
}
/// When we need to suspend/resume something that has pointers into the shared buffer, we need to
/// switch out the shared buffer so that it is not in use
/// The caller must
pub fn resetSharedBuffer(this: *Fs, buffer: *MutableString) void {
if (buffer == &this.shared_buffer) {
this.shared_buffer = MutableString.initEmpty(bun.default_allocator);
} else if (buffer == &this.macro_shared_buffer) {
this.macro_shared_buffer = MutableString.initEmpty(bun.default_allocator);
} else {
bun.unreachablePanic("resetSharedBuffer: invalid buffer", .{});
}
}
pub fn deinit(c: *Fs) void {
var iter = c.entries.iterator();
while (iter.next()) |entry| {
entry.value.deinit(c.entries.allocator);
}
c.entries.deinit();
}
pub fn readFileShared(
this: *Fs,
_fs: *fs.FileSystem,
path: [:0]const u8,
_: StoredFileDescriptorType,
_file_handle: ?StoredFileDescriptorType,
shared: *MutableString,
) !Entry {
var rfs = _fs.fs;
const file_handle: std.fs.File = if (_file_handle) |__file|
std.fs.File{ .handle = __file }
else
try std.fs.openFileAbsoluteZ(path, .{ .mode = .read_only });
defer {
if (rfs.needToCloseFiles() and _file_handle == null) {
file_handle.close();
}
}
const file = if (this.stream)
rfs.readFileWithHandle(path, null, file_handle, true, shared, true) catch |err| {
if (comptime Environment.isDebug) {
Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) });
}
return err;
}
else
rfs.readFileWithHandle(path, null, file_handle, true, shared, false) catch |err| {
if (comptime Environment.isDebug) {
Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) });
}
return err;
};
return Entry{
.contents = file.contents,
.fd = if (FeatureFlags.store_file_descriptors) file_handle.handle else 0,
};
}
pub fn readFile(
c: *Fs,
_fs: *fs.FileSystem,
path: string,
dirname_fd: StoredFileDescriptorType,
comptime use_shared_buffer: bool,
_file_handle: ?StoredFileDescriptorType,
) !Entry {
return c.readFileWithAllocator(bun.fs_allocator, _fs, path, dirname_fd, use_shared_buffer, _file_handle);
}
pub fn readFileWithAllocator(
c: *Fs,
allocator: std.mem.Allocator,
_fs: *fs.FileSystem,
path: string,
dirname_fd: StoredFileDescriptorType,
comptime use_shared_buffer: bool,
_file_handle: ?StoredFileDescriptorType,
) !Entry {
var rfs = _fs.fs;
var file_handle: std.fs.File = if (_file_handle) |__file| __file.asFile() else undefined;
if (_file_handle == null) {
if (FeatureFlags.store_file_descriptors and dirname_fd != bun.invalid_fd and dirname_fd != .zero) {
file_handle = (bun.sys.openatA(dirname_fd, std.fs.path.basename(path), bun.O.RDONLY, 0).unwrap() catch |err| brk: {
switch (err) {
error.ENOENT => {
const handle = try bun.openFile(path, .{ .mode = .read_only });
Output.prettyErrorln(
"<r><d>Internal error: directory mismatch for directory \"{s}\", fd {}<r>. You don't need to do anything, but this indicates a bug.",
.{ path, dirname_fd },
);
break :brk bun.toFD(handle.handle);
},
else => return err,
}
}).asFile();
} else {
file_handle = try bun.openFile(path, .{ .mode = .read_only });
}
}
if (comptime !Environment.isWindows) // skip on Windows because NTCreateFile will do it.
debug("openat({}, {s}) = {}", .{ dirname_fd, path, bun.toFD(file_handle.handle) });
const will_close = rfs.needToCloseFiles() and _file_handle == null;
defer {
if (will_close) {
debug("readFileWithAllocator close({d})", .{file_handle.handle});
file_handle.close();
}
}
const file = if (c.stream)
rfs.readFileWithHandleAndAllocator(allocator, path, null, file_handle, use_shared_buffer, c.sharedBuffer(), true) catch |err| {
if (Environment.isDebug) {
Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) });
}
return err;
}
else
rfs.readFileWithHandleAndAllocator(allocator, path, null, file_handle, use_shared_buffer, c.sharedBuffer(), false) catch |err| {
if (Environment.isDebug) {
Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) });
}
return err;
};
return Entry{
.contents = file.contents,
.fd = if (FeatureFlags.store_file_descriptors and !will_close) bun.toFD(file_handle.handle) else bun.invalid_fd,
};
}
};
pub const Css = struct {
pub const Entry = struct {};
pub const Result = struct {
ok: bool,
value: void,
};
pub fn parse(_: *@This(), _: *logger.Log, _: logger.Source) !Result {
Global.notimpl();
}
};
pub const JavaScript = struct {
pub const Result = js_ast.Result;
pub fn init(_: std.mem.Allocator) JavaScript {
return JavaScript{};
}
// For now, we're not going to cache JavaScript ASTs.
// It's probably only relevant when bundling for production.
pub fn parse(
_: *const @This(),
allocator: std.mem.Allocator,
opts: js_parser.Parser.Options,
defines: *Define,
log: *logger.Log,
source: *const logger.Source,
) anyerror!?js_ast.Result {
var temp_log = logger.Log.init(allocator);
temp_log.level = log.level;
var parser = js_parser.Parser.init(opts, &temp_log, source, defines, allocator) catch {
temp_log.appendToMaybeRecycled(log, source) catch {};
return null;
};
const result = parser.parse() catch |err| {
if (temp_log.errors == 0) {
log.addRangeError(source, parser.lexer.range(), @errorName(err)) catch unreachable;
}
temp_log.appendToMaybeRecycled(log, source) catch {};
return null;
};
temp_log.appendToMaybeRecycled(log, source) catch {};
return result;
}
pub fn scan(
_: *@This(),
allocator: std.mem.Allocator,
scan_pass_result: *js_parser.ScanPassResult,
opts: js_parser.Parser.Options,
defines: *Define,
log: *logger.Log,
source: *const logger.Source,
) anyerror!void {
if (strings.trim(source.contents, "\n\t\r ").len == 0) {
return;
}
var temp_log = logger.Log.init(allocator);
defer temp_log.appendToMaybeRecycled(log, source) catch {};
var parser = js_parser.Parser.init(opts, &temp_log, source, defines, allocator) catch return;
return try parser.scanImports(scan_pass_result);
}
};
pub const Json = struct {
pub fn init(_: std.mem.Allocator) Json {
return Json{};
}
fn parse(_: *@This(), log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator, comptime func: anytype, comptime force_utf8: bool) anyerror!?js_ast.Expr {
var temp_log = logger.Log.init(allocator);
defer {
temp_log.appendToMaybeRecycled(log, &source) catch {};
}
return func(&source, &temp_log, allocator, force_utf8) catch handler: {
break :handler null;
};
}
pub fn parseJSON(cache: *@This(), log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator) anyerror!?js_ast.Expr {
// tsconfig.* and jsconfig.* files are JSON files, but they are not valid JSON files.
// They are JSON files with comments and trailing commas.
// Sometimes tooling expects this to work.
if (source.path.isJSONCFile()) {
return try parse(cache, log, source, allocator, json_parser.parseTSConfig, true);
}
return try parse(cache, log, source, allocator, json_parser.parse, false);
}
pub fn parsePackageJSON(cache: *@This(), log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator, comptime force_utf8: bool) anyerror!?js_ast.Expr {
return try parse(cache, log, source, allocator, json_parser.parseTSConfig, force_utf8);
}
pub fn parseTSConfig(cache: *@This(), log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator) anyerror!?js_ast.Expr {
return try parse(cache, log, source, allocator, json_parser.parseTSConfig, true);
}
};