diff options
Diffstat (limited to 'src/javascript/jsc')
-rw-r--r-- | src/javascript/jsc/bindings/exports.zig | 54 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/process.d.ts | 8 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/shimmer.zig | 4 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 91 | ||||
-rw-r--r-- | src/javascript/jsc/node/types.zig | 6 |
5 files changed, 106 insertions, 57 deletions
diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index 52d1a224a..ba05930d0 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -828,14 +828,26 @@ pub const ZigConsoleClient = struct { Output.enable_ansi_colors_stderr else Output.enable_ansi_colors_stdout; + var buffered_writer = if (level == .Warning or level == .Error) console.error_writer else console.writer; var writer = buffered_writer.writer(); - const BufferedWriterType = @TypeOf(writer); - format(level, global, vals, len, BufferedWriterType, writer, enable_colors, true); + const Writer = @TypeOf(writer); + format( + level, + global, + vals, + len, + @TypeOf(buffered_writer.unbuffered_writer.context), + Writer, + writer, + enable_colors, + true, + true, + ); } pub fn format( @@ -843,10 +855,12 @@ pub const ZigConsoleClient = struct { global: *JSGlobalObject, vals: [*]const JSValue, len: usize, - comptime BufferedWriterType: type, - writer: BufferedWriterType, + comptime RawWriter: type, + comptime Writer: type, + writer: Writer, enable_colors: bool, add_newline: bool, + flush: bool, ) void { var fmt: Formatter = undefined; defer { @@ -861,8 +875,10 @@ pub const ZigConsoleClient = struct { fmt = Formatter{ .remaining_values = &[_]JSValue{} }; const tag = Formatter.Tag.get(vals[0], global); - var unbuffered_writer = writer.context.unbuffered_writer.context.writer(); - const UnbufferedWriterType = @TypeOf(unbuffered_writer); + var unbuffered_writer = if (comptime Writer != RawWriter) + writer.context.unbuffered_writer.context.writer() + else + writer; if (tag.tag == .String) { if (enable_colors) { @@ -871,7 +887,7 @@ pub const ZigConsoleClient = struct { } fmt.format( tag, - UnbufferedWriterType, + @TypeOf(unbuffered_writer), unbuffered_writer, vals[0], global, @@ -883,7 +899,7 @@ pub const ZigConsoleClient = struct { } else { fmt.format( tag, - UnbufferedWriterType, + @TypeOf(unbuffered_writer), unbuffered_writer, vals[0], global, @@ -892,11 +908,15 @@ pub const ZigConsoleClient = struct { } if (add_newline) _ = unbuffered_writer.write("\n") catch 0; } else { - defer writer.context.flush() catch {}; + defer { + if (comptime Writer != RawWriter) { + if (flush) writer.context.flush() catch {}; + } + } if (enable_colors) { fmt.format( tag, - BufferedWriterType, + Writer, writer, vals[0], global, @@ -905,7 +925,7 @@ pub const ZigConsoleClient = struct { } else { fmt.format( tag, - BufferedWriterType, + Writer, writer, vals[0], global, @@ -918,7 +938,11 @@ pub const ZigConsoleClient = struct { return; } - defer writer.context.flush() catch {}; + defer { + if (comptime Writer != RawWriter) { + if (flush) writer.context.flush() catch {}; + } + } var this_value: JSValue = vals[0]; fmt = Formatter{ .remaining_values = vals[0..len][1..] }; @@ -940,7 +964,7 @@ pub const ZigConsoleClient = struct { tag.tag = .StringPossiblyFormatted; } - fmt.format(tag, BufferedWriterType, writer, this_value, global, true); + fmt.format(tag, Writer, writer, this_value, global, true); if (fmt.remaining_values.len == 0) { break; } @@ -962,7 +986,7 @@ pub const ZigConsoleClient = struct { tag.tag = .StringPossiblyFormatted; } - fmt.format(tag, BufferedWriterType, writer, this_value, global, false); + fmt.format(tag, Writer, writer, this_value, global, false); if (fmt.remaining_values.len == 0) break; @@ -1636,7 +1660,7 @@ pub const ZigConsoleClient = struct { const id = std.hash.Wyhash.hash(0, chars[0..len]); var result = (pending_time_logs.fetchPut(id, null) catch null) orelse return; - const value: std.time.Timer = result.value orelse return; + var value: std.time.Timer = result.value orelse return; // get the duration in microseconds // then display it in milliseconds Output.printElapsed(@intToFloat(f64, value.read() / std.time.ns_per_us) / std.time.us_per_ms); diff --git a/src/javascript/jsc/bindings/process.d.ts b/src/javascript/jsc/bindings/process.d.ts new file mode 100644 index 000000000..194ea2b6a --- /dev/null +++ b/src/javascript/jsc/bindings/process.d.ts @@ -0,0 +1,8 @@ +/** + * The process object provides information about, and control over, the + * current Bun.js process. While it is available as a global, it is + * recommended to explicitly access it via require or import + */ +export interface Process { + // +} diff --git a/src/javascript/jsc/bindings/shimmer.zig b/src/javascript/jsc/bindings/shimmer.zig index b4d5fe16b..116327207 100644 --- a/src/javascript/jsc/bindings/shimmer.zig +++ b/src/javascript/jsc/bindings/shimmer.zig @@ -127,7 +127,7 @@ pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comp if (!@hasDecl(Parent, typeName)) { @compileError(@typeName(Parent) ++ " is missing cppFn: " ++ typeName); } - break :ret std.meta.declarationInfo(Parent, typeName).data.Fn.return_type; + break :ret @typeInfo(@TypeOf(@field(Parent, typeName))).Fn.return_type.?; }) { @setEvalBranchQuota(99999); if (comptime is_bindgen) { @@ -135,7 +135,7 @@ pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comp } else { const Fn = comptime @field(headers, symbolName(typeName)); return matchNullable( - comptime std.meta.declarationInfo(Parent, typeName).data.Fn.return_type, + comptime @typeInfo(@TypeOf(@field(Parent, typeName))).Fn.return_type.?, comptime @typeInfo(@TypeOf(Fn)).Fn.return_type.?, @call(.{}, Fn, args), ); diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index afdf01587..828bad39f 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -250,29 +250,60 @@ pub const Bun = struct { if (arguments.len == 0) return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); - var array = std.ArrayList(u8).init(getAllocator(ctx)); - var writer = array.writer(); + for (arguments) |arg| { + JSC.C.JSValueProtect(ctx, arg); + } + defer { + for (arguments) |arg| { + JSC.C.JSValueUnprotect(ctx, arg); + } + } + + // very stable memory address + var array = MutableString.init(getAllocator(ctx), 0) catch unreachable; + var buffered_writer_ = MutableString.BufferedWriter{ .context = &array }; + var buffered_writer = &buffered_writer_; + + var writer = buffered_writer.writer(); + const Writer = @TypeOf(writer); // we buffer this because it'll almost always be < 4096 - const BufferedWriter = std.io.BufferedWriter(4096, std.ArrayList(u8).Writer); - var buffered_writer = BufferedWriter{ .unbuffered_writer = writer }; + // when it's under 4096, we want to avoid the dynamic allocation ZigConsoleClient.format( .Debug, ctx.ptr(), @ptrCast([*]const JSValue, arguments.ptr), arguments.len, - @TypeOf(buffered_writer.writer()), - buffered_writer.writer(), + Writer, + Writer, + writer, + false, false, false, ); - buffered_writer.flush() catch unreachable; - var zig_str = ZigString.init(array.toOwnedSlice()).withEncoding(); - if (zig_str.len == 0) return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); - if (!zig_str.isUTF8()) { - return zig_str.toExternalValue(ctx.ptr()).asObjectRef(); - } else { + + // when it's a small thing, rely on GC to manage the memory + if (writer.context.pos < 2048 and array.list.items.len == 0) { + var slice = writer.context.buffer[0..writer.context.pos]; + if (slice.len == 0) { + return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); + } + + var zig_str = ZigString.init(slice).withEncoding(); return zig_str.toValueGC(ctx.ptr()).asObjectRef(); } + + // when it's a big thing, we will manage it + { + writer.context.flush() catch {}; + var slice = writer.context.context.toOwnedSlice(); + + var zig_str = ZigString.init(slice).withEncoding(); + if (!zig_str.isUTF8()) { + return zig_str.toExternalValue(ctx.ptr()).asObjectRef(); + } else { + return zig_str.toValueGC(ctx.ptr()).asObjectRef(); + } + } } pub fn registerMacro( @@ -501,7 +532,7 @@ pub const Bun = struct { exception: js.ExceptionRef, ) js.JSValueRef { const path = buf_z.ptr[0..buf_z.len]; - var file = std.fs.cwd().openFileZ(buf_z, .{ .read = true, .write = false }) catch |err| { + var file = std.fs.cwd().openFileZ(buf_z, .{ .mode = .read_only }) catch |err| { JSError(getAllocator(ctx), "Opening file {s} for path: \"{s}\"", .{ @errorName(err), path }, ctx, exception); return js.JSValueMakeUndefined(ctx); }; @@ -541,7 +572,7 @@ pub const Bun = struct { ) js.JSValueRef { const path = buf_z.ptr[0..buf_z.len]; - var file = std.fs.cwd().openFileZ(buf_z, .{ .read = true, .write = false }) catch |err| { + var file = std.fs.cwd().openFileZ(buf_z, .{ .mode = .read_only }) catch |err| { JSError(getAllocator(ctx), "Opening file {s} for path: \"{s}\"", .{ @errorName(err), path }, ctx, exception); return js.JSValueMakeUndefined(ctx); }; @@ -2728,14 +2759,11 @@ pub const VirtualMachine = struct { } } - const ChildWriterType = comptime if (@typeInfo(Writer) == .Pointer) - Writer - else - *Writer; - if (value.isAggregateError(this.global)) { const AggregateErrorIterator = struct { - pub var current_exception_list: ?*ExceptionList = null; + writer: Writer, + current_exception_list: ?*ExceptionList = null, + pub fn iteratorWithColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, ctx: ?*anyopaque, nextValue: JSValue) callconv(.C) void { iterator(_vm, globalObject, nextValue, ctx.?, true); } @@ -2743,26 +2771,15 @@ pub const VirtualMachine = struct { iterator(_vm, globalObject, nextValue, ctx.?, false); } inline fn iterator(_: [*c]VM, _: [*c]JSGlobalObject, nextValue: JSValue, ctx: ?*anyopaque, comptime color: bool) void { - var casted = @intToPtr(ChildWriterType, @ptrToInt(ctx)); - if (comptime ChildWriterType == Writer) { - VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, ChildWriterType, casted, color); - } else { - VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, Writer, casted.*, color); - } + var this_ = @intToPtr(*@This(), @ptrToInt(ctx)); + VirtualMachine.vm.printErrorlikeObject(nextValue, null, this_.current_exception_list, Writer, this_.writer, color); } }; - AggregateErrorIterator.current_exception_list = exception_list; - defer AggregateErrorIterator.current_exception_list = null; - var writer_ctx: ?*anyopaque = null; - if (comptime @typeInfo(Writer) == .Pointer) { - writer_ctx = @intToPtr(?*anyopaque, @ptrToInt(writer)); - } else { - writer_ctx = @intToPtr(?*anyopaque, @ptrToInt(&writer)); - } + var iter = AggregateErrorIterator{ .writer = writer, .current_exception_list = exception_list }; if (comptime allow_ansi_color) { - value.getErrorsProperty(this.global).forEach(this.global, writer_ctx, AggregateErrorIterator.iteratorWithColor); + value.getErrorsProperty(this.global).forEach(this.global, &iter, AggregateErrorIterator.iteratorWithColor); } else { - value.getErrorsProperty(this.global).forEach(this.global, writer_ctx, AggregateErrorIterator.iteratorWithOutColor); + value.getErrorsProperty(this.global).forEach(this.global, &iter, AggregateErrorIterator.iteratorWithOutColor); } return; } @@ -2897,7 +2914,7 @@ pub const VirtualMachine = struct { var line_numbers = exception.stack.source_lines_numbers[0..exception.stack.source_lines_len]; var max_line: i32 = -1; - for (line_numbers) |line| max_line = std.math.max(max_line, line); + for (line_numbers) |line| max_line = @maximum(max_line, line); const max_line_number_pad = std.fmt.count("{d}", .{max_line}); var source_lines = exception.stack.sourceLineIterator(); diff --git a/src/javascript/jsc/node/types.zig b/src/javascript/jsc/node/types.zig index a02645853..0eac3b504 100644 --- a/src/javascript/jsc/node/types.zig +++ b/src/javascript/jsc/node/types.zig @@ -804,12 +804,12 @@ fn StatsLike(comptime name: string, comptime T: type) type { .birthtime_ms = if (Environment.isLinux) 0 else - @truncate(T, @intCast(i64, if (stat_.birthtimensec > 0) (@intCast(usize, stat_.birthtimensec) / std.time.ns_per_ms) else 0)), + @truncate(T, @intCast(i64, if (stat_.birthtime().tv_nsec > 0) (@intCast(usize, stat_.birthtime().tv_nsec) / std.time.ns_per_ms) else 0)), .birthtime = if (Environment.isLinux) @intToEnum(Date, 0) else - @intToEnum(Date, @intCast(u64, @maximum(stat_.birthtimesec, 0))), + @intToEnum(Date, @intCast(u64, @maximum(stat_.birthtime().tv_sec, 0))), }; } @@ -2427,7 +2427,7 @@ pub const Process = struct { { var args_iterator = std.process.args(); - if (args_iterator.nextPosix()) |arg0| { + if (args_iterator.next()) |arg0| { var argv0 = JSC.ZigString.init(std.mem.span(arg0)); argv0.setOutputEncoding(); // https://github.com/yargs/yargs/blob/adb0d11e02c613af3d9427b3028cc192703a3869/lib/utils/process-argv.ts#L1 |