@@ -9,64 +9,82 @@ extern "C" {
9
9
pub fn lua_pcall ( state : * mut c_void , nargs : c_int , nresults : c_int , errfunc : c_int ) -> c_int ;
10
10
}
11
11
12
- pub unsafe fn lua_getglobal ( state : * mut c_void , k : * const c_char ) {
13
- lua_getfield ( state, -10002 /* LUA_GLOBALSINDEX */ , k) ;
14
- }
15
-
16
- #[ test]
17
- fn test_lua ( ) {
12
+ #[ cfg( test) ]
13
+ mod tests {
18
14
use std:: { ptr, slice} ;
19
- unsafe {
20
- let state = luaL_newstate ( ) ;
21
- assert ! ( state != ptr:: null_mut( ) ) ;
22
15
23
- luaL_openlibs ( state ) ;
16
+ use super :: * ;
24
17
25
- let version = {
26
- lua_getglobal ( state, "_VERSION\0 " . as_ptr ( ) . cast ( ) ) ;
27
- let mut len: c_long = 0 ;
28
- let version_ptr = lua_tolstring ( state, -1 , & mut len) ;
29
- slice:: from_raw_parts ( version_ptr as * const u8 , len as usize )
30
- } ;
18
+ pub unsafe fn lua_getglobal ( state : * mut c_void , k : * const c_char ) {
19
+ lua_getfield ( state, -10002 /* LUA_GLOBALSINDEX */ , k) ;
20
+ }
31
21
32
- assert_eq ! ( version, b"Lua 5.1" ) ;
22
+ pub unsafe fn to_string < ' a > ( state : * mut c_void , index : c_int ) -> & ' a str {
23
+ let mut len: c_long = 0 ;
24
+ let str_ptr = lua_tolstring ( state, index, & mut len) ;
25
+ let bytes = slice:: from_raw_parts ( str_ptr as * const u8 , len as usize ) ;
26
+ str:: from_utf8 ( bytes) . unwrap ( )
33
27
}
34
- }
35
28
36
- #[ test]
37
- fn test_lua52compat ( ) {
38
- use std:: { ptr, slice} ;
39
- unsafe {
40
- let state = luaL_newstate ( ) ;
41
- assert ! ( state != ptr:: null_mut( ) ) ;
29
+ #[ test]
30
+ fn test_lua ( ) {
31
+ unsafe {
32
+ let state = luaL_newstate ( ) ;
33
+ assert ! ( state != ptr:: null_mut( ) ) ;
34
+
35
+ luaL_openlibs ( state) ;
36
+
37
+ let version = {
38
+ lua_getglobal ( state, "_VERSION\0 " . as_ptr ( ) . cast ( ) ) ;
39
+ to_string ( state, -1 )
40
+ } ;
41
+ assert_eq ! ( version, "Lua 5.1" ) ;
42
+
43
+ let jit_version = {
44
+ luaL_loadstring ( state, c"return jit.version" . as_ptr ( ) . cast ( ) ) ;
45
+ let ret = lua_pcall ( state, 0 , 1 , 0 ) ;
46
+ assert_eq ! ( 0 , ret) ;
47
+ to_string ( state, -1 )
48
+ } ;
49
+ let mut version_it = jit_version. split ( '.' ) ;
50
+ assert_eq ! ( version_it. next( ) . unwrap( ) , "LuaJIT 2" ) ;
51
+ assert_eq ! ( version_it. next( ) . unwrap( ) , "1" ) ;
52
+ assert ! ( version_it. next( ) . unwrap( ) . parse:: <u32 >( ) . is_ok( ) ) ;
53
+ }
54
+ }
55
+
56
+ #[ test]
57
+ fn test_lua52compat ( ) {
58
+ unsafe {
59
+ let state = luaL_newstate ( ) ;
60
+ assert ! ( state != ptr:: null_mut( ) ) ;
42
61
43
- luaL_openlibs ( state) ;
62
+ luaL_openlibs ( state) ;
44
63
45
- let code = "
46
- lua52compat = \" no\"
47
- t = setmetatable({}, {
48
- __pairs = function(t)
49
- lua52compat = \" yes\"
50
- return next, t, nil
51
- end
52
- })
53
- for k,v in pairs(t) do end
54
- \0 " ;
55
- let ret1 = luaL_loadstring ( state, code. as_ptr ( ) . cast ( ) ) ;
56
- assert_eq ! ( 0 , ret1) ;
57
- let ret2 = lua_pcall ( state, 0 , 0 , 0 ) ;
58
- assert_eq ! ( 0 , ret2) ;
64
+ let code = "
65
+ lua52compat = \" no\"
66
+ t = setmetatable({}, {
67
+ __pairs = function(t)
68
+ lua52compat = \" yes\"
69
+ return next, t, nil
70
+ end
71
+ })
72
+ for k,v in pairs(t) do end
73
+ \0 " ;
74
+ let ret1 = luaL_loadstring ( state, code. as_ptr ( ) . cast ( ) ) ;
75
+ assert_eq ! ( 0 , ret1) ;
76
+ let ret2 = lua_pcall ( state, 0 , 0 , 0 ) ;
77
+ assert_eq ! ( 0 , ret2) ;
59
78
60
- let lua52compat = {
61
- lua_getglobal ( state, "lua52compat\0 " . as_ptr ( ) . cast ( ) ) ;
62
- let mut len: c_long = 0 ;
63
- let lua52compat_ptr = lua_tolstring ( state, -1 , & mut len) ;
64
- slice:: from_raw_parts ( lua52compat_ptr as * const u8 , len as usize )
65
- } ;
79
+ let lua52compat = {
80
+ lua_getglobal ( state, "lua52compat\0 " . as_ptr ( ) . cast ( ) ) ;
81
+ to_string ( state, -1 )
82
+ } ;
66
83
67
- #[ cfg( feature = "lua52compat" ) ]
68
- assert_eq ! ( lua52compat, b"yes" ) ;
69
- #[ cfg( not( feature = "lua52compat" ) ) ]
70
- assert_eq ! ( lua52compat, b"no" ) ;
84
+ #[ cfg( feature = "lua52compat" ) ]
85
+ assert_eq ! ( lua52compat, "yes" ) ;
86
+ #[ cfg( not( feature = "lua52compat" ) ) ]
87
+ assert_eq ! ( lua52compat, "no" ) ;
88
+ }
71
89
}
72
90
}
0 commit comments