tsuki 0.4.8

Lua 5.4 ported to Rust
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
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
#![allow(
    dead_code,
    mutable_transmutes,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
#![allow(unsafe_op_in_unsafe_fn)]

use crate::lapi::{lua_copy, lua_getfield, lua_load, lua_pushboolean, lua_rawgeti, lua_rawseti};
use crate::lauxlib::{luaL_addgsub, luaL_getsubtable, luaL_gsub};
use crate::lstate::lua_CFunction;
use crate::{
    C2RustUnnamed, Thread, lua_call, lua_createtable, lua_isstring, lua_pushcclosure,
    lua_pushlstring, lua_pushnil, lua_pushstring, lua_pushvalue, lua_rotate, lua_setfield,
    lua_settop, lua_toboolean, lua_tolstring, lua_type, luaL_Buffer, luaL_Reg, luaL_addstring,
    luaL_buffinit, luaL_checklstring, luaL_error, luaL_optlstring, luaL_prepbuffsize,
    luaL_pushresult, luaL_setfuncs,
};
use libc::{fclose, fopen, strchr};
use std::ffi::{CStr, c_int};
use std::fmt::Write;
use std::ptr::{addr_of, null_mut};

unsafe fn setpath(
    mut L: *mut Thread,
    mut fieldname: *const libc::c_char,
    mut dft: *const libc::c_char,
) -> Result<(), Box<dyn std::error::Error>> {
    lua_pushstring(L, dft)?;
    lua_setfield(L, -(2 as libc::c_int), fieldname)
}

unsafe fn readable(mut filename: *const libc::c_char) -> libc::c_int {
    let mut f = fopen(filename, b"r\0" as *const u8 as *const libc::c_char);
    if f.is_null() {
        return 0 as libc::c_int;
    }
    fclose(f);
    return 1 as libc::c_int;
}

unsafe fn getnextfilename(
    mut path: *mut *mut libc::c_char,
    mut end: *mut libc::c_char,
) -> *const libc::c_char {
    let mut sep: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut name: *mut libc::c_char = *path;
    if name == end {
        return 0 as *const libc::c_char;
    } else if *name as libc::c_int == '\0' as i32 {
        *name = *(b";\0" as *const u8 as *const libc::c_char);
        name = name.offset(1);
    }
    sep = strchr(
        name,
        *(b";\0" as *const u8 as *const libc::c_char) as libc::c_int,
    );
    if sep.is_null() {
        sep = end;
    }
    *sep = '\0' as i32 as libc::c_char;
    *path = sep;
    return name;
}

unsafe fn pusherrornotfound(
    mut L: *mut Thread,
    mut path: *const libc::c_char,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut b: luaL_Buffer = luaL_Buffer {
        b: 0 as *mut libc::c_char,
        size: 0,
        n: 0,
        L: 0 as *mut Thread,
        init: C2RustUnnamed { n: 0. },
    };
    luaL_buffinit(L, &mut b);
    luaL_addstring(&mut b, b"no file '\0" as *const u8 as *const libc::c_char)?;
    luaL_addgsub(
        &mut b,
        path,
        b";\0" as *const u8 as *const libc::c_char,
        b"'\n\tno file '\0" as *const u8 as *const libc::c_char,
    )?;
    luaL_addstring(&mut b, b"'\0" as *const u8 as *const libc::c_char)?;
    luaL_pushresult(&mut b)
}

unsafe fn searchpath(
    mut L: *mut Thread,
    mut name: *const libc::c_char,
    mut path: *const libc::c_char,
    mut sep: *const libc::c_char,
    mut dirsep: *const libc::c_char,
) -> Result<*const libc::c_char, Box<dyn std::error::Error>> {
    let mut buff: luaL_Buffer = luaL_Buffer {
        b: 0 as *mut libc::c_char,
        size: 0,
        n: 0,
        L: 0 as *mut Thread,
        init: C2RustUnnamed { n: 0. },
    };
    let mut pathname: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut endpathname: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut filename: *const libc::c_char = 0 as *const libc::c_char;
    if *sep as libc::c_int != '\0' as i32 && !(strchr(name, *sep as libc::c_int)).is_null() {
        name = luaL_gsub(L, name, sep, dirsep)?;
    }
    luaL_buffinit(L, &mut buff);
    luaL_addgsub(
        &mut buff,
        path,
        b"?\0" as *const u8 as *const libc::c_char,
        name,
    )?;
    (buff.n < buff.size || !(luaL_prepbuffsize(&mut buff, 1 as libc::c_int as usize))?.is_null())
        as libc::c_int;
    let fresh2 = buff.n;
    buff.n = (buff.n).wrapping_add(1);
    *(buff.b).offset(fresh2 as isize) = '\0' as i32 as libc::c_char;
    pathname = buff.b;
    endpathname = pathname
        .offset(buff.n as isize)
        .offset(-(1 as libc::c_int as isize));
    loop {
        filename = getnextfilename(&mut pathname, endpathname);
        if filename.is_null() {
            break;
        }
        if readable(filename) != 0 {
            return lua_pushstring(L, filename);
        }
    }
    luaL_pushresult(&mut buff)?;
    pusherrornotfound(L, lua_tolstring(L, -(1 as libc::c_int), 0 as *mut usize)?)?;
    return Ok(0 as *const libc::c_char);
}

unsafe fn ll_searchpath(mut L: *mut Thread) -> Result<libc::c_int, Box<dyn std::error::Error>> {
    let mut f: *const libc::c_char = searchpath(
        L,
        luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize)?,
        luaL_checklstring(L, 2 as libc::c_int, 0 as *mut usize)?,
        luaL_optlstring(
            L,
            3 as libc::c_int,
            b".\0" as *const u8 as *const libc::c_char,
            0 as *mut usize,
        )?,
        luaL_optlstring(
            L,
            4 as libc::c_int,
            b"/\0" as *const u8 as *const libc::c_char,
            0 as *mut usize,
        )?,
    )?;

    if !f.is_null() {
        return Ok(1 as libc::c_int);
    } else {
        lua_pushnil(L);
        lua_rotate(L, -(2 as libc::c_int), 1 as libc::c_int);
        return Ok(2 as libc::c_int);
    };
}

unsafe fn findfile(
    mut L: *mut Thread,
    mut name: *const libc::c_char,
    pname: &str,
    mut dirsep: *const libc::c_char,
) -> Result<*const libc::c_char, Box<dyn std::error::Error>> {
    let mut path: *const libc::c_char = 0 as *const libc::c_char;

    lua_getfield(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int - 1 as libc::c_int,
        pname,
    )?;

    path = lua_tolstring(L, -(1 as libc::c_int), 0 as *mut usize)?;

    if path == 0 as *mut libc::c_void as *const libc::c_char {
        luaL_error(L, format_args!("'package.{pname}' must be a string"))?;
    }

    return searchpath(
        L,
        name,
        path,
        b".\0" as *const u8 as *const libc::c_char,
        dirsep,
    );
}

unsafe fn searcher_Lua(mut L: *mut Thread) -> Result<c_int, Box<dyn std::error::Error>> {
    let name: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize)?;
    let filename = findfile(
        L,
        name,
        "path",
        #[cfg(unix)]
        c"/".as_ptr(),
        #[cfg(windows)]
        c"\\".as_ptr(),
    )?;

    if filename.is_null() {
        return Ok(1 as libc::c_int);
    }

    match lua_load(L, null_mut(), null_mut(), "error('not implemented yet')") {
        Ok(_) => {
            lua_pushstring(L, filename)?;
            return Ok(2 as libc::c_int);
        }
        Err(e) => luaL_error(
            L,
            format_args!(
                "error loading module '{}' from file '{}':\n\t{}",
                CStr::from_ptr(name).to_string_lossy(),
                CStr::from_ptr(filename).to_string_lossy(),
                e
            ),
        ),
    }
}

unsafe fn searcher_preload(L: *mut Thread) -> Result<c_int, Box<dyn std::error::Error>> {
    let name = CStr::from_ptr(luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize)?);

    lua_getfield(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int,
        "_PRELOAD",
    )?;

    if lua_getfield(L, -(1 as libc::c_int), name.to_bytes())? == 0 as libc::c_int {
        lua_pushlstring(
            L,
            format!("no field package.preload['{}']", name.to_string_lossy()),
        )?;
        return Ok(1 as libc::c_int);
    } else {
        lua_pushstring(L, b":preload:\0" as *const u8 as *const libc::c_char)?;
        return Ok(2 as libc::c_int);
    };
}

unsafe fn findloader(mut L: *mut Thread, name: &CStr) -> Result<(), Box<dyn std::error::Error>> {
    if (lua_getfield(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int - 1 as libc::c_int,
        "searchers",
    )? != 5) as libc::c_int as libc::c_long
        != 0
    {
        luaL_error(L, "'package.searchers' must be a table")?;
    }

    let mut msg = String::new();
    let mut i = 1 as libc::c_int;

    loop {
        if ((lua_rawgeti(L, 3 as libc::c_int, i as i64) == 0 as libc::c_int) as libc::c_int
            != 0 as libc::c_int) as libc::c_int as libc::c_long
            != 0
        {
            lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int)?;
            luaL_error(
                L,
                format_args!("module '{}' not found:{}", name.to_string_lossy(), msg),
            )?;
        }

        lua_pushstring(L, name.as_ptr())?;
        lua_call(L, 1 as libc::c_int, 2 as libc::c_int)?;

        if lua_type(L, -(2 as libc::c_int)) == 6 as libc::c_int {
            return Ok(());
        } else if lua_isstring(L, -(2 as libc::c_int)) != 0 {
            let e = CStr::from_ptr(lua_tolstring(L, -2, null_mut())?);

            write!(msg, "\n\t{}", e.to_string_lossy())?;
        }

        lua_settop(L, -(2 as libc::c_int) - 1 as libc::c_int)?;

        i += 1;
    }
}

unsafe fn ll_require(mut L: *mut Thread) -> Result<c_int, Box<dyn std::error::Error>> {
    let name = luaL_checklstring(L, 1, 0 as *mut usize)?;
    let name = CStr::from_ptr(name);

    lua_settop(L, 1)?;
    lua_getfield(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int,
        b"_LOADED",
    )?;
    lua_getfield(L, 2, name.to_bytes())?;

    if lua_toboolean(L, -1) != 0 {
        return Ok(1 as libc::c_int);
    }

    lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int)?;
    findloader(L, name)?;
    lua_rotate(L, -(2 as libc::c_int), 1 as libc::c_int);
    lua_pushvalue(L, 1 as libc::c_int);
    lua_pushvalue(L, -(3 as libc::c_int));
    lua_call(L, 2 as libc::c_int, 1 as libc::c_int)?;
    if !(lua_type(L, -(1 as libc::c_int)) == 0 as libc::c_int) {
        lua_setfield(L, 2 as libc::c_int, name.as_ptr())?;
    } else {
        lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int)?;
    }
    if lua_getfield(L, 2 as libc::c_int, name.to_bytes())? == 0 as libc::c_int {
        lua_pushboolean(L, 1 as libc::c_int);
        lua_copy(L, -(1 as libc::c_int), -(2 as libc::c_int));
        lua_setfield(L, 2 as libc::c_int, name.as_ptr())?;
    }
    lua_rotate(L, -(2 as libc::c_int), 1 as libc::c_int);
    return Ok(2 as libc::c_int);
}

static mut pk_funcs: [luaL_Reg; 6] = [
    {
        let mut init = luaL_Reg {
            name: b"searchpath\0" as *const u8 as *const libc::c_char,
            func: Some(ll_searchpath),
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: b"preload\0" as *const u8 as *const libc::c_char,
            func: None,
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: b"path\0" as *const u8 as *const libc::c_char,
            func: None,
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: b"searchers\0" as *const u8 as *const libc::c_char,
            func: None,
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: b"loaded\0" as *const u8 as *const libc::c_char,
            func: None,
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: 0 as *const libc::c_char,
            func: None,
        };
        init
    },
];

static mut ll_funcs: [luaL_Reg; 2] = [
    {
        let mut init = luaL_Reg {
            name: b"require\0" as *const u8 as *const libc::c_char,
            func: Some(ll_require),
        };
        init
    },
    {
        let mut init = luaL_Reg {
            name: 0 as *const libc::c_char,
            func: None,
        };
        init
    },
];

unsafe fn createsearcherstable(mut L: *mut Thread) -> Result<(), Box<dyn std::error::Error>> {
    static searchers: [lua_CFunction; 2] = [searcher_preload, searcher_Lua];

    lua_createtable(
        L,
        (::core::mem::size_of::<[lua_CFunction; 5]>() as libc::c_ulong)
            .wrapping_div(::core::mem::size_of::<lua_CFunction>() as libc::c_ulong)
            .wrapping_sub(1 as libc::c_int as libc::c_ulong) as libc::c_int,
        0 as libc::c_int,
    )?;

    for (i, s) in searchers.iter().enumerate() {
        lua_pushvalue(L, -(2 as libc::c_int));
        lua_pushcclosure(L, *s, 1 as libc::c_int);
        lua_rawseti(L, -(2 as libc::c_int), (i + 1) as i64)?;
    }

    lua_setfield(
        L,
        -(2 as libc::c_int),
        b"searchers\0" as *const u8 as *const libc::c_char,
    )
}

pub unsafe fn luaopen_package(mut L: *mut Thread) -> Result<c_int, Box<dyn std::error::Error>> {
    lua_createtable(
        L,
        0 as libc::c_int,
        (::core::mem::size_of::<[luaL_Reg; 8]>() as libc::c_ulong)
            .wrapping_div(::core::mem::size_of::<luaL_Reg>() as libc::c_ulong)
            .wrapping_sub(1 as libc::c_int as libc::c_ulong) as libc::c_int,
    )?;
    luaL_setfuncs(L, addr_of!(pk_funcs).cast(), 0 as libc::c_int)?;
    createsearcherstable(L)?;
    setpath(
        L,
        b"path\0" as *const u8 as *const libc::c_char,
        b"./?.lua;./?/init.lua\0" as *const u8 as *const libc::c_char,
    )?;

    if cfg!(windows) {
        lua_pushstring(L, b"\\\n;\n?\n!\n-\n\0" as *const u8 as *const libc::c_char)?;
    } else {
        lua_pushstring(L, b"/\n;\n?\n!\n-\n\0" as *const u8 as *const libc::c_char)?;
    }

    lua_setfield(
        L,
        -(2 as libc::c_int),
        b"config\0" as *const u8 as *const libc::c_char,
    )?;
    luaL_getsubtable(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int,
        b"_LOADED\0" as *const u8 as *const libc::c_char,
    )?;
    lua_setfield(
        L,
        -(2 as libc::c_int),
        b"loaded\0" as *const u8 as *const libc::c_char,
    )?;
    luaL_getsubtable(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int,
        b"_PRELOAD\0" as *const u8 as *const libc::c_char,
    )?;
    lua_setfield(
        L,
        -(2 as libc::c_int),
        b"preload\0" as *const u8 as *const libc::c_char,
    )?;
    lua_rawgeti(
        L,
        -(1000000 as libc::c_int) - 1000 as libc::c_int,
        2 as libc::c_int as i64,
    );
    lua_pushvalue(L, -(2 as libc::c_int));
    luaL_setfuncs(L, addr_of!(ll_funcs).cast(), 1 as libc::c_int)?;
    lua_settop(L, -(1 as libc::c_int) - 1 as libc::c_int)?;

    return Ok(1 as libc::c_int);
}