Skip to content

Commit

Permalink
source maps work for app code in bun dev!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Mar 6, 2022
1 parent 0938073 commit 7c5c6cd
Show file tree
Hide file tree
Showing 19 changed files with 1,098 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"search.exclude": {},
"search.followSymlinks": false,
"search.useIgnoreFiles": true,
"zig.buildOnSave": true,
"zig.buildOnSave": false,
"[zig]": {
"editor.defaultFormatter": "AugusteRame.zls-vscode"
},
Expand Down
7 changes: 7 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ pub fn build(b: *std.build.Builder) !void {
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
}

{
const headers_step = b.step("string-bench", "Build string bench");
var headers_obj: *std.build.LibExeObjStep = b.addExecutable("string-bench", "src/bench/string-handling.zig");
defer headers_step.dependOn(&headers_obj.step);
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
}

{
const headers_step = b.step("tgz-obj", "Build tgz (object files)");
var headers_obj: *std.build.LibExeObjStep = b.addObject("tgz", "misctools/tgz.zig");
Expand Down
71 changes: 71 additions & 0 deletions src/bench/string-handling.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const strings = @import("strings");
const std = @import("std");

pub fn main() anyerror!void {
const args = try std.process.argsAlloc(std.heap.c_allocator);
const filepath = args[args.len - 3];
const find = args[args.len - 2];
const amount = try std.fmt.parseInt(usize, args[args.len - 1], 10);
var file = try std.fs.cwd().openFile(filepath, .{ .mode = .read_only });
var contents = try file.readToEndAlloc(std.heap.c_allocator, std.math.maxInt(usize));

{
var timer = try std.time.Timer.start();
var index: usize = std.math.maxInt(usize);
var j: usize = 0;
var i: usize = 0;
while (j < amount) : (j += 1) {
i = 0;
if (strings.indexOf(contents, find)) |k| {
i += k;
index = k;
}
}

if (index == std.math.maxInt(usize)) {
std.debug.print("<vec 32> [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
} else {
std.debug.print("<vec 32> [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
}
}

{
var timer = try std.time.Timer.start();
var index: usize = std.math.maxInt(usize);
var j: usize = 0;
var i: usize = 0;
while (j < amount) : (j += 1) {
i = 0;
if (strings.indexOf16(contents, find)) |k| {
i += k;
index = k;
}
}

if (index == std.math.maxInt(usize)) {
std.debug.print("<vec 16> [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
} else {
std.debug.print("<vec 16> [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
}
}

{
var timer = try std.time.Timer.start();
var index: usize = std.math.maxInt(usize);
var j: usize = 0;
var i: usize = 0;
while (j < amount) : (j += 1) {
i = 0;
if (std.mem.indexOf(u8, contents, find)) |k| {
i += k;
index = k;
}
}

if (index == std.math.maxInt(usize)) {
std.debug.print("<std> [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
} else {
std.debug.print("<std> [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
}
}
}
68 changes: 61 additions & 7 deletions src/bundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,8 @@ pub const Bundler = struct {
watcher: *WatcherType,
client_entry_point: ?*EntryPoints.ClientEntryPoint,
origin: URL,
comptime is_source_map: bool,
source_map_handler: ?js_printer.SourceMapHandler,
) !BuildResolveResultPair {
if (resolve_result.is_external) {
return BuildResolveResultPair{
Expand Down Expand Up @@ -2394,17 +2396,21 @@ pub const Bundler = struct {
if (bundler.options.platform.isBun()) {
return BuildResolveResultPair{
.written = switch (result.ast.exports_kind) {
.esm => try bundler.print(
.esm => try bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
.esm_ascii,
is_source_map,
source_map_handler,
),
.cjs => try bundler.print(
.cjs => try bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
.cjs_ascii,
is_source_map,
source_map_handler,
),
else => unreachable,
},
Expand All @@ -2414,17 +2420,21 @@ pub const Bundler = struct {

return BuildResolveResultPair{
.written = switch (result.ast.exports_kind) {
.none, .esm => try bundler.print(
.none, .esm => try bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
.esm,
is_source_map,
source_map_handler,
),
.cjs => try bundler.print(
.cjs => try bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
.cjs,
is_source_map,
source_map_handler,
),
else => unreachable,
},
Expand Down Expand Up @@ -2598,12 +2608,14 @@ pub const Bundler = struct {
return output_file;
}

pub fn print(
pub fn printWithSourceMapMaybe(
bundler: *ThisBundler,
result: ParseResult,
comptime Writer: type,
writer: Writer,
comptime format: js_printer.Format,
comptime enable_source_map: bool,
source_map_context: ?js_printer.SourceMapHandler,
) !usize {
const ast = result.ast;
var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols});
Expand All @@ -2622,9 +2634,11 @@ pub const Bundler = struct {
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
.source_map_handler = source_map_context,
},
Linker,
&bundler.linker,
enable_source_map,
),

.esm => try js_printer.printAst(
Expand All @@ -2639,11 +2653,12 @@ pub const Bundler = struct {
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,

.source_map_handler = source_map_context,
.css_import_behavior = bundler.options.cssImportBehavior(),
},
Linker,
&bundler.linker,
enable_source_map,
),
.esm_ascii => try js_printer.printAst(
Writer,
Expand All @@ -2657,11 +2672,12 @@ pub const Bundler = struct {
.externals = ast.externals,
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,

.css_import_behavior = bundler.options.cssImportBehavior(),
.source_map_handler = source_map_context,
},
Linker,
&bundler.linker,
enable_source_map,
),
.cjs_ascii => try js_printer.printCommonJS(
Writer,
Expand All @@ -2676,13 +2692,50 @@ pub const Bundler = struct {
.runtime_imports = ast.runtime_imports,
.require_ref = ast.require_ref,
.css_import_behavior = bundler.options.cssImportBehavior(),
.source_map_handler = source_map_context,
},
Linker,
&bundler.linker,
enable_source_map,
),
};
}

pub fn print(
bundler: *ThisBundler,
result: ParseResult,
comptime Writer: type,
writer: Writer,
comptime format: js_printer.Format,
) !usize {
return bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
format,
false,
null,
);
}

pub fn printWithSourceMap(
bundler: *ThisBundler,
result: ParseResult,
comptime Writer: type,
writer: Writer,
comptime format: js_printer.Format,
handler: js_printer.SourceMapHandler,
) !usize {
return bundler.printWithSourceMapMaybe(
result,
Writer,
writer,
format,
true,
handler,
);
}

pub const ParseOptions = struct {
allocator: std.mem.Allocator,
dirname_fd: StoredFileDescriptorType,
Expand Down Expand Up @@ -3447,6 +3500,7 @@ pub const Transformer = struct {
},
?*anyopaque,
null,
false,
);
},
else => {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/colon_list_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub fn ColonListType(comptime t: type, value_resolver: anytype) type {
// Support either ":" or "=" as the separator, preferring whichever is first.
// ":" is less confusing IMO because that syntax is used with flags
// but "=" is what esbuild uses and I want this to be somewhat familiar for people using esbuild
const midpoint = @minimum(strings.indexOfChar(str, ':') orelse std.math.maxInt(usize), strings.indexOfChar(str, '=') orelse std.math.maxInt(usize));
if (midpoint == std.math.maxInt(usize)) {
const midpoint = @minimum(strings.indexOfChar(str, ':') orelse std.math.maxInt(u32), strings.indexOfChar(str, '=') orelse std.math.maxInt(u32));
if (midpoint == std.math.maxInt(u32)) {
return error.InvalidSeparator;
}

Expand Down
2 changes: 1 addition & 1 deletion src/emcc_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define STBI_ASSERT(x)
#include <stdint.h>out.w
#include <stdint.h>

#include <stdlib.h>

Expand Down
13 changes: 12 additions & 1 deletion src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ pub const FileSystem = struct {
rfs.entries.remove(file_path);
}

pub const Limit = struct {
pub var handles: usize = 0;
pub var stack: usize = 0;
};

// Always try to max out how many files we can keep open
pub fn adjustUlimit() !usize {
const LIMITS = [_]std.os.rlimit_resource{ std.os.rlimit_resource.STACK, std.os.rlimit_resource.NOFILE };
Expand All @@ -627,7 +632,13 @@ pub const FileSystem = struct {
new_limit.cur = limit.max;
new_limit.max = limit.max;

try std.os.setrlimit(limit_type, new_limit);
if (std.os.setrlimit(limit_type, new_limit)) {
if (i == 1) {
Limit.handles = limit.max;
} else {
Limit.stack = limit.max;
}
} else |_| {}
}

if (i == LIMITS.len - 1) return limit.max;
Expand Down
Loading

0 comments on commit 7c5c6cd

Please sign in to comment.