Update JSValue.toSliceClone to use JSError (#24949)

### What does this PR do?
Removes a TODO
### How did you verify your code works?

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Dylan Conway
2025-11-23 00:32:38 -08:00
committed by GitHub
parent 3c60fcda33
commit 25b91e5c86
8 changed files with 20 additions and 33 deletions

View File

@@ -946,8 +946,8 @@ pub const JSBundler = struct {
resolve.value = .{ .no_match = {} };
} else {
const global = resolve.bv2.plugins.?.globalObject();
const path = path_value.toSliceCloneWithAllocator(global, bun.default_allocator) orelse @panic("Unexpected: path is not a string");
const namespace = namespace_value.toSliceCloneWithAllocator(global, bun.default_allocator) orelse @panic("Unexpected: namespace is not a string");
const path = path_value.toSliceCloneWithAllocator(global, bun.default_allocator) catch @panic("Unexpected: path is not a string");
const namespace = namespace_value.toSliceCloneWithAllocator(global, bun.default_allocator) catch @panic("Unexpected: namespace is not a string");
resolve.value = .{
.success = .{
.path = path.slice(),

View File

@@ -388,7 +388,7 @@ pub const Config = struct {
const replacementValue = try value.getIndex(globalThis, 1);
if (try exportReplacementValue(replacementValue, globalThis, allocator)) |to_replace| {
const replacementKey = try value.getIndex(globalThis, 0);
var slice = replacementKey.toSliceCloneWithAllocator(globalThis, allocator) orelse return error.JSError;
var slice = try replacementKey.toSliceCloneWithAllocator(globalThis, allocator);
errdefer slice.deinit();
const replacement_name = slice.slice();

View File

@@ -275,7 +275,7 @@ pub fn constructor(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) b
return globalThis.throw("Glob.constructor: first argument is not a string", .{});
}
const pat_str: []u8 = @constCast((pat_arg.toSliceClone(globalThis) orelse return error.JSError).slice());
const pat_str: []u8 = @constCast((try pat_arg.toSliceClone(globalThis)).slice());
const glob = bun.handleOom(alloc.create(Glob));
glob.* = .{ .pattern = pat_str };

View File

@@ -1219,30 +1219,18 @@ pub const JSValue = enum(i64) {
/// On exception or out of memory, this returns null.
///
/// Remember that `Symbol` throws an exception when you call `toString()`.
pub fn toSliceClone(this: JSValue, globalThis: *JSGlobalObject) ?ZigString.Slice {
pub fn toSliceClone(this: JSValue, globalThis: *JSGlobalObject) bun.JSError!ZigString.Slice {
return this.toSliceCloneWithAllocator(globalThis, bun.default_allocator);
}
/// Call `toString()` on the JSValue and clone the result.
/// On exception or out of memory, this returns null.
///
/// Remember that `Symbol` throws an exception when you call `toString()`.
pub fn toSliceCloneZ(this: JSValue, globalThis: *JSGlobalObject) JSError!?[:0]u8 {
var str = try bun.String.fromJS(this, globalThis);
return try str.toOwnedSliceZ(bun.default_allocator);
}
/// On exception or out of memory, this returns null, to make exception checks clearer.
pub fn toSliceCloneWithAllocator(
this: JSValue,
globalThis: *JSGlobalObject,
allocator: std.mem.Allocator,
) ?ZigString.Slice {
var str = this.toJSString(globalThis) catch return null;
return str.toSliceClone(globalThis, allocator) catch {
globalThis.throwOutOfMemory() catch {}; // TODO: properly propagate exception upwards
return null;
};
) JSError!ZigString.Slice {
var str = try this.toJSString(globalThis);
return str.toSliceClone(globalThis, allocator);
}
/// Runtime conversion to an object. This can have side effects.

View File

@@ -3859,7 +3859,8 @@ fn fromJSWithoutDeferGC(
} else {
return build.blob.dupe();
}
} else if (current.toSliceClone(global)) |sliced| {
} else {
const sliced = try current.toSliceClone(global);
if (sliced.allocator.get()) |allocator| {
return Blob.initWithAllASCII(@constCast(sliced.slice()), allocator, global, false);
}
@@ -3955,7 +3956,8 @@ fn fromJSWithoutDeferGC(
could_have_non_ascii = could_have_non_ascii or blob.charset != .all_ascii;
joiner.pushStatic(blob.sharedView());
continue;
} else if (current.toSliceClone(global)) |sliced| {
} else {
const sliced = try current.toSliceClone(global);
const allocator = sliced.allocator.get();
could_have_non_ascii = could_have_non_ascii or allocator != null;
joiner.push(sliced.slice(), allocator);
@@ -3973,7 +3975,8 @@ fn fromJSWithoutDeferGC(
if (current.as(Blob)) |blob| {
could_have_non_ascii = could_have_non_ascii or blob.charset != .all_ascii;
joiner.pushStatic(blob.sharedView());
} else if (current.toSliceClone(global)) |sliced| {
} else {
const sliced = try current.toSliceClone(global);
const allocator = sliced.allocator.get();
could_have_non_ascii = could_have_non_ascii or allocator != null;
joiner.push(sliced.slice(), allocator);

View File

@@ -490,9 +490,7 @@ pub fn Bun__fetch_(
if (objects_to_try[i] != .zero) {
if (try objects_to_try[i].get(globalThis, "unix")) |socket_path| {
if (socket_path.isString() and try socket_path.getLength(ctx) > 0) {
if (socket_path.toSliceCloneWithAllocator(globalThis, allocator)) |slice| {
break :extract_unix_socket_path slice;
}
break :extract_unix_socket_path try socket_path.toSliceCloneWithAllocator(globalThis, allocator);
}
}

View File

@@ -56,21 +56,19 @@ pub fn fromJS(globalThis: *jsc.JSGlobalObject, input: jsc.JSValue) bun.JSError!j
var log = logger.Log.init(allocator);
if (input.isString()) {
var input_str = input.toSliceCloneWithAllocator(
var input_str = try input.toSliceCloneWithAllocator(
globalThis,
allocator,
) orelse return .zero;
);
if (input_str.len > 0)
try all_positionals.append(input_str.slice());
} else if (input.isArray()) {
var iter = try input.arrayIterator(globalThis);
while (try iter.next()) |item| {
const slice = item.toSliceCloneWithAllocator(globalThis, allocator) orelse return .zero;
if (globalThis.hasException()) return .zero;
const slice = try item.toSliceCloneWithAllocator(globalThis, allocator);
if (slice.len == 0) continue;
try all_positionals.append(slice.slice());
}
if (globalThis.hasException()) return .zero;
} else {
return .js_undefined;
}

View File

@@ -17,14 +17,14 @@
"EXCEPTION_ASSERT(!scope.exception())": 0,
"JSValue.false": 0,
"JSValue.true": 0,
"TODO: properly propagate exception upwards": 118,
"TODO: properly propagate exception upwards": 117,
"alloc.ptr !=": 0,
"alloc.ptr ==": 0,
"allocator.ptr !=": 1,
"allocator.ptr ==": 0,
"global.hasException": 28,
"globalObject.hasException": 47,
"globalThis.hasException": 125,
"globalThis.hasException": 123,
"std.StringArrayHashMap(": 1,
"std.StringArrayHashMapUnmanaged(": 11,
"std.StringHashMap(": 0,