mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 11:59:00 +00:00
Fixes memory leak with private data never being cleared Fixes a case where a ResolveError could actually be a BuildError
172 lines
5.3 KiB
Zig
172 lines
5.3 KiB
Zig
const bun = @import("root").bun;
|
|
const logger = bun.logger;
|
|
const std = @import("std");
|
|
const Fs = bun.fs;
|
|
const string = bun.string;
|
|
const Resolver = @import("../resolver//resolver.zig").Resolver;
|
|
const JSC = bun.JSC;
|
|
const JSGlobalObject = JSC.JSGlobalObject;
|
|
const strings = bun.strings;
|
|
const default_allocator = bun.default_allocator;
|
|
const ZigString = JSC.ZigString;
|
|
const JSValue = JSC.JSValue;
|
|
|
|
pub const BuildMessage = struct {
|
|
msg: logger.Msg,
|
|
// resolve_result: Resolver.Result,
|
|
allocator: std.mem.Allocator,
|
|
logged: bool = false,
|
|
|
|
pub usingnamespace JSC.Codegen.JSBuildMessage;
|
|
|
|
pub fn constructor(
|
|
globalThis: *JSC.JSGlobalObject,
|
|
_: *JSC.CallFrame,
|
|
) callconv(.C) ?*BuildMessage {
|
|
globalThis.throw("BuildMessage is not constructable", .{});
|
|
return null;
|
|
}
|
|
|
|
pub fn toStringFn(this: *BuildMessage, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
|
|
var text = std.fmt.allocPrint(default_allocator, "BuildMessage: {s}", .{this.msg.data.text}) catch {
|
|
globalThis.throwOutOfMemory();
|
|
return .zero;
|
|
};
|
|
var str = ZigString.init(text);
|
|
str.setOutputEncoding();
|
|
if (str.isUTF8()) {
|
|
const out = str.toValueGC(globalThis);
|
|
default_allocator.free(text);
|
|
return out;
|
|
}
|
|
|
|
return str.toExternalValue(globalThis);
|
|
}
|
|
|
|
pub fn create(
|
|
globalThis: *JSC.JSGlobalObject,
|
|
allocator: std.mem.Allocator,
|
|
msg: logger.Msg,
|
|
// resolve_result: *const Resolver.Result,
|
|
) JSC.JSValue {
|
|
var build_error = allocator.create(BuildMessage) catch unreachable;
|
|
build_error.* = BuildMessage{
|
|
.msg = msg.clone(allocator) catch unreachable,
|
|
// .resolve_result = resolve_result.*,
|
|
.allocator = allocator,
|
|
};
|
|
|
|
return build_error.toJS(globalThis);
|
|
}
|
|
|
|
pub fn toString(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
_: *JSC.CallFrame,
|
|
) callconv(.C) JSC.JSValue {
|
|
return this.toStringFn(globalThis);
|
|
}
|
|
|
|
pub fn toPrimitive(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
callframe: *JSC.CallFrame,
|
|
) callconv(.C) JSC.JSValue {
|
|
const args_ = callframe.arguments(1);
|
|
const args = args_.ptr[0..args_.len];
|
|
if (args.len > 0) {
|
|
if (!args[0].isString()) {
|
|
return JSC.JSValue.jsNull();
|
|
}
|
|
|
|
const str = args[0].getZigString(globalThis);
|
|
if (str.eqlComptime("default") or str.eqlComptime("string")) {
|
|
return this.toStringFn(globalThis);
|
|
}
|
|
}
|
|
|
|
return JSC.JSValue.jsNull();
|
|
}
|
|
|
|
pub fn toJSON(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
_: *JSC.CallFrame,
|
|
) callconv(.C) JSC.JSValue {
|
|
var object = JSC.JSValue.createEmptyObject(globalThis, 4);
|
|
object.put(globalThis, ZigString.static("name"), ZigString.init("BuildMessage").toValueGC(globalThis));
|
|
object.put(globalThis, ZigString.static("position"), this.getPosition(globalThis));
|
|
object.put(globalThis, ZigString.static("message"), this.getMessage(globalThis));
|
|
object.put(globalThis, ZigString.static("level"), this.getLevel(globalThis));
|
|
return object;
|
|
}
|
|
|
|
pub fn generatePositionObject(msg: logger.Msg, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
|
|
const location = msg.data.location orelse return JSC.JSValue.jsNull();
|
|
var object = JSC.JSValue.createEmptyObject(globalThis, 7);
|
|
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("lineText"),
|
|
ZigString.init(location.line_text orelse "").toValueGC(globalThis),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("file"),
|
|
ZigString.init(location.file).toValueGC(globalThis),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("namespace"),
|
|
ZigString.init(location.namespace).toValueGC(globalThis),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("line"),
|
|
JSValue.jsNumber(location.line),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("column"),
|
|
JSValue.jsNumber(location.column),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("length"),
|
|
JSValue.jsNumber(location.length),
|
|
);
|
|
object.put(
|
|
globalThis,
|
|
ZigString.static("offset"),
|
|
JSValue.jsNumber(location.offset),
|
|
);
|
|
|
|
return object;
|
|
}
|
|
|
|
pub fn getPosition(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
) callconv(.C) JSC.JSValue {
|
|
return BuildMessage.generatePositionObject(this.msg, globalThis);
|
|
}
|
|
|
|
pub fn getMessage(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
) callconv(.C) JSC.JSValue {
|
|
return ZigString.init(this.msg.data.text).toValueGC(globalThis);
|
|
}
|
|
|
|
pub fn getLevel(
|
|
this: *BuildMessage,
|
|
globalThis: *JSC.JSGlobalObject,
|
|
) callconv(.C) JSC.JSValue {
|
|
return ZigString.init(this.msg.kind.string()).toValueGC(globalThis);
|
|
}
|
|
|
|
pub fn finalize(this: *BuildMessage) callconv(.C) void {
|
|
this.msg.deinit(bun.default_allocator);
|
|
}
|
|
};
|