Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
paperdave committed Oct 11, 2024
1 parent 1ec981e commit 157c673
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ pub fn inspect(

// we are going to always clone to keep things simple for now
// the common case here will be stack-allocated, so it should be fine
var out = ZigString.init(array.toOwnedSliceLeaky()).withEncoding();
var out = ZigString.init(array.slice()).withEncoding();
const ret = out.toJS(globalThis);
array.deinit();
return ret;
Expand Down Expand Up @@ -3932,7 +3932,7 @@ const TOMLObject = struct {
return .zero;
};

const slice = writer.ctx.buffer.toOwnedSliceLeaky();
const slice = writer.ctx.buffer.slice();
var out = bun.String.fromUTF8(slice);
defer out.deref();

Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ pub const JSBundler = struct {
self.public_path.deinit();
self.conditions.deinit();
self.drop.deinit();
self.banner.deinit();
self.footer.deinit();
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/html_rewriter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ pub const HTMLRewriter = struct {

// pub fn done(this: *StreamOutputSink) void {
// var prev_value = this.response.body.value;
// var bytes = this.bytes.toOwnedSliceLeaky();
// var bytes = this.bytes.slice();
// this.response.body.value = .{
// .Blob = JSC.WebCore.Blob.init(bytes, this.bytes.allocator, this.global),
// };
Expand Down
12 changes: 6 additions & 6 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3129,7 +3129,7 @@ pub const JSGlobalObject = opaque {
return ZigString.static(fmt).toErrorInstance(this);

// Ensure we clone it.
var str = ZigString.initUTF8(buf.toOwnedSliceLeaky());
var str = ZigString.initUTF8(buf.slice());

return str.toErrorInstance(this);
} else {
Expand All @@ -3148,7 +3148,7 @@ pub const JSGlobalObject = opaque {
defer buf.deinit();
var writer = buf.writer();
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
var str = ZigString.fromUTF8(buf.slice());
return str.toTypeErrorInstance(this);
} else {
return ZigString.static(fmt).toTypeErrorInstance(this);
Expand All @@ -3162,7 +3162,7 @@ pub const JSGlobalObject = opaque {
defer buf.deinit();
var writer = buf.writer();
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
var str = ZigString.fromUTF8(buf.slice());
return str.toSyntaxErrorInstance(this);
} else {
return ZigString.static(fmt).toSyntaxErrorInstance(this);
Expand All @@ -3176,7 +3176,7 @@ pub const JSGlobalObject = opaque {
defer buf.deinit();
var writer = buf.writer();
writer.print(fmt, args) catch return ZigString.static(fmt).toErrorInstance(this);
var str = ZigString.fromUTF8(buf.toOwnedSliceLeaky());
var str = ZigString.fromUTF8(buf.slice());
return str.toRangeErrorInstance(this);
} else {
return ZigString.static(fmt).toRangeErrorInstance(this);
Expand Down Expand Up @@ -4619,7 +4619,7 @@ pub const JSValue = enum(JSValueReprInt) {

var writer = buf.writer();
try writer.print(fmt, args);
return String.init(buf.toOwnedSliceLeaky()).toJS(globalThis);
return String.init(buf.slice()).toJS(globalThis);
}

/// Create a JSValue string from a zig format-print (fmt + args), with pretty format
Expand All @@ -4633,7 +4633,7 @@ pub const JSValue = enum(JSValueReprInt) {
switch (Output.enable_ansi_colors) {
inline else => |enabled| try writer.print(Output.prettyFmt(fmt, enabled), args),
}
return String.init(buf.toOwnedSliceLeaky()).toJS(globalThis);
return String.init(buf.slice()).toJS(globalThis);
}

pub fn fromEntries(globalThis: *JSGlobalObject, keys_array: [*c]ZigString, values_array: [*c]ZigString, strings_count: usize, clone: bool) JSValue {
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2143,7 +2143,7 @@ pub const ModuleLoader = struct {
writer.writeAll(";\n") catch bun.outOfMemory();
}

const public_url = bun.String.createUTF8(buf.toOwnedSliceLeaky());
const public_url = bun.String.createUTF8(buf.slice());
return ResolvedSource{
.allocator = &jsc_vm.allocator,
.source_code = public_url,
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/test/diff_format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ pub const DiffFormatter = struct {
buffered_writer.flush() catch unreachable;
}

const received_slice = received_buf.toOwnedSliceLeaky();
const expected_slice = expected_buf.toOwnedSliceLeaky();
const received_slice = received_buf.slice();
const expected_slice = expected_buf.slice();

if (this.not) {
const not_fmt = "Expected: not <green>{s}<r>";
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2757,7 +2757,7 @@ pub const Expect = struct {
};
defer pretty_value.deinit();

if (strings.eqlLong(pretty_value.toOwnedSliceLeaky(), saved_value, true)) {
if (strings.eqlLong(pretty_value.slice(), saved_value, true)) {
Jest.runner.?.snapshots.passed += 1;
return .undefined;
}
Expand All @@ -2766,7 +2766,7 @@ pub const Expect = struct {
const signature = comptime getSignature("toMatchSnapshot", "<green>expected<r>", false);
const fmt = signature ++ "\n\n{any}\n";
const diff_format = DiffFormatter{
.received_string = pretty_value.toOwnedSliceLeaky(),
.received_string = pretty_value.slice(),
.expected_string = saved_value,
.globalThis = globalThis,
};
Expand Down Expand Up @@ -5443,7 +5443,7 @@ pub const ExpectCustomAsymmetricMatcher = struct {
return .zero;
};
if (printed) {
return bun.String.init(mutable_string.toOwnedSliceLeaky()).toJS();
return bun.String.init(mutable_string.slice()).toJS();
}
return ExpectMatcherUtils.printValue(globalThis, this, null);
}
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ inline fn createScope(
buffer.reset();
appendParentLabel(&buffer, parent) catch @panic("Bun ran out of memory while filtering tests");
buffer.append(label) catch unreachable;
const str = bun.String.fromBytes(buffer.toOwnedSliceLeaky());
const str = bun.String.fromBytes(buffer.slice());
is_skip = !regex.matches(str);
if (is_skip) {
tag_to_use = .skip;
Expand Down Expand Up @@ -2087,7 +2087,7 @@ fn eachBind(
buffer.reset();
appendParentLabel(&buffer, parent) catch @panic("Bun ran out of memory while filtering tests");
buffer.append(formattedLabel) catch unreachable;
const str = bun.String.fromBytes(buffer.toOwnedSliceLeaky());
const str = bun.String.fromBytes(buffer.slice());
is_skip = !regex.matches(str);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/web_worker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ pub const WebWorker = struct {
bun.outOfMemory();
};
JSC.markBinding(@src());
WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.toOwnedSliceLeaky()), error_instance);
WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.slice()), error_instance);
if (vm.worker) |worker_| {
_ = worker.setRequestedTerminate();
worker.parent_poll_ref.unrefConcurrently(worker.parent);
Expand Down
16 changes: 8 additions & 8 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ pub const BundleV2 = struct {
.entry_points = config.entry_points.keys(),
.target = config.target.toAPI(),
.absolute_working_dir = if (config.dir.list.items.len > 0)
config.dir.toOwnedSliceLeaky()
config.dir.slice()
else
null,
.inject = &.{},
Expand Down Expand Up @@ -1467,8 +1467,8 @@ pub const BundleV2 = struct {
bundler.options.output_format = config.format;
bundler.options.bytecode = config.bytecode;

bundler.options.output_dir = config.outdir.toOwnedSliceLeaky();
bundler.options.root_dir = config.rootdir.toOwnedSliceLeaky();
bundler.options.output_dir = config.outdir.slice();
bundler.options.root_dir = config.rootdir.slice();
bundler.options.minify_syntax = config.minify.syntax;
bundler.options.minify_whitespace = config.minify.whitespace;
bundler.options.minify_identifiers = config.minify.identifiers;
Expand All @@ -1479,8 +1479,8 @@ pub const BundleV2 = struct {
bundler.options.emit_dce_annotations = config.emit_dce_annotations orelse !config.minify.whitespace;
bundler.options.ignore_dce_annotations = config.ignore_dce_annotations;
bundler.options.experimental_css = config.experimental_css;
bundler.options.banner = config.banner.toOwnedSliceLeaky();
bundler.options.footer = config.footer.toOwnedSliceLeaky();
bundler.options.banner = config.banner.slice();
bundler.options.footer = config.footer.slice();

bundler.configureLinker();
try bundler.configureDefines();
Expand Down Expand Up @@ -1546,7 +1546,7 @@ pub const BundleV2 = struct {
bun.default_allocator.dupe(
u8,
bun.path.joinAbsString(
this.config.outdir.toOwnedSliceLeaky(),
this.config.outdir.slice(),
&[_]string{output_file.dest_path},
.auto,
),
Expand All @@ -1556,7 +1556,7 @@ pub const BundleV2 = struct {
u8,
bun.path.joinAbsString(
Fs.FileSystem.instance.top_level_dir,
&[_]string{ this.config.dir.toOwnedSliceLeaky(), this.config.outdir.toOwnedSliceLeaky(), output_file.dest_path },
&[_]string{ this.config.dir.slice(), this.config.outdir.slice(), output_file.dest_path },
.auto,
),
) catch unreachable
Expand Down Expand Up @@ -8951,7 +8951,7 @@ pub const LinkerContext = struct {
const input = c.parse_graph.input_files.items(.source)[chunk.entry_point.source_index].path;
var buf = MutableString.initEmpty(worker.allocator);
js_printer.quoteForJSONBuffer(input.pretty, &buf, true) catch bun.outOfMemory();
const str = buf.toOwnedSliceLeaky(); // worker.allocator is an arena
const str = buf.slice(); // worker.allocator is an arena
j.pushStatic(str);
line_offset.advance(str);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/upgrade_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub const UpgradeCommand = struct {
else => return error.HTTPError,
}

const bytes = zip_file_buffer.toOwnedSliceLeaky();
const bytes = zip_file_buffer.slice();

progress.end();
refresher.refresh();
Expand Down
6 changes: 3 additions & 3 deletions src/js_printer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5804,7 +5804,7 @@ const FileWriterInternal = struct {
ctx: *FileWriterInternal,
) anyerror!void {
defer buffer.reset();
const result_ = buffer.toOwnedSliceLeaky();
const result_ = buffer.slice();
var result = result_;

while (result.len > 0) {
Expand Down Expand Up @@ -5955,9 +5955,9 @@ pub const BufferWriter = struct {

if (ctx.append_null_byte) {
ctx.sentinel = ctx.buffer.toOwnedSentinelLeaky();
ctx.written = ctx.buffer.toOwnedSliceLeaky();
ctx.written = ctx.buffer.slice();
} else {
ctx.written = ctx.buffer.toOwnedSliceLeaky();
ctx.written = ctx.buffer.slice();
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/renamer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,9 @@ pub const NumberRenamer = struct {
mutable_name.appendSlice(prefix) catch unreachable;
mutable_name.appendInt(tries) catch unreachable;

switch (NameUse.find(this, mutable_name.toOwnedSliceLeaky())) {
switch (NameUse.find(this, mutable_name.slice())) {
.unused => {
name = mutable_name.toOwnedSliceLeaky();
name = mutable_name.slice();

if (use == .same_scope) {
const existing = this.name_counts.getOrPut(allocator, prefix) catch unreachable;
Expand All @@ -775,7 +775,7 @@ pub const NumberRenamer = struct {

tries += 1;

switch (NameUse.find(this, mutable_name.toOwnedSliceLeaky())) {
switch (NameUse.find(this, mutable_name.slice())) {
.unused => {
if (cur_use == .same_scope) {
const existing = this.name_counts.getOrPut(allocator, prefix) catch unreachable;
Expand All @@ -790,7 +790,7 @@ pub const NumberRenamer = struct {
existing.value_ptr.* = tries;
}

name = mutable_name.toOwnedSliceLeaky();
name = mutable_name.slice();
break;
},
else => {},
Expand Down Expand Up @@ -847,7 +847,7 @@ pub const ExportRenamer = struct {
var writer = this.string_buffer.writer();
writer.print("{s}{d}", .{ input, tries }) catch unreachable;
tries += 1;
const attempt = this.string_buffer.toOwnedSliceLeaky();
const attempt = this.string_buffer.slice();
entry = this.used.getOrPut(attempt) catch unreachable;
if (!entry.found_existing) {
const to_use = this.string_buffer.allocator.dupe(u8, attempt) catch unreachable;
Expand Down
2 changes: 1 addition & 1 deletion src/sourcemap/CodeCoverage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ pub const ByteRangeMapping = struct {
return .zero;
};

var str = bun.String.createUTF8(mutable_str.toOwnedSliceLeaky());
var str = bun.String.createUTF8(mutable_str.slice());
defer str.deref();
return str.toJS(globalThis);
}
Expand Down
19 changes: 7 additions & 12 deletions src/string_mutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,16 @@ pub const MutableString = struct {
try self.list.ensureUnusedCapacity(self.allocator, amount);
}

pub inline fn appendSlice(self: *MutableString, slice: []const u8) !void {
try self.list.appendSlice(self.allocator, slice);
pub inline fn appendSlice(self: *MutableString, items: []const u8) !void {
try self.list.appendSlice(self.allocator, items);
}

pub inline fn appendSliceExact(self: *MutableString, slice: []const u8) !void {
pub inline fn appendSliceExact(self: *MutableString, items: []const u8) !void {
if (slice.len == 0) return;

try self.list.ensureTotalCapacityPrecise(self.allocator, self.list.items.len + slice.len);
try self.list.ensureTotalCapacityPrecise(self.allocator, self.list.items.len + items.len);
var end = self.list.items.ptr + self.list.items.len;
self.list.items.len += slice.len;
@memcpy(end[0..slice.len], slice);
self.list.items.len += items.len;
@memcpy(end[0..items.len], items);
}

pub inline fn reset(
Expand Down Expand Up @@ -237,7 +236,7 @@ pub const MutableString = struct {
return self.list.toOwnedSlice(self.allocator) catch bun.outOfMemory(); // TODO
}

pub fn toOwnedSliceLeaky(self: *MutableString) []u8 {
pub fn slice(self: *MutableString) []u8 {
return self.list.items;
}

Expand All @@ -264,10 +263,6 @@ pub const MutableString = struct {
return self.list.toOwnedSlice(self.allocator) catch bun.outOfMemory(); // TODO
}

// pub fn deleteAt(self: *MutableString, i: usize) {
// self.list.swapRemove(i);
// }

pub fn containsChar(self: *const MutableString, char: u8) bool {
return self.indexOfChar(char) != null;
}
Expand Down

0 comments on commit 157c673

Please sign in to comment.