luallaby 0.1.0

**Work in progress** A pure-Rust Lua interpreter/compiler
Documentation
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
use std::cell::RefCell;
use std::io::{ErrorKind, Read, Seek, SeekFrom, Write};
use std::os::unix::process::ExitStatusExt;
use std::rc::Rc;

use super::Module;
use crate::error::Result;
use crate::value::{
    from_str_radix_wrapping, parse_f64_hex, FileBufMode, FileDesc, FileHandle, FuncBuiltin,
    Numeric, Table, UserData, ValueType,
};
use crate::{Error, LuaError, Value, VM};

pub(super) fn create(vm: &mut VM, file: FileDesc) -> Result<Rc<RefCell<FileHandle>>> {
    _create(file, vm.alloc_table(), vm.alloc_table())
}

pub(super) fn _create(
    file: FileDesc,
    meta: Rc<RefCell<Table>>,
    index: Rc<RefCell<Table>>,
) -> Result<Rc<RefCell<FileHandle>>> {
    Module {
        name: "",
        tbl: meta.clone(),
    }
    .func("__close", __close)?
    .func("__gc", __close)?
    .cons(
        "__index",
        Value::Table(
            Module {
                name: "",
                tbl: index,
            }
            .func("close", close)?
            .func("flush", flush)?
            .func("lines", lines)?
            .func("read", read)?
            .func("seek", seek)?
            .func("setvbuf", setvbuf)?
            .func("write", write)?
            .tbl
            .clone(),
        ),
    )?
    .cons("__name", Value::str("FILE*"))?
    .func("__tostring", __tostring)?;

    Ok(Rc::new(RefCell::new(FileHandle::new(file, Some(meta)))))
}

fn close(vm: &mut VM) -> Result<Value> {
    let file = vm
        .arg(0)
        .map_err(|_| Error::from_lua(LuaError::ArgumentNoValue))?
        .to_file()?;
    Ok(_close_helper(file))
}

fn __close(vm: &mut VM) -> Result<Value> {
    // Unwrap manually, to_file() throws error
    Ok(
        if let Value::UserData(UserData::File(file)) = vm
            .arg(0)
            .map_err(|_| Error::from_lua(LuaError::ArgumentNoValue))?
        {
            _close_helper(file)
        } else {
            Value::Bool(true)
        },
    )
}

pub(super) fn _close_helper(file: Rc<RefCell<FileHandle>>) -> Value {
    let mut file = file.borrow_mut();
    let file = file.desc_mut();

    match file {
        FileDesc::File(Some(_), mode) => {
            *file = FileDesc::File(None, *mode);
            Value::Bool(true)
        }
        FileDesc::Child(Some(child), mode) => {
            let res = match child.wait() {
                Ok(exit) => {
                    let success = if exit.success() {
                        Value::Bool(true)
                    } else {
                        Value::Nil
                    };
                    let (why, code) = match exit.code() {
                        Some(code) => ("exit", code as i64),
                        None => ("signal", exit.signal().unwrap_or_default() as i64),
                    };
                    Value::Mult(vec![success, Value::str(why), Value::int(code)])
                }
                Err(e) => Value::Mult(vec![Value::Bool(false), e.into()]),
            };
            *file = FileDesc::Child(None, *mode);
            res
        }
        FileDesc::File(None, _) | FileDesc::Child(None, _) => {
            Value::Mult(vec![Value::Nil, Value::str("file already closed")])
        }
        FileDesc::Buffer(..) | FileDesc::StdIn | FileDesc::StdOut | FileDesc::StdErr => {
            Value::Mult(vec![Value::Nil, Value::str("cannot close standard file")])
        }
    }
}

fn flush(vm: &mut VM) -> Result<Value> {
    let file = vm
        .arg(0)
        .map_err(|_| Error::from_lua(LuaError::ArgumentNoValue))?
        .to_file()?;
    Ok(_flush_helper(file))
}

pub(super) fn _flush_helper(file: Rc<RefCell<FileHandle>>) -> Value {
    let mut file = file.borrow_mut();
    match file.flush() {
        Ok(_) => Value::Bool(true),
        Err(e) => e.into(),
    }
}

fn lines(vm: &mut VM) -> Result<Value> {
    let file = vm.arg_file(0)?;
    let args = vm.arg_split(1);
    if args.len() > 250 {
        return err!(LuaError::ArgumentTooMany);
    }

    Ok(Value::Func(vm.alloc_builtin(FuncBuiltin {
        module: "",
        name: "__lines",
        func: Rc::new(move |_| {
            _read_helper(file.clone(), args.clone()).and_then(|res| match &res {
                Value::Mult(vec) => {
                    if vec.first() == Some(&Value::Nil) {
                        if let Some(Value::String(buf)) = vec.get(1) {
                            err!(LuaError::CustomString(
                                String::from_utf8_lossy(&buf).into_owned()
                            ))
                        } else {
                            Ok(res)
                        }
                    } else {
                        Ok(res)
                    }
                }
                _ => Ok(res),
            })
        }),
    })))
}

fn read(vm: &mut VM) -> Result<Value> {
    _read_helper(vm.arg_file(0)?, vm.arg_split(1))
}

pub(super) fn _read_helper(file: Rc<RefCell<FileHandle>>, mut args: Vec<Value>) -> Result<Value> {
    let mut file = file.borrow_mut();

    if args.is_empty() {
        args.push(Value::str("l"));
    } else if args.len() > 250 {
        return err!(LuaError::ArgumentTooMany);
    }

    let mut out = Vec::new();
    for arg in args {
        let value = match arg {
            Value::Number(n) => {
                let n = n.coerce_int()? as usize;
                // Read at least one byte, when reading zero bytes this
                // should still return fail
                let mut buf = vec![0; n.max(1)];
                let read = loop {
                    match file.read(&mut buf) {
                        Ok(read) => break read,
                        Err(ref e) if matches!(e.kind(), ErrorKind::Interrupted) => {}
                        Err(e) => return Ok(e.into()),
                    }
                };
                if read == 0 {
                    break;
                } else if n == 0 {
                    // In the case of zero byte read, revert by bytes read,
                    // no need to roll back on error or EOF
                    file.seek(SeekFrom::Current(-(read as i64)))?;
                    buf.clear();
                }
                buf.truncate(read);
                Value::str_bytes(buf)
            }
            // Lua only reads first character of strings
            Value::String(s) => match if s.get(0) == Some(&b'*') {
                &s[1] // old format, skip
            } else {
                &s[0]
            } {
                b'n' => {
                    let num = match __read_numeric(&mut file) {
                        Ok(Some(num)) => num,
                        Ok(None) => break,
                        Err(e) => {
                            if let ErrorKind::UnexpectedEof = e.kind() {
                                break;
                            } else {
                                return Ok(e.into());
                            }
                        }
                    };
                    Value::Number(num)
                }
                b'a' => {
                    let mut buf = Vec::new();
                    if let Err(e) = file.read_to_end(&mut buf) {
                        return Ok(e.into());
                    }
                    Value::str_bytes(buf)
                }
                c if matches!(c, b'l' | b'L') => {
                    let line = match __read_line(&mut file, c.is_ascii_uppercase()) {
                        Ok(line) => line,
                        Err(e) => {
                            if let ErrorKind::UnexpectedEof = e.kind() {
                                break;
                            } else {
                                return Ok(e.into());
                            }
                        }
                    };
                    Value::str_bytes(line)
                }
                _ => return err!(LuaError::FileRead),
            },
            v => return err!(LuaError::ExpectedType(ValueType::String, v.value_type())),
        };
        out.push(value);
    }

    Ok(Value::Mult(out))
}

fn __read_numeric(file: &mut FileHandle) -> std::io::Result<Option<Numeric>> {
    let mut hex = false;
    let mut int = true;
    let mut exp = false;
    let mut digits = vec![];
    let mut buf = vec![0];

    // Consume leading whitespace
    loop {
        file.read_exact(&mut buf)?;
        if !buf[0].is_ascii_whitespace() {
            break;
        }
    }

    loop {
        if digits.len() > 200 {
            return Ok(None);
        }
        match &buf[0] {
            b'+' | b'-'
                if digits.is_empty()
                    || matches!(digits.last(), Some(b'e' | b'E' | b'p' | b'P')) => {}
            b'a'..=b'f' | b'A'..=b'F' if hex && !exp => {}
            b'0'..=b'9' => {}
            b'.' if int => {
                int = false;
            }
            b'x' | b'X' if !hex && digits.iter().all(|c| matches!(c, b'+' | b'-' | b'0')) => {
                hex = true;
            }
            b'e' | b'E' if !exp && !hex && digits.iter().any(u8::is_ascii_digit) => {
                int = false;
                exp = true;
            }
            b'p' | b'P' if !exp && hex && digits.iter().any(u8::is_ascii_hexdigit) => {
                int = false;
                exp = true;
            }
            _ => break,
        }
        digits.push(buf[0]);
        file.read_exact(&mut buf)?;
    }
    file.seek(SeekFrom::Current(-1))?;

    let digits: String = digits.into_iter().map(char::from).collect();
    Ok(if digits.is_empty() {
        None
    } else if int {
        if hex {
            match from_str_radix_wrapping(digits.as_bytes(), 16) {
                Some(int) => Some(Numeric::Integer(int)),
                None => parse_f64_hex(digits.as_bytes()).map(Numeric::Float),
            }
        } else {
            match digits.parse::<i64>() {
                Ok(int) => Some(Numeric::Integer(int as i64)),
                Err(..) => digits.parse().ok().map(Numeric::Float),
            }
        }
    } else {
        if hex {
            parse_f64_hex(digits.as_bytes()).map(Numeric::Float)
        } else {
            digits.parse().ok().map(Numeric::Float)
        }
    })
}

fn __read_line(file: &mut FileHandle, keep: bool) -> std::io::Result<Vec<u8>> {
    let mut buf = vec![0];
    let mut line = Vec::new();

    loop {
        match file.read_exact(&mut buf) {
            Ok(_) => {
                if buf[0] == b'\n' {
                    if keep {
                        line.push(b'\n');
                    }
                    break;
                }
            }
            Err(e) if !line.is_empty() && e.kind() == ErrorKind::UnexpectedEof => break,
            Err(e) => return Err(e),
        }
        line.push(buf[0]);
    }
    Ok(line)
}

fn seek(vm: &mut VM) -> Result<Value> {
    let file = vm.arg_file(0)?;
    let mut file = file.borrow_mut();
    let whence = match vm.arg_opt(1) {
        Some(val) => String::from_utf8_lossy(val.to_string()?).into_owned(),
        None => "cur".to_string(),
    };
    let offset = match vm.arg_opt(2) {
        Some(val) => val.to_int_coerce()?,
        None => 0,
    };

    let seek = match whence.as_str() {
        "set" => {
            if offset < 0 {
                return Ok(Value::Mult(vec![
                    Value::Nil,
                    Value::str("illegal seek"),
                    Value::int(1),
                ]));
            } else {
                SeekFrom::Start(offset as u64)
            }
        }
        "cur" => SeekFrom::Current(offset),
        "end" => SeekFrom::End(offset),
        _ => return err!(LuaError::FileSeek(whence)),
    };

    Ok(match file.seek(seek) {
        Ok(pos) => Value::int(pos as i64),
        Err(e) => e.into(),
    })
}

fn setvbuf(vm: &mut VM) -> Result<Value> {
    let file = vm.arg_file(0)?;
    let mode = vm.arg_string_coerce(1)?;
    let size = match vm.arg_opt(2) {
        Some(val) => Some(val.to_int_coerce()? as usize),
        None => None,
    };
    let mode = match String::from_utf8_lossy(&mode).as_ref() {
        "no" => FileBufMode::None,
        "full" => FileBufMode::Full(size),
        "line" => FileBufMode::Line(size),
        str => return err!(LuaError::FileBufOpt(str.to_string())),
    };

    let mut file = file.borrow_mut();
    Ok(Value::Bool(file.set_buffering(mode)))
}

fn __tostring(vm: &mut VM) -> Result<Value> {
    vm.arg(0)?.write_as_value()
}

fn write(vm: &mut VM) -> Result<Value> {
    let strs = (1..vm.arg_len())
        .map(|idx| vm.arg_string_coerce(idx))
        .collect::<Result<Vec<_>>>()?;
    Ok(_write_helper(vm.arg_file(0)?, strs))
}

pub(super) fn _write_helper(file: Rc<RefCell<FileHandle>>, args: Vec<Vec<u8>>) -> Value {
    {
        let mut file = file.borrow_mut();
        for arg in args {
            if let Err(e) = file.write_all(&arg) {
                return e.into();
            }
        }
    }
    Value::UserData(UserData::File(file))
}