1use nativeview_abi::table::Word;
4
5use crate::id::{NodeIndex, ResourceId};
6pub(crate) mod patch;
7
8use crate::resource::{FlatResource, ResourceStore};
9use crate::span_pool::{StrArena, StrSpan};
10use crate::store::NodeRow;
11use crate::table::TableMeta;
12
13#[derive(Clone, Copy)]
19pub struct RowRef<'a> {
20 words: &'a [Word],
21 text: &'a StrArena,
22 str_words: u32,
23}
24
25impl<'a> RowRef<'a> {
26 #[inline(always)]
27 pub(crate) const fn new(words: &'a [Word], text: &'a StrArena, str_words: u32) -> Self {
28 Self {
29 words,
30 text,
31 str_words,
32 }
33 }
34
35 #[doc(hidden)]
36 #[inline(always)]
37 pub fn word(self, idx: usize) -> Word {
38 self.words[idx]
39 }
40
41 #[doc(hidden)]
42 #[inline(always)]
43 pub fn words(self) -> &'a [Word] {
44 self.words
45 }
46
47 #[doc(hidden)]
48 #[inline(always)]
49 pub fn str_at(self, word: usize) -> &'a str {
50 assert!(
51 word < 31 && (self.str_words & (1u32 << word)) != 0,
52 "RowRef::str_at: word {word} is not a declared string word (str_words={:#x})",
53 self.str_words
54 );
55 self.text
56 .str_unchecked(StrSpan::new(self.words[word], self.words[word + 1]))
57 }
58
59 #[doc(hidden)]
60 #[inline(always)]
61 pub fn str_span_at(self, word: usize) -> StrSpan {
62 assert!(
63 word < 31 && (self.str_words & (1u32 << word)) != 0,
64 "RowRef::str_span_at: word {word} is not a declared string word (str_words={:#x})",
65 self.str_words
66 );
67 StrSpan::new(self.words[word], self.words[word + 1])
68 }
69}
70
71#[derive(Debug, Default, Clone)]
74pub struct DirtyList {
75 buf: Vec<NodeIndex>,
76 len: usize,
77}
78
79impl DirtyList {
80 #[inline(always)]
81 pub(crate) fn push_if(&mut self, value: NodeIndex, cond: bool) {
82 if self.len == self.buf.len() {
83 self.grow();
84 }
85 self.buf[self.len] = value;
86 self.len += cond as usize;
87 }
88
89 #[cold]
90 fn grow(&mut self) {
91 let n = (self.buf.len() * 2).max(64);
92 self.buf.resize(n, NodeIndex::NULL);
93 }
94
95 #[inline]
96 pub(crate) fn try_reserve(&mut self, additional: usize) -> Result<(), crate::wire::WireError> {
97 let needed = self
101 .len
102 .checked_add(additional)
103 .and_then(|n| n.checked_add(1))
104 .ok_or(crate::wire::WireError::Alloc)?;
105 let min_cap = needed.max(64);
106 if self.buf.len() < min_cap {
107 self.buf
108 .try_reserve(min_cap - self.buf.len())
109 .map_err(|_| crate::wire::WireError::Alloc)?;
110 self.buf.resize(min_cap, NodeIndex::NULL);
111 }
112 Ok(())
113 }
114
115 #[cfg(test)]
116 #[inline]
117 pub(crate) fn buf_len(&self) -> usize {
118 self.buf.len()
119 }
120
121 #[inline]
122 pub fn as_slice(&self) -> &[NodeIndex] {
123 &self.buf[..self.len]
124 }
125
126 #[inline]
127 pub(crate) fn clear(&mut self) {
128 self.len = 0;
129 }
130}
131
132pub struct RowWriter<'a> {
138 pub(crate) row: &'a mut [Word],
139 pub(crate) mask: &'a mut u32,
140 pub(crate) dirty: &'a mut DirtyList,
141 pub(crate) node_index: NodeIndex,
142 pub(crate) node: &'a mut NodeRow,
143 pub(crate) touched: &'a mut DirtyList,
144 pub(crate) desc: &'a TableMeta,
145 pub(crate) text: &'a mut StrArena,
146 pub(crate) resources: &'a mut ResourceStore,
147}
148
149impl<'a> RowWriter<'a> {
150 #[inline(always)]
151 fn mark_with_effects(&mut self, word: usize, changed: u32, effect_bits: u8) {
152 let was = *self.mask;
153 *self.mask = was | (changed << word);
154 self.dirty
155 .push_if(self.node_index, (was == 0) & (changed != 0));
156 let fx = effect_bits & 0u8.wrapping_sub(changed as u8);
157 let had = self.node.effects;
158 self.node.effects = had | fx;
159 self.touched
160 .push_if(self.node_index, (had == 0) & (fx != 0));
161 }
162
163 #[inline(always)]
164 fn mark(&mut self, word: usize, changed: u32) {
165 let effect_bits = self.desc.word_effects()[word];
166 self.mark_with_effects(word, changed, effect_bits);
167 }
168
169 #[doc(hidden)]
173 #[inline(always)]
174 pub fn set_explicit_bit(&mut self, bit: u32, field_effects: u8) {
175 assert!(
176 self.desc.inherited()
177 && bit < 32
178 && ((self.desc.str_words() | self.desc.resource_words()) & 1) == 0,
179 "RowWriter::set_explicit_bit requires an inherited table with bit < 32 on `{}`",
180 self.desc.name()
181 );
182 let bit_mask = 1u32 << bit;
183 let old = self.row[0];
184 let new = old | bit_mask;
185 self.row[0] = new;
186 self.mark_with_effects(0, (old != new) as u32, field_effects);
187 }
188
189 #[doc(hidden)]
192 #[inline(always)]
193 pub fn clear_explicit_bit(&mut self, bit: u32, field_effects: u8) {
194 assert!(
195 self.desc.inherited()
196 && bit < 32
197 && ((self.desc.str_words() | self.desc.resource_words()) & 1) == 0,
198 "RowWriter::clear_explicit_bit requires an inherited table with bit < 32 on `{}`",
199 self.desc.name()
200 );
201 let bit_mask = 1u32 << bit;
202 let old = self.row[0];
203 let new = old & !bit_mask;
204 self.row[0] = new;
205 self.mark_with_effects(0, (old != new) as u32, field_effects);
206 }
207
208 #[inline]
211 pub(crate) fn put_explicit_word(&mut self, new_mask: u32) {
212 debug_assert!(self.desc.inherited());
213 let old = self.row[0];
214 let diff = old ^ new_mask;
215 if diff == 0 {
216 return;
217 }
218 self.row[0] = new_mask;
219 let fx = if let Some(sd) = self.desc.as_static() {
220 let mut acc = 0u8;
221 let mut d = diff;
222 while d != 0 {
223 let b = d.trailing_zeros() as usize;
224 d &= d - 1;
225 if let Some(f) = sd.fields.get(b) {
226 acc |= f.effects;
227 }
228 }
229 acc
230 } else {
231 self.desc.effects()
232 };
233 self.mark_with_effects(0, 1, fx);
234 }
235
236 #[doc(hidden)]
239 #[inline(always)]
240 pub fn put(&mut self, word: usize, shift: u32, mask: u32, bits: u32) {
241 let str_w = self.desc.str_words();
242 let protected = str_w | (str_w << 1) | self.desc.resource_words();
243 assert!(
244 word < 32 && (protected & (1u32 << word)) == 0,
245 "RowWriter::put cannot overwrite string/resource word {word} on `{}`",
246 self.desc.name()
247 );
248 let w = &mut self.row[word];
249 let old = *w;
250 let new = (old & !mask) | ((bits << shift) & mask);
251 *w = new;
252 self.mark(word, (old != new) as u32);
253 }
254
255 #[doc(hidden)]
257 #[inline(always)]
258 pub fn as_row_ref(&self) -> RowRef<'_> {
259 RowRef::new(self.row, self.text, self.desc.str_words())
260 }
261
262 #[inline(always)]
264 pub fn words(&self) -> &[Word] {
265 self.row
266 }
267
268 #[doc(hidden)]
270 #[inline]
271 pub fn put_str(&mut self, word: usize, text: &str) {
272 assert!(
273 word < 31 && (self.desc.str_words() & (1u32 << word)) != 0,
274 "RowWriter::put_str: word {word} is not a declared string word on `{}`",
275 self.desc.name()
276 );
277 let old = StrSpan::new(self.row[word], self.row[word + 1]);
278 if self.text.str_unchecked(old) == text {
279 return;
280 }
281 let new = self.text.replace_str(old, text);
282 self.row[word] = new.off();
283 self.row[word + 1] = new.len();
284 self.mark(word, 1);
285 self.mark(word + 1, 1);
286 }
287
288 #[inline]
290 pub fn str(&self, word: usize) -> &str {
291 assert!(
292 word < 31 && (self.desc.str_words() & (1u32 << word)) != 0,
293 "RowWriter::str: word {word} is not a declared string word on `{}`",
294 self.desc.name()
295 );
296 self.text
297 .str_unchecked(StrSpan::new(self.row[word], self.row[word + 1]))
298 }
299
300 #[inline]
301 fn swap_resource(&mut self, word: usize, id: ResourceId) {
302 let old = ResourceId(self.row[word]);
303 self.row[word] = id.0;
304 self.mark(word, (old != id) as u32);
305 self.resources.release(old);
306 }
307
308 #[doc(hidden)]
310 #[inline]
311 pub fn put_resource<R: FlatResource>(&mut self, word: usize, value: Option<&R>) {
312 assert!(
313 word < 32 && (self.desc.resource_words() & (1u32 << word)) != 0,
314 "RowWriter::put_resource: word {word} is not a declared resource word on `{}`",
315 self.desc.name()
316 );
317 let id = match value {
318 Some(v) => self.resources.intern(v),
319 None => ResourceId::NONE,
320 };
321 self.swap_resource(word, id);
322 }
323
324 #[doc(hidden)]
326 #[inline]
327 pub fn put_resource_with<R: FlatResource>(
328 &mut self,
329 word: usize,
330 write: impl FnOnce(&mut Vec<u32>) -> bool,
331 ) {
332 assert!(
333 word < 32 && (self.desc.resource_words() & (1u32 << word)) != 0,
334 "RowWriter::put_resource_with: word {word} is not a declared resource word on `{}`",
335 self.desc.name()
336 );
337 let id = self
338 .resources
339 .try_intern_with::<R>(write)
340 .unwrap_or(ResourceId::NONE);
341 self.swap_resource(word, id);
342 }
343
344 #[doc(hidden)]
346 #[inline]
347 pub fn put_resource_ephemeral<R: FlatResource>(&mut self, word: usize, value: &R) {
348 assert!(
349 word < 32 && (self.desc.resource_words() & (1u32 << word)) != 0,
350 "RowWriter::put_resource_ephemeral: word {word} is not a declared resource word on `{}`",
351 self.desc.name()
352 );
353 let old = ResourceId(self.row[word]);
354 let id = self.resources.set_ephemeral(old, value);
355 if id == old && !id.is_none() {
356 self.mark(word, 1);
357 } else {
358 self.swap_resource(word, id);
359 }
360 }
361
362 #[doc(hidden)]
364 #[inline]
365 pub fn put_resource_ephemeral_with<R: FlatResource>(
366 &mut self,
367 word: usize,
368 write: impl FnOnce(&mut Vec<u32>) -> bool,
369 ) {
370 assert!(
371 word < 32 && (self.desc.resource_words() & (1u32 << word)) != 0,
372 "RowWriter::put_resource_ephemeral_with: word {word} is not a declared resource word on `{}`",
373 self.desc.name()
374 );
375 let old = ResourceId(self.row[word]);
376 let id = self
377 .resources
378 .try_set_ephemeral_with::<R>(old, write)
379 .unwrap_or(ResourceId::NONE);
380 if id == old && !id.is_none() {
381 self.mark(word, 1);
382 } else {
383 self.swap_resource(word, id);
384 }
385 }
386
387 #[inline]
389 pub fn resource<R: FlatResource>(&self, word: usize) -> Option<R::View<'_>> {
390 self.resources.get::<R>(ResourceId(self.row[word]))
391 }
392
393 #[doc(hidden)]
397 #[inline]
398 pub fn put_resource_id<R: FlatResource>(&mut self, word: usize, id: ResourceId) -> bool {
399 if word >= 32 || (self.desc.resource_words() & (1u32 << word)) == 0 {
400 return false;
401 }
402 if !id.is_none() && !self.resources.is_live_of::<R>(id) {
403 return false;
404 }
405 if ResourceId(self.row[word]) == id {
406 return true;
407 }
408 self.resources.retain(id);
409 self.swap_resource(word, id);
410 true
411 }
412
413 #[inline]
416 pub(crate) fn put_resource_id_wire(&mut self, word: usize, id: ResourceId) {
417 if word >= 32 || (self.desc.resource_words() & (1u32 << word)) == 0 {
418 return;
419 }
420 if !id.is_none() && !self.resources.is_defined(id) {
421 return;
422 }
423 if ResourceId(self.row[word]) == id {
424 return;
425 }
426 self.resources.retain(id);
427 self.swap_resource(word, id);
428 }
429}