Skip to content

Commit 480f3a9

Browse files
committed
Migrate lzio
1 parent 2178d25 commit 480f3a9

File tree

18 files changed

+1271
-72
lines changed

18 files changed

+1271
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/.vscode
12
/target

build/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@ fn main() {
3535
.file("build/lundump.c")
3636
.file("build/lutf8lib.c")
3737
.file("build/lvm.c")
38-
.file("build/lzio.c")
3938
.compile("lua");
4039
}

build/lzio.c

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/bin/lua.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ fn main() {
1919
) as i32)
2020
}
2121
}
22+
23+
extern crate lua53_rs;

src/ldo.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use libc::c_int;
2+
3+
pub type jmp_buf = [libc::c_int; 37];
4+
5+
/* chain list of long jump buffers */
6+
#[derive(Copy, Clone)]
7+
#[repr(C)]
8+
pub struct lua_longjmp {
9+
pub previous: *mut lua_longjmp,
10+
pub b: jmp_buf,
11+
pub status: c_int,
12+
}

src/lfunc.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use libc::c_int;
2+
3+
use crate::llimits::lu_mem;
4+
use crate::lobject::TValue;
5+
6+
/*
7+
** Upvalues for Lua closures
8+
*/
9+
#[derive(Copy, Clone)]
10+
#[repr(C)]
11+
pub struct UpVal {
12+
pub v: *mut TValue, /* points to stack or to its own value */
13+
pub refcount: lu_mem, /* reference counter */
14+
pub u: C2RustUnnamed_3,
15+
}
16+
17+
#[derive(Copy, Clone)]
18+
#[repr(C)]
19+
pub union C2RustUnnamed_3 {
20+
pub open: C2RustUnnamed_4, /* (when open) */
21+
pub value: TValue, /* the value (when closed) */
22+
}
23+
24+
#[derive(Copy, Clone)]
25+
#[repr(C)]
26+
pub struct C2RustUnnamed_4 {
27+
pub next: *mut UpVal, /* linked list */
28+
pub touched: c_int, /* mark to avoid cycles with dead threads */
29+
}

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![allow(non_camel_case_types, non_snake_case, dead_code)]
2+
3+
pub(crate) mod ldo;
4+
pub(crate) mod lfunc;
5+
pub(crate) mod llimits;
6+
pub(crate) mod lobject;
7+
pub(crate) mod lstate;
8+
pub(crate) mod ltemp;
9+
pub(crate) mod ltm;
10+
pub(crate) mod lzio;
11+
pub(crate) mod types;

src/llimits.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use libc::{c_int, c_uchar, c_uint, ptrdiff_t, size_t};
2+
3+
pub type lu_byte = c_uchar;
4+
pub type l_mem = ptrdiff_t;
5+
pub type lu_mem = size_t;
6+
pub type sig_atomic_t = c_int;
7+
8+
/*
9+
** type for virtual-machine instructions;
10+
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
11+
*/
12+
pub type Instruction = c_uint;
13+
14+
/*
15+
** Size of cache for strings in the API. 'N' is the number of
16+
** sets (better be a prime) and "M" is the size of each set (M == 1
17+
** makes a direct cache.)
18+
*/
19+
pub const STRCACHE_N: usize = 53;
20+
pub const STRCACHE_M: usize = 2;
21+
22+
/*
23+
@@ LUA_IDSIZE gives the maximum size for the description of the source
24+
@@ of a function in debug information.
25+
** CHANGE it if you want a different size.
26+
*/
27+
pub const LUA_IDSIZE: usize = 60;

src/lmem.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
** About the realloc function:
3+
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
4+
** ('osize' is the old size, 'nsize' is the new size)
5+
**
6+
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
7+
** matter 'x').
8+
**
9+
** * frealloc(ud, p, x, 0) frees the block 'p'
10+
** (in this specific case, frealloc must return NULL);
11+
** particularly, frealloc(ud, NULL, 0, 0) does nothing
12+
** (which is equivalent to free(NULL) in ISO C)
13+
**
14+
** frealloc returns NULL if it cannot create or reallocate the area
15+
** (any reallocation to an equal or smaller size cannot fail!)
16+
*/
17+
18+
pub const MINSIZEARRAY: libc::c_int = 4 as libc::c_int;
19+
20+
pub unsafe fn luaM_growaux_(
21+
mut L: *mut lua_State,
22+
mut block: *mut libc::c_void,
23+
mut size: *mut libc::c_int,
24+
mut size_elems: size_t,
25+
mut limit: libc::c_int,
26+
mut what: *const libc::c_char,
27+
) -> *mut libc::c_void {
28+
let mut newblock = 0 as *mut libc::c_void;
29+
let mut newsize: libc::c_int = 0;
30+
if *size >= limit / 2 as libc::c_int {
31+
/* cannot double it? */
32+
if *size >= limit {
33+
/* cannot grow even a little? */
34+
luaG_runerror(
35+
L,
36+
b"too many %s (limit is %d)\0" as *const u8 as *const libc::c_char,
37+
what,
38+
limit,
39+
);
40+
}
41+
newsize = limit;
42+
} else {
43+
newsize = *size * 2 as libc::c_int;
44+
if newsize < MINSIZEARRAY {
45+
/* minimum size */
46+
newsize = MINSIZEARRAY;
47+
}
48+
}
49+
newblock = luaM_realloc_(
50+
L,
51+
block,
52+
(*size as libc::c_ulong).checked_mul(size_elems),
53+
(newsize as libc::c_ulong).checked_mul(size_elems),
54+
);
55+
/* update only when everything else is OK */
56+
*size = newsize;
57+
return newblock;
58+
}
59+
60+
/*
61+
** generic allocation routine.
62+
*/
63+
64+
pub unsafe fn luaM_realloc_(
65+
mut L: *mut lua_State,
66+
mut block: *mut libc::c_void,
67+
mut osize: size_t,
68+
mut nsize: size_t,
69+
) -> *mut libc::c_void {
70+
let mut newblock = 0 as *mut libc::c_void;
71+
let mut g = (*L).l_G;
72+
let mut realosize = if !block.is_null() {
73+
osize
74+
} else {
75+
0 as libc::c_int as libc::c_ulong
76+
};
77+
// TODO: HARDMEMTESTS
78+
newblock = (((*g).frealloc).expect("non-null function pointer"))((*g).ud, block, osize, nsize);
79+
if newblock.is_null() && nsize > 0 as libc::c_int as libc::c_ulong {
80+
/* cannot fail when shrinking a block */
81+
debug_assert!(nsize > realosize);
82+
if !((*g).version).is_null() {
83+
/* is state fully built? */
84+
/* try to free some memory... */
85+
luaC_fullgc(L, 1 as libc::c_int);
86+
/* try again */
87+
newblock =
88+
(((*g).frealloc).expect("non-null function pointer"))((*g).ud, block, osize, nsize);
89+
}
90+
if newblock.is_null() {
91+
luaD_throw(L, LUA_ERRMEM);
92+
}
93+
}
94+
debug_assert!((nsize == 0) == newblock.is_null());
95+
(*g).GCdebt = ((*g).GCdebt as libc::c_ulong)
96+
.wrapping_add(nsize)
97+
.wrapping_sub(realosize) as l_mem;
98+
return newblock;
99+
}

src/lobject.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
use libc::{c_int, c_uint, c_void, size_t};
2+
3+
use crate::llimits::lu_byte;
4+
use crate::types::{lua_CFunction, lua_Integer, lua_Number};
5+
6+
/*
7+
** Common type has only the common header
8+
*/
9+
#[derive(Copy, Clone)]
10+
#[repr(C)]
11+
pub struct GCObject {
12+
pub next: *mut GCObject,
13+
pub tt: lu_byte,
14+
pub marked: lu_byte,
15+
}
16+
17+
/*
18+
** Tagged Values. This is the basic representation of values in Lua,
19+
** an actual value plus a tag with its type.
20+
*/
21+
22+
/*
23+
** Union of all Lua values
24+
*/
25+
#[derive(Copy, Clone)]
26+
#[repr(C)]
27+
pub union Value {
28+
pub gc: *mut GCObject, /* collectable objects */
29+
pub p: *mut c_void, /* light userdata */
30+
pub b: c_int, /* booleans */
31+
pub f: lua_CFunction, /* light C functions */
32+
pub i: lua_Integer, /* integer numbers */
33+
pub n: lua_Number, /* float numbers */
34+
}
35+
36+
pub type TValue = lua_TValue;
37+
38+
#[derive(Copy, Clone)]
39+
#[repr(C)]
40+
pub struct lua_TValue {
41+
pub value_: Value,
42+
pub tt_: c_int,
43+
}
44+
45+
/*
46+
** ======================================================
47+
** types and prototypes
48+
** =======================================================
49+
*/
50+
51+
pub type StkId = *mut TValue; /* index to stack elements */
52+
53+
/*
54+
** Header for string value; string bytes follow the end of this structure
55+
** (aligned according to 'UTString'; see next).
56+
*/
57+
#[derive(Copy, Clone)]
58+
#[repr(C)]
59+
pub struct TString {
60+
pub next: *mut GCObject,
61+
pub tt: lu_byte,
62+
pub marked: lu_byte,
63+
pub extra: lu_byte, /* reserved words for short strings; "has hash" for longs */
64+
pub shrlen: lu_byte, /* length for short strings */
65+
pub hash: c_uint,
66+
pub u: C2RustUnnamed_5,
67+
}
68+
69+
#[derive(Copy, Clone)]
70+
#[repr(C)]
71+
pub union C2RustUnnamed_5 {
72+
pub lnglen: size_t, /* length for long strings */
73+
pub hnext: *mut TString, /* linked list for hash table */
74+
}
75+
76+
/*
77+
** Tables
78+
*/
79+
80+
#[derive(Copy, Clone)]
81+
#[repr(C)]
82+
pub union TKey {
83+
pub nk: C2RustUnnamed_6,
84+
pub tvk: TValue,
85+
}
86+
87+
#[derive(Copy, Clone)]
88+
#[repr(C)]
89+
pub struct C2RustUnnamed_6 {
90+
pub value_: Value,
91+
pub tt_: c_int,
92+
pub next: c_int, /* for chaining (offset for next node) */
93+
}
94+
95+
#[derive(Copy, Clone)]
96+
#[repr(C)]
97+
pub struct Node {
98+
pub i_val: TValue,
99+
pub i_key: TKey,
100+
}
101+
102+
#[derive(Copy, Clone)]
103+
#[repr(C)]
104+
pub struct Table {
105+
pub next: *mut GCObject,
106+
pub tt: lu_byte,
107+
pub marked: lu_byte,
108+
pub flags: lu_byte, /* 1<<p means tagmethod(p) is not present */
109+
pub lsizenode: lu_byte, /* log2 of size of 'node' array */
110+
pub sizearray: c_uint, /* size of 'array' array */
111+
pub array: *mut TValue, /* array part */
112+
pub node: *mut Node,
113+
pub lastfree: *mut Node, /* any free position is before this position */
114+
pub metatable: *mut Table,
115+
pub gclist: *mut GCObject,
116+
}

0 commit comments

Comments
 (0)