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
//! Lua 5.4 UTF-8 builtins on the resumable library engine.
use super::library::{AuxWork, Ctx, Next};
use super::*;
use crate::library::Work;
use crate::utf8::{continuation, decode, relative};
use crate::utf8lib::{CHARPATTERN, FUNCTIONS, LAX_STEP, SEQUENCES, STRICT_STEP, Utf8Fn, Utf8Work};
impl AuxWork for Utf8Work {
fn next(runtime: &mut Runtime, ctx: &Ctx, work: &mut Self) -> Result<Next, VmError> {
runtime.utf8_next(ctx, work)
}
fn wrap(self) -> Work {
Work::Utf8(Box::new(self))
}
fn wrap_boxed(self: Box<Self>) -> Work {
Work::Utf8(self)
}
}
fn invalid() -> Next {
Next::Error(LuaFault::Argument, b"invalid UTF-8 code".to_vec())
}
impl Runtime {
/// Install the six-field UTF-8 module in globals and package.loaded.
///
/// # Errors
///
/// Returns a VM resource error when library tables, strings or closures
/// exceed limits, a missing-symbol error if required natives were not registered,
/// or a VM integrity error for invalid runtime state.
pub fn install_utf8(&mut self) -> Result<(), VmError> {
let table = self.new_library_table("utf8")?;
self.register_module("utf8", table)?;
for (name, symbol, _) in FUNCTIONS {
let value = self.native_value(symbol)?;
self.set_field(table, name, value)?;
}
let pattern = self.new_string(CHARPATTERN.to_vec())?;
self.set_field(table, "charpattern", pattern)
}
pub(super) fn call_utf8(
&mut self,
active: Handle<ThreadObj>,
function: Utf8Fn,
journal: &mut Journal,
) -> Result<Poll, VmError> {
let (func, _, passed, _) = self.call_site(active)?;
let ctx = Ctx {
active,
func,
passed,
framed: false,
};
macro_rules! check {
($result:expr) => {
match $result {
Ok(value) => value,
Err(next) => return Ok(self.finish_next(active, next)),
}
};
}
if function == Utf8Fn::Char {
return self.run_aux(
ctx,
Utf8Work::Char {
next: 0,
out: Vec::new(),
},
None,
journal,
);
}
let Some(subject) = self.string_arg(&ctx, 0)? else {
return Ok(self.finish_next(active, self.bad_type(&ctx, 0, "string")));
};
let len = self
.heap
.string_bytes(subject)
.ok_or(VmError::Corrupt)?
.len();
let work = match function {
Utf8Fn::Len | Utf8Fn::Codepoint => {
let first = relative(check!(self.opt_int_arg(&ctx, 1, 1)), len);
let last = relative(
check!(self.opt_int_arg(
&ctx,
2,
if function == Utf8Fn::Len { -1 } else { first }
)),
len,
);
let points = function == Utf8Fn::Codepoint;
if first < 1 || (!points && first - 1 > len as i64) {
return Ok(self.finish_next(
active,
self.bad_arg(
&ctx,
1,
if points {
"out of bounds"
} else {
"initial position out of bounds"
},
),
));
}
if last > len as i64 {
return Ok(self.finish_next(
active,
self.bad_arg(
&ctx,
2,
if points {
"out of bounds"
} else {
"final position out of bounds"
},
),
));
}
if first > last {
return self
.base_return(active, if points { &[] } else { &[Value::Integer(0)] });
}
if points {
let n = (last - first + 1) as u32;
if !matches!(
self.slot_fault(active, ctx.scratch_slot(n).saturating_add(4)),
Ok(None)
) {
return Ok(self.finish_next(
active,
Next::Error(
LuaFault::StringSlice,
b"stack overflow (string slice too long)".to_vec(),
),
));
}
}
Utf8Work::Scan {
pos: (first - 1) as u32,
end: last as u32,
count: 0,
strict: !self.lib_arg(&ctx, 3).truthy(),
points,
}
}
Utf8Fn::Offset => {
let n = check!(self.int_arg(&ctx, 1));
let first = relative(
check!(self.opt_int_arg(&ctx, 2, if n >= 0 { 1 } else { len as i64 + 1 })),
len,
);
if first < 1 || first - 1 > len as i64 {
return Ok(
self.finish_next(active, self.bad_arg(&ctx, 2, "position out of bounds"))
);
}
let pos = (first - 1) as u32;
if n != 0
&& self
.heap
.string_bytes(subject)
.and_then(|s| s.get(pos as usize))
.is_some_and(|b| continuation(*b))
{
return Ok(self.finish_next(
active,
Next::Error(
LuaFault::Argument,
b"initial position is a continuation byte".to_vec(),
),
));
}
Utf8Work::Offset {
pos,
remaining: if n > 0 { n - 1 } else { n },
direction: n.signum(),
seeking: false,
}
}
Utf8Fn::Codes => {
if self
.heap
.string_bytes(subject)
.and_then(|s| s.first())
.is_some_and(|b| continuation(*b))
{
return Ok(
self.finish_next(active, self.bad_arg(&ctx, 0, "invalid UTF-8 code"))
);
}
let iterator = self.native_value(if self.lib_arg(&ctx, 1).truthy() {
LAX_STEP
} else {
STRICT_STEP
})?;
return self.base_return(
active,
&[iterator, Value::String(subject), Value::Integer(0)],
);
}
Utf8Fn::StrictStep | Utf8Fn::LaxStep => {
// lua_tointeger, with no argument error and unsigned negative handling.
let pos =
crate::base::lua_integer(&self.heap, self.lib_arg(&ctx, 1)).unwrap_or(0) as u64;
if pos >= len as u64 {
return self.base_return(active, &[]);
}
Utf8Work::Iterate {
pos: pos as u32,
strict: function == Utf8Fn::StrictStep,
}
}
Utf8Fn::Char => return Err(VmError::Corrupt),
};
self.run_aux(ctx, work, None, journal)
}
pub(super) fn utf8_next(&mut self, ctx: &Ctx, work: &mut Utf8Work) -> Result<Next, VmError> {
if let Utf8Work::Char { next, out } = work {
for _ in 0..SEQUENCES {
if *next == ctx.passed {
let bytes = std::mem::take(out);
return Ok(Next::Done(vec![self.new_string(bytes)?]));
}
let code = match self.int_arg(ctx, *next) {
Ok(code) => code,
Err(next) => return Ok(next),
};
if !(0..=0x7fffffff).contains(&code) {
return Ok(self.bad_arg(ctx, *next, "value out of range"));
}
let bytes = crate::utf8::encode(code as u32);
if out
.len()
.checked_add(bytes.len())
.is_none_or(|total| total > self.heap.max_string)
{
return Ok(Next::Fault(LuaFault::StringTooLarge));
}
if !self.heap.gc.fits(bytes.len() as u64) {
return Ok(Next::Fault(LuaFault::Memory));
}
self.heap.charge_held(bytes.len() as u64);
out.extend(bytes);
*next += 1;
}
if *next == ctx.passed {
let bytes = std::mem::take(out);
return Ok(Next::Done(vec![self.new_string(bytes)?]));
}
return Ok(Next::Busy);
}
let Value::String(subject) = self.lib_arg(ctx, 0) else {
return Err(VmError::Corrupt);
};
match work {
Utf8Work::Scan {
pos,
end,
count,
strict,
points,
} => {
for _ in 0..SEQUENCES {
if *pos >= *end {
break;
}
let bytes = self.heap.string_bytes(subject).ok_or(VmError::Corrupt)?;
let Some((code, next)) = decode(bytes, *pos as usize, *strict) else {
return Ok(if *points {
invalid()
} else {
Next::Done(vec![Value::Nil, Value::Integer(i64::from(*pos) + 1)])
});
};
if *points {
self.write_abs(
ctx.active,
ctx.scratch_slot(*count),
Value::Integer(i64::from(code)),
)?;
}
*count += 1;
*pos = next as u32;
}
if *pos < *end {
return Ok(Next::Busy);
}
Ok(Next::Done(if *points {
(0..*count).map(|i| self.scratch(ctx, i)).collect()
} else {
vec![Value::Integer(i64::from(*count))]
}))
}
Utf8Work::Offset {
pos,
remaining,
direction,
seeking,
} => {
let bytes = self.heap.string_bytes(subject).ok_or(VmError::Corrupt)?;
let len = bytes.len() as u32;
for _ in 0..crate::strlib::BYTE_BATCH {
let cont = bytes.get(*pos as usize).is_some_and(|b| continuation(*b));
if *direction == 0 {
if *pos == 0 || !cont {
return Ok(Next::Done(vec![Value::Integer(i64::from(*pos) + 1)]));
}
*pos -= 1;
} else if *seeking && cont && (*direction > 0 || *pos > 0) {
*pos = if *direction > 0 { *pos + 1 } else { *pos - 1 };
} else {
if *seeking {
*remaining -= *direction;
*seeking = false;
}
if *remaining == 0 {
return Ok(Next::Done(vec![Value::Integer(i64::from(*pos) + 1)]));
}
if (*direction < 0 && *pos == 0) || (*direction > 0 && *pos == len) {
return Ok(Next::Done(vec![Value::Nil]));
}
*pos = if *direction > 0 { *pos + 1 } else { *pos - 1 };
*seeking = true;
}
}
Ok(Next::Busy)
}
Utf8Work::Iterate { pos, strict } => {
let bytes = self.heap.string_bytes(subject).ok_or(VmError::Corrupt)?;
for _ in 0..crate::strlib::BYTE_BATCH {
if !bytes.get(*pos as usize).is_some_and(|b| continuation(*b)) {
break;
}
*pos += 1;
}
if *pos as usize >= bytes.len() {
return Ok(Next::Done(Vec::new()));
}
if continuation(bytes[*pos as usize]) {
return Ok(Next::Busy);
}
let Some((code, next)) = decode(bytes, *pos as usize, *strict) else {
return Ok(invalid());
};
if bytes.get(next).is_some_and(|b| continuation(*b)) {
return Ok(invalid());
}
Ok(Next::Done(vec![
Value::Integer(i64::from(*pos) + 1),
Value::Integer(i64::from(code)),
]))
}
Utf8Work::Char { .. } => Err(VmError::Corrupt),
}
}
}