File size: 6,099 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{fmt::Display, marker::PhantomData};

use oxc::{
	allocator::{Allocator, Vec},
	span::Span,
};
use thiserror::Error;

pub mod transform;
use transform::{Transform, TransformType};

#[derive(Debug, Error)]
pub enum TransformError {
	#[cfg(not(feature = "debug"))]
	#[error("out of bounds while applying range {0}..{1} for {2} (span {3}..{4})")]
	Oob(u32, u32, &'static str, u32, u32),
	#[cfg(feature = "debug")]
	#[error(

		"out of bounds while applying range {0}..{1} for {2} (span {3}..{4}, last few spans: {5})"

	)]
	Oob(u32, u32, &'static str, u32, u32, LastSpans),

	#[cfg(feature = "debug")]
	#[error("Spans inside each other, all spans: {0}")]
	InvalidSpans(LastSpans),

	#[error("too much code added while applying changes at cursor {0}")]
	AddedTooLarge(u32),
	#[error("Allocator already set")]
	AllocSet,
	#[error("Allocator not set")]
	AllocUnset,
}

#[cfg(feature = "debug")]
#[derive(Debug)]
pub struct LastSpans(std::vec::Vec<(Span, std::string::String)>);
#[cfg(feature = "debug")]
impl Display for LastSpans {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "\n(current) ")?;
		for span in &self.0 {
			writeln!(f, "{}..{}: {}", span.0.start, span.0.end, span.1)?;
		}

		Ok(())
	}
}

pub struct TransformResult<'alloc> {
	pub source: Vec<'alloc, u8>,
	pub map: Vec<'alloc, u8>,
}

pub struct Transformer<'alloc, 'data, T: Transform<'data>> {
	phantom: PhantomData<&'data str>,
	alloc: Option<&'alloc Allocator>,
	inner: std::vec::Vec<T>,
}

impl<'data, T: Transform<'data>> Default for Transformer<'_, 'data, T> {
	fn default() -> Self {
		Self::new()
	}
}

impl<'alloc, 'data, T: Transform<'data>> Transformer<'alloc, 'data, T> {
	pub fn new() -> Self {
		Self {
			phantom: PhantomData,
			inner: std::vec::Vec::new(),
			alloc: None,
		}
	}

	pub fn add(&mut self, rewrite: impl IntoIterator<Item = T>) {
		self.inner.extend(rewrite);
	}

	pub fn set_alloc(&mut self, alloc: &'alloc Allocator) -> Result<(), TransformError> {
		if self.alloc.is_some() {
			Err(TransformError::AllocSet)
		} else {
			self.alloc.replace(alloc);
			Ok(())
		}
	}

	pub fn take_alloc(&mut self) -> Result<(), TransformError> {
		self.alloc
			.take()
			.ok_or(TransformError::AllocUnset)
			.map(|_| ())
	}

	pub fn get_alloc(&self) -> Result<&'alloc Allocator, TransformError> {
		self.alloc.ok_or(TransformError::AllocUnset)
	}

	pub fn empty(&self) -> bool {
		self.inner.is_empty()
	}

	pub fn perform(
		&mut self,
		js: &'data str,
		data: &T::ToLowLevelData,
		build_map: bool,
	) -> Result<TransformResult<'alloc>, TransformError> {
		let mut itoa = itoa::Buffer::new();

		let alloc = self.get_alloc()?;

		let mut cursor = 0;
		let mut offset = 0i32;
		let mut buffer = Vec::with_capacity_in(js.len() * 2, alloc);

		#[cfg(feature = "debug")]
		let mut debug_vec = std::vec::Vec::new();

		macro_rules! tryget {
			($reason:literal, $start:ident..$end:ident, $span:ident) => {{
				let ret = js.get($start as usize..$end as usize);
				#[cfg(not(feature = "debug"))]
				{
					ret.ok_or_else(|| {
						TransformError::Oob($start, $end, $reason, $span.start, $span.end)
					})?
				}
				#[cfg(feature = "debug")]
				{
					ret.ok_or_else(|| {
						TransformError::Oob(
							$start,
							$end,
							$reason,
							$span.start,
							$span.end,
							LastSpans(debug_vec.iter().rev().cloned().take(6).collect()),
						)
					})?
				}
			}};
		}

		// insert has a 9 byte size, replace has a 13 byte minimum and usually it's like 5 bytes
		// for the old str added on so use 16 as a really rough estimate
		let mut map = if build_map {
			let mut map = Vec::with_capacity_in((self.inner.len() * 16) + 4, alloc);
			map.extend_from_slice(&(self.inner.len() as u32).to_le_bytes());
			map
		} else {
			Vec::new_in(alloc)
		};

		self.inner.sort();

		#[cfg(feature = "debug")]
		{
			let mut last_end = 0;
			for change in &self.inner {
				let span = change.span();
				if last_end > span.start {
					let vec = self
						.inner
						.drain(..)
						.map(|x| {
							(
								x.span(),
								x.into_low_level(data, 0).to_string(&mut itoa, alloc),
							)
						})
						.collect();
					return Err(TransformError::InvalidSpans(LastSpans(vec)));
				}
				last_end = span.end;
			}
		}

		for change in self.inner.drain(..) {
			let span = change.span();
			let Span { start, end, .. } = span;

			let transform = change.into_low_level(data, offset);
			#[cfg(feature = "debug")]
			debug_vec.push((span, transform.to_string(&mut itoa, alloc)));

			buffer.extend_from_slice(tryget!("cursor -> start", cursor..start, span).as_bytes());

			let len = transform.apply(&mut itoa, &mut buffer);
			if build_map {
				// pos
				map.extend_from_slice(&start.wrapping_add_signed(offset).to_le_bytes());
				// size
				map.extend_from_slice(&len.to_le_bytes());
			}

			match transform.ty {
				TransformType::Insert => {
					buffer.extend_from_slice(
						tryget!("insert: start -> end", start..end, span).as_bytes(),
					);

					if build_map {
						// INSERT op
						map.push(0);
					}

					offset = offset.wrapping_add_unsigned(len);
				}
				TransformType::Replace => {
					if build_map {
						// REPLACE op
						map.push(1);
						// len
						map.extend_from_slice(&(end - start).to_le_bytes());
						// oldstr
						map.extend_from_slice(
							tryget!("replace: start -> end", start..end, span).as_bytes(),
						);
					}

					let len =
						i32::try_from(len).map_err(|_| TransformError::AddedTooLarge(cursor))?;
					let diff = len.wrapping_sub_unsigned(end - start);
					offset = offset.wrapping_add(diff);
				}
			}

			cursor = end;
		}

		let js_len = js.len() as u32;
		let span = Span::new(0, js_len);
		buffer.extend_from_slice(tryget!("cursor -> js end", cursor..js_len, span).as_bytes());

		Ok(TransformResult {
			source: buffer,
			map,
		})
	}
}