Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig: make JSValue.toBunString use JSError #15295

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bake/FrameworkRouter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ pub const JSFrameworkRouter = struct {

pub fn match(jsfr: *JSFrameworkRouter, global: *JSGlobalObject, callframe: *JSC.CallFrame) !JSValue {
const path_js = callframe.argumentsAsArray(1)[0];
const path_str = try path_js.toBunString2(global);
const path_str = try path_js.toBunString(global);
defer path_str.deref();
const path_slice = path_str.toSlice(bun.default_allocator);
defer path_slice.deinit();
Expand Down
6 changes: 3 additions & 3 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub const Framework = struct {
) !Framework {
_ = bundler_options; // autofix
if (opts.isString()) {
const str = try opts.toBunString2(global);
const str = try opts.toBunString(global);
defer str.deref();

// Deprecated
Expand Down Expand Up @@ -303,7 +303,7 @@ pub const Framework = struct {
return global.throwInvalidArguments("'framework.reactFastRefresh' is missing 'importSource'", .{});
};

const str = try prop.toBunString2(global);
const str = try prop.toBunString(global);
defer str.deref();

break :brk .{
Expand Down Expand Up @@ -556,7 +556,7 @@ fn getOptionalString(
return null;
if (value == .undefined or value == .null)
return null;
const str = try value.toBunString2(global);
const str = try value.toBunString(global);
return allocations.track(str.toUTF8(arena));
}

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ pub const TablePrinter = struct {
var properties_iter = JSC.JSArrayIterator.init(this.properties, globalObject);
while (properties_iter.next()) |value| {
try columns.append(.{
.name = value.toBunString(globalObject),
.name = try value.toBunString(globalObject),
});
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub fn shellEscape(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b
}

const jsval = arguments.ptr[0];
const bunstr = jsval.toBunString(globalThis);
const bunstr = try jsval.toBunString(globalThis);
if (globalThis.hasException()) return .zero;
defer bunstr.deref();

Expand Down Expand Up @@ -299,7 +299,7 @@ pub fn braces(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JS
globalThis.throw("braces: expected at least 1 argument, got 0", .{});
return .zero;
};
const brace_str = brace_str_js.toBunString(globalThis);
const brace_str = try brace_str_js.toBunString(globalThis);
defer brace_str.deref();
if (globalThis.hasException()) return .zero;

Expand Down Expand Up @@ -914,9 +914,9 @@ fn doResolve(globalThis: *JSC.JSGlobalObject, arguments: []const JSValue) bun.JS
}
}

const specifier_str = specifier.toBunString(globalThis);
const specifier_str = try specifier.toBunString(globalThis);
defer specifier_str.deref();
const from_str = from.toBunString(globalThis);
const from_str = try from.toBunString(globalThis);
defer from_str.deref();
return doResolveWithArgs(
globalThis,
Expand Down Expand Up @@ -996,10 +996,10 @@ pub fn resolve(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun
}

export fn Bun__resolve(global: *JSGlobalObject, specifier: JSValue, source: JSValue, is_esm: bool) JSC.JSValue {
const specifier_str = specifier.toBunString(global);
const specifier_str = specifier.toBunString(global) catch return .zero;
defer specifier_str.deref();

const source_str = source.toBunString(global);
const source_str = source.toBunString(global) catch return .zero;
defer source_str.deref();

const value = doResolveWithArgs(global, specifier_str, source_str, is_esm, true) catch {
Expand All @@ -1011,10 +1011,10 @@ export fn Bun__resolve(global: *JSGlobalObject, specifier: JSValue, source: JSVa
}

export fn Bun__resolveSync(global: *JSGlobalObject, specifier: JSValue, source: JSValue, is_esm: bool) JSC.JSValue {
const specifier_str = specifier.toBunString(global);
const specifier_str = specifier.toBunString(global) catch return .zero;
defer specifier_str.deref();

const source_str = source.toBunString(global);
const source_str = source.toBunString(global) catch return .zero;
defer source_str.deref();

return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier_str, source_str, is_esm, true));
Expand All @@ -1026,7 +1026,7 @@ export fn Bun__resolveSyncWithStrings(global: *JSGlobalObject, specifier: *bun.S
}

export fn Bun__resolveSyncWithSource(global: *JSGlobalObject, specifier: JSValue, source: *bun.String, is_esm: bool) JSC.JSValue {
const specifier_str = specifier.toBunString(global);
const specifier_str = specifier.toBunString(global) catch return .zero;
defer specifier_str.deref();
return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier_str, source.*, is_esm, true));
}
Expand Down Expand Up @@ -4306,7 +4306,7 @@ fn stringWidth(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun
return JSC.jsNumber(0);
}

const str = value.toBunString(globalObject);
const str = try value.toBunString(globalObject);
defer str.deref();

var count_ansi_escapes = false;
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
if (!kind.isStringLike()) {
tsconfig.jsonStringify(globalThis, 0, &out);
} else {
out = tsconfig.toBunString(globalThis);
out = try tsconfig.toBunString(globalThis);
}

if (out.isEmpty()) break :tsconfig;
Expand Down Expand Up @@ -486,7 +486,7 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
if (is_object) {
macros.jsonStringify(globalThis, 0, &out);
} else {
out = macros.toBunString(globalThis);
out = try macros.toBunString(globalThis);
}

if (out.isEmpty()) break :macros;
Expand Down
14 changes: 7 additions & 7 deletions src/bun.js/api/bun/udp_socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub const UDPSocketConfig = struct {
if (!value.isString()) {
return globalThis.throwInvalidArguments("Expected \"hostname\" to be a string", .{});
}
const str = value.toBunString(globalThis);
const str = try value.toBunString(globalThis);
defer str.deref();
break :brk str.toOwnedSliceZ(default_allocator) catch bun.outOfMemory();
} else {
Expand Down Expand Up @@ -209,7 +209,7 @@ pub const UDPSocketConfig = struct {
};
const connect_port = connect_port_js.coerceToInt32(globalThis);

const str = connect_host_js.toBunString(globalThis);
const str = try connect_host_js.toBunString(globalThis);
defer str.deref();
const connect_host = str.toOwnedSliceZ(default_allocator) catch bun.outOfMemory();

Expand Down Expand Up @@ -418,7 +418,7 @@ pub const UDPSocket = struct {
continue;
}
if (i % 3 == 2) {
if (!this.parseAddr(globalThis, port, val, &addrs[slice_idx])) {
if (!try this.parseAddr(globalThis, port, val, &addrs[slice_idx])) {
return globalThis.throwInvalidArguments("Invalid address", .{});
}
addr_ptrs[slice_idx] = &addrs[slice_idx];
Expand Down Expand Up @@ -482,7 +482,7 @@ pub const UDPSocket = struct {
var addr: std.posix.sockaddr.storage = std.mem.zeroes(std.posix.sockaddr.storage);
const addr_ptr = brk: {
if (dst) |dest| {
if (!this.parseAddr(globalThis, dest.port, dest.address, &addr)) {
if (!try this.parseAddr(globalThis, dest.port, dest.address, &addr)) {
return globalThis.throwInvalidArguments("Invalid address", .{});
}
break :brk &addr;
Expand All @@ -505,12 +505,12 @@ pub const UDPSocket = struct {
port_val: JSValue,
address_val: JSValue,
storage: *std.posix.sockaddr.storage,
) bool {
) bun.JSError!bool {
_ = this;
const number = port_val.coerceToInt32(globalThis);
const port: u16 = if (number < 1 or number > 0xffff) 0 else @intCast(number);

const str = address_val.toBunString(globalThis);
const str = try address_val.toBunString(globalThis);
defer str.deref();
const address_slice = str.toOwnedSliceZ(default_allocator) catch bun.outOfMemory();
defer default_allocator.free(address_slice);
Expand Down Expand Up @@ -685,7 +685,7 @@ pub const UDPSocket = struct {
return globalThis.throwInvalidArguments("Expected 2 arguments", .{});
}

const str = args.ptr[0].toBunString(globalThis);
const str = try args.ptr[0].toBunString(globalThis);
defer str.deref();
const connect_host = str.toOwnedSliceZ(default_allocator) catch bun.outOfMemory();
defer default_allocator.free(connect_host);
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ pub const BinaryType = enum(u4) {

pub fn fromJSValue(globalThis: *JSC.JSGlobalObject, input: JSValue) bun.JSError!?BinaryType {
if (input.isString()) {
return Map.getWithEql(try input.toBunString2(globalThis), bun.String.eqlComptime);
return Map.getWithEql(try input.toBunString(globalThis), bun.String.eqlComptime);
}

return null;
Expand Down
24 changes: 10 additions & 14 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5065,16 +5065,10 @@ pub const JSValue = enum(i64) {
}

/// Increments the reference count, you must call `.deref()` or it will leak memory.
/// Returns String.Dead on error. Deprecated in favor of `toBunString2`
pub fn toBunString(this: JSValue, globalObject: *JSC.JSGlobalObject) bun.String {
pub fn toBunString(this: JSValue, globalObject: *JSC.JSGlobalObject) JSError!bun.String {
return bun.String.fromJS(this, globalObject);
}

/// Increments the reference count, you must call `.deref()` or it will leak memory.
pub fn toBunString2(this: JSValue, globalObject: *JSC.JSGlobalObject) JSError!bun.String {
return bun.String.fromJS2(this, globalObject);
}

/// this: RegExp value
/// other: string value
pub fn toMatch(this: JSValue, global: *JSGlobalObject, other: JSValue) bool {
Expand Down Expand Up @@ -5127,7 +5121,7 @@ pub const JSValue = enum(i64) {
});
}

/// Deprecated: replace with 'toBunString2'
/// Deprecated: replace with 'toBunString'
pub inline fn getZigString(this: JSValue, global: *JSGlobalObject) ZigString {
var str = ZigString.init("");
this.toZigString(&str, global);
Expand All @@ -5142,7 +5136,7 @@ pub const JSValue = enum(i64) {
///
/// To handle exceptions, use `JSValue.toSliceOrNull`.
pub inline fn toSlice(this: JSValue, global: *JSGlobalObject, allocator: std.mem.Allocator) ZigString.Slice {
const str = bun.String.fromJS(this, global);
const str = bun.String.fromJS_unsafe(this, global);
defer str.deref();

// This keeps the WTF::StringImpl alive if it was originally a latin1
Expand All @@ -5155,7 +5149,7 @@ pub const JSValue = enum(i64) {
/// Convert a JSValue to a string, potentially calling `toString` on the
/// JSValue in JavaScript. Can throw an error.
pub fn toSlice2(this: JSValue, global: *JSGlobalObject, allocator: std.mem.Allocator) JSError!ZigString.Slice {
const str = try bun.String.fromJS2(this, global);
const str = try bun.String.fromJS(this, global);
defer str.deref();

// This keeps the WTF::StringImpl alive if it was originally a latin1
Expand Down Expand Up @@ -5185,14 +5179,14 @@ pub const JSValue = enum(i64) {

/// Call `toString()` on the JSValue and clone the result.
pub fn toSliceOrNull(this: JSValue, globalThis: *JSGlobalObject) bun.JSError!ZigString.Slice {
const str = try bun.String.fromJS2(this, globalThis);
const str = try bun.String.fromJS(this, globalThis);
defer str.deref();
return str.toUTF8(bun.default_allocator);
}

/// Call `toString()` on the JSValue and clone the result.
pub fn toSliceOrNullWithAllocator(this: JSValue, globalThis: *JSGlobalObject, allocator: std.mem.Allocator) bun.JSError!ZigString.Slice {
const str = try bun.String.fromJS2(this, globalThis);
const str = try bun.String.fromJS(this, globalThis);
defer str.deref();
return str.toUTF8(allocator);
}
Expand Down Expand Up @@ -5512,7 +5506,7 @@ pub const JSValue = enum(i64) {
return error.JSError;
}

const str = prop.toBunString(global);
const str = try prop.toBunString(global);
if (global.hasException()) {
str.deref();
return error.JSError;
Expand Down Expand Up @@ -5829,7 +5823,7 @@ pub const JSValue = enum(i64) {
globalObject: *JSC.JSGlobalObject,

pub fn format(this: StringFormatter, comptime text: []const u8, opts: std.fmt.FormatOptions, writer: anytype) !void {
const str = this.value.toBunString(this.globalObject);
const str = try this.value.toBunString(this.globalObject);
defer str.deref();
try str.format(text, opts, writer);
}
Expand Down Expand Up @@ -6754,6 +6748,7 @@ pub const CallFrame = opaque {
pub fn arguments_old(self: *const CallFrame, comptime max: usize) Arguments(max) {
const len = self.argumentsCount();
const ptr = self.argumentsPtr();
if (max > 10) @compileError(std.fmt.comptimePrint("arguments({d})", .{max}));
return switch (@as(u4, @min(len, max))) {
0 => .{ .ptr = undefined, .len = 0 },
inline 1...10 => |count| Arguments(max).init(comptime @min(count, max), ptr),
Expand All @@ -6764,6 +6759,7 @@ pub const CallFrame = opaque {
pub fn argumentsUndef(self: *const CallFrame, comptime max: usize) Arguments(max) {
const len = self.argumentsCount();
const ptr = self.argumentsPtr();
if (max > 9) @compileError(std.fmt.comptimePrint("arguments({d})", .{max}));
return switch (@as(u4, @min(len, max))) {
0 => .{ .ptr = .{.undefined} ** max, .len = 0 },
inline 1...9 => |count| Arguments(max).initUndef(@min(count, max), ptr),
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1365,11 +1365,10 @@ pub const VirtualMachine = struct {

pub fn specifierIsEvalEntryPoint(this: *VirtualMachine, specifier: JSValue) callconv(.C) bool {
if (this.module_loader.eval_source) |eval_source| {
var specifier_str = specifier.toBunString(this.global);
var specifier_str = specifier.toBunString(this.global) catch return false;
defer specifier_str.deref();
return specifier_str.eqlUTF8(eval_source.path.text);
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ pub const Arguments = struct {
// will automatically be normalized to absolute path.
if (next_val.isString()) {
arguments.eat();
var str = next_val.toBunString(ctx);
var str = try next_val.toBunString(ctx);
defer str.deref();
if (str.eqlComptime("dir")) break :link_type .dir;
if (str.eqlComptime("file")) break :link_type .file;
Expand Down
10 changes: 5 additions & 5 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub const StringOrBuffer = union(enum) {
return try allocator.dupe(u8, array_buffer.byteSlice());
}

const str = try bun.String.fromJS2(value, globalObject);
const str = try bun.String.fromJS(value, globalObject);
defer str.deref();

const result = try str.toOwnedSlice(allocator);
Expand Down Expand Up @@ -534,7 +534,7 @@ pub const StringOrBuffer = union(enum) {
.StringObject,
.DerivedStringObject,
=> {
const str = bun.String.fromJS(value, global);
const str = bun.String.fromJS_unsafe(value, global);

if (is_async) {
defer str.deref();
Expand Down Expand Up @@ -592,7 +592,7 @@ pub const StringOrBuffer = union(enum) {
return fromJSMaybeAsync(global, allocator, value, is_async);
}

var str = try bun.String.fromJS2(value, global);
var str = try bun.String.fromJS(value, global);
defer str.deref();
if (str.isEmpty()) {
return fromJSMaybeAsync(global, allocator, value, is_async);
Expand Down Expand Up @@ -690,7 +690,7 @@ pub const Encoding = enum(u8) {
}

pub fn fromJSWithDefaultOnEmpty(value: JSC.JSValue, globalObject: *JSC.JSGlobalObject, default: Encoding) bun.JSError!?Encoding {
const str = try bun.String.fromJS2(value, globalObject);
const str = try bun.String.fromJS(value, globalObject);
defer str.deref();
if (str.isEmpty()) {
return default;
Expand Down Expand Up @@ -954,7 +954,7 @@ pub const PathLike = union(enum) {
JSC.JSValue.JSType.StringObject,
JSC.JSValue.JSType.DerivedStringObject,
=> {
var str = arg.toBunString(ctx);
var str = try arg.toBunString(ctx);
defer str.deref();

arguments.eat();
Expand Down
Loading