diff options
Diffstat (limited to 'src/bun.zig')
-rw-r--r-- | src/bun.zig | 137 |
1 files changed, 135 insertions, 2 deletions
diff --git a/src/bun.zig b/src/bun.zig index ae987d4ec..f5231b72e 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -370,6 +370,29 @@ pub fn span(ptr: anytype) Span(@TypeOf(ptr)) { pub const IdentityContext = @import("./identity_context.zig").IdentityContext; pub const ArrayIdentityContext = @import("./identity_context.zig").ArrayIdentityContext; +pub const StringHashMapUnowned = struct { + pub const Key = struct { + hash: u64, + len: usize, + + pub fn init(str: []const u8) Key { + return Key{ + .hash = hash(str), + .len = str.len, + }; + } + }; + + pub const Adapter = struct { + pub fn eql(_: @This(), a: Key, b: Key) bool { + return a.hash == b.hash and a.len == b.len; + } + + pub fn hash(_: @This(), key: Key) u64 { + return key.hash; + } + }; +}; pub const BabyList = @import("./baby_list.zig").BabyList; pub const ByteList = BabyList(u8); @@ -498,6 +521,11 @@ pub fn hash(content: []const u8) u64 { return std.hash.Wyhash.hash(0, content); } +pub fn hash32(content: []const u8) u32 { + const res = hash(content); + return @truncate(u32, res); +} + pub const HiveArray = @import("./hive_array.zig").HiveArray; pub fn rand(bytes: []u8) void { @@ -660,6 +688,27 @@ pub const StringArrayHashMapContext = struct { pub fn eql(_: @This(), a: []const u8, b: []const u8, _: usize) bool { return strings.eqlLong(a, b, true); } + + pub fn pre(input: []const u8) Prehashed { + return Prehashed{ + .value = @This().hash(.{}, input), + .input = input, + }; + } + + pub const Prehashed = struct { + value: u32, + input: []const u8, + pub fn hash(this: @This(), s: []const u8) u32 { + if (s.ptr == this.input.ptr and s.len == this.input.len) + return this.value; + return @truncate(u32, std.hash.Wyhash.hash(0, s)); + } + + pub fn eql(_: @This(), a: []const u8, b: []const u8) bool { + return strings.eqlLong(a, b, true); + } + }; }; pub const StringHashMapContext = struct { @@ -670,13 +719,20 @@ pub const StringHashMapContext = struct { return strings.eqlLong(a, b, true); } + pub fn pre(input: []const u8) Prehashed { + return Prehashed{ + .value = @This().hash(.{}, input), + .input = input, + }; + } + pub const Prehashed = struct { value: u64, input: []const u8, pub fn hash(this: @This(), s: []const u8) u64 { if (s.ptr == this.input.ptr and s.len == this.input.len) return this.value; - return std.hash.Wyhash.hash(0, s); + return StringHashMapContext.hash(.{}, s); } pub fn eql(_: @This(), a: []const u8, b: []const u8) bool { @@ -797,7 +853,7 @@ pub const js_printer = @import("./js_printer.zig"); pub const js_lexer = @import("./js_lexer.zig"); pub const JSON = @import("./json_parser.zig"); pub const JSAst = @import("./js_ast.zig"); -pub const bit_set = @import("./install/bit_set.zig"); +pub const bit_set = @import("./bit_set.zig"); pub fn enumMap(comptime T: type, comptime args: anytype) (fn (T) []const u8) { const Map = struct { @@ -1001,6 +1057,19 @@ pub fn cstring(input: []const u8) [:0]const u8 { } pub const Semver = @import("./install/semver.zig"); +pub const ImportRecord = @import("./import_record.zig").ImportRecord; +pub const ImportKind = @import("./import_record.zig").ImportKind; + +pub usingnamespace @import("./util.zig"); +pub const fast_debug_build_cmd = .None; +pub const fast_debug_build_mode = fast_debug_build_cmd != .None and + Environment.isDebug; + +pub const MultiArrayList = @import("./multi_array_list.zig").MultiArrayList; + +pub const Joiner = @import("./string_joiner.zig"); +pub const renamer = @import("./renamer.zig"); +pub const sourcemap = @import("./sourcemap/sourcemap.zig"); pub fn asByteSlice(buffer: anytype) []const u8 { return switch (@TypeOf(buffer)) { @@ -1017,6 +1086,68 @@ pub fn asByteSlice(buffer: anytype) []const u8 { }; } +comptime { + if (fast_debug_build_cmd != .RunCommand and fast_debug_build_mode) { + _ = @import("./bun.js/node/buffer.zig").BufferVectorized.fill; + _ = @import("./cli/upgrade_command.zig").Version; + } +} + +pub fn DebugOnlyDisabler(comptime Type: type) type { + return struct { + const T = Type; + threadlocal var disable_create_in_debug: if (Environment.allow_assert) usize else u0 = 0; + pub inline fn disable() void { + if (comptime !Environment.allow_assert) return; + disable_create_in_debug += 1; + } + + pub inline fn enable() void { + if (comptime !Environment.allow_assert) return; + disable_create_in_debug -= 1; + } + + pub inline fn assert() void { + if (comptime !Environment.allow_assert) return; + if (disable_create_in_debug > 0) { + Output.panic(comptime "[" ++ @typeName(T) ++ "] called while disabled (did you forget to call enable?)", .{}); + } + } + }; +} + +const FailingAllocator = struct { + fn alloc(_: *anyopaque, _: usize, _: u8, _: usize) ?[*]u8 { + if (comptime Environment.allow_assert) { + unreachablePanic("FailingAllocator should never be reached. This means some memory was not defined", .{}); + } + return null; + } + + fn resize(_: *anyopaque, _: []u8, _: u8, _: usize, _: usize) bool { + if (comptime Environment.allow_assert) { + unreachablePanic("FailingAllocator should never be reached. This means some memory was not defined", .{}); + } + return false; + } + + fn free( + _: *anyopaque, + _: []u8, + _: u8, + _: usize, + ) void { + unreachable; + } +}; + +/// When we want to avoid initializing a value as undefined, we can use this allocator +pub const failing_allocator = std.mem.Allocator{ .ptr = undefined, .vtable = &.{ + .alloc = FailingAllocator.alloc, + .resize = FailingAllocator.resize, + .free = FailingAllocator.free, +} }; + /// Reload Bun's process /// /// This clones envp, argv, and gets the current executable path @@ -1094,3 +1225,5 @@ pub fn reloadProcess( } } pub var auto_reload_on_crash = false; + +pub const options = @import("./options.zig"); |