-
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathlib.rs
More file actions
1114 lines (999 loc) · 36 KB
/
lib.rs
File metadata and controls
1114 lines (999 loc) · 36 KB
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
//! A Latin1 or UTF-16 encoded, reference counted, immutable string.
// Required per unsafe code standards to ensure every unsafe usage is properly documented.
// - `unsafe_op_in_unsafe_fn` will be warn-by-default in edition 2024:
// https://github.com/rust-lang/rust/issues/71668#issuecomment-1189396860
// - `undocumented_unsafe_blocks` and `missing_safety_doc` requires a `Safety:` section in the
// comment or doc of the unsafe block or function, respectively.
#![deny(
unsafe_op_in_unsafe_fn,
clippy::undocumented_unsafe_blocks,
clippy::missing_safety_doc
)]
#![allow(clippy::module_name_repetitions)]
mod builder;
mod common;
mod display;
mod iter;
mod str;
mod tagged;
#[cfg(test)]
mod tests;
use self::{iter::Windows, str::JsSliceIndex};
use crate::display::{JsStrDisplayEscaped, JsStrDisplayLossy};
use crate::tagged::{Tagged, UnwrappedTagged};
#[doc(inline)]
pub use crate::{
builder::{CommonJsStringBuilder, Latin1JsStringBuilder, Utf16JsStringBuilder},
common::StaticJsStrings,
iter::Iter,
str::{JsStr, JsStrVariant},
};
use std::borrow::Cow;
use std::fmt::Write;
use std::{
alloc::{alloc, dealloc, Layout},
cell::Cell,
convert::Infallible,
hash::{Hash, Hasher},
mem::ManuallyDrop,
process::abort,
ptr::{self, addr_of, addr_of_mut, NonNull},
str::FromStr,
};
fn alloc_overflow() -> ! {
panic!("detected overflow during string allocation")
}
/// Helper function to check if a `char` is trimmable.
pub(crate) const fn is_trimmable_whitespace(c: char) -> bool {
// The rust implementation of `trim` does not regard the same characters whitespace as ecma standard does
//
// Rust uses \p{White_Space} by default, which also includes:
// `\u{0085}' (next line)
// And does not include:
// '\u{FEFF}' (zero width non-breaking space)
// Explicit whitespace: https://tc39.es/ecma262/#sec-white-space
matches!(
c,
'\u{0009}' | '\u{000B}' | '\u{000C}' | '\u{0020}' | '\u{00A0}' | '\u{FEFF}' |
// Unicode Space_Separator category
'\u{1680}' | '\u{2000}'
..='\u{200A}' | '\u{202F}' | '\u{205F}' | '\u{3000}' |
// Line terminators: https://tc39.es/ecma262/#sec-line-terminators
'\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}'
)
}
/// Helper function to check if a `u8` latin1 character is trimmable.
pub(crate) const fn is_trimmable_whitespace_latin1(c: u8) -> bool {
// The rust implementation of `trim` does not regard the same characters whitespace as ecma standard does
//
// Rust uses \p{White_Space} by default, which also includes:
// `\u{0085}' (next line)
// And does not include:
// '\u{FEFF}' (zero width non-breaking space)
// Explicit whitespace: https://tc39.es/ecma262/#sec-white-space
matches!(
c,
0x09 | 0x0B | 0x0C | 0x20 | 0xA0 |
// Line terminators: https://tc39.es/ecma262/#sec-line-terminators
0x0A | 0x0D
)
}
/// Represents a Unicode codepoint within a [`JsString`], which could be a valid
/// '[Unicode scalar value]', or an unpaired surrogate.
///
/// [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CodePoint {
/// A valid Unicode scalar value.
Unicode(char),
/// An unpaired surrogate.
UnpairedSurrogate(u16),
}
impl CodePoint {
/// Get the number of UTF-16 code units needed to encode this code point.
#[inline]
#[must_use]
pub const fn code_unit_count(self) -> usize {
match self {
Self::Unicode(c) => c.len_utf16(),
Self::UnpairedSurrogate(_) => 1,
}
}
/// Convert the code point to its [`u32`] representation.
#[inline]
#[must_use]
pub fn as_u32(self) -> u32 {
match self {
Self::Unicode(c) => u32::from(c),
Self::UnpairedSurrogate(surr) => u32::from(surr),
}
}
/// If the code point represents a valid 'Unicode scalar value', returns its [`char`]
/// representation, otherwise returns [`None`] on unpaired surrogates.
#[inline]
#[must_use]
pub const fn as_char(self) -> Option<char> {
match self {
Self::Unicode(c) => Some(c),
Self::UnpairedSurrogate(_) => None,
}
}
/// Encodes this code point as UTF-16 into the provided u16 buffer, and then returns the subslice
/// of the buffer that contains the encoded character.
///
/// # Panics
///
/// Panics if the buffer is not large enough. A buffer of length 2 is large enough to encode any
/// code point.
#[inline]
#[must_use]
pub fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] {
match self {
Self::Unicode(c) => c.encode_utf16(dst),
Self::UnpairedSurrogate(surr) => {
dst[0] = surr;
&mut dst[0..=0]
}
}
}
}
impl std::fmt::Display for CodePoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CodePoint::Unicode(c) => f.write_char(*c),
CodePoint::UnpairedSurrogate(c) => {
write!(f, "\\u{c:04X}")
}
}
}
}
/// A `usize` contains a flag and the length of Latin1/UTF-16 .
/// ```text
/// ┌────────────────────────────────────┐
/// │ length (usize::BITS - 1) │ flag(1) │
/// └────────────────────────────────────┘
/// ```
/// The latin1/UTF-16 flag is stored in the bottom bit.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
struct TaggedLen(usize);
impl TaggedLen {
const LATIN1_BITFLAG: usize = 1 << 0;
const BITFLAG_COUNT: usize = 1;
const fn new(len: usize, latin1: bool) -> Self {
Self((len << Self::BITFLAG_COUNT) | (latin1 as usize))
}
const fn is_latin1(self) -> bool {
(self.0 & Self::LATIN1_BITFLAG) != 0
}
const fn len(self) -> usize {
self.0 >> Self::BITFLAG_COUNT
}
}
/// The raw representation of a [`JsString`] from a string literal.
#[derive(Debug)]
#[repr(C)]
pub struct StaticJsString {
tagged_len: TaggedLen,
_zero: usize,
ptr: *const u8,
}
// SAFETY: This is Sync because reads to `_zero` will always read 0 and
// `ptr` cannot be mutated thanks to the 'static requirement.
unsafe impl Sync for StaticJsString {}
impl StaticJsString {
/// Create a `StaticJsString` from a static `JsStr`.
#[must_use]
pub const fn new(string: JsStr<'static>) -> StaticJsString {
match string.variant() {
JsStrVariant::Latin1(l) => StaticJsString {
tagged_len: TaggedLen::new(l.len(), true),
_zero: 0,
ptr: l.as_ptr(),
},
JsStrVariant::Utf16(u) => StaticJsString {
tagged_len: TaggedLen::new(u.len(), false),
_zero: 0,
ptr: u.as_ptr().cast(),
},
}
}
}
/// Memory variant to pass `Miri` test.
///
/// If it equals to `0usize`,
/// we mark it read-only, otherwise it is readable and writable
union RefCount {
read_only: usize,
read_write: ManuallyDrop<Cell<usize>>,
}
/// The raw representation of a [`JsString`] in the heap.
#[repr(C)]
struct RawJsString {
tagged_len: TaggedLen,
refcount: RefCount,
data: [u8; 0],
}
impl RawJsString {
const fn is_latin1(&self) -> bool {
self.tagged_len.is_latin1()
}
const fn len(&self) -> usize {
self.tagged_len.len()
}
}
const DATA_OFFSET: usize = size_of::<RawJsString>();
/// A Latin1 or UTF-16–encoded, reference counted, immutable string.
///
/// This is pretty similar to a <code>[Rc][std::rc::Rc]\<[\[u16\]][slice]\></code>, but without the
/// length metadata associated with the `Rc` fat pointer. Instead, the length of every string is
/// stored on the heap, along with its reference counter and its data.
///
/// The string can be latin1 (stored as a byte for space efficiency) or U16 encoding.
///
/// We define some commonly used string constants in an interner. For these strings, we don't allocate
/// memory on the heap to reduce the overhead of memory allocation and reference counting.
#[allow(clippy::module_name_repetitions)]
pub struct JsString {
ptr: Tagged<RawJsString>,
}
// JsString should always be pointer sized.
static_assertions::assert_eq_size!(JsString, *const ());
impl<'a> From<&'a JsString> for JsStr<'a> {
#[inline]
fn from(value: &'a JsString) -> Self {
value.as_str()
}
}
impl<'a> IntoIterator for &'a JsString {
type IntoIter = Iter<'a>;
type Item = u16;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl JsString {
/// Create an iterator over the [`JsString`].
#[inline]
#[must_use]
pub fn iter(&self) -> Iter<'_> {
self.as_str().iter()
}
/// Create an iterator over overlapping subslices of length size.
#[inline]
#[must_use]
pub fn windows(&self, size: usize) -> Windows<'_> {
self.as_str().windows(size)
}
/// Decodes a [`JsString`] into a [`String`], replacing invalid data with its escaped representation
/// in 4 digit hexadecimal.
#[inline]
#[must_use]
pub fn to_std_string_escaped(&self) -> String {
self.display_escaped().to_string()
}
/// Decodes a [`JsString`] into a [`String`], replacing invalid data with the
/// replacement character U+FFFD.
#[inline]
#[must_use]
pub fn to_std_string_lossy(&self) -> String {
self.display_lossy().to_string()
}
/// Decodes a [`JsString`] into a [`String`], returning an error if the string contains unpaired
/// surrogates.
///
/// # Errors
///
/// [`FromUtf16Error`][std::string::FromUtf16Error] if it contains any invalid data.
#[inline]
pub fn to_std_string(&self) -> Result<String, std::string::FromUtf16Error> {
self.as_str().to_std_string()
}
/// Decodes a [`JsString`] into an iterator of [`Result<String, u16>`], returning surrogates as
/// errors.
#[inline]
pub fn to_std_string_with_surrogates(&self) -> impl Iterator<Item = Result<String, u16>> + '_ {
self.as_str().to_std_string_with_surrogates()
}
/// Maps the valid segments of an UTF16 string and leaves the unpaired surrogates unchanged.
#[inline]
#[must_use]
pub fn map_valid_segments<F>(&self, mut f: F) -> Self
where
F: FnMut(String) -> String,
{
let mut text = Vec::new();
for part in self.to_std_string_with_surrogates() {
match part {
Ok(string) => text.extend(f(string).encode_utf16()),
Err(surr) => text.push(surr),
}
}
Self::from(&text[..])
}
/// Gets an iterator of all the Unicode codepoints of a [`JsString`].
#[inline]
pub fn code_points(&self) -> impl Iterator<Item = CodePoint> + Clone + '_ {
self.as_str().code_points()
}
/// Abstract operation `StringIndexOf ( string, searchValue, fromIndex )`
///
/// Note: Instead of returning an isize with `-1` as the "not found" value, we make use of the
/// type system and return <code>[Option]\<usize\></code> with [`None`] as the "not found" value.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-stringindexof
#[inline]
#[must_use]
pub fn index_of(&self, search_value: JsStr<'_>, from_index: usize) -> Option<usize> {
self.as_str().index_of(search_value, from_index)
}
/// Abstract operation `CodePointAt( string, position )`.
///
/// The abstract operation `CodePointAt` takes arguments `string` (a String) and `position` (a
/// non-negative integer) and returns a Record with fields `[[CodePoint]]` (a code point),
/// `[[CodeUnitCount]]` (a positive integer), and `[[IsUnpairedSurrogate]]` (a Boolean). It
/// interprets string as a sequence of UTF-16 encoded code points, as described in 6.1.4, and reads
/// from it a single code point starting with the code unit at index `position`.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-codepointat
///
/// # Panics
///
/// If `position` is smaller than size of string.
#[inline]
#[must_use]
pub fn code_point_at(&self, position: usize) -> CodePoint {
self.as_str().code_point_at(position)
}
/// Abstract operation `StringToNumber ( str )`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-stringtonumber
#[inline]
#[must_use]
pub fn to_number(&self) -> f64 {
self.as_str().to_number()
}
/// Get the length of the [`JsString`].
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.as_str().len()
}
/// Return true if the [`JsString`] is emtpy.
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Convert the [`JsString`] into a [`Vec<U16>`].
#[inline]
#[must_use]
pub fn to_vec(&self) -> Vec<u16> {
self.as_str().to_vec()
}
/// Check if the [`JsString`] contains a byte.
#[inline]
#[must_use]
pub fn contains(&self, element: u8) -> bool {
self.as_str().contains(element)
}
/// Trim whitespace from the start and end of the [`JsString`].
#[inline]
#[must_use]
pub fn trim(&self) -> JsStr<'_> {
self.as_str().trim()
}
/// Trim whitespace from the start of the [`JsString`].
#[inline]
#[must_use]
pub fn trim_start(&self) -> JsStr<'_> {
self.as_str().trim_start()
}
/// Trim whitespace from the end of the [`JsString`].
#[inline]
#[must_use]
pub fn trim_end(&self) -> JsStr<'_> {
self.as_str().trim_end()
}
/// Get the element a the given index, [`None`] otherwise.
#[inline]
#[must_use]
pub fn get<'a, I>(&'a self, index: I) -> Option<I::Value>
where
I: JsSliceIndex<'a>,
{
self.as_str().get(index)
}
/// Returns an element or subslice depending on the type of index, without doing bounds check.
///
/// # Safety
///
/// Caller must ensure the index is not out of bounds
#[inline]
#[must_use]
pub unsafe fn get_unchecked<'a, I>(&'a self, index: I) -> I::Value
where
I: JsSliceIndex<'a>,
{
// SAFETY: Caller must ensure the index is not out of bounds
unsafe { self.as_str().get_unchecked(index) }
}
/// Get the element a the given index.
///
/// # Panics
///
/// If the index is out of bounds.
#[inline]
#[must_use]
pub fn get_expect<'a, I>(&'a self, index: I) -> I::Value
where
I: JsSliceIndex<'a>,
{
self.as_str().get_expect(index)
}
/// Gets a displayable escaped string. This may be faster and has fewer
/// allocations than `format!("{}", str.to_string_escaped())` when
/// displaying.
#[inline]
#[must_use]
pub fn display_escaped(&self) -> JsStrDisplayEscaped<'_> {
self.as_str().display_escaped()
}
/// Gets a displayable lossy string. This may be faster and has fewer
/// allocations than `format!("{}", str.to_string_lossy())` when displaying.
#[inline]
#[must_use]
pub fn display_lossy(&self) -> JsStrDisplayLossy<'_> {
self.as_str().display_lossy()
}
}
impl JsString {
/// Create a [`JsString`] from a static js string.
#[must_use]
pub const fn from_static_js_string(src: &'static StaticJsString) -> Self {
let src = ptr::from_ref(src).cast::<RawJsString>();
JsString {
// SAFETY:
// `StaticJsString` has the same memory layout as `RawJsString` for the first 2 fields
// which means it is safe to use it to represent `RawJsString` as long as we only acccess the first 2 fields,
// and the static reference indicates that the pointer cast is valid.
ptr: unsafe { Tagged::from_ptr(src.cast_mut()) },
}
}
/// Obtains the underlying [`&[u16]`][slice] slice of a [`JsString`]
#[inline]
#[must_use]
pub fn as_str(&self) -> JsStr<'_> {
match self.ptr.unwrap() {
UnwrappedTagged::Ptr(h) => {
// SAFETY:
// - The `RawJsString` type has all the necessary information to reconstruct a valid
// slice (length and starting pointer).
//
// - We aligned `h.data()` on allocation, and the block is of size `h.len`, so this
// should only generate valid reads.
//
// - The lifetime of `&Self::Target` is shorter than the lifetime of `self`, as seen
// by its signature, so this doesn't outlive `self`.
//
// - The `RawJsString` created from string literal has a static reference to the string literal,
// making it safe to be dereferenced and used as a static `JsStr`.
//
// - `Cell<usize>` is readable as an usize as long as we don't try to mutate the pointed variable,
// which means it is safe to read the `refcount` as `read_only` here.
unsafe {
let h = h.as_ptr();
if (*h).refcount.read_only == 0 {
let h = h.cast::<StaticJsString>();
return if (*h).tagged_len.is_latin1() {
JsStr::latin1(std::slice::from_raw_parts(
(*h).ptr,
(*h).tagged_len.len(),
))
} else {
JsStr::utf16(std::slice::from_raw_parts(
(*h).ptr.cast(),
(*h).tagged_len.len(),
))
};
}
let len = (*h).len();
if (*h).is_latin1() {
JsStr::latin1(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
} else {
JsStr::utf16(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
}
}
}
UnwrappedTagged::Tag(index) => {
// SAFETY: all static strings are valid indices on `STATIC_JS_STRINGS`, so `get` should always
// return `Some`.
unsafe { StaticJsStrings::get(index).unwrap_unchecked() }
}
}
}
/// Creates a new [`JsString`] from the concatenation of `x` and `y`.
#[inline]
#[must_use]
pub fn concat(x: JsStr<'_>, y: JsStr<'_>) -> Self {
Self::concat_array(&[x, y])
}
/// Creates a new [`JsString`] from the concatenation of every element of
/// `strings`.
#[inline]
#[must_use]
pub fn concat_array(strings: &[JsStr<'_>]) -> Self {
let mut latin1_encoding = true;
let mut full_count = 0usize;
for string in strings {
let Some(sum) = full_count.checked_add(string.len()) else {
alloc_overflow()
};
if !string.is_latin1() {
latin1_encoding = false;
}
full_count = sum;
}
let ptr = Self::allocate_inner(full_count, latin1_encoding);
let string = {
// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
let mut data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
for &string in strings {
// SAFETY:
// The sum of all `count` for each `string` equals `full_count`, and since we're
// iteratively writing each of them to `data`, `copy_non_overlapping` always stays
// in-bounds for `count` reads of each string and `full_count` writes to `data`.
//
// Each `string` must be properly aligned to be a valid slice, and `data` must be
// properly aligned by `allocate_inner`.
//
// `allocate_inner` must return a valid pointer to newly allocated memory, meaning
// `ptr` and all `string`s should never overlap.
unsafe {
// NOTE: The aligment is checked when we allocate the array.
#[allow(clippy::cast_ptr_alignment)]
match (latin1_encoding, string.variant()) {
(true, JsStrVariant::Latin1(s)) => {
let count = s.len();
ptr::copy_nonoverlapping(s.as_ptr(), data.cast::<u8>(), count);
data = data.cast::<u8>().add(count).cast::<u8>();
}
(false, JsStrVariant::Latin1(s)) => {
let count = s.len();
for (i, byte) in s.iter().enumerate() {
*data.cast::<u16>().add(i) = u16::from(*byte);
}
data = data.cast::<u16>().add(count).cast::<u8>();
}
(false, JsStrVariant::Utf16(s)) => {
let count = s.len();
ptr::copy_nonoverlapping(s.as_ptr(), data.cast::<u16>(), count);
data = data.cast::<u16>().add(count).cast::<u8>();
}
(true, JsStrVariant::Utf16(_)) => {
unreachable!("Already checked that it's latin1 encoding")
}
}
}
}
Self {
// SAFETY: We already know it's a valid heap pointer.
ptr: unsafe { Tagged::from_ptr(ptr.as_ptr()) },
}
};
StaticJsStrings::get_string(&string.as_str()).unwrap_or(string)
}
/// Allocates a new [`RawJsString`] with an internal capacity of `str_len` chars.
///
/// # Panics
///
/// Panics if `try_allocate_inner` returns `Err`.
fn allocate_inner(str_len: usize, latin1: bool) -> NonNull<RawJsString> {
match Self::try_allocate_inner(str_len, latin1) {
Ok(v) => v,
Err(None) => alloc_overflow(),
Err(Some(layout)) => std::alloc::handle_alloc_error(layout),
}
}
// This is marked as safe because it is always valid to call this function to request any number
// of `u16`, since this function ought to fail on an OOM error.
/// Allocates a new [`RawJsString`] with an internal capacity of `str_len` chars.
///
/// # Errors
///
/// Returns `Err(None)` on integer overflows `usize::MAX`.
/// Returns `Err(Some(Layout))` on allocation error.
fn try_allocate_inner(
str_len: usize,
latin1: bool,
) -> Result<NonNull<RawJsString>, Option<Layout>> {
let (layout, offset) = if latin1 {
Layout::array::<u8>(str_len)
} else {
Layout::array::<u16>(str_len)
}
.and_then(|arr| Layout::new::<RawJsString>().extend(arr))
.map(|(layout, offset)| (layout.pad_to_align(), offset))
.map_err(|_| None)?;
debug_assert_eq!(offset, DATA_OFFSET);
#[allow(clippy::cast_ptr_alignment)]
// SAFETY:
// The layout size of `RawJsString` is never zero, since it has to store
// the length of the string and the reference count.
let inner = unsafe { alloc(layout).cast::<RawJsString>() };
// We need to verify that the pointer returned by `alloc` is not null, otherwise
// we should abort, since an allocation error is pretty unrecoverable for us
// right now.
let inner = NonNull::new(inner).ok_or(Some(layout))?;
// SAFETY:
// `NonNull` verified for us that the pointer returned by `alloc` is valid,
// meaning we can write to its pointed memory.
unsafe {
// Write the first part, the `RawJsString`.
inner.as_ptr().write(RawJsString {
tagged_len: TaggedLen::new(str_len, latin1),
refcount: RefCount {
read_write: ManuallyDrop::new(Cell::new(1)),
},
data: [0; 0],
});
}
debug_assert!({
let inner = inner.as_ptr();
// SAFETY:
// - `inner` must be a valid pointer, since it comes from a `NonNull`,
// meaning we can safely dereference it to `RawJsString`.
// - `offset` should point us to the beginning of the array,
// and since we requested an `RawJsString` layout with a trailing
// `[u16; str_len]`, the memory of the array must be in the `usize`
// range for the allocation to succeed.
unsafe {
ptr::eq(
inner.cast::<u8>().add(offset).cast(),
(*inner).data.as_mut_ptr(),
)
}
});
Ok(inner)
}
/// Creates a new [`JsString`] from `data`, without checking if the string is in the interner.
fn from_slice_skip_interning(string: JsStr<'_>) -> Self {
let count = string.len();
let ptr = Self::allocate_inner(count, string.is_latin1());
// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
let data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
// SAFETY:
// - We read `count = data.len()` elements from `data`, which is within the bounds of the slice.
// - `allocate_inner` must allocate at least `count` elements, which allows us to safely
// write at least `count` elements.
// - `allocate_inner` should already take care of the alignment of `ptr`, and `data` must be
// aligned to be a valid slice.
// - `allocate_inner` must return a valid pointer to newly allocated memory, meaning `ptr`
// and `data` should never overlap.
unsafe {
// NOTE: The aligment is checked when we allocate the array.
#[allow(clippy::cast_ptr_alignment)]
match string.variant() {
JsStrVariant::Latin1(s) => {
ptr::copy_nonoverlapping(s.as_ptr(), data.cast::<u8>(), count);
}
JsStrVariant::Utf16(s) => {
ptr::copy_nonoverlapping(s.as_ptr(), data.cast::<u16>(), count);
}
}
}
Self {
// SAFETY: `allocate_inner` guarantees `ptr` is a valid heap pointer.
ptr: Tagged::from_non_null(ptr),
}
}
/// Creates a new [`JsString`] from `data`.
fn from_slice(string: JsStr<'_>) -> Self {
if let Some(s) = StaticJsStrings::get_string(&string) {
return s;
}
Self::from_slice_skip_interning(string)
}
/// Check if the [`JsString`] is static.
#[inline]
#[must_use]
pub fn is_static(&self) -> bool {
self.refcount().is_none()
}
/// Gets the number of `JsString`s which point to this allocation.
#[inline]
#[must_use]
pub fn refcount(&self) -> Option<usize> {
match self.ptr.unwrap() {
UnwrappedTagged::Ptr(inner) => {
// SAFETY:
// `NonNull` and the constructions of `JsString` guarantee that `inner` is always valid.
// And `Cell<usize>` is readable as an usize as long as we don't try to mutate the pointed variable,
// which means it is safe to read the `refcount` as `read_only` here.
let rc = unsafe { (*inner.as_ptr()).refcount.read_only };
if rc == 0 {
None
} else {
Some(rc)
}
}
UnwrappedTagged::Tag(_inner) => None,
}
}
}
impl Clone for JsString {
#[inline]
fn clone(&self) -> Self {
if let UnwrappedTagged::Ptr(inner) = self.ptr.unwrap() {
// SAFETY:
// `NonNull` and the constructions of `JsString` guarantee that `inner` is always valid.
// And `Cell<usize>` is readable as an usize as long as we don't try to mutate the pointed variable,
// which means it is safe to read the `refcount` as `read_only` here.
let rc = unsafe { (*inner.as_ptr()).refcount.read_only };
if rc == 0 {
// pointee is a static string
return Self { ptr: self.ptr };
}
// SAFETY: `NonNull` and the constructions of `JsString` guarantee that `inner` is always valid.
let inner = unsafe { inner.as_ref() };
let strong = rc.wrapping_add(1);
if strong == 0 {
abort()
}
// SAFETY:
// This has been checked aboved to ensure it is a `read_write` variant,
// which means it is safe to write the `refcount` as `read_write` here.
unsafe {
inner.refcount.read_write.set(strong);
}
}
Self { ptr: self.ptr }
}
}
impl Default for JsString {
#[inline]
fn default() -> Self {
StaticJsStrings::EMPTY_STRING
}
}
impl Drop for JsString {
#[inline]
fn drop(&mut self) {
if let UnwrappedTagged::Ptr(raw) = self.ptr.unwrap() {
// See https://doc.rust-lang.org/src/alloc/sync.rs.html#1672 for details.
// SAFETY:
// `NonNull` and the constructions of `JsString` guarantees that `raw` is always valid.
// And `Cell<usize>` is readable as an usize as long as we don't try to mutate the pointed variable,
// which means it is safe to read the `refcount` as `read_only` here.
let refcount = unsafe { (*raw.as_ptr()).refcount.read_only };
if refcount == 0 {
// Just a static string. No need to drop.
return;
}
// SAFETY: `NonNull` and the constructions of `JsString` guarantees that `raw` is always valid.
let inner = unsafe { raw.as_ref() };
// SAFETY:
// This has been checked aboved to ensure it is a `read_write` variant,
// which means it is safe to write the `refcount` as `read_write` here.
unsafe {
inner.refcount.read_write.set(refcount - 1);
if inner.refcount.read_write.get() != 0 {
return;
}
}
// SAFETY:
// All the checks for the validity of the layout have already been made on `alloc_inner`,
// so we can skip the unwrap.
let layout = unsafe {
if inner.is_latin1() {
Layout::for_value(inner)
.extend(Layout::array::<u8>(inner.len()).unwrap_unchecked())
.unwrap_unchecked()
.0
.pad_to_align()
} else {
Layout::for_value(inner)
.extend(Layout::array::<u16>(inner.len()).unwrap_unchecked())
.unwrap_unchecked()
.0
.pad_to_align()
}
};
// SAFETY:
// If refcount is 0 and we call drop, that means this is the last `JsString` which
// points to this memory allocation, so deallocating it is safe.
unsafe {
dealloc(raw.as_ptr().cast(), layout);
}
}
}
}
impl std::fmt::Debug for JsString {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl Eq for JsString {}
macro_rules! impl_from_number_for_js_string {
($($module: ident => $($ty:ty),+)+) => {
$(
$(
impl From<$ty> for JsString {
#[inline]
fn from(value: $ty) -> Self {
JsString::from_slice_skip_interning(JsStr::latin1(
$module::Buffer::new().format(value).as_bytes(),
))
}
}
)+
)+
};
}
impl_from_number_for_js_string!(
itoa => i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, isize, usize
ryu_js => f32, f64
);
impl From<&[u16]> for JsString {
#[inline]
fn from(s: &[u16]) -> Self {
JsString::from_slice(JsStr::utf16(s))
}
}
impl From<&str> for JsString {
#[inline]
fn from(s: &str) -> Self {
// TODO: Check for latin1 encoding
if s.is_ascii() {
let js_str = JsStr::latin1(s.as_bytes());
return StaticJsStrings::get_string(&js_str)
.unwrap_or_else(|| JsString::from_slice_skip_interning(js_str));
}
let s = s.encode_utf16().collect::<Vec<_>>();
JsString::from_slice_skip_interning(JsStr::utf16(&s[..]))
}
}
impl From<JsStr<'_>> for JsString {
#[inline]
fn from(value: JsStr<'_>) -> Self {
StaticJsStrings::get_string(&value)
.unwrap_or_else(|| JsString::from_slice_skip_interning(value))
}
}
impl From<&[JsString]> for JsString {
#[inline]
fn from(value: &[JsString]) -> Self {
Self::concat_array(&value.iter().map(Self::as_str).collect::<Vec<_>>()[..])
}
}
impl From<String> for JsString {
#[inline]
fn from(s: String) -> Self {
Self::from(s.as_str())
}
}
impl<'a> From<Cow<'a, str>> for JsString {
#[inline]
fn from(s: Cow<'a, str>) -> Self {
match s {
Cow::Borrowed(s) => s.into(),
Cow::Owned(s) => s.into(),
}
}
}
impl<const N: usize> From<&[u16; N]> for JsString {
#[inline]
fn from(s: &[u16; N]) -> Self {
Self::from(&s[..])
}
}