Skip to content

Commit

Permalink
refactor: isolate root.C
Browse files Browse the repository at this point in the history
  • Loading branch information
fel1x-developer committed Jan 13, 2025
1 parent acae4a3 commit a7a090d
Show file tree
Hide file tree
Showing 126 changed files with 709 additions and 659 deletions.
2 changes: 1 addition & 1 deletion misctools/features.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;
const Features = @import("../src/analytics/analytics_thread.zig").Features;

// zig run --main-pkg-path ../ ./features.zig
Expand Down
2 changes: 1 addition & 1 deletion misctools/http_bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;
const clap = @import("../src/deps/zig-clap/clap.zig");

const URL = @import("../src/url.zig").URL;
Expand Down
2 changes: 1 addition & 1 deletion misctools/machbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;
const clap = @import("../src/deps/zig-clap/clap.zig");

const URL = @import("../src/url.zig").URL;
Expand Down
2 changes: 1 addition & 1 deletion misctools/readlink-getfd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;

// zig build-exe -Doptimize=ReleaseFast --main-pkg-path ../ ./readlink-getfd.zig
pub fn main() anyerror!void {
Expand Down
2 changes: 1 addition & 1 deletion misctools/readlink-realpath.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;

// zig build-exe -Doptimize=ReleaseFast --main-pkg-path ../ ./readlink-getfd.zig
pub fn main() anyerror!void {
Expand Down
2 changes: 1 addition & 1 deletion misctools/tgz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const C = @import("root").C;

const Archive = @import("../src/libarchive/libarchive.zig").Archive;
const Zlib = @import("../src/zlib.zig");
Expand Down
2 changes: 1 addition & 1 deletion root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub const completions = struct {
};

pub const JavaScriptCore = @import("./src/jsc.zig");
pub const C = @import("./src/c.zig");
pub const C = @import("./src/C.zig");
22 changes: 11 additions & 11 deletions src/c.zig → src/C.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const std = @import("std");
const bun = @import("root").bun;
const Environment = @import("./env.zig");
const C = @import("root").C;
const Environment = bun.Environment;

pub const translated = @import("translated-c-headers");

const PlatformSpecific = switch (Environment.os) {
.mac => @import("./darwin_c.zig"),
.linux => @import("./linux_c.zig"),
.windows => @import("./windows_c.zig"),
.mac => @import("C/darwin.zig"),
.linux => @import("C/linux.zig"),
.windows => @import("C/windows.zig"),
else => struct {},
};
pub usingnamespace PlatformSpecific;

const C = std.c;
const builtin = @import("builtin");
const posix = std.posix;
const mem = std.mem;
Expand All @@ -25,8 +25,8 @@ const mode_t = bun.Mode;
const libc_stat = bun.Stat;

const zeroes = mem.zeroes;
pub const darwin = @import("./darwin_c.zig");
pub const linux = @import("./linux_c.zig");
pub const darwin = @import("C/darwin.zig");
pub const linux = @import("C/linux.zig");
pub extern "c" fn chmod([*c]const u8, mode_t) c_int;
pub extern "c" fn fchmod(std.c.fd_t, mode_t) c_int;
pub extern "c" fn fchmodat(c_int, [*c]const u8, mode_t, c_int) c_int;
Expand All @@ -50,7 +50,7 @@ pub fn lstat_absolute(path: [:0]const u8) !Stat {
}

var st = zeroes(libc_stat);
switch (errno(bun.C.lstat(path.ptr, &st))) {
switch (errno(C.lstat(path.ptr, &st))) {
.SUCCESS => {},
.NOENT => return error.FileNotFound,
// .EINVAL => unreachable,
Expand Down Expand Up @@ -167,9 +167,9 @@ pub fn copyFileZSlowWithHandle(in_handle: bun.FileDescriptor, to_dir: bun.FileDe
};
const src_len = bun.windows.GetFinalPathNameByHandleW(in_handle.cast(), &buf1, buf1.len, 0);
if (src_len == 0) {
return Maybe(void).errno(bun.C.E.BUSY, .GetFinalPathNameByHandle);
return Maybe(void).errno(C.E.BUSY, .GetFinalPathNameByHandle);
} else if (src_len >= buf1.len) {
return Maybe(void).errno(bun.C.E.NAMETOOLONG, .GetFinalPathNameByHandle);
return Maybe(void).errno(C.E.NAMETOOLONG, .GetFinalPathNameByHandle);
}
const src = buf1[0..src_len :0];
return bun.copyFile(src, dest);
Expand Down Expand Up @@ -501,5 +501,5 @@ pub const geteuid = translated.geteuid;
pub const getpwuid_r = translated.getpwuid_r;

export fn Bun__errnoName(err: c_int) ?[*:0]const u8 {
return @tagName(bun.C.SystemErrno.init(err) orelse return null);
return @tagName(C.SystemErrno.init(err) orelse return null);
}
File renamed without changes.
5 changes: 3 additions & 2 deletions src/linux_c.zig → src/C/linux.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const bun = @import("root").bun;
const C = @import("root").C;
pub extern "C" fn memmem(haystack: [*]const u8, haystacklen: usize, needle: [*]const u8, needlelen: usize) ?[*]const u8;
pub const SystemErrno = enum(u8) {
SUCCESS = 0,
Expand Down Expand Up @@ -619,14 +620,14 @@ const posix_spawn_file_actions_addchdir_np_type = *const fn (actions: *posix_spa

/// When not available, these functions will return 0.
pub fn posix_spawn_file_actions_addfchdir_np(actions: *posix_spawn_file_actions_t, filedes: std.posix.fd_t) c_int {
const function = bun.C.dlsym(posix_spawn_file_actions_addfchdir_np_type, "posix_spawn_file_actions_addfchdir_np") orelse
const function = C.dlsym(posix_spawn_file_actions_addfchdir_np_type, "posix_spawn_file_actions_addfchdir_np") orelse
return 0;
return function(actions, filedes);
}

/// When not available, these functions will return 0.
pub fn posix_spawn_file_actions_addchdir_np(actions: *posix_spawn_file_actions_t, path: [*:0]const u8) c_int {
const function = bun.C.dlsym(posix_spawn_file_actions_addchdir_np_type, "posix_spawn_file_actions_addchdir_np") orelse
const function = C.dlsym(posix_spawn_file_actions_addchdir_np_type, "posix_spawn_file_actions_addchdir_np") orelse
return 0;
return function(actions, path);
}
Expand Down
5 changes: 3 additions & 2 deletions src/windows_c.zig → src/C/windows.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const bun = @import("root").bun;
const C = @import("root").C;
const builtin = @import("builtin");
const win32 = std.os.windows;
const posix = std.posix;
Expand Down Expand Up @@ -1067,7 +1068,7 @@ pub const UV_EUNATCH = -uv.UV_EUNATCH;
pub const off_t = i64;
pub fn preallocate_file(_: posix.fd_t, _: off_t, _: off_t) !void {}

const uv = @import("./deps/libuv.zig");
const uv = @import("../deps/libuv.zig");

pub const E = enum(u16) {
SUCCESS = 0,
Expand Down Expand Up @@ -1437,7 +1438,7 @@ pub fn moveOpenedFileAt(
var rename_info_buf: [struct_buf_len]u8 align(@alignOf(w.FILE_RENAME_INFORMATION_EX)) = undefined;

const struct_len = @sizeOf(w.FILE_RENAME_INFORMATION_EX) - 1 + new_file_name.len * 2;
if (struct_len > struct_buf_len) return Maybe(void).errno(bun.C.E.NAMETOOLONG, .NtSetInformationFile);
if (struct_len > struct_buf_len) return Maybe(void).errno(C.E.NAMETOOLONG, .NtSetInformationFile);

const rename_info = @as(*w.FILE_RENAME_INFORMATION_EX, @ptrCast(&rename_info_buf));
var io_status_block: w.IO_STATUS_BLOCK = undefined;
Expand Down
3 changes: 2 additions & 1 deletion src/Global.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const use_mimalloc = bun.use_mimalloc;
const StringTypes = @import("./string_types.zig");
const Mimalloc = bun.Mimalloc;
const bun = @import("root").bun;
const C = @import("root").C;

const version_string = Environment.version_string;

Expand Down Expand Up @@ -119,7 +120,7 @@ pub fn exit(code: u32) noreturn {
Bun__onExit();
std.os.windows.kernel32.ExitProcess(code);
},
else => bun.C.quick_exit(@bitCast(code)),
else => C.quick_exit(@bitCast(code)),
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/StandaloneModuleGraph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! But this incurred a fixed 350ms overhead on every build, which is unacceptable
//! so we give up on codesigning support on macOS for now until we can find a better solution
const bun = @import("root").bun;
const C = @import("root").C;
const std = @import("std");
const Schema = bun.Schema.Api;
const strings = bun.strings;
Expand Down Expand Up @@ -638,7 +639,7 @@ pub const StandaloneModuleGraph = struct {
// the final 8 bytes in the file are the length of the module graph with padding, excluding the trailer and offsets
_ = Syscall.write(cloned_executable_fd, std.mem.asBytes(&total_byte_count));
if (comptime !Environment.isWindows) {
_ = bun.C.fchmod(cloned_executable_fd.int(), 0o777);
_ = C.fchmod(cloned_executable_fd.int(), 0o777);
}

if (Environment.isWindows and inject_options.windows_hide_console) {
Expand Down Expand Up @@ -709,14 +710,14 @@ pub const StandaloneModuleGraph = struct {
break :brk outfile_buf_u16[0..outfile_w.len :0];
};

bun.C.moveOpenedFileAtLoose(fd, bun.toFD(root_dir.fd), outfile_slice, true).unwrap() catch |err| {
C.moveOpenedFileAtLoose(fd, bun.toFD(root_dir.fd), outfile_slice, true).unwrap() catch |err| {
if (err == error.EISDIR) {
Output.errGeneric("{} is a directory. Please choose a different --outfile or delete the directory", .{bun.fmt.utf16(outfile_slice)});
} else {
Output.err(err, "failed to move executable to result path", .{});
}

_ = bun.C.deleteOpenedFile(fd);
_ = C.deleteOpenedFile(fd);

Global.exit(1);
};
Expand Down Expand Up @@ -761,7 +762,7 @@ pub const StandaloneModuleGraph = struct {
}
}

bun.C.moveFileZWithHandle(
C.moveFileZWithHandle(
fd,
bun.FD.cwd(),
bun.sliceTo(&(try std.posix.toPosixPath(temp_location)), 0),
Expand Down
2 changes: 1 addition & 1 deletion src/analytics/analytics_thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const FeatureFlags = bun.FeatureFlags;
const C = bun.C;
const C = @import("root").C;

const sync = @import("../sync.zig");
const std = @import("std");
Expand Down
7 changes: 4 additions & 3 deletions src/async/posix_event_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Output = bun.Output;
const JSC = bun.JSC;
const uws = bun.uws;
const Environment = bun.Environment;
const C = @import("root").C;
const std = @import("std");

pub const Loop = uws.Loop;
Expand Down Expand Up @@ -902,7 +903,7 @@ pub const FilePoll = struct {
&timeout,
);

if (bun.C.getErrno(rc) == .INTR) continue;
if (C.getErrno(rc) == .INTR) continue;
break :rc rc;
}
};
Expand All @@ -919,7 +920,7 @@ pub const FilePoll = struct {
// indicate the error condition.
}

const errno = bun.C.getErrno(rc);
const errno = C.getErrno(rc);

if (errno != .SUCCESS) {
this.deactivate(loop);
Expand Down Expand Up @@ -1071,7 +1072,7 @@ pub const FilePoll = struct {
// indicate the error condition.
}

const errno = bun.C.getErrno(rc);
const errno = C.getErrno(rc);
switch (rc) {
std.math.minInt(@TypeOf(rc))...-1 => return JSC.Maybe(void).errnoSys(@intFromEnum(errno), .kevent).?,
else => {},
Expand Down
93 changes: 48 additions & 45 deletions src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@
//! adjusting imports) must always rebundle only that one file.
//!
//! All work is held in-memory, using manually managed data-oriented design.

const std = @import("std");
const Allocator = std.mem.Allocator;
const Mutex = bun.Mutex;
const ArrayListUnmanaged = std.ArrayListUnmanaged;
const AutoArrayHashMapUnmanaged = std.AutoArrayHashMapUnmanaged;

const bun = @import("root").bun;
const Environment = bun.Environment;
const assert = bun.assert;
const DynamicBitSetUnmanaged = bun.bit_set.DynamicBitSetUnmanaged;

const bake = bun.bake;
const FrameworkRouter = bake.FrameworkRouter;
const Route = FrameworkRouter.Route;
const OpaqueFileId = FrameworkRouter.OpaqueFileId;

const Log = bun.logger.Log;
const Output = bun.Output;

const Transpiler = bun.transpiler.Transpiler;
const BundleV2 = bun.bundle_v2.BundleV2;

const Define = bun.options.Define;
const OutputFile = bun.options.OutputFile;

const uws = bun.uws;
const App = uws.NewApp(false);
const AnyWebSocket = uws.AnyWebSocket;
const Request = uws.Request;
const Response = App.Response;

const MimeType = bun.http.MimeType;

const JSC = bun.JSC;
const Watcher = bun.JSC.Watcher;
const JSValue = JSC.JSValue;
const VirtualMachine = JSC.VirtualMachine;
const JSModuleLoader = JSC.JSModuleLoader;
const EventLoopHandle = JSC.EventLoopHandle;
const JSInternalPromise = JSC.JSInternalPromise;

const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const Chunk = bun.bundle_v2.Chunk;

const C = @import("root").C;

pub const DevServer = @This();
pub const debug = bun.Output.Scoped(.Bake, false);
pub const igLog = bun.Output.scoped(.IncrementalGraph, false);
Expand Down Expand Up @@ -4258,7 +4305,7 @@ pub fn onFileUpdate(dev: *DevServer, events: []Watcher.Event, changed_files: []?

pub fn onWatchError(_: *DevServer, err: bun.sys.Error) void {
// TODO: how to recover? the watcher can't just ... crash????????
Output.err(@as(bun.C.E, @enumFromInt(err.errno)), "Watcher crashed", .{});
Output.err(@as(C.E, @enumFromInt(err.errno)), "Watcher crashed", .{});
if (bun.Environment.isDebug) {
bun.todoPanic(@src(), "Watcher crash", .{});
}
Expand Down Expand Up @@ -4443,47 +4490,3 @@ pub const EntryPointList = struct {
}
}
};

const std = @import("std");
const Allocator = std.mem.Allocator;
const Mutex = bun.Mutex;
const ArrayListUnmanaged = std.ArrayListUnmanaged;
const AutoArrayHashMapUnmanaged = std.AutoArrayHashMapUnmanaged;

const bun = @import("root").bun;
const Environment = bun.Environment;
const assert = bun.assert;
const DynamicBitSetUnmanaged = bun.bit_set.DynamicBitSetUnmanaged;

const bake = bun.bake;
const FrameworkRouter = bake.FrameworkRouter;
const Route = FrameworkRouter.Route;
const OpaqueFileId = FrameworkRouter.OpaqueFileId;

const Log = bun.logger.Log;
const Output = bun.Output;

const Transpiler = bun.transpiler.Transpiler;
const BundleV2 = bun.bundle_v2.BundleV2;

const Define = bun.options.Define;
const OutputFile = bun.options.OutputFile;

const uws = bun.uws;
const App = uws.NewApp(false);
const AnyWebSocket = uws.AnyWebSocket;
const Request = uws.Request;
const Response = App.Response;

const MimeType = bun.http.MimeType;

const JSC = bun.JSC;
const Watcher = bun.JSC.Watcher;
const JSValue = JSC.JSValue;
const VirtualMachine = JSC.VirtualMachine;
const JSModuleLoader = JSC.JSModuleLoader;
const EventLoopHandle = JSC.EventLoopHandle;
const JSInternalPromise = JSC.JSInternalPromise;

const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const Chunk = bun.bundle_v2.Chunk;
Loading

0 comments on commit a7a090d

Please sign in to comment.