Skip to content

Commit

Permalink
Merge branch 'main' into jarred/ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Feb 3, 2024
2 parents 7fc0bce + e0c0fe2 commit 4ed5404
Show file tree
Hide file tree
Showing 31 changed files with 306 additions and 109 deletions.
1 change: 1 addition & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"name": "Debug",
"forcedInclude": ["${workspaceFolder}/src/bun.js/bindings/root.h"],
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"includePath": [
"${workspaceFolder}/build/bun-webkit/include",
"${workspaceFolder}/build/codegen",
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "modificationsIfAvailable",
"editor.formatOnSaveMode": "file",

// Search
"search.quickOpen.includeSymbols": false,
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(DEBUG ON)
set(DEFAULT_ZIG_OPTIMIZE "Debug")
set(bun "bun-debug")

# COMPILE_COMMANDS
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
set(DEBUG OFF)
set(DEFAULT_ZIG_OPTIMIZE "ReleaseFast")
Expand Down
22 changes: 19 additions & 3 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,30 @@ pub fn openInEditor(
}

pub fn getPublicPath(to: string, origin: URL, comptime Writer: type, writer: Writer) void {
return getPublicPathWithAssetPrefix(to, VirtualMachine.get().bundler.fs.top_level_dir, origin, VirtualMachine.get().bundler.options.routes.asset_prefix_path, comptime Writer, writer);
return getPublicPathWithAssetPrefix(
to,
VirtualMachine.get().bundler.fs.top_level_dir,
origin,
VirtualMachine.get().bundler.options.routes.asset_prefix_path,
comptime Writer,
writer,
.loose,
);
}

pub fn getPublicPathWithAssetPrefix(to: string, dir: string, origin: URL, asset_prefix: string, comptime Writer: type, writer: Writer) void {
pub fn getPublicPathWithAssetPrefix(
to: string,
dir: string,
origin: URL,
asset_prefix: string,
comptime Writer: type,
writer: Writer,
comptime platform: bun.path.Platform,
) void {
const relative_path = if (strings.hasPrefix(to, dir))
strings.withoutTrailingSlash(to[dir.len..])
else
VirtualMachine.get().bundler.fs.relative(dir, to);
VirtualMachine.get().bundler.fs.relativePlatform(dir, to, platform);
if (origin.isAbsolute()) {
if (strings.hasPrefix(relative_path, "..") or strings.hasPrefix(relative_path, "./")) {
writer.writeAll(origin.origin) catch return;
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/api/filesystem_router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub const FileSystemRouter = struct {
};

this.arena.deinit();
this.router.deinit();
globalThis.allocator().destroy(this.arena);

this.arena = arena;
Expand Down Expand Up @@ -380,6 +381,7 @@ pub const FileSystemRouter = struct {
dir.deref();
}

this.router.deinit();
this.arena.deinit();
}
};
Expand Down Expand Up @@ -586,6 +588,7 @@ pub const MatchedRoute = struct {
if (this.asset_prefix) |prefix| prefix.slice() else "",
@TypeOf(&writer),
&writer,
.posix,
);
return ZigString.init(buf[0..writer.context.pos])
.withEncoding()
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/bindings/CommonJSModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ JSC_DEFINE_HOST_FUNCTION(functionCommonJSModuleRecord_compile, (JSGlobalObject *
moduleObject->sourceCode.set(vm, moduleObject, jsSourceCode);

auto index = filenameString.reverseFind(PLATFORM_SEP, filenameString.length());
// filenameString is coming from js, any separator could be used
if (index == WTF::notFound)
index = filenameString.reverseFind(NOT_PLATFORM_SEP, filenameString.length());
String dirnameString;
if (index != WTF::notFound) {
dirnameString = filenameString.substring(0, index);
Expand Down
6 changes: 2 additions & 4 deletions src/bun.js/bindings/JSBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1495,10 +1495,8 @@ static inline JSC::EncodedJSValue jsBufferToString(JSC::VM& vm, JSC::JSGlobalObj
return JSC::JSValue::encode(JSC::jsEmptyString(vm));
} else {
auto str = String::createUninitialized(u16length, data);
// always zero out the last byte of the string incase the buffer is not a multiple of 2
data[u16length - 1] = 0;
memcpy(data, reinterpret_cast<const char*>(castedThis->typedVector() + offset), length);
return JSC::JSValue::encode(JSC::jsString(vm, WTFMove(str)));
memcpy(reinterpret_cast<void*>(data), reinterpret_cast<void*>(castedThis->typedVector() + offset), u16length * 2);
return JSC::JSValue::encode(JSC::jsString(vm, str));
}

break;
Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/bindings/PathInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#if OS(WINDOWS)
#define PLATFORM_SEP_s "\\"_s
#define PLATFORM_SEP '\\'
#define NOT_PLATFORM_SEP_s "/"_s
#define NOT_PLATFORM_SEP '/'
#else
#define PLATFORM_SEP_s "/"_s
#define PLATFORM_SEP '/'
#define NOT_PLATFORM_SEP_s "\\"_s
#define NOT_PLATFORM_SEP '\\'
#endif

ALWAYS_INLINE bool isAbsolutePath(WTF::String input)
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4046,7 +4046,7 @@ pub const JSValue = enum(JSValueReprInt) {
return jsNumberFromInt32(@as(i32, @intCast(i)));
}

return jsNumberFromDouble(@as(f64, @floatFromInt(@as(i52, @truncate(i)))));
return jsNumberFromDouble(@floatFromInt(i));
}

pub inline fn toJS(this: JSValue, _: *const JSGlobalObject) JSValue {
Expand All @@ -4062,7 +4062,7 @@ pub const JSValue = enum(JSValueReprInt) {
}

pub fn jsNumberFromPtrSize(i: usize) JSValue {
return jsNumberFromDouble(@as(f64, @floatFromInt(@as(i52, @intCast(@as(u51, @truncate(i)))))));
return jsNumberFromDouble(@floatFromInt(i));
}

pub fn coerceDoubleTruncatingIntoInt64(this: JSValue) i64 {
Expand Down
16 changes: 12 additions & 4 deletions src/bundler/entry_points.zig
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,20 @@ pub const MacroEntryPoint = struct {
\\Bun.registerMacro({d}, Macros['{s}']);
,
.{
dir_to_use,
import_path.filename,
bun.fmt.fmtPath(u8, dir_to_use, .{
.escape_backslashes = true,
}),
bun.fmt.fmtPath(u8, import_path.filename, .{
.escape_backslashes = true,
}),
function_name,
function_name,
dir_to_use,
import_path.filename,
bun.fmt.fmtPath(u8, dir_to_use, .{
.escape_backslashes = true,
}),
bun.fmt.fmtPath(u8, import_path.filename, .{
.escape_backslashes = true,
}),
macro_id,
function_name,
},
Expand Down
4 changes: 3 additions & 1 deletion src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ pub fn getRelease(buf: []u8) []const u8 {
if (err != 0) {
return "unknown";
}
return bun.sliceTo(&info.version, 0);
const release = bun.sliceTo(&info.release, 0);
@memcpy(buf[0..release.len], release);
return buf[0..release.len];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/create_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ pub const CreateCommand = struct {

progress_.refresh();

Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path) });
Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path, .{}) });
Global.exit(1);
};
};
Expand All @@ -513,7 +513,7 @@ pub const CreateCommand = struct {
}

CopyFile.copyFile(infile.handle, outfile.handle) catch |err| {
Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path) });
Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path, .{}) });
Global.exit(1);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/feature_flags.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@ pub const concurrent_transpiler = !env.isWindows;
// https://github.com/oven-sh/bun/issues/5426#issuecomment-1813865316
pub const disable_auto_js_to_ts_in_node_modules = true;

pub const runtime_transpiler_cache = !env.isWindows;
pub const runtime_transpiler_cache = true;
86 changes: 80 additions & 6 deletions src/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,107 @@ pub fn formatUTF16Type(comptime Slice: type, slice_: Slice, writer: anytype) !vo
}
}

pub fn formatUTF16TypeEscapeBackslashes(comptime Slice: type, slice_: Slice, writer: anytype) !void {
var chunk = getSharedBuffer();

// Defensively ensure recursion doesn't cause the buffer to be overwritten in-place
shared_temp_buffer_ptr = null;
defer {
if (shared_temp_buffer_ptr) |existing| {
if (existing != chunk.ptr) {
bun.default_allocator.destroy(@as(*SharedTempBuffer, @ptrCast(chunk.ptr)));
}
} else {
shared_temp_buffer_ptr = @ptrCast(chunk.ptr);
}
}

var slice = slice_;

while (slice.len > 0) {
const result = strings.copyUTF16IntoUTF8(chunk, Slice, slice, true);
if (result.read == 0 or result.written == 0)
break;

const to_write = chunk[0..result.written];
var ptr = to_write;
while (strings.indexOfChar(ptr, '\\')) |i| {
try writer.writeAll(ptr[0 .. i + 1]);
try writer.writeAll("\\");
ptr = ptr[i + 1 ..];
}
try writer.writeAll(ptr);
slice = slice[result.read..];
}
}

pub fn formatUTF16(slice_: []align(1) const u16, writer: anytype) !void {
return formatUTF16Type([]align(1) const u16, slice_, writer);
}

pub const FormatUTF16 = struct {
buf: []const u16,
pub fn format(self: @This(), comptime _: []const u8, opts: anytype, writer: anytype) !void {
_ = opts;
try formatUTF16Type([]const u16, self.buf, writer);
escape_backslashes: bool = false,
pub fn format(self: @This(), comptime _: []const u8, _: anytype, writer: anytype) !void {
if (self.escape_backslashes) {
try formatUTF16TypeEscapeBackslashes([]const u16, self.buf, writer);
} else {
try formatUTF16Type([]const u16, self.buf, writer);
}
}
};

pub const FormatUTF8 = struct {
buf: []const u8,
escape_backslashes: bool = false,
pub fn format(self: @This(), comptime _: []const u8, _: anytype, writer: anytype) !void {
try writer.writeAll(self.buf);
if (self.escape_backslashes) {
var ptr = self.buf;
while (strings.indexOfChar(ptr, '\\')) |i| {
try writer.writeAll(ptr[0 .. i + 1]);
try writer.writeAll("\\");
ptr = ptr[i + 1 ..];
}
try writer.writeAll(ptr);
} else {
try writer.writeAll(self.buf);
}
}
};

pub const PathFormatOptions = struct {
escape_backslashes: bool = false,
};

pub fn fmtUTF16(buf: []const u16) FormatUTF16 {
return FormatUTF16{ .buf = buf };
}

pub const FormatOSPath = if (Environment.isWindows) FormatUTF16 else FormatUTF8;

pub fn fmtOSPath(buf: bun.OSPathSlice) FormatOSPath {
return FormatOSPath{ .buf = buf };
pub fn fmtOSPath(buf: bun.OSPathSlice, options: PathFormatOptions) FormatOSPath {
return FormatOSPath{
.buf = buf,
.escape_backslashes = options.escape_backslashes,
};
}

pub fn fmtPath(
comptime T: type,
path: []const T,
options: PathFormatOptions,
) if (T == u8) FormatUTF8 else FormatUTF16 {
if (T == u8) {
return FormatUTF8{
.buf = path,
.escape_backslashes = options.escape_backslashes,
};
}

return FormatUTF16{
.buf = path,
.escape_backslashes = options.escape_backslashes,
};
}

pub fn formatLatin1(slice_: []const u8, writer: anytype) !void {
Expand Down
9 changes: 9 additions & 0 deletions src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ pub const FileSystem = struct {
});
}

pub fn relativePlatform(_: *@This(), from: string, to: string, comptime platform: path_handler.Platform) string {
return @call(.always_inline, path_handler.relativePlatform, .{
from,
to,
platform,
false,
});
}

pub fn relativeTo(f: *@This(), to: string) string {
return @call(.always_inline, path_handler.relative, .{
f.top_level_dir,
Expand Down
10 changes: 5 additions & 5 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ pub const PackageInstall = struct {

progress_.refresh();

Output.prettyErrorln("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path) });
Output.prettyErrorln("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path, .{}) });
Global.crash();
};
};
Expand All @@ -1316,7 +1316,7 @@ pub const PackageInstall = struct {

progress_.refresh();

Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path) });
Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path, .{}) });
Global.crash();
};
} else {
Expand All @@ -1330,7 +1330,7 @@ pub const PackageInstall = struct {

progress_.refresh();

Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path) });
Output.prettyError("<r><red>{s}<r>: copying file {}", .{ @errorName(err), bun.fmt.fmtOSPath(entry.path, .{}) });
Global.crash();
};
}
Expand Down Expand Up @@ -1378,8 +1378,8 @@ pub const PackageInstall = struct {

defer subdir.close();

var buf: if (Environment.isWindows) bun.WPathBuffer else [0]u16 = undefined;
var buf2: if (Environment.isWindows) bun.WPathBuffer else [0]u16 = undefined;
var buf: bun.windows.WPathBuffer = undefined;
var buf2: bun.windows.WPathBuffer = undefined;
var to_copy_buf: []u16 = undefined;
var to_copy_buf2: []u16 = undefined;
if (comptime Environment.isWindows) {
Expand Down
6 changes: 3 additions & 3 deletions src/libarchive/libarchive.zig
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ pub const Archive = struct {
const path_slice: bun.OSPathSlice = pathname.ptr[0..pathname.len];

if (comptime log) {
Output.prettyln(" {}", .{bun.fmt.fmtOSPath(path_slice)});
Output.prettyln(" {}", .{bun.fmt.fmtOSPath(path_slice, .{})});
}

count += 1;
Expand Down Expand Up @@ -699,7 +699,7 @@ pub const Archive = struct {
lib.ARCHIVE_RETRY => {
if (comptime log) {
Output.err("libarchive error", "extracting {}, retry {d} / {d}", .{
bun.fmt.fmtOSPath(path_slice),
bun.fmt.fmtOSPath(path_slice, .{}),
retries_remaining,
5,
});
Expand All @@ -709,7 +709,7 @@ pub const Archive = struct {
if (comptime log) {
const archive_error = std.mem.span(lib.archive_error_string(archive));
Output.err("libarchive error", "extracting {}: {s}", .{
bun.fmt.fmtOSPath(path_slice),
bun.fmt.fmtOSPath(path_slice, .{}),
archive_error,
});
}
Expand Down
Loading

0 comments on commit 4ed5404

Please sign in to comment.