| ... | @@ -14,8 +14,15 @@ const missing_feature_url_escape = @import("html_render.zig").missing_feature_ur | ... | @@ -14,8 +14,15 @@ const missing_feature_url_escape = @import("html_render.zig").missing_feature_ur |
| 14 | const gpa = std.heap.wasm_allocator; | 14 | const gpa = std.heap.wasm_allocator; |
| 15 | | 15 | |
| 16 | const js = struct { | 16 | const js = struct { |
| 17 | extern "js" fn log(ptr: [*]const u8, len: usize) void; | 17 | /// Keep in sync with the `LOG_` constants in `main.js`. |
| 18 | extern "js" fn panic(ptr: [*]const u8, len: usize) noreturn; | 18 | const LogLevel = enum(u8) { |
| | 19 | err, |
| | 20 | warn, |
| | 21 | info, |
| | 22 | debug, |
| | 23 | }; |
| | 24 | |
| | 25 | extern "js" fn log(level: LogLevel, ptr: [*]const u8, len: usize) void; |
| 19 | }; | 26 | }; |
| 20 | | 27 | |
| 21 | pub const std_options: std.Options = .{ | 28 | pub const std_options: std.Options = .{ |
| ... | @@ -36,14 +43,13 @@ fn logFn( | ... | @@ -36,14 +43,13 @@ fn logFn( |
| 36 | comptime format: []const u8, | 43 | comptime format: []const u8, |
| 37 | args: anytype, | 44 | args: anytype, |
| 38 | ) void { | 45 | ) void { |
| 39 | const level_txt = comptime message_level.asText(); | 46 | const prefix = if (scope == .default) "" else @tagName(scope) ++ ": "; |
| 40 | const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; | | |
| 41 | var buf: [500]u8 = undefined; | 47 | var buf: [500]u8 = undefined; |
| 42 | const line = std.fmt.bufPrint(&buf, level_txt ++ prefix2 ++ format, args) catch l: { | 48 | const line = std.fmt.bufPrint(&buf, prefix ++ format, args) catch l: { |
| 43 | buf[buf.len - 3 ..][0..3].* = "...".*; | 49 | buf[buf.len - 3 ..][0..3].* = "...".*; |
| 44 | break :l &buf; | 50 | break :l &buf; |
| 45 | }; | 51 | }; |
| 46 | js.log(line.ptr, line.len); | 52 | js.log(@field(js.LogLevel, @tagName(message_level)), line.ptr, line.len); |
| 47 | } | 53 | } |
| 48 | | 54 | |
| 49 | export fn alloc(n: usize) [*]u8 { | 55 | export fn alloc(n: usize) [*]u8 { |
| ... | @@ -56,7 +62,7 @@ export fn unpack(tar_ptr: [*]u8, tar_len: usize) void { | ... | @@ -56,7 +62,7 @@ export fn unpack(tar_ptr: [*]u8, tar_len: usize) void { |
| 56 | //log.debug("received {d} bytes of tar file", .{tar_bytes.len}); | 62 | //log.debug("received {d} bytes of tar file", .{tar_bytes.len}); |
| 57 | | 63 | |
| 58 | unpackInner(tar_bytes) catch |err| { | 64 | unpackInner(tar_bytes) catch |err| { |
| 59 | fatal("unable to unpack tar: {s}", .{@errorName(err)}); | 65 | std.debug.panic("unable to unpack tar: {s}", .{@errorName(err)}); |
| 60 | }; | 66 | }; |
| 61 | } | 67 | } |
| 62 | | 68 | |
| ... | @@ -514,7 +520,7 @@ export fn decl_fn_proto_html(decl_index: Decl.Index, linkify_fn_name: bool) Stri | ... | @@ -514,7 +520,7 @@ export fn decl_fn_proto_html(decl_index: Decl.Index, linkify_fn_name: bool) Stri |
| 514 | .collapse_whitespace = true, | 520 | .collapse_whitespace = true, |
| 515 | .fn_link = if (linkify_fn_name) decl_index else .none, | 521 | .fn_link = if (linkify_fn_name) decl_index else .none, |
| 516 | }) catch |err| { | 522 | }) catch |err| { |
| 517 | fatal("unable to render source: {s}", .{@errorName(err)}); | 523 | std.debug.panic("unable to render source: {s}", .{@errorName(err)}); |
| 518 | }; | 524 | }; |
| 519 | return String.init(string_result.items); | 525 | return String.init(string_result.items); |
| 520 | } | 526 | } |
| ... | @@ -524,7 +530,7 @@ export fn decl_source_html(decl_index: Decl.Index) String { | ... | @@ -524,7 +530,7 @@ export fn decl_source_html(decl_index: Decl.Index) String { |
| 524 | | 530 | |
| 525 | string_result.clearRetainingCapacity(); | 531 | string_result.clearRetainingCapacity(); |
| 526 | fileSourceHtml(decl.file, &string_result, decl.ast_node, .{}) catch |err| { | 532 | fileSourceHtml(decl.file, &string_result, decl.ast_node, .{}) catch |err| { |
| 527 | fatal("unable to render source: {s}", .{@errorName(err)}); | 533 | std.debug.panic("unable to render source: {s}", .{@errorName(err)}); |
| 528 | }; | 534 | }; |
| 529 | return String.init(string_result.items); | 535 | return String.init(string_result.items); |
| 530 | } | 536 | } |
| ... | @@ -536,7 +542,7 @@ export fn decl_doctest_html(decl_index: Decl.Index) String { | ... | @@ -536,7 +542,7 @@ export fn decl_doctest_html(decl_index: Decl.Index) String { |
| 536 | | 542 | |
| 537 | string_result.clearRetainingCapacity(); | 543 | string_result.clearRetainingCapacity(); |
| 538 | fileSourceHtml(decl.file, &string_result, doctest_ast_node, .{}) catch |err| { | 544 | fileSourceHtml(decl.file, &string_result, doctest_ast_node, .{}) catch |err| { |
| 539 | fatal("unable to render source: {s}", .{@errorName(err)}); | 545 | std.debug.panic("unable to render source: {s}", .{@errorName(err)}); |
| 540 | }; | 546 | }; |
| 541 | return String.init(string_result.items); | 547 | return String.init(string_result.items); |
| 542 | } | 548 | } |
| ... | @@ -740,7 +746,7 @@ export fn decl_type_html(decl_index: Decl.Index) String { | ... | @@ -740,7 +746,7 @@ export fn decl_type_html(decl_index: Decl.Index) String { |
| 740 | .skip_comments = true, | 746 | .skip_comments = true, |
| 741 | .collapse_whitespace = true, | 747 | .collapse_whitespace = true, |
| 742 | }) catch |e| { | 748 | }) catch |e| { |
| 743 | fatal("unable to render html: {s}", .{@errorName(e)}); | 749 | std.debug.panic("unable to render html: {s}", .{@errorName(e)}); |
| 744 | }; | 750 | }; |
| 745 | string_result.appendSlice(gpa, "</code>") catch @panic("OOM"); | 751 | string_result.appendSlice(gpa, "</code>") catch @panic("OOM"); |
| 746 | break :t; | 752 | break :t; |
| ... | @@ -791,15 +797,6 @@ fn unpackInner(tar_bytes: []u8) !void { | ... | @@ -791,15 +797,6 @@ fn unpackInner(tar_bytes: []u8) !void { |
| 791 | } | 797 | } |
| 792 | } | 798 | } |
| 793 | | 799 | |
| 794 | fn fatal(comptime format: []const u8, args: anytype) noreturn { | | |
| 795 | var buf: [500]u8 = undefined; | | |
| 796 | const line = std.fmt.bufPrint(&buf, format, args) catch l: { | | |
| 797 | buf[buf.len - 3 ..][0..3].* = "...".*; | | |
| 798 | break :l &buf; | | |
| 799 | }; | | |
| 800 | js.panic(line.ptr, line.len); | | |
| 801 | } | | |
| 802 | | | |
| 803 | fn ascii_lower(bytes: []u8) void { | 800 | fn ascii_lower(bytes: []u8) void { |
| 804 | for (bytes) |*b| b.* = std.ascii.toLower(b.*); | 801 | for (bytes) |*b| b.* = std.ascii.toLower(b.*); |
| 805 | } | 802 | } |