forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.zig
5791 lines (4947 loc) · 186 KB
/
bindings.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const bun = @import("root").bun;
const string = bun.string;
const Output = bun.Output;
const hasRef = std.meta.trait.hasField("ref");
const C_API = @import("root").bun.JSC.C;
const StringPointer = @import("../../api/schema.zig").Api.StringPointer;
const Exports = @import("./exports.zig");
const strings = bun.strings;
const ErrorableZigString = Exports.ErrorableZigString;
const ErrorableResolvedSource = Exports.ErrorableResolvedSource;
const ZigException = Exports.ZigException;
const ZigStackTrace = Exports.ZigStackTrace;
const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false;
const ArrayBuffer = @import("../base.zig").ArrayBuffer;
const JSC = @import("root").bun.JSC;
const Shimmer = JSC.Shimmer;
const FFI = @import("./FFI.zig");
const NullableAllocator = @import("../../nullable_allocator.zig").NullableAllocator;
const MutableString = bun.MutableString;
const JestPrettyFormat = @import("../test/pretty_format.zig").JestPrettyFormat;
const String = bun.String;
const ErrorableString = JSC.ErrorableString;
pub const JSObject = extern struct {
pub const shim = Shimmer("JSC", "JSObject", @This());
bytes: shim.Bytes,
const cppFn = shim.cppFn;
pub const include = "JavaScriptCore/JSObject.h";
pub const name = "JSC::JSObject";
pub const namespace = "JSC";
pub fn getArrayLength(this: *JSObject) usize {
return cppFn("getArrayLength", .{
this,
});
}
const InitializeCallback = *const fn (ctx: ?*anyopaque, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void;
pub fn create(global_object: *JSGlobalObject, length: usize, ctx: *anyopaque, initializer: InitializeCallback) JSValue {
return cppFn("create", .{
global_object,
length,
ctx,
initializer,
});
}
pub fn Initializer(comptime Ctx: type, comptime func: fn (*Ctx, obj: *JSObject, global: *JSGlobalObject) void) type {
return struct {
pub fn call(this: ?*anyopaque, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void {
@call(.always_inline, func, .{ @as(*Ctx, @ptrCast(@alignCast(this.?))), obj.?, global.? });
}
};
}
pub fn createWithInitializer(comptime Ctx: type, creator: *Ctx, global: *JSGlobalObject, length: usize) JSValue {
const Type = Initializer(Ctx, Ctx.create);
return create(global, length, creator, Type.call);
}
pub fn getIndex(this: JSValue, globalThis: *JSGlobalObject, i: u32) JSValue {
return cppFn("getIndex", .{
this,
globalThis,
i,
});
}
pub fn putRecord(this: *JSObject, global: *JSGlobalObject, key: *ZigString, values: [*]ZigString, values_len: usize) void {
return cppFn("putRecord", .{ this, global, key, values, values_len });
}
pub fn getDirect(this: *JSObject, globalThis: *JSGlobalObject, str: *const ZigString) JSValue {
return cppFn("getDirect", .{
this,
globalThis,
str,
});
}
pub const Extern = [_][]const u8{
"putRecord",
"create",
"getArrayLength",
"getIndex",
"putAtIndex",
"getDirect",
};
};
/// Prefer using bun.String instead of ZigString in new code.
pub const ZigString = extern struct {
/// This can be a UTF-16, Latin1, or UTF-8 string.
/// The pointer itself is tagged, so it cannot be used without untagging it first
/// Accessing it directly is unsafe.
_unsafe_ptr_do_not_use: [*]const u8,
len: usize,
pub const ByteString = union(enum) {
latin1: []const u8,
utf16: []const u16,
};
pub fn fromBytes(slice_: []const u8) ZigString {
if (!strings.isAllASCII(slice_)) {
return initUTF8(slice_);
}
return init(slice_);
}
pub inline fn as(this: ZigString) ByteString {
return if (this.is16Bit()) .{ .utf16 = this.utf16SliceAligned() } else .{ .latin1 = this.slice() };
}
pub fn encode(this: ZigString, encoding: JSC.Node.Encoding) []u8 {
return switch (this.as()) {
inline else => |repr| switch (encoding) {
inline else => |enc| JSC.WebCore.Encoder.constructFrom(std.meta.Child(@TypeOf(repr)), repr, enc),
},
};
}
pub fn dupeForJS(utf8: []const u8, allocator: std.mem.Allocator) !ZigString {
if (try strings.toUTF16Alloc(allocator, utf8, false)) |utf16| {
var out = ZigString.init16(utf16);
out.mark();
out.markUTF16();
return out;
} else {
var out = ZigString.init(try allocator.dupe(u8, utf8));
out.mark();
return out;
}
}
pub fn toJS(this: ZigString, ctx: *JSC.JSGlobalObject, _: JSC.C.ExceptionRef) JSValue {
if (this.isGloballyAllocated()) {
return this.toExternalValue(ctx);
}
return this.toValueAuto(ctx);
}
/// This function is not optimized!
pub fn eqlCaseInsensitive(this: ZigString, other: ZigString) bool {
var fallback = std.heap.stackFallback(1024, bun.default_allocator);
var fallback_allocator = fallback.get();
var utf16_slice = this.toSliceLowercase(fallback_allocator);
var latin1_slice = other.toSliceLowercase(fallback_allocator);
defer utf16_slice.deinit();
defer latin1_slice.deinit();
return strings.eqlLong(utf16_slice.slice(), latin1_slice.slice(), true);
}
pub fn toSliceLowercase(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
var fallback = std.heap.stackFallback(512, allocator);
var fallback_allocator = fallback.get();
var uppercase_buffer = this.toOwnedSlice(fallback_allocator) catch unreachable;
var buffer = allocator.alloc(u8, uppercase_buffer.len) catch unreachable;
var out = strings.copyLowercase(uppercase_buffer, buffer);
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = out.ptr,
.len = @as(u32, @truncate(out.len)),
};
}
pub fn indexOfAny(this: ZigString, comptime chars: []const u8) ?strings.OptionalUsize {
if (this.is16Bit()) {
return strings.indexOfAny16(this.utf16SliceAligned(), chars);
} else {
return strings.indexOfAny(this.slice(), chars);
}
}
pub fn charAt(this: ZigString, offset: usize) u8 {
if (this.is16Bit()) {
return @as(u8, @truncate(this.utf16SliceAligned()[offset]));
} else {
return @as(u8, @truncate(this.slice()[offset]));
}
}
pub fn eql(this: ZigString, other: ZigString) bool {
if (this.len == 0 or other.len == 0)
return this.len == other.len;
const left_utf16 = this.is16Bit();
const right_utf16 = other.is16Bit();
if (left_utf16 == right_utf16 and left_utf16) {
return strings.eqlLong(std.mem.sliceAsBytes(this.utf16SliceAligned()), std.mem.sliceAsBytes(other.utf16SliceAligned()), true);
} else if (left_utf16 == right_utf16) {
return strings.eqlLong(this.slice(), other.slice(), true);
}
const utf16: ZigString = if (left_utf16) this else other;
const latin1: ZigString = if (left_utf16) other else this;
if (latin1.isAllASCII()) {
return strings.utf16EqlString(utf16.utf16SliceAligned(), latin1.slice());
}
// slow path
var utf16_slice = utf16.toSlice(bun.default_allocator);
var latin1_slice = latin1.toSlice(bun.default_allocator);
defer utf16_slice.deinit();
defer latin1_slice.deinit();
return strings.eqlLong(utf16_slice.slice(), latin1_slice.slice(), true);
}
pub fn isAllASCII(this: ZigString) bool {
if (this.is16Bit()) {
return strings.firstNonASCII16([]const u16, this.utf16SliceAligned()) == null;
}
return strings.isAllASCII(this.slice());
}
pub fn clone(this: ZigString, allocator: std.mem.Allocator) !ZigString {
var sliced = this.toSlice(allocator);
if (!sliced.isAllocated()) {
var str = ZigString.init(try allocator.dupe(u8, sliced.slice()));
str.mark();
str.markUTF8();
return str;
}
return this;
}
extern fn ZigString__toJSONObject(this: *const ZigString, *JSC.JSGlobalObject) callconv(.C) JSC.JSValue;
pub fn toJSONObject(this: ZigString, globalThis: *JSC.JSGlobalObject) JSValue {
JSC.markBinding(@src());
return ZigString__toJSONObject(&this, globalThis);
}
pub fn hasPrefixChar(this: ZigString, char: u8) bool {
if (this.len == 0)
return false;
if (this.is16Bit()) {
return this.utf16SliceAligned()[0] == char;
}
return this.slice()[0] == char;
}
pub fn substringWithLen(this: ZigString, offset: usize, len: usize) ZigString {
if (this.is16Bit()) {
return ZigString.from16Slice(this.utf16SliceAligned()[@min(this.len, offset)..len]);
}
var out = ZigString.init(this.slice()[@min(this.len, offset)..len]);
if (this.isUTF8()) {
out.markUTF8();
}
if (this.isGloballyAllocated()) {
out.mark();
}
return out;
}
pub fn substring(this: ZigString, offset: usize, maxlen: usize) ZigString {
var len: usize = undefined;
if (maxlen == 0) {
len = this.len;
} else {
len = @max(this.len, maxlen);
}
return this.substringWithLen(offset, len);
}
pub fn maxUTF8ByteLength(this: ZigString) usize {
if (this.isUTF8())
return this.len;
if (this.is16Bit()) {
return this.utf16SliceAligned().len * 3;
}
// latin1
return this.len * 2;
}
pub fn utf16ByteLength(this: ZigString) usize {
if (this.isUTF8()) {
return bun.simdutf.length.utf16.from.utf8.le(this.slice());
}
if (this.is16Bit()) {
return this.len * 2;
}
return JSC.WebCore.Encoder.byteLengthU8(this.slice().ptr, this.slice().len, .utf16le);
}
pub fn latin1ByteLength(this: ZigString) usize {
if (this.isUTF8()) {
@panic("TODO");
}
return this.len;
}
/// Count the number of bytes in the UTF-8 version of the string.
/// This function is slow. Use maxUITF8ByteLength() to get a quick estimate
pub fn utf8ByteLength(this: ZigString) usize {
if (this.isUTF8()) {
return this.len;
}
if (this.is16Bit()) {
return JSC.WebCore.Encoder.byteLengthU16(this.utf16SliceAligned().ptr, this.utf16Slice().len, .utf8);
}
return JSC.WebCore.Encoder.byteLengthU8(this.slice().ptr, this.slice().len, .utf8);
}
pub fn toOwnedSlice(this: ZigString, allocator: std.mem.Allocator) ![]u8 {
if (this.isUTF8())
return try allocator.dupeZ(u8, this.slice());
var list = std.ArrayList(u8).init(allocator);
list = if (this.is16Bit())
try strings.toUTF8ListWithType(list, []const u16, this.utf16SliceAligned())
else
try strings.allocateLatin1IntoUTF8WithList(list, 0, []const u8, this.slice());
if (list.capacity > list.items.len) {
list.items.ptr[list.items.len] = 0;
}
return list.items;
}
pub fn toOwnedSliceZ(this: ZigString, allocator: std.mem.Allocator) ![:0]u8 {
if (this.isUTF8())
return allocator.dupeZ(u8, this.slice());
var list = std.ArrayList(u8).init(allocator);
list = if (this.is16Bit())
try strings.toUTF8ListWithType(list, []const u16, this.utf16SliceAligned())
else
try strings.allocateLatin1IntoUTF8WithList(list, 0, []const u8, this.slice());
try list.append(0);
return list.items[0 .. list.items.len - 1 :0];
}
pub fn trunc(this: ZigString, len: usize) ZigString {
return .{ ._unsafe_ptr_do_not_use = this._unsafe_ptr_do_not_use, .len = @min(len, this.len) };
}
pub fn eqlComptime(this: ZigString, comptime other: []const u8) bool {
if (this.is16Bit()) {
return strings.eqlComptimeUTF16(this.utf16SliceAligned(), other);
}
if (comptime strings.isAllASCIISimple(other)) {
if (this.len != other.len)
return false;
return strings.eqlComptimeIgnoreLen(this.slice(), other);
}
@compileError("Not implemented yet for latin1");
}
pub const shim = Shimmer("", "ZigString", @This());
pub inline fn length(this: ZigString) usize {
return this.len;
}
pub fn byteSlice(this: ZigString) []const u8 {
if (this.is16Bit()) {
return std.mem.sliceAsBytes(this.utf16SliceAligned());
}
return this.slice();
}
pub fn markStatic(this: *ZigString) void {
this.ptr = @as([*]const u8, @ptrFromInt(@intFromPtr(this.ptr) | (1 << 60)));
}
pub fn isStatic(this: *const ZigString) bool {
return @intFromPtr(this.ptr) & (1 << 60) != 0;
}
pub const Slice = struct {
allocator: NullableAllocator = .{},
ptr: [*]const u8 = undefined,
len: u32 = 0,
pub fn init(allocator: std.mem.Allocator, input: []const u8) Slice {
return .{
.ptr = input.ptr,
.len = @as(u32, @truncate(input.len)),
.allocator = NullableAllocator.init(allocator),
};
}
pub fn toZigString(this: Slice) ZigString {
if (this.isAllocated())
return ZigString.initUTF8(this.ptr[0..this.len]);
return ZigString.init(this.slice());
}
pub inline fn length(this: Slice) usize {
return this.len;
}
pub const byteSlice = Slice.slice;
pub fn from(input: []u8, allocator: std.mem.Allocator) Slice {
return .{
.ptr = input.ptr,
.len = @as(u32, @truncate(input.len)),
.allocator = NullableAllocator.init(allocator),
};
}
pub fn fromUTF8NeverFree(input: []const u8) Slice {
return .{
.ptr = input.ptr,
.len = @as(u32, @truncate(input.len)),
.allocator = .{},
};
}
pub const empty = Slice{ .ptr = undefined, .len = 0 };
pub inline fn isAllocated(this: Slice) bool {
return !this.allocator.isNull();
}
pub fn clone(this: Slice, allocator: std.mem.Allocator) !Slice {
if (this.isAllocated()) {
return Slice{ .allocator = this.allocator, .ptr = this.ptr, .len = this.len };
}
var duped = try allocator.dupe(u8, this.ptr[0..this.len]);
return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = duped.ptr, .len = this.len };
}
pub fn cloneIfNeeded(this: Slice, allocator: std.mem.Allocator) !Slice {
if (this.isAllocated()) {
return this;
}
var duped = try allocator.dupe(u8, this.ptr[0..this.len]);
return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = duped.ptr, .len = this.len };
}
pub fn cloneWithTrailingSlash(this: Slice, allocator: std.mem.Allocator) !Slice {
var buf = try strings.cloneNormalizingSeparators(allocator, this.slice());
return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = buf.ptr, .len = @as(u32, @truncate(buf.len)) };
}
pub fn cloneZ(this: Slice, allocator: std.mem.Allocator) !Slice {
if (this.isAllocated() or this.len == 0) {
return this;
}
var duped = try allocator.dupeZ(u8, this.ptr[0..this.len]);
return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = duped.ptr, .len = this.len };
}
pub fn slice(this: Slice) []const u8 {
return this.ptr[0..this.len];
}
pub fn sliceZ(this: Slice) [:0]const u8 {
return bun.cstring(this.ptr[0..this.len]);
}
pub fn toSliceZ(this: Slice, buf: []u8) [:0]const u8 {
if (this.len == 0) {
return "";
}
if (this.ptr[this.len] == 0) {
return this.sliceZ();
}
if (this.len >= buf.len) {
return "";
}
bun.copy(u8, buf, this.slice());
buf[this.len] = 0;
return bun.cstring(buf[0..this.len]);
}
pub fn mut(this: Slice) []u8 {
return @as([*]u8, @ptrFromInt(@intFromPtr(this.ptr)))[0..this.len];
}
/// Does nothing if the slice is not allocated
pub fn deinit(this: *const Slice) void {
if (this.allocator.get()) |allocator| {
if (bun.String.isWTFAllocator(allocator)) {
// workaround for https://github.com/ziglang/zig/issues/4298
bun.String.StringImplAllocator.free(allocator.ptr, bun.constStrToU8(this.slice()), 0, 0);
return;
}
allocator.free(this.slice());
}
}
};
pub const name = "ZigString";
pub const namespace = "";
pub inline fn is16Bit(this: *const ZigString) bool {
return (@intFromPtr(this._unsafe_ptr_do_not_use) & (1 << 63)) != 0;
}
pub inline fn utf16Slice(this: *const ZigString) []align(1) const u16 {
if (comptime bun.Environment.allow_assert) {
if (this.len > 0 and !this.is16Bit()) {
@panic("ZigString.utf16Slice() called on a latin1 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first.");
}
}
return @as([*]align(1) const u16, @ptrCast(untagged(this._unsafe_ptr_do_not_use)))[0..this.len];
}
pub inline fn utf16SliceAligned(this: *const ZigString) []const u16 {
if (comptime bun.Environment.allow_assert) {
if (this.len > 0 and !this.is16Bit()) {
@panic("ZigString.utf16SliceAligned() called on a latin1 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first.");
}
}
return @as([*]const u16, @ptrCast(@alignCast(untagged(this._unsafe_ptr_do_not_use))))[0..this.len];
}
pub inline fn isEmpty(this: *const ZigString) bool {
return this.len == 0;
}
pub fn fromStringPointer(ptr: StringPointer, buf: string, to: *ZigString) void {
to.* = ZigString{
.len = ptr.length,
._unsafe_ptr_do_not_use = buf[ptr.offset..][0..ptr.length].ptr,
};
}
pub fn sortDesc(slice_: []ZigString) void {
std.sort.block(ZigString, slice_, {}, cmpDesc);
}
pub fn cmpDesc(_: void, a: ZigString, b: ZigString) bool {
return strings.cmpStringsDesc({}, a.slice(), b.slice());
}
pub fn sortAsc(slice_: []ZigString) void {
std.sort.block(ZigString, slice_, {}, cmpAsc);
}
pub fn cmpAsc(_: void, a: ZigString, b: ZigString) bool {
return strings.cmpStringsAsc({}, a.slice(), b.slice());
}
pub inline fn init(slice_: []const u8) ZigString {
return ZigString{ ._unsafe_ptr_do_not_use = slice_.ptr, .len = slice_.len };
}
pub fn initUTF8(slice_: []const u8) ZigString {
var out = init(slice_);
out.markUTF8();
return out;
}
pub fn fromUTF8(slice_: []const u8) ZigString {
var out = init(slice_);
if (!strings.isAllASCII(slice_))
out.markUTF8();
return out;
}
pub fn static(comptime slice_: []const u8) *const ZigString {
const Holder = struct {
pub const value = ZigString{ ._unsafe_ptr_do_not_use = slice_.ptr, .len = slice_.len };
};
return &Holder.value;
}
pub const GithubActionFormatter = struct {
text: ZigString,
pub fn format(this: GithubActionFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var bytes = this.text.toSlice(bun.default_allocator);
defer bytes.deinit();
try strings.githubActionWriter(writer, bytes.slice());
}
};
pub fn githubAction(this: ZigString) GithubActionFormatter {
return GithubActionFormatter{ .text = this };
}
pub fn toAtomicValue(this: *const ZigString, globalThis: *JSC.JSGlobalObject) JSValue {
return shim.cppFn("toAtomicValue", .{ this, globalThis });
}
pub fn init16(slice_: []const u16) ZigString {
var out = ZigString{ ._unsafe_ptr_do_not_use = std.mem.sliceAsBytes(slice_).ptr, .len = slice_.len };
out.markUTF16();
return out;
}
pub fn from(slice_: JSC.C.JSValueRef, ctx: JSC.C.JSContextRef) ZigString {
return JSC.JSValue.fromRef(slice_).getZigString(ctx.ptr());
}
pub fn from16Slice(slice_: []const u16) ZigString {
return from16(slice_.ptr, slice_.len);
}
/// Globally-allocated memory only
pub fn from16(slice_: [*]const u16, len: usize) ZigString {
var str = init(@as([*]const u8, @ptrCast(slice_))[0..len]);
str.markUTF16();
str.mark();
str.assertGlobal();
return str;
}
pub fn toBase64DataURL(this: ZigString, allocator: std.mem.Allocator) ![]const u8 {
const slice_ = this.slice();
const size = std.base64.standard.Encoder.calcSize(slice_.len);
var buf = try allocator.alloc(u8, size + "data:;base64,".len);
var encoded = std.base64.url_safe.Encoder.encode(buf["data:;base64,".len..], slice_);
buf[0.."data:;base64,".len].* = "data:;base64,".*;
return buf[0 .. "data:;base64,".len + encoded.len];
}
pub fn detectEncoding(this: *ZigString) void {
if (!strings.isAllASCII(this.slice())) {
this.markUTF16();
}
}
pub fn toExternalU16(ptr: [*]const u16, len: usize, global: *JSGlobalObject) JSValue {
return shim.cppFn("toExternalU16", .{ ptr, len, global });
}
pub fn isUTF8(this: ZigString) bool {
return (@intFromPtr(this._unsafe_ptr_do_not_use) & (1 << 61)) != 0;
}
pub fn markUTF8(this: *ZigString) void {
this._unsafe_ptr_do_not_use = @as([*]const u8, @ptrFromInt(@intFromPtr(this._unsafe_ptr_do_not_use) | (1 << 61)));
}
pub fn markUTF16(this: *ZigString) void {
this._unsafe_ptr_do_not_use = @as([*]const u8, @ptrFromInt(@intFromPtr(this._unsafe_ptr_do_not_use) | (1 << 63)));
}
pub fn setOutputEncoding(this: *ZigString) void {
if (!this.is16Bit()) this.detectEncoding();
if (this.is16Bit()) this.markUTF8();
}
pub inline fn isGloballyAllocated(this: ZigString) bool {
return (@intFromPtr(this._unsafe_ptr_do_not_use) & (1 << 62)) != 0;
}
pub inline fn deinitGlobal(this: ZigString) void {
bun.default_allocator.free(this.slice());
}
pub const mark = markGlobal;
pub inline fn markGlobal(this: *ZigString) void {
this._unsafe_ptr_do_not_use = @as([*]const u8, @ptrFromInt(@intFromPtr(this._unsafe_ptr_do_not_use) | (1 << 62)));
}
pub fn format(self: ZigString, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
if (self.isUTF8()) {
try writer.writeAll(self.slice());
return;
}
if (self.is16Bit()) {
try strings.formatUTF16(self.utf16Slice(), writer);
return;
}
try strings.formatLatin1(self.slice(), writer);
}
pub inline fn toRef(slice_: []const u8, global: *JSGlobalObject) C_API.JSValueRef {
return init(slice_).toValue(global).asRef();
}
pub const Empty = ZigString{ ._unsafe_ptr_do_not_use = "", .len = 0 };
inline fn untagged(ptr: [*]const u8) [*]const u8 {
// this can be null ptr, so long as it's also a 0 length string
@setRuntimeSafety(false);
return @as([*]const u8, @ptrFromInt(@as(u53, @truncate(@intFromPtr(ptr)))));
}
pub fn slice(this: *const ZigString) []const u8 {
if (comptime bun.Environment.allow_assert) {
if (this.len > 0 and this.is16Bit()) {
@panic("ZigString.slice() called on a UTF-16 string.\nPlease use .toSlice() instead or carefully check that .is16Bit() is false first.");
}
}
return untagged(this._unsafe_ptr_do_not_use)[0..@min(this.len, std.math.maxInt(u32))];
}
pub fn dupe(this: ZigString, allocator: std.mem.Allocator) ![]const u8 {
return try allocator.dupe(u8, this.slice());
}
pub fn toSliceFast(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
if (is16Bit(&this)) {
var buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.ptr = buffer.ptr,
.len = @as(u32, @truncate(buffer.len)),
.allocator = NullableAllocator.init(allocator),
};
}
return Slice{
.ptr = untagged(this._unsafe_ptr_do_not_use),
.len = @as(u32, @truncate(this.len)),
};
}
/// This function checks if the input is latin1 non-ascii
/// It is slow but safer when the input is from JavaScript
pub fn toSlice(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
if (is16Bit(&this)) {
const buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = buffer.ptr,
.len = @as(u32, @truncate(buffer.len)),
};
}
if (!this.isUTF8() and !strings.isAllASCII(untagged(this._unsafe_ptr_do_not_use)[0..this.len])) {
const buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = buffer.ptr,
.len = @as(u32, @truncate(buffer.len)),
};
}
return Slice{
.ptr = untagged(this._unsafe_ptr_do_not_use),
.len = @as(u32, @truncate(this.len)),
};
}
pub fn toSliceClone(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
const buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = buffer.ptr,
.len = @as(u32, @truncate(buffer.len)),
};
}
pub fn toSliceZ(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
if (is16Bit(&this)) {
var buffer = this.toOwnedSliceZ(allocator) catch unreachable;
return Slice{
.ptr = buffer.ptr,
.len = @as(u32, @truncate(buffer.len)),
.allocator = NullableAllocator.init(allocator),
};
}
return Slice{
.ptr = untagged(this._unsafe_ptr_do_not_use),
.len = @as(u32, @truncate(this.len)),
};
}
pub fn sliceZBuf(this: ZigString, buf: *[bun.MAX_PATH_BYTES]u8) ![:0]const u8 {
return try std.fmt.bufPrintZ(buf, "{}", .{this});
}
pub inline fn full(this: *const ZigString) []const u8 {
return untagged(this._unsafe_ptr_do_not_use)[0..this.len];
}
pub fn trimmedSlice(this: *const ZigString) []const u8 {
return strings.trim(this.full(), " \r\n");
}
pub fn toValueAuto(this: *const ZigString, global: *JSGlobalObject) JSValue {
if (!this.is16Bit()) {
return this.toValue(global);
} else {
return this.to16BitValue(global);
}
}
inline fn assertGlobalIfNeeded(this: *const ZigString) void {
if (comptime bun.Environment.allow_assert) {
if (this.isGloballyAllocated()) {
this.assertGlobal();
}
}
}
inline fn assertGlobal(this: *const ZigString) void {
if (comptime bun.Environment.allow_assert) {
std.debug.assert(this.len == 0 or
bun.Mimalloc.mi_is_in_heap_region(untagged(this._unsafe_ptr_do_not_use)) or
bun.Mimalloc.mi_check_owned(untagged(this._unsafe_ptr_do_not_use)));
}
}
pub fn toValue(this: *const ZigString, global: *JSGlobalObject) JSValue {
this.assertGlobalIfNeeded();
return shim.cppFn("toValue", .{ this, global });
}
pub fn toExternalValue(this: *const ZigString, global: *JSGlobalObject) JSValue {
this.assertGlobal();
return shim.cppFn("toExternalValue", .{ this, global });
}
pub fn toExternalValueWithCallback(
this: *const ZigString,
global: *JSGlobalObject,
callback: *const fn (ctx: ?*anyopaque, ptr: ?*anyopaque, len: usize) callconv(.C) void,
) JSValue {
return shim.cppFn("toExternalValueWithCallback", .{ this, global, callback });
}
pub fn external(
this: *const ZigString,
global: *JSGlobalObject,
ctx: ?*anyopaque,
callback: *const fn (ctx: ?*anyopaque, ptr: ?*anyopaque, len: usize) callconv(.C) void,
) JSValue {
return shim.cppFn("external", .{ this, global, ctx, callback });
}
pub fn to16BitValue(this: *const ZigString, global: *JSGlobalObject) JSValue {
this.assertGlobal();
return shim.cppFn("to16BitValue", .{ this, global });
}
pub fn toValueGC(this: *const ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toValueGC", .{ this, global });
}
pub fn withEncoding(this: *const ZigString) ZigString {
var out = this.*;
out.setOutputEncoding();
return out;
}
pub fn toJSStringRef(this: *const ZigString) C_API.JSStringRef {
if (comptime @hasDecl(@import("root").bun, "bindgen")) {
return undefined;
}
return if (this.is16Bit())
C_API.JSStringCreateWithCharactersNoCopy(@as([*]const u16, @ptrCast(@alignCast(untagged(this._unsafe_ptr_do_not_use)))), this.len)
else
C_API.JSStringCreateStatic(untagged(this._unsafe_ptr_do_not_use), this.len);
}
pub fn toErrorInstance(this: *const ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toErrorInstance", .{ this, global });
}
pub fn toTypeErrorInstance(this: *const ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toTypeErrorInstance", .{ this, global });
}
pub fn toSyntaxErrorInstance(this: *const ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toSyntaxErrorInstance", .{ this, global });
}
pub fn toRangeErrorInstance(this: *const ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toRangeErrorInstance", .{ this, global });
}
pub const Extern = [_][]const u8{
"toAtomicValue",
"toValue",
"toExternalValue",
"to16BitValue",
"toValueGC",
"toErrorInstance",
"toExternalU16",
"toExternalValueWithCallback",
"external",
"toTypeErrorInstance",
"toSyntaxErrorInstance",
"toRangeErrorInstance",
};
};
pub const DOMURL = opaque {
pub const shim = Shimmer("WebCore", "DOMURL", @This());
const cppFn = shim.cppFn;
pub const name = "WebCore::DOMURL";
pub fn cast_(value: JSValue, vm: *VM) ?*DOMURL {
return shim.cppFn("cast_", .{ value, vm });
}
pub fn cast(value: JSValue) ?*DOMURL {
return cast_(value, JSC.VirtualMachine.get().global.vm());
}
pub fn href_(this: *DOMURL, out: *ZigString) void {
return shim.cppFn("href_", .{ this, out });
}
pub fn href(this: *DOMURL) ZigString {
var out = ZigString.Empty;
this.href_(&out);
return out;
}
pub fn fileSystemPath(this: *DOMURL) bun.String {
return shim.cppFn("fileSystemPath", .{this});
}
pub fn pathname_(this: *DOMURL, out: *ZigString) void {
return shim.cppFn("pathname_", .{ this, out });
}
pub fn pathname(this: *DOMURL) ZigString {
var out = ZigString.Empty;
this.pathname_(&out);
return out;
}
pub const Extern = [_][]const u8{
"cast_",
"href_",
"pathname_",
"fileSystemPath",
};
};
const Api = @import("../../api/schema.zig").Api;
pub const DOMFormData = opaque {
pub const shim = Shimmer("WebCore", "DOMFormData", @This());
pub const name = "WebCore::DOMFormData";
pub const include = "DOMFormData.h";
pub const namespace = "WebCore";
const cppFn = shim.cppFn;
pub fn create(
global: *JSGlobalObject,
) JSValue {
return shim.cppFn("create", .{
global,
});
}
pub fn createFromURLQuery(
global: *JSGlobalObject,
query: *ZigString,