forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildMessage.zig
189 lines (168 loc) · 6.03 KB
/
BuildMessage.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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 getNotes(this: *BuildMessage, globalThis: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
const notes: []const logger.Data = this.msg.notes orelse &[_]logger.Data{};
const array = JSC.JSValue.createEmptyArray(globalThis, notes.len);
for (notes, 0..) |note, i| {
const cloned = note.clone(bun.default_allocator) catch {
globalThis.throwOutOfMemory();
return .zero;
};
array.putIndex(
globalThis,
@intCast(i),
BuildMessage.create(globalThis, bun.default_allocator, logger.Msg{ .data = cloned, .kind = .note }),
);
}
return array;
}
pub fn toStringFn(this: *BuildMessage, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
const 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);
}
};