diff --git a/src/StandaloneModuleGraph.zig b/src/StandaloneModuleGraph.zig index fc5530aabc..18ac9f62e0 100644 --- a/src/StandaloneModuleGraph.zig +++ b/src/StandaloneModuleGraph.zig @@ -170,7 +170,7 @@ pub const StandaloneModuleGraph = struct { if (this.wtf_string.isEmpty()) { switch (this.encoding) { .binary, .utf8 => { - this.wtf_string = bun.String.createUTF8(this.contents); + this.wtf_string = bun.String.cloneUTF8(this.contents); }, .latin1 => { this.wtf_string = bun.String.createStaticExternal(this.contents, true); @@ -203,9 +203,9 @@ pub const StandaloneModuleGraph = struct { // The pretty name goes here: if (strings.hasPrefixComptime(this.name, base_public_path_with_default_suffix)) { - b.name = bun.String.createUTF8(this.name[base_public_path_with_default_suffix.len..]); + b.name = bun.String.cloneUTF8(this.name[base_public_path_with_default_suffix.len..]); } else if (this.name.len > 0) { - b.name = bun.String.createUTF8(this.name); + b.name = bun.String.cloneUTF8(this.name); } this.cached_blob = b; diff --git a/src/bake/DevServer.zig b/src/bake/DevServer.zig index 48e5038f12..8167f28e8d 100644 --- a/src/bake/DevServer.zig +++ b/src/bake/DevServer.zig @@ -1576,9 +1576,9 @@ fn onFrameworkRequestWithBundle( // Create a JavaScript object with params const obj = JSValue.createEmptyObject(global, params_array.len); for (params_array) |param| { - const key_str = bun.String.createUTF8(param.key); + const key_str = bun.String.cloneUTF8(param.key); defer key_str.deref(); - const value_str = bun.String.createUTF8(param.value); + const value_str = bun.String.cloneUTF8(param.value); defer value_str.deref(); obj.put(global, key_str, value_str.toJS(global)); @@ -1617,13 +1617,13 @@ fn onFrameworkRequestWithBundle( } const arr = try JSValue.createEmptyArray(global, n); route = dev.router.routePtr(bundle.route_index); - var route_name = bun.String.createUTF8(dev.relativePath(keys[fromOpaqueFileId(.server, route.file_page.unwrap().?).get()])); + var route_name = bun.String.cloneUTF8(dev.relativePath(keys[fromOpaqueFileId(.server, route.file_page.unwrap().?).get()])); try arr.putIndex(global, 0, route_name.transferToJS(global)); dev.releaseRelativePathBuf(); n = 1; while (true) { if (route.file_layout.unwrap()) |layout| { - var layout_name = bun.String.createUTF8(dev.relativePath(keys[fromOpaqueFileId(.server, layout).get()])); + var layout_name = bun.String.cloneUTF8(dev.relativePath(keys[fromOpaqueFileId(.server, layout).get()])); defer dev.releaseRelativePathBuf(); try arr.putIndex(global, @intCast(n), layout_name.transferToJS(global)); n += 1; @@ -1960,7 +1960,7 @@ fn startAsyncBundle( str.deref(); }; for (entry_points.set.keys()) |key| { - try trigger_files.append(bun.String.createUTF8(key)); + try trigger_files.append(bun.String.cloneUTF8(key)); } agent.notifyBundleStart(dev.inspector_server_id, trigger_files.items); @@ -2207,7 +2207,7 @@ fn generateCssJSArray(dev: *DevServer, route_bundle: *RouteBundle) bun.JSError!J const path = std.fmt.bufPrint(&buf, asset_prefix ++ "/{s}.css", .{ &std.fmt.bytesToHex(std.mem.asBytes(&item), .lower), }) catch unreachable; - const str = bun.String.createUTF8(path); + const str = bun.String.cloneUTF8(path); defer str.deref(); try arr.putIndex(dev.vm.global, @intCast(i), str.toJS(dev.vm.global)); } @@ -2250,7 +2250,7 @@ fn makeArrayForServerComponentsPatch(dev: *DevServer, global: *JSC.JSGlobalObjec const arr = try JSC.JSArray.createEmpty(global, items.len); const names = dev.server_graph.bundled_files.keys(); for (items, 0..) |item, i| { - const str = bun.String.createUTF8(dev.relativePath(names[item.get()])); + const str = bun.String.cloneUTF8(dev.relativePath(names[item.get()])); defer dev.releaseRelativePathBuf(); defer str.deref(); try arr.putIndex(global, @intCast(i), str.toJS(global)); @@ -2589,7 +2589,7 @@ pub fn finalizeBundle( const server_bundle = try dev.server_graph.takeJSBundle(&.{ .kind = .hmr_chunk }); defer dev.allocator.free(server_bundle); - const server_modules = c.BakeLoadServerHmrPatch(@ptrCast(dev.vm.global), bun.String.createLatin1(server_bundle)) catch |err| { + const server_modules = c.BakeLoadServerHmrPatch(@ptrCast(dev.vm.global), bun.String.cloneLatin1(server_bundle)) catch |err| { // No user code has been evaluated yet, since everything is to // be wrapped in a function clousure. This means that the likely // error is going to be a syntax error, or other mistake in the diff --git a/src/bake/FrameworkRouter.zig b/src/bake/FrameworkRouter.zig index 98b9074cb8..4b42240dbc 100644 --- a/src/bake/FrameworkRouter.zig +++ b/src/bake/FrameworkRouter.zig @@ -1201,7 +1201,7 @@ pub const JSFrameworkRouter = struct { .params = if (params_out.params.len > 0) params: { const obj = JSValue.createEmptyObject(global, params_out.params.len); for (params_out.params.slice()) |param| { - const value = bun.String.createUTF8(param.value); + const value = bun.String.cloneUTF8(param.value); defer value.deref(); obj.put(global, param.key, value.toJS(global)); } @@ -1307,7 +1307,7 @@ pub const JSFrameworkRouter = struct { defer rendered.deinit(); var it = pattern.iterate(); while (it.next()) |part| try part.toStringForInternalUse(rendered.writer()); - var str = bun.String.createUTF8(rendered.items); + var str = bun.String.cloneUTF8(rendered.items); return str.transferToJS(global); } @@ -1315,12 +1315,12 @@ pub const JSFrameworkRouter = struct { var rendered = std.ArrayList(u8).init(temp_allocator); defer rendered.deinit(); try part.toStringForInternalUse(rendered.writer()); - var str = bun.String.createUTF8(rendered.items); + var str = bun.String.cloneUTF8(rendered.items); return str.transferToJS(global); } pub fn getFileIdForRouter(jsfr: *JSFrameworkRouter, abs_path: []const u8, _: Route.Index, _: Route.FileKind) !OpaqueFileId { - try jsfr.files.append(bun.default_allocator, bun.String.createUTF8(abs_path)); + try jsfr.files.append(bun.default_allocator, bun.String.cloneUTF8(abs_path)); return OpaqueFileId.init(@intCast(jsfr.files.items.len - 1)); } diff --git a/src/bake/production.zig b/src/bake/production.zig index 37dbb5ef1b..e8ee00620f 100644 --- a/src/bake/production.zig +++ b/src/bake/production.zig @@ -128,7 +128,7 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa bun.Global.crash(); }; - const config_entry_point_string = bun.String.createUTF8(config_entry_point.pathConst().?.text); + const config_entry_point_string = bun.String.cloneUTF8(config_entry_point.pathConst().?.text); defer config_entry_point_string.deref(); const config_promise = bun.JSC.JSModuleLoader.loadAndEvaluateModule(global, &config_entry_point_string) orelse { @@ -510,11 +510,11 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa } // Init the items - var pattern_string = bun.String.createUTF8(pattern.slice()); + var pattern_string = bun.String.cloneUTF8(pattern.slice()); defer pattern_string.deref(); try route_patterns.putIndex(global, @intCast(nav_index), pattern_string.toJS(global)); - var src_path = bun.String.createUTF8(bun.path.relative(cwd, pt.inputFile(main_file_route_index).absPath())); + var src_path = bun.String.cloneUTF8(bun.path.relative(cwd, pt.inputFile(main_file_route_index).absPath())); try route_source_files.putIndex(global, @intCast(nav_index), src_path.transferToJS(global)); try route_nested_files.putIndex(global, @intCast(nav_index), file_list); diff --git a/src/bun.js/Debugger.zig b/src/bun.js/Debugger.zig index dca0512a39..617a3afaef 100644 --- a/src/bun.js/Debugger.zig +++ b/src/bun.js/Debugger.zig @@ -179,7 +179,7 @@ fn start(other_vm: *VirtualMachine) void { const loop = this.eventLoop(); if (debugger.from_environment_variable.len > 0) { - var url = bun.String.createUTF8(debugger.from_environment_variable); + var url = bun.String.cloneUTF8(debugger.from_environment_variable); loop.enter(); defer loop.exit(); @@ -187,7 +187,7 @@ fn start(other_vm: *VirtualMachine) void { } if (debugger.path_or_port) |path_or_port| { - var url = bun.String.createUTF8(path_or_port); + var url = bun.String.cloneUTF8(path_or_port); loop.enter(); defer loop.exit(); diff --git a/src/bun.js/ModuleLoader.zig b/src/bun.js/ModuleLoader.zig index 77fc7d328f..72b7c89f8e 100644 --- a/src/bun.js/ModuleLoader.zig +++ b/src/bun.js/ModuleLoader.zig @@ -779,7 +779,7 @@ pub const AsyncModule = struct { return ResolvedSource{ .allocator = null, - .source_code = bun.String.createLatin1(printer.ctx.getWritten()), + .source_code = bun.String.cloneLatin1(printer.ctx.getWritten()), .specifier = String.init(specifier), .source_url = String.init(path.text), .is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs, @@ -1077,7 +1077,7 @@ pub fn transpileSourceCode( if (loader == .json) { return ResolvedSource{ .allocator = null, - .source_code = bun.String.createUTF8(source.contents), + .source_code = bun.String.cloneUTF8(source.contents), .specifier = input_specifier, .source_url = input_specifier.createIfDifferent(path.text), .tag = ResolvedSource.Tag.json_for_object_loader, @@ -1121,7 +1121,7 @@ pub fn transpileSourceCode( const bytecode_slice = parse_result.already_bundled.bytecodeSlice(); return ResolvedSource{ .allocator = null, - .source_code = bun.String.createLatin1(source.contents), + .source_code = bun.String.cloneLatin1(source.contents), .specifier = input_specifier, .source_url = input_specifier.createIfDifferent(path.text), .already_bundled = true, @@ -1163,7 +1163,7 @@ pub fn transpileSourceCode( .source_code = switch (entry.output_code) { .string => entry.output_code.string, .utf8 => brk: { - const result = bun.String.createUTF8(entry.output_code.utf8); + const result = bun.String.cloneUTF8(entry.output_code.utf8); cache.output_code_allocator.free(entry.output_code.utf8); entry.output_code.utf8 = ""; break :brk result; @@ -1290,7 +1290,7 @@ pub fn transpileSourceCode( .allocator = null, .source_code = brk: { const written = printer.ctx.getWritten(); - const result = cache.output_code orelse bun.String.createLatin1(written); + const result = cache.output_code orelse bun.String.cloneLatin1(written); if (written.len > 1024 * 1024 * 2 or jsc_vm.smol) { printer.ctx.buffer.deinit(); @@ -1423,7 +1423,7 @@ pub fn transpileSourceCode( return ResolvedSource{ .allocator = null, - .source_code = bun.String.createUTF8(sqlite_module_source_code_string), + .source_code = bun.String.cloneUTF8(sqlite_module_source_code_string), .specifier = input_specifier, .source_url = input_specifier.createIfDifferent(path.text), .tag = .esm, @@ -1860,7 +1860,7 @@ fn getHardcodedModule(jsc_vm: *VirtualMachine, specifier: bun.String, hardcoded: return switch (hardcoded) { .@"bun:main" => .{ .allocator = null, - .source_code = bun.String.createUTF8(jsc_vm.entry_point.source.contents), + .source_code = bun.String.cloneUTF8(jsc_vm.entry_point.source.contents), .specifier = specifier, .source_url = specifier, .tag = .esm, @@ -1894,7 +1894,7 @@ pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?Reso if (jsc_vm.macro_entry_points.get(MacroEntryPoint.generateIDFromSpecifier(spec.slice()))) |entry| { return .{ .allocator = null, - .source_code = bun.String.createUTF8(entry.source.contents), + .source_code = bun.String.cloneUTF8(entry.source.contents), .specifier = specifier, .source_url = specifier.dupeRef(), }; @@ -2280,7 +2280,7 @@ pub const RuntimeTranspilerStore = struct { var resolved_source = this.resolved_source; const specifier = brk: { if (this.parse_error != null) { - break :brk bun.String.createUTF8(this.path.text); + break :brk bun.String.cloneUTF8(this.path.text); } const out = this.non_threadsafe_input_specifier; @@ -2502,7 +2502,7 @@ pub const RuntimeTranspilerStore = struct { .source_code = switch (entry.output_code) { .string => entry.output_code.string, .utf8 => brk: { - const result = bun.String.createUTF8(entry.output_code.utf8); + const result = bun.String.cloneUTF8(entry.output_code.utf8); cache.output_code_allocator.free(entry.output_code.utf8); entry.output_code.utf8 = ""; break :brk result; @@ -2519,7 +2519,7 @@ pub const RuntimeTranspilerStore = struct { const bytecode_slice = parse_result.already_bundled.bytecodeSlice(); this.resolved_source = ResolvedSource{ .allocator = null, - .source_code = bun.String.createLatin1(parse_result.source.contents), + .source_code = bun.String.cloneLatin1(parse_result.source.contents), .already_bundled = true, .bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null, .bytecode_cache_size = bytecode_slice.len, @@ -2599,7 +2599,7 @@ pub const RuntimeTranspilerStore = struct { const source_code = brk: { const written = printer.ctx.getWritten(); - const result = cache.output_code orelse bun.String.createLatin1(written); + const result = cache.output_code orelse bun.String.cloneLatin1(written); if (written.len > 1024 * 1024 * 2 or vm.smol) { printer.ctx.buffer.deinit(); @@ -3055,7 +3055,7 @@ export fn Bun__resolveEmbeddedNodeFile(vm: *VirtualMachine, in_out_str: *bun.Str const input_path = in_out_str.toUTF8(bun.default_allocator); defer input_path.deinit(); const result = ModuleLoader.resolveEmbeddedFile(vm, input_path.slice(), "node") orelse return false; - in_out_str.* = bun.String.createUTF8(result); + in_out_str.* = bun.String.cloneUTF8(result); return true; } diff --git a/src/bun.js/RuntimeTranspilerCache.zig b/src/bun.js/RuntimeTranspilerCache.zig index ff195b989a..535ec6d9d7 100644 --- a/src/bun.js/RuntimeTranspilerCache.zig +++ b/src/bun.js/RuntimeTranspilerCache.zig @@ -634,7 +634,7 @@ pub const RuntimeTranspilerCache = struct { return; } bun.assert(this.entry == null); - const output_code = bun.String.createLatin1(output_code_bytes); + const output_code = bun.String.cloneLatin1(output_code_bytes); this.output_code = output_code; toFile(this.input_byte_length.?, this.input_hash.?, this.features_hash.?, sourcemap, output_code, this.exports_kind) catch |err| { diff --git a/src/bun.js/VirtualMachine.zig b/src/bun.js/VirtualMachine.zig index b2dc00d0d2..2351d2c487 100644 --- a/src/bun.js/VirtualMachine.zig +++ b/src/bun.js/VirtualMachine.zig @@ -1707,7 +1707,7 @@ pub fn resolveMaybeNeedsTrailingSlash( else specifier_utf8.slice()[namespace.len + 1 .. specifier_utf8.len]; - if (try plugin_runner.onResolveJSC(bun.String.init(namespace), bun.String.fromUTF8(after_namespace), source, .bun)) |resolved_path| { + if (try plugin_runner.onResolveJSC(bun.String.init(namespace), bun.String.borrowUTF8(after_namespace), source, .bun)) |resolved_path| { res.* = resolved_path; return; } @@ -3318,7 +3318,7 @@ pub noinline fn printGithubAnnotation(exception: *ZigException) void { while (strings.indexOfNewlineOrNonASCIIOrANSI(msg, cursor)) |i| { cursor = i + 1; if (msg[i] == '\n') { - const first_line = bun.String.fromUTF8(msg[0..i]); + const first_line = bun.String.borrowUTF8(msg[0..i]); writer.print(": {s}::", .{first_line.githubAction()}) catch {}; break; } diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 2f37b996a8..ab6f5325f0 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -199,7 +199,7 @@ pub fn shellEscape(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) b if (!result) { return globalThis.throw("String has invalid utf-16: {s}", .{bunstr.byteSlice()}); } - var str = bun.String.createUTF8(outbuf.items[0..]); + var str = bun.String.cloneUTF8(outbuf.items[0..]); return str.transferToJS(globalThis); } @@ -472,7 +472,7 @@ export fn Bun__inspect(globalThis: *JSGlobalObject, value: JSValue) bun.String { defer formatter.deinit(); writer.print("{}", .{value.toFmt(&formatter)}) catch return .empty; buffered_writer.flush() catch return .empty; - return bun.String.createUTF8(array.slice()); + return bun.String.cloneUTF8(array.slice()); } export fn Bun__inspect_singleline(globalThis: *JSGlobalObject, value: JSValue) bun.String { @@ -492,7 +492,7 @@ export fn Bun__inspect_singleline(globalThis: *JSGlobalObject, value: JSValue) b }) catch return .empty; if (globalThis.hasException()) return .empty; buffered_writer.flush() catch return .empty; - return bun.String.createUTF8(array.slice()); + return bun.String.cloneUTF8(array.slice()); } pub fn getInspect(globalObject: *JSC.JSGlobalObject, _: *JSC.JSObject) JSC.JSValue { @@ -606,7 +606,7 @@ pub fn getMain(globalThis: *JSC.JSGlobalObject, _: *JSC.JSObject) JSC.JSValue { if (comptime Environment.isWindows) { var wpath: bun.WPathBuffer = undefined; const fdpath = bun.getFdPathW(fd, &wpath) catch break :use_resolved_path; - vm.main_resolved_path = bun.String.createUTF16(fdpath); + vm.main_resolved_path = bun.String.cloneUTF16(fdpath); } else { var path: bun.PathBuffer = undefined; const fdpath = bun.getFdPath(fd, &path) catch break :use_resolved_path; @@ -615,7 +615,7 @@ pub fn getMain(globalThis: *JSC.JSGlobalObject, _: *JSC.JSObject) JSC.JSValue { if (bun.String.tryCreateAtom(fdpath)) |atom| { vm.main_resolved_path = atom; } else { - vm.main_resolved_path = bun.String.createUTF8(fdpath); + vm.main_resolved_path = bun.String.cloneUTF8(fdpath); } } } diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index 7ffa49672d..245d38a250 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -936,8 +936,8 @@ pub const JSBundler = struct { const namespace_string = if (path.isFile()) bun.String.empty else - bun.String.createUTF8(path.namespace); - const path_string = bun.String.createUTF8(path.text); + bun.String.cloneUTF8(path.namespace); + const path_string = bun.String.cloneUTF8(path.text); defer namespace_string.deref(); defer path_string.deref(); return JSBundlerPlugin__anyMatches(this, &namespace_string, &path_string, is_onLoad); @@ -958,8 +958,8 @@ pub const JSBundler = struct { const namespace_string = if (namespace.len == 0) bun.String.static("file") else - bun.String.createUTF8(namespace); - const path_string = bun.String.createUTF8(path); + bun.String.cloneUTF8(namespace); + const path_string = bun.String.cloneUTF8(path); defer namespace_string.deref(); defer path_string.deref(); JSBundlerPlugin__matchOnLoad(this, &namespace_string, &path_string, context, @intFromEnum(default_loader), is_server_side); @@ -979,9 +979,9 @@ pub const JSBundler = struct { const namespace_string = if (strings.eqlComptime(namespace, "file")) bun.String.empty else - bun.String.createUTF8(namespace); - const path_string = bun.String.createUTF8(path); - const importer_string = bun.String.createUTF8(importer); + bun.String.cloneUTF8(namespace); + const path_string = bun.String.cloneUTF8(path); + const importer_string = bun.String.cloneUTF8(importer); defer namespace_string.deref(); defer path_string.deref(); defer importer_string.deref(); diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index 376c68e560..298fc15354 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -167,7 +167,7 @@ pub const TransformTask = struct { if (printed > 0) { buffer_writer = printer.ctx; buffer_writer.buffer.list.items = buffer_writer.written; - this.output_code = bun.String.createUTF8(buffer_writer.written); + this.output_code = bun.String.cloneUTF8(buffer_writer.written); } else { this.output_code = bun.String.empty; } @@ -1043,7 +1043,7 @@ fn namedExportsToJS(global: *JSGlobalObject, named_exports: *JSAst.Ast.NamedExpo }); var i: usize = 0; while (named_exports_iter.next()) |entry| { - names[i] = bun.String.createUTF8(entry.key_ptr.*); + names[i] = bun.String.cloneUTF8(entry.key_ptr.*); i += 1; } return bun.String.toJSArray(global, names); diff --git a/src/bun.js/api/TOMLObject.zig b/src/bun.js/api/TOMLObject.zig index 5a1bb0103c..62ee13fc08 100644 --- a/src/bun.js/api/TOMLObject.zig +++ b/src/bun.js/api/TOMLObject.zig @@ -50,7 +50,7 @@ pub fn parse( }; const slice = writer.ctx.buffer.slice(); - var out = bun.String.fromUTF8(slice); + var out = bun.String.borrowUTF8(slice); defer out.deref(); return out.toJSByParseJSON(globalThis); diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 1be36b9bf0..edd8c0e9d8 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -760,8 +760,8 @@ pub fn NewSocket(comptime ssl: bool) type { const reason = if (ssl_error.reason == null) "" else ssl_error.reason[0..bun.len(ssl_error.reason)]; const fallback = JSC.SystemError{ - .code = bun.String.createUTF8(code), - .message = bun.String.createUTF8(reason), + .code = bun.String.cloneUTF8(code), + .message = bun.String.cloneUTF8(reason), }; return fallback.toErrorInstance(globalObject); diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 152b658e07..bd920d78f5 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -2687,7 +2687,7 @@ fn throwCommandNotFound(globalThis: *JSC.JSGlobalObject, command: []const u8) bu .message = bun.String.createFormat("Executable not found in $PATH: \"{s}\"", .{command}) catch bun.outOfMemory(), .code = bun.String.static("ENOENT"), .errno = -bun.sys.UV_E.NOENT, - .path = bun.String.createUTF8(command), + .path = bun.String.cloneUTF8(command), }; return globalThis.throwValue(err.toErrorInstance(globalThis)); } diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig index 406ecf077c..38b38a1cf5 100644 --- a/src/bun.js/api/ffi.zig +++ b/src/bun.js/api/ffi.zig @@ -965,7 +965,7 @@ pub const FFI = struct { symbols.clearAndFree(allocator); return ZigString.init("Error while printing code").toErrorInstance(global); }; - strs.appendAssumeCapacity(bun.String.createUTF8(arraylist.items)); + strs.appendAssumeCapacity(bun.String.cloneUTF8(arraylist.items)); } const ret = try bun.String.toJSArray(global, strs.items); @@ -1044,9 +1044,9 @@ pub const FFI = struct { break :brk std.DynLib.open(backup_name) catch { // Then, if that fails, report an error. const system_error = JSC.SystemError{ - .code = bun.String.createUTF8(@tagName(.ERR_DLOPEN_FAILED)), - .message = bun.String.createUTF8("Failed to open library. This is usually caused by a missing library or an invalid library path."), - .syscall = bun.String.createUTF8("dlopen"), + .code = bun.String.cloneUTF8(@tagName(.ERR_DLOPEN_FAILED)), + .message = bun.String.cloneUTF8("Failed to open library. This is usually caused by a missing library or an invalid library path."), + .syscall = bun.String.cloneUTF8("dlopen"), }; return system_error.toErrorInstance(global); }; diff --git a/src/bun.js/api/html_rewriter.zig b/src/bun.js/api/html_rewriter.zig index 337649cd95..a368d0df6f 100644 --- a/src/bun.js/api/html_rewriter.zig +++ b/src/bun.js/api/html_rewriter.zig @@ -1064,7 +1064,7 @@ fn createLOLHTMLStringError() bun.String { // We must clone this string. const err = LOLHTML.HTMLString.lastError(); defer err.deinit(); - return bun.String.createUTF8(err.slice()); + return bun.String.cloneUTF8(err.slice()); } fn htmlStringValue(input: LOLHTML.HTMLString, globalObject: *JSGlobalObject) JSValue { diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 120aeb248a..bedc7ecf0c 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -1246,7 +1246,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d } existing_request = Request.init( - bun.String.createUTF8(url.href), + bun.String.cloneUTF8(url.href), headers, this.vm.initRequestBodyValue(body) catch bun.outOfMemory(), method, @@ -1347,7 +1347,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d pub fn getAddress(this: *ThisServer, globalThis: *JSGlobalObject) JSC.JSValue { switch (this.config.address) { .unix => |unix| { - var value = bun.String.createUTF8(unix); + var value = bun.String.cloneUTF8(unix); defer value.deref(); return value.toJS(globalThis); }, @@ -1402,7 +1402,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d const buf = try std.fmt.allocPrint(default_allocator, "{any}", .{fmt}); defer default_allocator.free(buf); - return bun.String.createUTF8(buf); + return bun.String.cloneUTF8(buf); } pub fn getURL(this: *ThisServer, globalThis: *JSGlobalObject) bun.OOM!JSC.JSValue { @@ -1424,7 +1424,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d if (listener.socket().remoteAddress(buf[0..1024])) |addr| { if (addr.len > 0) { - this.cached_hostname = bun.String.createUTF8(addr); + this.cached_hostname = bun.String.cloneUTF8(addr); } } } @@ -1433,7 +1433,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d switch (this.config.address) { .tcp => |tcp| { if (tcp.hostname) |hostname| { - this.cached_hostname = bun.String.createUTF8(bun.sliceTo(hostname, 0)); + this.cached_hostname = bun.String.cloneUTF8(bun.sliceTo(hostname, 0)); } else { this.cached_hostname = bun.String.createAtomASCII("localhost"); } diff --git a/src/bun.js/bindings/URL.zig b/src/bun.js/bindings/URL.zig index e87a6bd0dc..8c2b286f64 100644 --- a/src/bun.js/bindings/URL.zig +++ b/src/bun.js/bindings/URL.zig @@ -59,7 +59,7 @@ pub const URL = opaque { } pub fn fromUTF8(input: []const u8) ?*URL { - return fromString(String.fromUTF8(input)); + return fromString(String.borrowUTF8(input)); } pub fn fromString(str: bun.String) ?*URL { JSC.markBinding(@src()); diff --git a/src/bun.js/ipc.zig b/src/bun.js/ipc.zig index c4a9518cbe..d51d7b23d2 100644 --- a/src/bun.js/ipc.zig +++ b/src/bun.js/ipc.zig @@ -201,7 +201,7 @@ const json = struct { return IPCDecodeError.OutOfMemory; } break :ascii s; - } else bun.String.fromUTF8(json_data); + } else bun.String.borrowUTF8(json_data); defer { str.deref(); diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig index 324a45e3de..bebe7a08f9 100644 --- a/src/bun.js/node/node_fs.zig +++ b/src/bun.js/node/node_fs.zig @@ -4496,7 +4496,7 @@ pub const NodeFS = struct { JSC.Node.Dirent => { dirent_path.ref(); entries.append(.{ - .name = bun.String.createUTF16(utf16_name), + .name = bun.String.cloneUTF16(utf16_name), .path = dirent_path, .kind = current.kind, }) catch bun.outOfMemory(); @@ -4505,7 +4505,7 @@ pub const NodeFS = struct { .buffer => unreachable, // in node.js, libuv converts to utf8 before node.js converts those bytes into other stuff // all encodings besides hex, base64, and base64url are mis-interpreting filesystem bytes. - .utf8 => entries.append(bun.String.createUTF16(utf16_name)) catch bun.outOfMemory(), + .utf8 => entries.append(bun.String.cloneUTF16(utf16_name)) catch bun.outOfMemory(), else => |enc| { const utf8_path = bun.strings.fromWPath(re_encoding_buffer.?, utf16_name); entries.append(JSC.WebCore.encoding.toBunString(utf8_path, enc)) catch bun.outOfMemory(); @@ -4632,12 +4632,12 @@ pub const NodeFS = struct { const path_u8 = bun.path.dirname(bun.path.join(&[_]string{ root_basename, name_to_copy }, .auto), .auto); if (dirent_path_prev.isEmpty() or !bun.strings.eql(dirent_path_prev.byteSlice(), path_u8)) { dirent_path_prev.deref(); - dirent_path_prev = bun.String.createUTF8(path_u8); + dirent_path_prev = bun.String.cloneUTF8(path_u8); } dirent_path_prev.ref(); entries.append(.{ - .name = bun.String.createUTF8(utf8_name), + .name = bun.String.cloneUTF8(utf8_name), .path = dirent_path_prev, .kind = current.kind, }) catch bun.outOfMemory(); @@ -4646,7 +4646,7 @@ pub const NodeFS = struct { entries.append(Buffer.fromString(name_to_copy, bun.default_allocator) catch bun.outOfMemory()) catch bun.outOfMemory(); }, bun.String => { - entries.append(bun.String.createUTF8(name_to_copy)) catch bun.outOfMemory(); + entries.append(bun.String.cloneUTF8(name_to_copy)) catch bun.outOfMemory(); }, else => bun.outOfMemory(), } @@ -5381,7 +5381,7 @@ pub const NodeFS = struct { } else .{ - .string = .{ .utf8 = .{}, .underlying = bun.String.createUTF8(link_path) }, + .string = .{ .utf8 = .{}, .underlying = bun.String.cloneUTF8(link_path) }, }, }, }; @@ -5451,7 +5451,7 @@ pub const NodeFS = struct { } } break :utf8 .{ - .string = .{ .utf8 = .{}, .underlying = bun.String.createUTF8(buf) }, + .string = .{ .utf8 = .{}, .underlying = bun.String.cloneUTF8(buf) }, }; }, else => |enc| .{ @@ -5503,7 +5503,7 @@ pub const NodeFS = struct { } } break :utf8 .{ - .string = .{ .utf8 = .{}, .underlying = bun.String.createUTF8(buf) }, + .string = .{ .utf8 = .{}, .underlying = bun.String.cloneUTF8(buf) }, }; }, else => |enc| .{ diff --git a/src/bun.js/node/node_os.zig b/src/bun.js/node/node_os.zig index 2ab9917dce..0ea595a673 100644 --- a/src/bun.js/node/node_os.zig +++ b/src/bun.js/node/node_os.zig @@ -320,7 +320,7 @@ pub fn homedir(global: *JSC.JSGlobalObject) !bun.String { if (libuv.uv_os_homedir(&out, &size).toError(.uv_os_homedir)) |err| { return global.throwValue(err.toJS(global)); } - return bun.String.createUTF8(out[0..size]); + return bun.String.cloneUTF8(out[0..size]); } else { // The posix implementation of uv_os_homedir first checks the HOME @@ -384,7 +384,7 @@ pub fn homedir(global: *JSC.JSGlobalObject) !bun.String { } return if (pw.pw_dir) |dir| - bun.String.createUTF8(bun.span(dir)) + bun.String.cloneUTF8(bun.span(dir)) else bun.String.empty; } @@ -394,7 +394,7 @@ pub fn hostname(global: *JSC.JSGlobalObject) bun.JSError!JSC.JSValue { if (Environment.isWindows) { var name_buffer: [129:0]u16 = undefined; if (bun.windows.GetHostNameW(&name_buffer, name_buffer.len) == 0) { - const str = bun.String.createUTF16(bun.sliceTo(&name_buffer, 0)); + const str = bun.String.cloneUTF16(bun.sliceTo(&name_buffer, 0)); defer str.deref(); return str.toJS(global); } @@ -402,7 +402,7 @@ pub fn hostname(global: *JSC.JSGlobalObject) bun.JSError!JSC.JSValue { var result: std.os.windows.ws2_32.WSADATA = undefined; if (std.os.windows.ws2_32.WSAStartup(0x202, &result) == 0) { if (bun.windows.GetHostNameW(&name_buffer, name_buffer.len) == 0) { - var y = bun.String.createUTF16(bun.sliceTo(&name_buffer, 0)); + var y = bun.String.cloneUTF16(bun.sliceTo(&name_buffer, 0)); defer y.deref(); return y.toJS(global); } @@ -801,7 +801,7 @@ pub fn release() bun.String { else => @compileError("unsupported os"), }; - return bun.String.createUTF8(value); + return bun.String.cloneUTF8(value); } pub extern fn set_process_priority(pid: i32, priority: i32) i32; @@ -1004,7 +1004,7 @@ pub fn version() bun.JSError!bun.String { else => @compileError("unsupported os"), }; - return bun.String.createUTF8(slice); + return bun.String.cloneUTF8(slice); } /// Given a netmask returns a CIDR suffix. Returns null if the mask is not valid. diff --git a/src/bun.js/node/node_process.zig b/src/bun.js/node/node_process.zig index c17ca62d18..849778a302 100644 --- a/src/bun.js/node/node_process.zig +++ b/src/bun.js/node/node_process.zig @@ -72,7 +72,7 @@ fn createExecArgv(globalObject: *JSC.JSGlobalObject) bun.JSError!JSC.JSValue { defer prev = arg; if (arg.len >= 1 and arg[0] == '-') { - try args.append(bun.String.createUTF8(arg)); + try args.append(bun.String.cloneUTF8(arg)); continue; } @@ -107,7 +107,7 @@ fn createExecArgv(globalObject: *JSC.JSGlobalObject) bun.JSError!JSC.JSValue { }); if (prev) |p| if (map.has(p)) { - try args.append(bun.String.createUTF8(arg)); + try args.append(bun.String.cloneUTF8(arg)); continue; }; @@ -152,7 +152,7 @@ fn createArgv(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { } else { const exe_path = bun.selfExePath() catch null; args_list.appendAssumeCapacity( - if (exe_path) |str| bun.String.fromUTF8(str) else bun.String.static("bun"), + if (exe_path) |str| bun.String.borrowUTF8(str) else bun.String.static("bun"), ); } @@ -163,7 +163,7 @@ fn createArgv(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { if (vm.worker != null and vm.worker.?.eval_mode) { args_list.appendAssumeCapacity(bun.String.static("[worker eval]")); } else { - args_list.appendAssumeCapacity(bun.String.fromUTF8(vm.main)); + args_list.appendAssumeCapacity(bun.String.borrowUTF8(vm.main)); } } @@ -175,7 +175,7 @@ fn createArgv(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { } } else { for (vm.argv) |arg| { - const str = bun.String.fromUTF8(arg); + const str = bun.String.borrowUTF8(arg); // https://github.com/yargs/yargs/blob/adb0d11e02c613af3d9427b3028cc192703a3869/lib/utils/process-argv.ts#L1 args_list.appendAssumeCapacity(str); } @@ -248,7 +248,7 @@ fn setCwd_(globalObject: *JSC.JSGlobalObject, to: *JSC.ZigString) bun.JSError!JS fs.top_level_dir = fs.top_level_dir_buf[0 .. len + 1 :0]; } const withoutTrailingSlash = if (Environment.isWindows) strings.withoutTrailingSlashWindowsPath else strings.withoutTrailingSlash; - var str = bun.String.createUTF8(withoutTrailingSlash(fs.top_level_dir)); + var str = bun.String.cloneUTF8(withoutTrailingSlash(fs.top_level_dir)); return str.transferToJS(globalObject); }, .err => |e| { diff --git a/src/bun.js/node/node_util_binding.zig b/src/bun.js/node/node_util_binding.zig index 4b33e4f7aa..29f9a5af51 100644 --- a/src/bun.js/node/node_util_binding.zig +++ b/src/bun.js/node/node_util_binding.zig @@ -168,9 +168,9 @@ fn split( var it: SplitNewlineIterator(Char) = .{ .buffer = buffer, .index = 0 }; while (it.next()) |line| { const encoded_line = switch (encoding) { - inline .utf8 => bun.String.fromUTF8(line), - inline .latin1 => bun.String.createLatin1(line), - inline .utf16 => bun.String.fromUTF16(line), + inline .utf8 => bun.String.borrowUTF8(line), + inline .latin1 => bun.String.cloneLatin1(line), + inline .utf16 => bun.String.borrowUTF16(line), }; errdefer encoded_line.deref(); try lines.append(alloc, encoded_line); diff --git a/src/bun.js/node/path.zig b/src/bun.js/node/path.zig index dbbf45682b..62d772345b 100644 --- a/src/bun.js/node/path.zig +++ b/src/bun.js/node/path.zig @@ -1102,10 +1102,10 @@ export fn Bun__Node__Path_joinWTF(lhs: *bun.String, rhs_ptr: [*]const u8, rhs_le defer slice.deinit(); if (Environment.isWindows) { const win = joinWindowsT(u8, &.{ slice.slice(), rhs }, &buf, &buf2); - result.* = bun.String.createUTF8(win); + result.* = bun.String.cloneUTF8(win); } else { const posix = joinPosixT(u8, &.{ slice.slice(), rhs }, &buf, &buf2); - result.* = bun.String.createUTF8(posix); + result.* = bun.String.cloneUTF8(posix); } } diff --git a/src/bun.js/node/util/parse_args.zig b/src/bun.js/node/util/parse_args.zig index 5bfa86afbc..2804469070 100644 --- a/src/bun.js/node/util/parse_args.zig +++ b/src/bun.js/node/util/parse_args.zig @@ -119,7 +119,7 @@ const OptionToken = struct { const raw = this.raw.asBunString(globalThis); var buf: [8]u8 = undefined; const str = std.fmt.bufPrint(&buf, "-{}", .{raw.substringWithLen(optgroup_idx, optgroup_idx + 1)}) catch unreachable; - return String.fromUTF8(str).toJS(globalThis); + return String.borrowUTF8(str).toJS(globalThis); } else { switch (this.parse_type) { .lone_short_option, .lone_long_option => { diff --git a/src/bun.js/node/win_watcher.zig b/src/bun.js/node/win_watcher.zig index 429fc82d62..40482e4d9b 100644 --- a/src/bun.js/node/win_watcher.zig +++ b/src/bun.js/node/win_watcher.zig @@ -156,7 +156,7 @@ pub const PathWatcher = struct { if (event.emit(hash, timestamp, event_type)) { const ctx: *FSWatcher = @alignCast(@ptrCast(this.handlers.keys()[i])); onPathUpdateFn(ctx, event_type.toEvent(switch (ctx.encoding) { - .utf8 => .{ .string = bun.String.createUTF8(path) }, + .utf8 => .{ .string = bun.String.cloneUTF8(path) }, else => .{ .bytes_to_free = bun.default_allocator.dupeZ(u8, path) catch bun.outOfMemory() }, }), is_file); if (comptime bun.Environment.isDebug) diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig index f545a8bc3d..2bd9169b8f 100644 --- a/src/bun.js/web_worker.zig +++ b/src/bun.js/web_worker.zig @@ -421,7 +421,7 @@ fn onUnhandledRejection(vm: *jsc.VirtualMachine, globalObject: *jsc.JSGlobalObje bun.outOfMemory(); }; jsc.markBinding(@src()); - WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.slice()), error_instance); + WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.cloneUTF8(array.slice()), error_instance); if (vm.worker) |worker_| { _ = worker.setRequestedTerminate(); worker.parent_poll_ref.unrefConcurrently(worker.parent); diff --git a/src/bun.js/webcore/Blob.zig b/src/bun.js/webcore/Blob.zig index b935de14bb..3eb6b4185d 100644 --- a/src/bun.js/webcore/Blob.zig +++ b/src/bun.js/webcore/Blob.zig @@ -2890,7 +2890,7 @@ pub fn getNameString(this: *Blob) ?bun.String { if (this.name.tag != .Dead) return this.name; if (this.getFileName()) |path| { - this.name = bun.String.createUTF8(path); + this.name = bun.String.cloneUTF8(path); return this.name; } @@ -3410,7 +3410,7 @@ pub fn toStringWithBytes(this: *Blob, global: *JSGlobalObject, raw_bytes: []cons if (bom == .utf16_le) { defer if (lifetime == .temporary) bun.default_allocator.free(raw_bytes); - var out = bun.String.createUTF16(bun.reinterpretSlice(u16, buf)); + var out = bun.String.cloneUTF16(bun.reinterpretSlice(u16, buf)); defer out.deref(); return out.toJS(global); } @@ -3466,7 +3466,7 @@ pub fn toStringWithBytes(this: *Blob, global: *JSGlobalObject, raw_bytes: []cons // if there was a UTF-8 BOM, we need to clone the buffer because // external doesn't support this case here yet. if (buf.len != raw_bytes.len) { - var out = bun.String.createLatin1(buf); + var out = bun.String.cloneLatin1(buf); defer { bun.default_allocator.free(raw_bytes); out.deref(); @@ -3519,7 +3519,7 @@ pub fn toJSONWithBytes(this: *Blob, global: *JSGlobalObject, raw_bytes: []const if (buf.len == 0) return global.createSyntaxErrorInstance("Unexpected end of JSON input", .{}); if (bom == .utf16_le) { - var out = bun.String.createUTF16(bun.reinterpretSlice(u16, buf)); + var out = bun.String.cloneUTF16(bun.reinterpretSlice(u16, buf)); defer if (lifetime == .temporary) bun.default_allocator.free(raw_bytes); defer if (lifetime == .transfer) this.detach(); defer out.deref(); @@ -4366,7 +4366,7 @@ pub const Internal = struct { // If there was a UTF8 BOM, we clone it (bytes_without_bom.len != this.bytes.items.len) { defer this.deinit(); - var out = bun.String.createLatin1(this.bytes.items[3..]); + var out = bun.String.cloneLatin1(this.bytes.items[3..]); defer out.deref(); return out.toJS(globalThis); } else { diff --git a/src/bun.js/webcore/Request.zig b/src/bun.js/webcore/Request.zig index efcd5ab1eb..c3b10fd4ae 100644 --- a/src/bun.js/webcore/Request.zig +++ b/src/bun.js/webcore/Request.zig @@ -440,14 +440,14 @@ pub fn ensureURL(this: *Request) bun.OOM!void { var href = bun.JSC.URL.hrefFromString(bun.String.fromBytes(url)); if (!href.isEmpty()) { if (href.byteSlice().ptr == url.ptr) { - this.url = bun.String.createLatin1(url[0..href.length()]); + this.url = bun.String.cloneLatin1(url[0..href.length()]); href.deref(); } else { this.url = href; } } else { // TODO: what is the right thing to do for invalid URLS? - this.url = bun.String.createUTF8(url); + this.url = bun.String.cloneUTF8(url); } return; @@ -470,7 +470,7 @@ pub fn ensureURL(this: *Request) bun.OOM!void { req_url, }); defer bun.default_allocator.free(temp_url); - this.url = bun.String.createUTF8(temp_url); + this.url = bun.String.cloneUTF8(temp_url); } const href = bun.JSC.URL.hrefFromString(this.url); @@ -487,7 +487,7 @@ pub fn ensureURL(this: *Request) bun.OOM!void { if (comptime Environment.allow_assert) { bun.assert(this.sizeOfURL() == req_url.len); } - this.url = bun.String.createUTF8(req_url); + this.url = bun.String.cloneUTF8(req_url); } } diff --git a/src/bun.js/webcore/S3Stat.zig b/src/bun.js/webcore/S3Stat.zig index 0436b5de00..e657dfb7e6 100644 --- a/src/bun.js/webcore/S3Stat.zig +++ b/src/bun.js/webcore/S3Stat.zig @@ -32,8 +32,8 @@ pub const S3Stat = struct { return S3Stat.new(.{ .size = size, - .etag = bun.String.createUTF8(etag), - .contentType = bun.String.createUTF8(contentType), + .etag = bun.String.cloneUTF8(etag), + .contentType = bun.String.cloneUTF8(contentType), .lastModified = last_modified, }); } diff --git a/src/bun.js/webcore/TextDecoder.zig b/src/bun.js/webcore/TextDecoder.zig index 4dbc8dd798..190198feb0 100644 --- a/src/bun.js/webcore/TextDecoder.zig +++ b/src/bun.js/webcore/TextDecoder.zig @@ -269,7 +269,7 @@ fn decodeSlice(this: *TextDecoder, globalThis: *JSC.JSGlobalObject, buffer_slice return globalThis.ERR(.ENCODING_INVALID_ENCODED_DATA, "The encoded data was not valid {s} data", .{@tagName(utf16_encoding)}).throw(); } - var output = bun.String.fromUTF16(decoded.items); + var output = bun.String.borrowUTF16(decoded.items); return output.toJS(globalThis); }, } diff --git a/src/bun.js/webcore/blob/copy_file.zig b/src/bun.js/webcore/blob/copy_file.zig index 7c35e7e369..02c3e24374 100644 --- a/src/bun.js/webcore/blob/copy_file.zig +++ b/src/bun.js/webcore/blob/copy_file.zig @@ -65,7 +65,7 @@ pub const CopyFile = struct { const globalThis = this.globalThis; var system_error: SystemError = this.system_error orelse SystemError{ .message = .empty }; if (this.source_file_store.pathlike == .path and system_error.path.isEmpty()) { - system_error.path = bun.String.createUTF8(this.source_file_store.pathlike.path.slice()); + system_error.path = bun.String.cloneUTF8(this.source_file_store.pathlike.path.slice()); } if (system_error.message.isEmpty()) { diff --git a/src/bun.js/webcore/blob/read_file.zig b/src/bun.js/webcore/blob/read_file.zig index 4ce7514532..021596ee3b 100644 --- a/src/bun.js/webcore/blob/read_file.zig +++ b/src/bun.js/webcore/blob/read_file.zig @@ -238,7 +238,7 @@ pub const ReadFile = struct { this.system_error = err.toSystemError(); if (this.system_error.?.path.isEmpty()) { this.system_error.?.path = if (this.file_store.pathlike == .path) - bun.String.createUTF8(this.file_store.pathlike.path.slice()) + bun.String.cloneUTF8(this.file_store.pathlike.path.slice()) else bun.String.empty; } @@ -351,7 +351,7 @@ pub const ReadFile = struct { this.system_error = JSC.SystemError{ .code = bun.String.static("EISDIR"), .path = if (this.file_store.pathlike == .path) - bun.String.createUTF8(this.file_store.pathlike.path.slice()) + bun.String.cloneUTF8(this.file_store.pathlike.path.slice()) else bun.String.empty, .message = bun.String.static("Directories cannot be read like files"), @@ -665,7 +665,7 @@ pub const ReadFileUV = struct { this.system_error = JSC.SystemError{ .code = bun.String.static("EISDIR"), .path = if (this.file_store.pathlike == .path) - bun.String.createUTF8(this.file_store.pathlike.path.slice()) + bun.String.cloneUTF8(this.file_store.pathlike.path.slice()) else bun.String.empty, .message = bun.String.static("Directories cannot be read like files"), diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index 8eda33a62f..c4bce2e90b 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -211,7 +211,7 @@ pub fn toBunStringComptime(input: []const u8, comptime encoding: Encoding) bun.S // If we get here, it means we can safely assume the string is 100% ASCII characters // For this, we rely on WebKit to manage the memory. - return bun.String.createLatin1(input); + return bun.String.cloneLatin1(input); }, .ucs2, .utf16le => { // Avoid incomplete characters diff --git a/src/bun.js/webcore/fetch.zig b/src/bun.js/webcore/fetch.zig index deb177df2a..5c84f06066 100644 --- a/src/bun.js/webcore/fetch.zig +++ b/src/bun.js/webcore/fetch.zig @@ -686,7 +686,7 @@ pub const FetchTasklet = struct { this.result.fail = error.ERR_TLS_CERT_ALTNAME_INVALID; return false; }; - var hostname: bun.String = bun.String.createUTF8(certificate_info.hostname); + var hostname: bun.String = bun.String.cloneUTF8(certificate_info.hostname); defer hostname.deref(); const js_hostname = hostname.toJS(globalObject); js_hostname.ensureStillAlive(); @@ -763,9 +763,9 @@ pub const FetchTasklet = struct { // some times we don't have metadata so we also check http.url const path = if (this.metadata) |metadata| - bun.String.createUTF8(metadata.url) + bun.String.cloneUTF8(metadata.url) else if (this.http) |http_| - bun.String.createUTF8(http_.url.href) + bun.String.cloneUTF8(http_.url.href) else bun.String.empty; @@ -2287,7 +2287,7 @@ pub fn Bun__fetch_( break :brk fullpath; }; - url_string = JSC.URL.fileURLFromString(bun.String.fromUTF8(temp_file_path)); + url_string = JSC.URL.fileURLFromString(bun.String.borrowUTF8(temp_file_path)); var pathlike: JSC.Node.PathOrFileDescriptor = .{ .path = .{ diff --git a/src/cli/pack_command.zig b/src/cli/pack_command.zig index 056c4a2a2b..35e427ee49 100644 --- a/src/cli/pack_command.zig +++ b/src/cli/pack_command.zig @@ -2559,7 +2559,7 @@ pub const bindings = struct { sha512.final(&sha512_digest); var base64_buf: [std.base64.standard.Encoder.calcSize(sha.SHA512.digest)]u8 = undefined; const encode_count = bun.simdutf.base64.encode(&sha512_digest, &base64_buf, false); - const integrity_str = String.createUTF8(base64_buf[0..encode_count]); + const integrity_str = String.cloneUTF8(base64_buf[0..encode_count]); const EntryInfo = struct { pathname: String, @@ -2625,7 +2625,7 @@ pub const bindings = struct { const perm = archive_entry.perm(); var entry_info: EntryInfo = .{ - .pathname = String.createUTF8(pathname), + .pathname = String.cloneUTF8(pathname), .kind = String.static(@tagName(kind)), .perm = perm, }; @@ -2643,7 +2643,7 @@ pub const bindings = struct { }); } read_buf.items.len = @intCast(read); - entry_info.contents = String.createUTF8(read_buf.items); + entry_info.contents = String.cloneUTF8(read_buf.items); } entries_info.append(entry_info) catch bun.outOfMemory(); diff --git a/src/crash_handler.zig b/src/crash_handler.zig index a90b93871f..9410bdcf7f 100644 --- a/src/crash_handler.zig +++ b/src/crash_handler.zig @@ -1829,7 +1829,7 @@ pub const js_bindings = struct { // there is definitely enough space in the bounded array unreachable; }; - var str = bun.String.createLatin1(buf.slice()); + var str = bun.String.cloneLatin1(buf.slice()); return str.transferToJS(global); } diff --git a/src/css/values/color_js.zig b/src/css/values/color_js.zig index 5c76c3bacc..24c1f01322 100644 --- a/src/css/values/color_js.zig +++ b/src/css/values/color_js.zig @@ -339,7 +339,7 @@ pub fn jsFunctionColor(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFram .ansi_16 => { const ansi_16_color = Ansi256.get16(rgba.red, rgba.green, rgba.blue); // 16-color ansi, foreground text color - break :color bun.String.createLatin1(&[_]u8{ + break :color bun.String.cloneLatin1(&[_]u8{ // 0x1b is the escape character // 38 is the foreground color code // 5 is the 16-color mode @@ -364,13 +364,13 @@ pub fn jsFunctionColor(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFram rgba.blue, }) catch unreachable; - break :color bun.String.createLatin1(buf[0 .. 7 + additional.len]); + break :color bun.String.cloneLatin1(buf[0 .. 7 + additional.len]); }, .ansi_256 => { // ANSI escape sequence var buf: Ansi256.Buffer = undefined; const val = Ansi256.from(rgba, &buf); - break :color bun.String.createLatin1(val); + break :color bun.String.cloneLatin1(val); }, else => unreachable, } diff --git a/src/deps/c_ares.zig b/src/deps/c_ares.zig index c40fd271d4..ecf1c9a025 100644 --- a/src/deps/c_ares.zig +++ b/src/deps/c_ares.zig @@ -209,7 +209,7 @@ pub const struct_hostent = extern struct { if (this.h_name == null) { return try JSC.JSValue.createEmptyArray(globalThis, 0); } - return bun.String.toJSArray(globalThis, &[_]bun.String{bun.String.fromUTF8(this.h_name.?[0..bun.len(this.h_name.?)])}); + return bun.String.toJSArray(globalThis, &[_]bun.String{bun.String.borrowUTF8(this.h_name.?[0..bun.len(this.h_name.?)])}); } if (this.h_aliases == null) { @@ -1701,7 +1701,7 @@ pub const Error = enum(i32) { bun.String.createFormat("{s} {s} {s}", .{ this.syscall, this.errno.code()[4..], hostname }) catch bun.outOfMemory() else bun.String.createFormat("{s} {s}", .{ this.syscall, this.errno.code()[4..] }) catch bun.outOfMemory(), - .syscall = bun.String.createUTF8(this.syscall), + .syscall = bun.String.cloneUTF8(this.syscall), .hostname = this.hostname orelse bun.String.empty, }; @@ -1741,7 +1741,7 @@ pub const Error = enum(i32) { pub fn toDeferred(this: Error, syscall: []const u8, hostname: ?[]const u8, promise: *JSC.JSPromise.Strong) *Deferred { const host_string: ?bun.String = if (hostname) |host| - bun.String.createUTF8(host) + bun.String.cloneUTF8(host) else null; defer promise.* = .{}; @@ -1765,7 +1765,7 @@ pub const Error = enum(i32) { .code = bun.String.static(this.code()[4..]), .message = bun.String.createFormat("{s} {s} {s}", .{ syscall, this.code()[4..], hostname }) catch bun.outOfMemory(), .syscall = bun.String.static(syscall), - .hostname = bun.String.createUTF8(hostname), + .hostname = bun.String.cloneUTF8(hostname), }).toErrorInstance(globalThis); instance.put(globalThis, "name", bun.String.static("DNSException").toJS(globalThis)); return instance; diff --git a/src/deps/lol-html.zig b/src/deps/lol-html.zig index d011db105f..ac5af84a8e 100644 --- a/src/deps/lol-html.zig +++ b/src/deps/lol-html.zig @@ -592,7 +592,7 @@ pub const HTMLString = extern struct { return bun.String.createExternal([*]u8, bytes, true, @constCast(bytes.ptr), &deinit_external); } defer this.deinit(); - return bun.String.createUTF8(bytes); + return bun.String.cloneUTF8(bytes); } pub fn toJS(this: HTMLString, globalThis: *bun.JSC.JSGlobalObject) bun.JSC.JSValue { diff --git a/src/deps/uws.zig b/src/deps/uws.zig index 31475f8a48..32eebcd5f3 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -93,8 +93,8 @@ pub const us_bun_verify_error_t = extern struct { const reason = if (this.reason == null) "" else this.reason[0..bun.len(this.reason)]; const fallback = JSC.SystemError{ - .code = bun.String.createUTF8(code), - .message = bun.String.createUTF8(reason), + .code = bun.String.cloneUTF8(code), + .message = bun.String.cloneUTF8(reason), }; return fallback.toErrorInstance(globalObject); diff --git a/src/dns.zig b/src/dns.zig index 77ed67ccb8..b538646c63 100644 --- a/src/dns.zig +++ b/src/dns.zig @@ -417,11 +417,11 @@ pub fn addressToString(address: *const std.net.Address) bun.OOM!bun.String { // TODO: this is a hack, fix it // This removes [.*]:port // ^ ^^^^^^ - return String.createLatin1(out[1 .. out.len - 1 - std.fmt.count("{d}", .{address.in6.getPort()}) - 1]); + return String.cloneLatin1(out[1 .. out.len - 1 - std.fmt.count("{d}", .{address.in6.getPort()}) - 1]); }, std.posix.AF.UNIX => { if (comptime std.net.has_unix_sockets) { - return String.createLatin1(&address.un.path); + return String.cloneLatin1(&address.un.path); } return String.empty; diff --git a/src/fmt.zig b/src/fmt.zig index 2ecd2ce4d0..ed6f4b0727 100644 --- a/src/fmt.zig +++ b/src/fmt.zig @@ -1780,7 +1780,7 @@ pub const js_bindings = struct { return global.throwError(err, "while formatting"); }; - return bun.String.createUTF8(buffer.list.items); + return bun.String.cloneUTF8(buffer.list.items); } }; diff --git a/src/http/websocket_client.zig b/src/http/websocket_client.zig index 110b0aa5a9..4dfed2f0ec 100644 --- a/src/http/websocket_client.zig +++ b/src/http/websocket_client.zig @@ -906,7 +906,7 @@ pub fn NewWebSocketClient(comptime ssl: bool) type { this.terminate(ErrorCode.invalid_utf8); return; } - reason = bun.String.createUTF8(body_slice); + reason = bun.String.cloneUTF8(body_slice); @memcpy(final_body_bytes[8..][0..body_len], body_slice); } } diff --git a/src/install/NetworkTask.zig b/src/install/NetworkTask.zig index 8f0e711f06..cc7bf4e5ab 100644 --- a/src/install/NetworkTask.zig +++ b/src/install/NetworkTask.zig @@ -93,8 +93,8 @@ pub fn forManifest( } const tmp = bun.JSC.URL.join( - bun.String.fromUTF8(scope.url.href), - bun.String.fromUTF8(encoded_name), + bun.String.borrowUTF8(scope.url.href), + bun.String.borrowUTF8(encoded_name), ); defer tmp.deref(); diff --git a/src/install/install_binding.zig b/src/install/install_binding.zig index 03a9dc8485..94e67e1fae 100644 --- a/src/install/install_binding.zig +++ b/src/install/install_binding.zig @@ -76,7 +76,7 @@ pub const bun_install_js_bindings = struct { return globalObject.throw("failed to print lockfile as JSON: {s}", .{@errorName(err)}); }; - var str = bun.String.createUTF8(buffer.list.items); + var str = bun.String.cloneUTF8(buffer.list.items); defer str.deref(); return str.toJSByParseJSON(globalObject); diff --git a/src/install/npm.zig b/src/install/npm.zig index b4fd767b1b..eed840e4d9 100644 --- a/src/install/npm.zig +++ b/src/install/npm.zig @@ -1423,7 +1423,7 @@ pub const PackageManifest = struct { try writer.print("\"{}\",", .{version.fmt(package_manifest.string_buf)}); } - var result = bun.String.fromUTF8(buf.items); + var result = bun.String.borrowUTF8(buf.items); defer result.deref(); return result.toJSByParseJSON(global); diff --git a/src/main.zig b/src/main.zig index c97fe2e143..49d2ff1352 100644 --- a/src/main.zig +++ b/src/main.zig @@ -23,6 +23,25 @@ pub extern "c" var environ: ?*anyopaque; pub fn main() void { bun.crash_handler.init(); + // HERE! + const baby_list = bun.BabyList([]const u8){}; + const Ref = bun.bundle_v2.Ref; + + // Test different Ref types + const invalid_ref = Ref{ .tag = .invalid }; + const symbol_ref = Ref{ .inner_index = 42, .tag = .symbol, .source_index = 1 }; + const allocated_name_ref = Ref{ .inner_index = 100, .tag = .allocated_name, .source_index = 2 }; + const source_contents_ref = Ref{ .inner_index = 255, .tag = .source_contents_slice, .source_index = 3 }; + const str = bun.PathString.init("hello"); + + // Set a breakpoint here to inspect the refs + _ = baby_list; + _ = invalid_ref; + _ = symbol_ref; + _ = allocated_name_ref; + _ = source_contents_ref; + _ = str; + if (Environment.isPosix) { var act: std.posix.Sigaction = .{ .handler = .{ .handler = std.posix.SIG.IGN }, diff --git a/src/patch.zig b/src/patch.zig index df7c262fb1..4f4282f23f 100644 --- a/src/patch.zig +++ b/src/patch.zig @@ -1151,7 +1151,7 @@ pub const TestingAPIs = struct { defer patchfile.deinit(bun.default_allocator); const str = try std.json.stringifyAlloc(bun.default_allocator, patchfile, .{}); - const outstr = bun.String.fromUTF8(str); + const outstr = bun.String.borrowUTF8(str); return outstr.toJS(globalThis); } diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 9407090c06..46dbabd64b 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -1988,7 +1988,7 @@ export fn ResolvePath__joinAbsStringBufCurrentPlatformBunString( .auto, ); - return bun.String.createUTF8(out_slice); + return bun.String.cloneUTF8(out_slice); } pub fn platformToPosixInPlace(comptime T: type, path_buffer: []T) void { diff --git a/src/shell/shell.zig b/src/shell/shell.zig index 01eaa54bc0..38b3fb8281 100644 --- a/src/shell/shell.zig +++ b/src/shell/shell.zig @@ -87,7 +87,7 @@ pub const ShellErr = union(enum) { return globalThis.throwValue(err); }, .custom => { - const err_value = bun.String.createUTF8(this.custom).toErrorInstance(globalThis); + const err_value = bun.String.cloneUTF8(this.custom).toErrorInstance(globalThis); return globalThis.throwValue(err_value); // this.bunVM().allocator.free(JSC.ZigString.untagged(str._unsafe_ptr_do_not_use)[0..str.len]); }, @@ -3912,7 +3912,7 @@ pub const ShellSrcBuilder = struct { if (!invalid) return false; if (allow_escape) { if (needsEscapeUtf8AsciiLatin1(utf8)) { - const bunstr = bun.String.createUTF8(utf8); + const bunstr = bun.String.cloneUTF8(utf8); defer bunstr.deref(); try this.appendJSStrRef(bunstr); return true; diff --git a/src/sourcemap/sourcemap.zig b/src/sourcemap/sourcemap.zig index b580dfe61d..d6d512e092 100644 --- a/src/sourcemap/sourcemap.zig +++ b/src/sourcemap/sourcemap.zig @@ -491,12 +491,12 @@ pub const Mapping = struct { const name = source_map.external_source_names[@intCast(lookup.mapping.source_index)]; if (source_map.is_standalone_module_graph) { - return bun.String.createUTF8(name); + return bun.String.cloneUTF8(name); } if (std.fs.path.isAbsolute(base_filename)) { const dir = bun.path.dirname(base_filename, .auto); - return bun.String.createUTF8(bun.path.joinAbs(dir, .auto, name)); + return bun.String.cloneUTF8(bun.path.joinAbs(dir, .auto, name)); } return bun.String.init(name); diff --git a/src/sql/postgres/DataCell.zig b/src/sql/postgres/DataCell.zig index f2f8dd5f00..8f62fcc4da 100644 --- a/src/sql/postgres/DataCell.zig +++ b/src/sql/postgres/DataCell.zig @@ -286,7 +286,7 @@ pub const DataCell = extern struct { const buffer = if (needs_dynamic_buffer) try bun.default_allocator.alloc(u8, str_bytes.len) else stack_buffer[0..]; defer if (needs_dynamic_buffer) bun.default_allocator.free(buffer); const unescaped = unescapePostgresString(str_bytes, buffer) catch return error.InvalidByteSequence; - try array.append(bun.default_allocator, DataCell{ .tag = .json, .value = .{ .json = if (unescaped.len > 0) String.createUTF8(unescaped).value.WTFStringImpl else null }, .free_value = 1 }); + try array.append(bun.default_allocator, DataCell{ .tag = .json, .value = .{ .json = if (unescaped.len > 0) String.cloneUTF8(unescaped).value.WTFStringImpl else null }, .free_value = 1 }); slice = trySlice(slice, current_idx + 1); continue; }, @@ -303,7 +303,7 @@ pub const DataCell = extern struct { const buffer = if (needs_dynamic_buffer) try bun.default_allocator.alloc(u8, str_bytes.len) else stack_buffer[0..]; defer if (needs_dynamic_buffer) bun.default_allocator.free(buffer); const string_bytes = unescapePostgresString(str_bytes, buffer) catch return error.InvalidByteSequence; - try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (string_bytes.len > 0) String.createUTF8(string_bytes).value.WTFStringImpl else null }, .free_value = 1 }); + try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (string_bytes.len > 0) String.cloneUTF8(string_bytes).value.WTFStringImpl else null }, .free_value = 1 }); slice = trySlice(slice, current_idx + 1); continue; @@ -374,9 +374,9 @@ pub const DataCell = extern struct { } else { // the only escape sequency possible here is \b if (bun.strings.eqlComptime(element, "\\b")) { - try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = bun.String.createUTF8("\x08").value.WTFStringImpl }, .free_value = 1 }); + try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = bun.String.cloneUTF8("\x08").value.WTFStringImpl }, .free_value = 1 }); } else { - try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (element.len > 0) bun.String.createUTF8(element).value.WTFStringImpl else null }, .free_value = 0 }); + try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (element.len > 0) bun.String.cloneUTF8(element).value.WTFStringImpl else null }, .free_value = 0 }); } } slice = trySlice(slice, current_idx); @@ -536,7 +536,7 @@ pub const DataCell = extern struct { if (bigint) { try array.append(bun.default_allocator, DataCell{ .tag = .int8, .value = .{ .int8 = std.fmt.parseInt(i64, element, 0) catch return error.UnsupportedArrayFormat } }); } else { - try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (element.len > 0) bun.String.createUTF8(element).value.WTFStringImpl else null }, .free_value = 1 }); + try array.append(bun.default_allocator, DataCell{ .tag = .string, .value = .{ .string = if (element.len > 0) bun.String.cloneUTF8(element).value.WTFStringImpl else null }, .free_value = 1 }); } slice = trySlice(slice, current_idx); continue; @@ -667,7 +667,7 @@ pub const DataCell = extern struct { // .int8 is a 64-bit integer always string return DataCell{ .tag = .int8, .value = .{ .int8 = std.fmt.parseInt(i64, bytes, 0) catch 0 } }; } else { - return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) bun.String.createUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; + return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) bun.String.cloneUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; } }, .float8 => { @@ -697,14 +697,14 @@ pub const DataCell = extern struct { // if is binary format lets display as a string because JS cant handle it in a safe way const result = parseBinaryNumeric(bytes, &numeric_buffer) catch return error.UnsupportedNumericFormat; - return DataCell{ .tag = .string, .value = .{ .string = bun.String.createUTF8(result.slice()).value.WTFStringImpl }, .free_value = 1 }; + return DataCell{ .tag = .string, .value = .{ .string = bun.String.cloneUTF8(result.slice()).value.WTFStringImpl }, .free_value = 1 }; } else { // nice text is actually what we want here - return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) String.createUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; + return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) String.cloneUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; } }, .jsonb, .json => { - return DataCell{ .tag = .json, .value = .{ .json = if (bytes.len > 0) String.createUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; + return DataCell{ .tag = .json, .value = .{ .json = if (bytes.len > 0) String.cloneUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; }, .bool => { if (binary) { @@ -790,7 +790,7 @@ pub const DataCell = extern struct { return try parseArray(bytes, bigint, tag, globalObject, null, false); }, else => { - return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) bun.String.createUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; + return DataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) bun.String.cloneUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 }; }, } } diff --git a/src/sql/postgres/protocol/FieldMessage.zig b/src/sql/postgres/protocol/FieldMessage.zig index d3d2c1fdbf..03f0978c86 100644 --- a/src/sql/postgres/protocol/FieldMessage.zig +++ b/src/sql/postgres/protocol/FieldMessage.zig @@ -53,25 +53,25 @@ pub const FieldMessage = union(FieldType) { pub fn init(tag: FieldType, message: []const u8) !FieldMessage { return switch (tag) { - .severity => FieldMessage{ .severity = String.createUTF8(message) }, + .severity => FieldMessage{ .severity = String.cloneUTF8(message) }, // Ignore this one for now. // .localized_severity => FieldMessage{ .localized_severity = String.createUTF8(message) }, - .code => FieldMessage{ .code = String.createUTF8(message) }, - .message => FieldMessage{ .message = String.createUTF8(message) }, - .detail => FieldMessage{ .detail = String.createUTF8(message) }, - .hint => FieldMessage{ .hint = String.createUTF8(message) }, - .position => FieldMessage{ .position = String.createUTF8(message) }, - .internal_position => FieldMessage{ .internal_position = String.createUTF8(message) }, - .internal => FieldMessage{ .internal = String.createUTF8(message) }, - .where => FieldMessage{ .where = String.createUTF8(message) }, - .schema => FieldMessage{ .schema = String.createUTF8(message) }, - .table => FieldMessage{ .table = String.createUTF8(message) }, - .column => FieldMessage{ .column = String.createUTF8(message) }, - .datatype => FieldMessage{ .datatype = String.createUTF8(message) }, - .constraint => FieldMessage{ .constraint = String.createUTF8(message) }, - .file => FieldMessage{ .file = String.createUTF8(message) }, - .line => FieldMessage{ .line = String.createUTF8(message) }, - .routine => FieldMessage{ .routine = String.createUTF8(message) }, + .code => FieldMessage{ .code = String.cloneUTF8(message) }, + .message => FieldMessage{ .message = String.cloneUTF8(message) }, + .detail => FieldMessage{ .detail = String.cloneUTF8(message) }, + .hint => FieldMessage{ .hint = String.cloneUTF8(message) }, + .position => FieldMessage{ .position = String.cloneUTF8(message) }, + .internal_position => FieldMessage{ .internal_position = String.cloneUTF8(message) }, + .internal => FieldMessage{ .internal = String.cloneUTF8(message) }, + .where => FieldMessage{ .where = String.cloneUTF8(message) }, + .schema => FieldMessage{ .schema = String.cloneUTF8(message) }, + .table => FieldMessage{ .table = String.cloneUTF8(message) }, + .column => FieldMessage{ .column = String.cloneUTF8(message) }, + .datatype => FieldMessage{ .datatype = String.cloneUTF8(message) }, + .constraint => FieldMessage{ .constraint = String.cloneUTF8(message) }, + .file => FieldMessage{ .file = String.cloneUTF8(message) }, + .line => FieldMessage{ .line = String.cloneUTF8(message) }, + .routine => FieldMessage{ .routine = String.cloneUTF8(message) }, else => error.UnknownFieldType, }; } diff --git a/src/sql/postgres/protocol/NegotiateProtocolVersion.zig b/src/sql/postgres/protocol/NegotiateProtocolVersion.zig index 9b80f0fdd2..3a86b4d954 100644 --- a/src/sql/postgres/protocol/NegotiateProtocolVersion.zig +++ b/src/sql/postgres/protocol/NegotiateProtocolVersion.zig @@ -27,7 +27,7 @@ pub fn decodeInternal( if (option.slice().len == 0) break; defer option.deinit(); this.unrecognized_options.appendAssumeCapacity( - String.fromUTF8(option), + String.borrowUTF8(option), ); } } diff --git a/src/sql/postgres/protocol/NewReader.zig b/src/sql/postgres/protocol/NewReader.zig index 932d4d334d..da97c5e956 100644 --- a/src/sql/postgres/protocol/NewReader.zig +++ b/src/sql/postgres/protocol/NewReader.zig @@ -98,7 +98,7 @@ pub fn NewReaderWrap( pub fn String(this: @This()) !bun.String { var result = try this.readZ(); defer result.deinit(); - return bun.String.fromUTF8(result.slice()); + return bun.String.borrowUTF8(result.slice()); } }; } diff --git a/src/sql/postgres/types/PostgresString.zig b/src/sql/postgres/types/PostgresString.zig index f2e4cb4292..431f28e474 100644 --- a/src/sql/postgres/types/PostgresString.zig +++ b/src/sql/postgres/types/PostgresString.zig @@ -8,7 +8,7 @@ pub fn toJSWithType( ) AnyPostgresError!JSValue { switch (comptime Type) { [:0]u8, []u8, []const u8, [:0]const u8 => { - var str = bun.String.fromUTF8(value); + var str = bun.String.borrowUTF8(value); defer str.deinit(); return str.toJS(globalThis); }, @@ -18,7 +18,7 @@ pub fn toJSWithType( }, *Data => { - var str = bun.String.fromUTF8(value.slice()); + var str = bun.String.borrowUTF8(value.slice()); defer str.deinit(); defer value.deinit(); return str.toJS(globalThis); diff --git a/src/sql/postgres/types/json.zig b/src/sql/postgres/types/json.zig index 0aaa37c173..04b31275df 100644 --- a/src/sql/postgres/types/json.zig +++ b/src/sql/postgres/types/json.zig @@ -6,7 +6,7 @@ pub fn toJS( value: *Data, ) AnyPostgresError!JSValue { defer value.deinit(); - var str = bun.String.fromUTF8(value.slice()); + var str = bun.String.borrowUTF8(value.slice()); defer str.deref(); const parse_result = JSValue.parse(str.toJS(globalObject), globalObject); if (parse_result.AnyPostgresError()) { diff --git a/src/string.zig b/src/string.zig index 2a257fed2e..d0b1195bc4 100644 --- a/src/string.zig +++ b/src/string.zig @@ -117,7 +117,7 @@ pub const String = extern struct { } } - return createUTF8(utf8_slice); + return cloneUTF8(utf8_slice); } fn createUninitializedLatin1(len: usize) struct { String, []u8 } { @@ -172,7 +172,7 @@ pub const String = extern struct { }; } - pub fn createLatin1(bytes: []const u8) String { + pub fn cloneLatin1(bytes: []const u8) String { JSC.markBinding(@src()); if (bytes.len == 0) return String.empty; return validateRefCount(BunString__fromLatin1(bytes.ptr, bytes.len)); @@ -190,11 +190,11 @@ pub const String = extern struct { return this; } - pub fn createUTF8(bytes: []const u8) String { + pub fn cloneUTF8(bytes: []const u8) String { return JSC.WebCore.encoding.toBunStringComptime(bytes, .utf8); } - pub fn createUTF16(bytes: []const u16) String { + pub fn cloneUTF16(bytes: []const u16) String { if (bytes.len == 0) return String.empty; if (bun.strings.firstNonASCII16([]const u16, bytes) == null) { return validateRefCount(BunString__fromUTF16ToLatin1(bytes.ptr, bytes.len)); @@ -211,13 +211,13 @@ pub const String = extern struct { const alloc = sba.get(); const buf = try std.fmt.allocPrint(alloc, fmt, args); defer alloc.free(buf); - return createUTF8(buf); + return cloneUTF8(buf); } pub fn createFromOSPath(os_path: bun.OSPathSlice) String { return switch (@TypeOf(os_path)) { - []const u8 => createUTF8(os_path), - []const u16 => createUTF16(os_path), + []const u8 => cloneUTF8(os_path), + []const u16 => cloneUTF16(os_path), else => @compileError("unreachable"), }; } @@ -248,7 +248,7 @@ pub const String = extern struct { return new; } - return createUTF8(this.byteSlice()); + return cloneUTF8(this.byteSlice()); } extern fn BunString__createAtom(bytes: [*]const u8, len: usize) String; @@ -279,7 +279,7 @@ pub const String = extern struct { } } - return createUTF8(bytes); + return cloneUTF8(bytes); } pub fn utf8ByteLength(this: String) usize { @@ -475,7 +475,7 @@ pub const String = extern struct { /// - `value` is borrowed. /// - Never allocates or copies any memory /// - Does not increment reference counts - pub fn fromUTF8(value: []const u8) String { + pub fn borrowUTF8(value: []const u8) String { return String.init(ZigString.initUTF8(value)); } @@ -488,7 +488,7 @@ pub const String = extern struct { /// - `value` is borrowed. /// - Never allocates or copies any memory /// - Does not increment reference counts - pub fn fromUTF16(value: []const u16) String { + pub fn borrowUTF16(value: []const u16) String { return String.init(ZigString.initUTF16(value)); } diff --git a/src/sys.zig b/src/sys.zig index 6ab9d10606..d372aea12b 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -374,8 +374,8 @@ pub const Error = struct { var that = self.withoutPath().toShellSystemError(); bun.debugAssert(that.path.tag != .WTFStringImpl); bun.debugAssert(that.dest.tag != .WTFStringImpl); - that.path = bun.String.fromUTF8(self.path); - that.dest = bun.String.fromUTF8(self.dest); + that.path = bun.String.borrowUTF8(self.path); + that.dest = bun.String.borrowUTF8(self.dest); bun.debugAssert(that.path.tag != .WTFStringImpl); bun.debugAssert(that.dest.tag != .WTFStringImpl); @@ -555,11 +555,11 @@ pub const Error = struct { } if (this.path.len > 0) { - err.path = bun.String.createUTF8(this.path); + err.path = bun.String.cloneUTF8(this.path); } if (this.dest.len > 0) { - err.dest = bun.String.createUTF8(this.dest); + err.dest = bun.String.cloneUTF8(this.dest); } if (this.fd.unwrapValid()) |valid| { @@ -618,14 +618,14 @@ pub const Error = struct { } break :message stream.getWritten(); }; - err.message = bun.String.createUTF8(message); + err.message = bun.String.cloneUTF8(message); if (this.path.len > 0) { - err.path = bun.String.createUTF8(this.path); + err.path = bun.String.cloneUTF8(this.path); } if (this.dest.len > 0) { - err.dest = bun.String.createUTF8(this.dest); + err.dest = bun.String.cloneUTF8(this.dest); } if (this.fd.unwrapValid()) |valid| { diff --git a/src/unit_test.zig b/src/unit_test.zig index f17f0887fd..8054eafeec 100644 --- a/src/unit_test.zig +++ b/src/unit_test.zig @@ -8,7 +8,7 @@ test { } test "basic string usage" { - var s = bun.String.createUTF8("hi"); + var s = bun.String.cloneUTF8("hi"); defer s.deref(); try t.expect(s.tag != .Dead and s.tag != .Empty); try t.expectEqual(s.length(), 2); diff --git a/src/url.zig b/src/url.zig index d1b7e198e8..a080d2c3a0 100644 --- a/src/url.zig +++ b/src/url.zig @@ -75,7 +75,7 @@ pub const URL = struct { } pub fn fromUTF8(allocator: std.mem.Allocator, input: []const u8) !URL { - return fromString(allocator, bun.String.fromUTF8(input)); + return fromString(allocator, bun.String.borrowUTF8(input)); } pub fn isLocalhost(this: *const URL) bool {