@@ -15,9 +15,9 @@ use crate::lfunc::{luaF_newCclosure, UpVal};
15
15
use crate :: lgc:: { luaC_barrier_, luaC_barrierback_, luaC_checkGC, luaC_fullgc, luaC_upvalbarrier_} ;
16
16
use crate :: llimits:: { l_mem, lu_byte, lu_mem} ;
17
17
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 ,
21
21
} ;
22
22
use crate :: lstate:: { global_State, luaE_setdebt, lua_State, CallInfo , GCUnion } ;
23
23
use crate :: lstring:: { luaS_new, luaS_newlstr, luaS_newudata} ;
@@ -31,6 +31,7 @@ use crate::lzio::{luaZ_init, ZIO};
31
31
use crate :: types:: {
32
32
lua_Alloc, lua_CFunction, lua_Integer, lua_KContext, lua_KFunction, lua_Number, lua_Reader,
33
33
lua_Writer, LUA_MULTRET , LUA_REGISTRYINDEX , LUA_RIDX_GLOBALS , LUA_TFUNCTION , LUA_TNIL ,
34
+ LUA_VERSION_NUM ,
34
35
} ;
35
36
36
37
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
100
101
lua_pcallk ( L , n, r, f, 0 , None )
101
102
}
102
103
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
+ }
104
139
105
140
unsafe extern "C" fn growstack ( L : * mut lua_State , ud : * mut c_void ) {
106
141
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) -
159
194
return old;
160
195
}
161
196
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
+ }
163
206
164
207
/*
165
208
** basic stack manipulation
@@ -1825,7 +1868,6 @@ pub unsafe extern "C" fn lua_upvaluejoin(
1825
1868
}
1826
1869
1827
1870
extern "C" {
1828
- pub fn lua_version ( L : * mut lua_State ) -> * const lua_Number ;
1829
1871
pub fn luaC_step ( L : * mut lua_State ) ;
1830
1872
pub fn luaV_lessthan ( L : * mut lua_State , l : * const TValue , r : * const TValue ) -> c_int ;
1831
1873
pub fn luaV_lessequal ( L : * mut lua_State , l : * const TValue , r : * const TValue ) -> c_int ;
@@ -1844,7 +1886,6 @@ extern "C" {
1844
1886
val : StkId ,
1845
1887
slot : * const TValue ,
1846
1888
) ;
1847
- fn index2addr ( L : * mut lua_State , idx : c_int ) -> * mut TValue ;
1848
1889
pub fn luaC_upvdeccount ( L : * mut lua_State , uv : * mut UpVal ) ;
1849
1890
pub fn luaV_objlen ( L : * mut lua_State , ra : StkId , rb : * const TValue ) ;
1850
1891
}
0 commit comments