Skip to content

Commit b6c5bbb

Browse files
authored
Remove lapi (#6)
* Move remaining functions * Remove lapi.c * cargo fmt
1 parent 1842431 commit b6c5bbb

File tree

3 files changed

+48
-99
lines changed

3 files changed

+48
-99
lines changed

build/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ fn main() {
44
.define("LUA_USE_CTYPE", None)
55
.define("LUA_USE_POSIX", None)
66
// Lua core
7-
.file("build/lapi.c")
87
.file("build/lauxlib.c")
98
.file("build/lcode.c")
109
.file("build/lgc.c")

build/lapi.c

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

src/lapi.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use crate::lfunc::{luaF_newCclosure, UpVal};
1515
use crate::lgc::{luaC_barrier_, luaC_barrierback_, luaC_checkGC, luaC_fullgc, luaC_upvalbarrier_};
1616
use crate::llimits::{l_mem, lu_byte, lu_mem};
1717
use crate::lobject::{
18-
luaO_arith, luaO_nilobject_, luaO_pushvfstring, luaO_str2num, luaO_tostring, setivalue,
19-
setnilvalue, setobj, CClosure, GCObject, LClosure, Proto, StkId, TString, TValue, Table,
20-
UTString, UUdata, Udata, Value,
18+
clCvalue, luaO_arith, luaO_nilobject_, luaO_pushvfstring, luaO_str2num, luaO_tostring,
19+
setivalue, setnilvalue, setobj, ttislcf, CClosure, GCObject, LClosure, Proto, StkId, TString,
20+
TValue, Table, UTString, UUdata, Udata, Value,
2121
};
2222
use crate::lstate::{global_State, luaE_setdebt, lua_State, CallInfo, GCUnion};
2323
use crate::lstring::{luaS_new, luaS_newlstr, luaS_newudata};
@@ -31,6 +31,7 @@ use crate::lzio::{luaZ_init, ZIO};
3131
use crate::types::{
3232
lua_Alloc, lua_CFunction, lua_Integer, lua_KContext, lua_KFunction, lua_Number, lua_Reader,
3333
lua_Writer, LUA_MULTRET, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS, LUA_TFUNCTION, LUA_TNIL,
34+
LUA_VERSION_NUM,
3435
};
3536

3637
pub(crate) unsafe fn api_incr_top(L: *mut lua_State) {
@@ -100,7 +101,41 @@ pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_in
100101
lua_pcallk(L, n, r, f, 0, None)
101102
}
102103

103-
// static TValue *index2addr (lua_State *L, int idx) {
104+
/* test for pseudo index */
105+
#[inline(always)]
106+
fn ispseudo(i: c_int) -> bool {
107+
return i <= LUA_REGISTRYINDEX;
108+
}
109+
110+
unsafe extern "C" fn index2addr(L: *mut lua_State, mut idx: libc::c_int) -> *mut TValue {
111+
let ci = (*L).ci;
112+
if idx > 0 as libc::c_int {
113+
let o = ((*ci).func).offset(idx as isize);
114+
if o >= (*L).top {
115+
return &luaO_nilobject_ as *const TValue as *mut TValue;
116+
} else {
117+
return o;
118+
}
119+
} else if !ispseudo(idx) {
120+
return ((*L).top).offset(idx as isize);
121+
} else if idx == LUA_REGISTRYINDEX {
122+
return &mut (*(*L).l_G).l_registry;
123+
} else {
124+
idx = LUA_REGISTRYINDEX - idx;
125+
if ttislcf((*ci).func) {
126+
return &luaO_nilobject_ as *const TValue as *mut TValue;
127+
} else {
128+
let func: *mut CClosure = clCvalue((*ci).func);
129+
return if idx <= (*func).nupvalues as libc::c_int {
130+
&mut *((*func).upvalue)
131+
.as_mut_ptr()
132+
.offset((idx - 1 as libc::c_int) as isize) as *mut TValue
133+
} else {
134+
&luaO_nilobject_ as *const TValue as *mut TValue
135+
};
136+
}
137+
};
138+
}
104139

105140
unsafe extern "C" fn growstack(L: *mut lua_State, ud: *mut c_void) {
106141
let size: c_int = *(ud as *mut c_int);
@@ -159,7 +194,15 @@ pub unsafe extern "C" fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -
159194
return old;
160195
}
161196

162-
// LUA_API const lua_Number *lua_version (lua_State *L) {
197+
#[no_mangle]
198+
pub unsafe extern "C" fn lua_version(L: *mut lua_State) -> *const lua_Number {
199+
static mut version: lua_Number = LUA_VERSION_NUM as lua_Number;
200+
if L.is_null() {
201+
return &version;
202+
} else {
203+
return (*(*L).l_G).version;
204+
};
205+
}
163206

164207
/*
165208
** basic stack manipulation
@@ -1825,7 +1868,6 @@ pub unsafe extern "C" fn lua_upvaluejoin(
18251868
}
18261869

18271870
extern "C" {
1828-
pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
18291871
pub fn luaC_step(L: *mut lua_State);
18301872
pub fn luaV_lessthan(L: *mut lua_State, l: *const TValue, r: *const TValue) -> c_int;
18311873
pub fn luaV_lessequal(L: *mut lua_State, l: *const TValue, r: *const TValue) -> c_int;
@@ -1844,7 +1886,6 @@ extern "C" {
18441886
val: StkId,
18451887
slot: *const TValue,
18461888
);
1847-
fn index2addr(L: *mut lua_State, idx: c_int) -> *mut TValue;
18481889
pub fn luaC_upvdeccount(L: *mut lua_State, uv: *mut UpVal);
18491890
pub fn luaV_objlen(L: *mut lua_State, ra: StkId, rb: *const TValue);
18501891
}

0 commit comments

Comments
 (0)