diff options
Diffstat (limited to 'src/bun.js/bindings')
| -rw-r--r-- | src/bun.js/bindings/bindings.zig | 57 | ||||
| -rw-r--r-- | src/bun.js/bindings/exports.zig | 4 |
2 files changed, 32 insertions, 29 deletions
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 3c100d66c..634ec9f39 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -88,9 +88,10 @@ pub const JSObject = extern struct { }; pub const ZigString = extern struct { - // TODO: align this to align(2) - // That would improve perf a bit - ptr: [*]const u8, + /// This can be a UTF-16, Latin1, or UTF-8 string. + /// The pointer itself is tagged, so it cannot be used without untagging it first + /// Accessing it directly is unsafe. + _unsafe_ptr_do_not_use: [*]const u8, len: usize, pub const ByteString = union(enum) { @@ -327,7 +328,7 @@ pub const ZigString = extern struct { } pub fn trunc(this: ZigString, len: usize) ZigString { - return .{ .ptr = this.ptr, .len = @min(len, this.len) }; + return .{ ._unsafe_ptr_do_not_use = this._unsafe_ptr_do_not_use, .len = @min(len, this.len) }; } pub fn eqlComptime(this: ZigString, comptime other: []const u8) bool { @@ -448,7 +449,7 @@ pub const ZigString = extern struct { pub const namespace = ""; pub inline fn is16Bit(this: *const ZigString) bool { - return (@ptrToInt(this.ptr) & (1 << 63)) != 0; + return (@ptrToInt(this._unsafe_ptr_do_not_use) & (1 << 63)) != 0; } pub inline fn utf16Slice(this: *const ZigString) []align(1) const u16 { @@ -458,7 +459,7 @@ pub const ZigString = extern struct { } } - return @ptrCast([*]align(1) const u16, untagged(this.ptr))[0..this.len]; + return @ptrCast([*]align(1) const u16, untagged(this._unsafe_ptr_do_not_use))[0..this.len]; } pub inline fn utf16SliceAligned(this: *const ZigString) []const u16 { @@ -468,7 +469,7 @@ pub const ZigString = extern struct { } } - return @ptrCast([*]const u16, @alignCast(@alignOf(u16), untagged(this.ptr)))[0..this.len]; + return @ptrCast([*]const u16, @alignCast(@alignOf(u16), untagged(this._unsafe_ptr_do_not_use)))[0..this.len]; } pub inline fn isEmpty(this: *const ZigString) bool { @@ -478,7 +479,7 @@ pub const ZigString = extern struct { pub fn fromStringPointer(ptr: StringPointer, buf: string, to: *ZigString) void { to.* = ZigString{ .len = ptr.length, - .ptr = buf[ptr.offset..][0..ptr.length].ptr, + ._unsafe_ptr_do_not_use = buf[ptr.offset..][0..ptr.length].ptr, }; } @@ -499,7 +500,7 @@ pub const ZigString = extern struct { } pub inline fn init(slice_: []const u8) ZigString { - return ZigString{ .ptr = slice_.ptr, .len = slice_.len }; + return ZigString{ ._unsafe_ptr_do_not_use = slice_.ptr, .len = slice_.len }; } pub fn initUTF8(slice_: []const u8) ZigString { @@ -518,7 +519,7 @@ pub const ZigString = extern struct { pub fn static(comptime slice_: []const u8) *const ZigString { const Holder = struct { - pub const value = ZigString{ .ptr = slice_.ptr, .len = slice_.len }; + pub const value = ZigString{ ._unsafe_ptr_do_not_use = slice_.ptr, .len = slice_.len }; }; return &Holder.value; @@ -543,7 +544,7 @@ pub const ZigString = extern struct { } pub fn init16(slice_: []const u16) ZigString { - var out = ZigString{ .ptr = std.mem.sliceAsBytes(slice_).ptr, .len = slice_.len }; + var out = ZigString{ ._unsafe_ptr_do_not_use = std.mem.sliceAsBytes(slice_).ptr, .len = slice_.len }; out.markUTF16(); return out; } @@ -585,15 +586,15 @@ pub const ZigString = extern struct { } pub fn isUTF8(this: ZigString) bool { - return (@ptrToInt(this.ptr) & (1 << 61)) != 0; + return (@ptrToInt(this._unsafe_ptr_do_not_use) & (1 << 61)) != 0; } pub fn markUTF8(this: *ZigString) void { - this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 61)); + this._unsafe_ptr_do_not_use = @intToPtr([*]const u8, @ptrToInt(this._unsafe_ptr_do_not_use) | (1 << 61)); } pub fn markUTF16(this: *ZigString) void { - this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 63)); + this._unsafe_ptr_do_not_use = @intToPtr([*]const u8, @ptrToInt(this._unsafe_ptr_do_not_use) | (1 << 63)); } pub fn setOutputEncoding(this: *ZigString) void { @@ -602,7 +603,7 @@ pub const ZigString = extern struct { } pub inline fn isGloballyAllocated(this: ZigString) bool { - return (@ptrToInt(this.ptr) & (1 << 62)) != 0; + return (@ptrToInt(this._unsafe_ptr_do_not_use) & (1 << 62)) != 0; } pub inline fn deinitGlobal(this: ZigString) void { @@ -612,7 +613,7 @@ pub const ZigString = extern struct { pub const mark = markGlobal; pub inline fn markGlobal(this: *ZigString) void { - this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 62)); + this._unsafe_ptr_do_not_use = @intToPtr([*]const u8, @ptrToInt(this._unsafe_ptr_do_not_use) | (1 << 62)); } pub fn format(self: ZigString, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { @@ -633,7 +634,7 @@ pub const ZigString = extern struct { return init(slice_).toValue(global).asRef(); } - pub const Empty = ZigString{ .ptr = "", .len = 0 }; + pub const Empty = ZigString{ ._unsafe_ptr_do_not_use = "", .len = 0 }; inline fn untagged(ptr: [*]const u8) [*]const u8 { // this can be null ptr, so long as it's also a 0 length string @@ -648,7 +649,7 @@ pub const ZigString = extern struct { } } - return untagged(this.ptr)[0..@min(this.len, std.math.maxInt(u32))]; + return untagged(this._unsafe_ptr_do_not_use)[0..@min(this.len, std.math.maxInt(u32))]; } pub fn dupe(this: ZigString, allocator: std.mem.Allocator) ![]const u8 { @@ -668,7 +669,7 @@ pub const ZigString = extern struct { } return Slice{ - .ptr = untagged(this.ptr), + .ptr = untagged(this._unsafe_ptr_do_not_use), .len = @truncate(u32, this.len), }; } @@ -687,7 +688,7 @@ pub const ZigString = extern struct { }; } - if (!this.isUTF8() and !strings.isAllASCII(untagged(this.ptr)[0..this.len])) { + if (!this.isUTF8() and !strings.isAllASCII(untagged(this._unsafe_ptr_do_not_use)[0..this.len])) { const buffer = this.toOwnedSlice(allocator) catch unreachable; return Slice{ .allocator = NullableAllocator.init(allocator), @@ -697,7 +698,7 @@ pub const ZigString = extern struct { } return Slice{ - .ptr = untagged(this.ptr), + .ptr = untagged(this._unsafe_ptr_do_not_use), .len = @truncate(u32, this.len), }; } @@ -727,7 +728,7 @@ pub const ZigString = extern struct { } return Slice{ - .ptr = untagged(this.ptr), + .ptr = untagged(this._unsafe_ptr_do_not_use), .len = @truncate(u32, this.len), }; } @@ -737,7 +738,7 @@ pub const ZigString = extern struct { } pub inline fn full(this: *const ZigString) []const u8 { - return untagged(this.ptr)[0..this.len]; + return untagged(this._unsafe_ptr_do_not_use)[0..this.len]; } pub fn trimmedSlice(this: *const ZigString) []const u8 { @@ -762,7 +763,9 @@ pub const ZigString = extern struct { inline fn assertGlobal(this: *const ZigString) void { if (comptime bun.Environment.allow_assert) { - std.debug.assert(this.len == 0 or bun.Mimalloc.mi_is_in_heap_region(untagged(this.ptr)) or bun.Mimalloc.mi_check_owned(untagged(this.ptr))); + std.debug.assert(this.len == 0 or + bun.Mimalloc.mi_is_in_heap_region(untagged(this._unsafe_ptr_do_not_use)) or + bun.Mimalloc.mi_check_owned(untagged(this._unsafe_ptr_do_not_use))); } } @@ -814,9 +817,9 @@ pub const ZigString = extern struct { } return if (this.is16Bit()) - C_API.JSStringCreateWithCharactersNoCopy(@ptrCast([*]const u16, @alignCast(@alignOf([*]const u16), untagged(this.ptr))), this.len) + C_API.JSStringCreateWithCharactersNoCopy(@ptrCast([*]const u16, @alignCast(@alignOf([*]const u16), untagged(this._unsafe_ptr_do_not_use))), this.len) else - C_API.JSStringCreateStatic(untagged(this.ptr), this.len); + C_API.JSStringCreateStatic(untagged(this._unsafe_ptr_do_not_use), this.len); } pub fn toErrorInstance(this: *const ZigString, global: *JSGlobalObject) JSValue { @@ -2682,7 +2685,7 @@ pub const JSGlobalObject = extern struct { str.markUTF8(); var err_value = str.toErrorInstance(this); this.vm().throwError(this, err_value); - this.bunVM().allocator.free(ZigString.untagged(str.ptr)[0..str.len]); + this.bunVM().allocator.free(ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]); } pub fn handleError( diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 3cc70144d..3b1f7e014 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -688,9 +688,9 @@ pub const ZigStackFrame = extern struct { }; pub const Zero: ZigStackFrame = ZigStackFrame{ - .function_name = ZigString{ .ptr = "", .len = 0 }, + .function_name = ZigString{ ._unsafe_ptr_do_not_use = "", .len = 0 }, .code_type = ZigStackFrameCode.None, - .source_url = ZigString{ .ptr = "", .len = 0 }, + .source_url = ZigString{ ._unsafe_ptr_do_not_use = "", .len = 0 }, .position = ZigStackFramePosition.Invalid, }; |
