|
| 1 | +# Lua 5.3 in Rust - HACK-3870 |
| 2 | + |
| 3 | +# How to get started |
| 4 | + |
| 5 | + * Install rust (needs to be a nightly) |
| 6 | + * curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 7 | + * rustup default nightly |
| 8 | + * `cargo b` to build |
| 9 | + * `cargo test` to run the tests |
| 10 | + |
| 11 | +# Working on stuff |
| 12 | + |
| 13 | + * Send Alex (aorlenko/khvzak) your github username to be added to the repo to push branches (so you don't have to work from a fork). |
| 14 | + * Please don't push to master unless the tests pass! |
| 15 | + * Not everything needs porting. E.g. rust supplies printf already, so you don't need to port this |
| 16 | + * Note `cstr` from `macros.rs` lua uses C strings (0 byte terminated) rust doesn't, this macro helps with that. |
| 17 | + |
| 18 | +# Project tracking |
| 19 | + |
| 20 | + https://docs.google.com/document/d/1NmWf19Xf47-Y99b6yKMzoyr2vY6hWXaxNHi1I4nL_vA/ |
| 21 | + |
| 22 | + |
| 23 | +# Examples |
| 24 | + |
| 25 | +## Tests |
| 26 | + |
| 27 | +https://www.lua.org/tests/ |
| 28 | + |
| 29 | +Start at `tests/tests.rs` |
| 30 | + |
| 31 | +This sets up a lua environment and then runs the lua 5.3 tests. |
| 32 | + |
| 33 | +# C to rust transpiler |
| 34 | + |
| 35 | +https://github.com/immunant/c2rust |
| 36 | + |
| 37 | +See `onelua.rs` - lua C code transpiled, could be helpful, but kinda hell code |
| 38 | + |
| 39 | +Lots of C preprocessor + macros |
| 40 | + |
| 41 | +Does not include the lua standard libraries at the moment |
| 42 | + |
| 43 | +## How to port files |
| 44 | + |
| 45 | +See `build/` and specifically `build/build.rs` - this includes the mapping of all C files built and what rust code to link them with. |
| 46 | + |
| 47 | +Once things are ported, the `.c` files can be removed (but the `.h` files cannot). Any `.c` code in `build/` is still to be moved and should be deleted once moved. |
| 48 | + |
| 49 | +## Using rust code from C |
| 50 | + |
| 51 | +Search for anything with `[no_mangle]`. |
| 52 | + |
| 53 | +E.g. from lstate.rs |
| 54 | + |
| 55 | + /* |
| 56 | + ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
| 57 | + ** invariant (and avoiding underflows in 'totalbytes') |
| 58 | + */ |
| 59 | + #[no_mangle] |
| 60 | + pub unsafe extern "C" fn luaE_setdebt(g: *mut global_State, debt: l_mem) { |
| 61 | + let tb = gettotalbytes(g); |
| 62 | + debug_assert!(tb > 0); |
| 63 | + (*g).totalbytes = tb as isize - debt; |
| 64 | + (*g).GCdebt = debt; |
| 65 | + } |
| 66 | + |
| 67 | +## Using C code from rust |
| 68 | + |
| 69 | +Lua C API https://www.lua.org/pil/24.html |
| 70 | + |
| 71 | +See in lgc.rs |
| 72 | + |
| 73 | + extern "C" { |
| 74 | + pub fn luaC_upvalbarrier_(L: *mut lua_State, uv: *mut UpVal); |
| 75 | + pub fn luaC_barrierback_(L: *mut lua_State, t: *mut Table); |
| 76 | + pub fn luaC_fix(L: *mut lua_State, o: *mut GCObject); |
| 77 | + pub fn luaC_newobj(L: *mut lua_State, tt: c_int, sz: size_t) -> *mut GCObject; |
| 78 | + pub fn luaC_step(L: *mut lua_State); |
| 79 | + pub fn luaC_freeallobjects(L: *mut lua_State); |
| 80 | + pub fn luaC_fullgc(L: *mut lua_State, isemergency: c_int); |
| 81 | + pub fn luaC_barrier_(L: *mut lua_State, o: *mut GCObject, v: *mut GCObject); |
| 82 | + } |
| 83 | + |
| 84 | +## Refactoring |
| 85 | + |
| 86 | +Take some of the ready made `.rs` rust code and make more idiomatic. |
| 87 | + |
| 88 | + * Remove raw pointers |
| 89 | + * Use methods |
| 90 | + * Stop calculating hashes and use rust native |
| 91 | + * Use primitives like vectors |
| 92 | + * Remove libc |
| 93 | + |
| 94 | +## Exceptions |
| 95 | + |
| 96 | +Lua uses exceptions a lot (both in lua + in the C api). C by it's nature doesn't have exceptions, so the C api uses `setjmp` and `longjmp`. |
| 97 | + |
| 98 | +Replace with Rust native implementation of exceptions. |
| 99 | + |
| 100 | +## Wrapping |
| 101 | + |
| 102 | +Lua uses integer number overflow and wraparound explcitily. In rust this will cause an exception. |
| 103 | + |
0 commit comments