@@ -6,6 +6,34 @@ extern "C" {
66 pub fn lua_getfield ( state : * mut c_void , index : c_int , k : * const c_char ) ;
77 pub fn lua_tolstring ( state : * mut c_void , index : c_int , len : * mut c_long ) -> * const c_char ;
88 pub fn luaL_loadstring ( state : * mut c_void , s : * const c_char ) -> c_int ;
9+ pub fn luaL_error ( state : * mut c_void , fmt : * const c_char , ...) -> c_int ;
10+
11+ pub fn lua_pushcclosure (
12+ state : * mut c_void ,
13+ f : unsafe extern "C-unwind" fn ( state : * mut c_void ) -> c_int ,
14+ n : c_int ,
15+ ) ;
16+
17+ #[ cfg( feature = "lua51" ) ]
18+ pub fn lua_pcall ( state : * mut c_void , nargs : c_int , nresults : c_int , errfunc : c_int ) -> c_int ;
19+ #[ cfg( feature = "lua52" ) ]
20+ pub fn lua_pcallk (
21+ state : * mut c_void ,
22+ nargs : c_int ,
23+ nresults : c_int ,
24+ errfunc : c_int ,
25+ ctx : c_int ,
26+ k : * const c_void ,
27+ ) -> c_int ;
28+ #[ cfg( any( feature = "lua53" , feature = "lua54" ) ) ]
29+ pub fn lua_pcallk (
30+ state : * mut c_void ,
31+ nargs : c_int ,
32+ nresults : c_int ,
33+ errfunc : c_int ,
34+ ctx : isize ,
35+ k : * const c_void ,
36+ ) -> c_int ;
937
1038 #[ cfg( feature = "lua52" ) ]
1139 pub fn lua_getglobal ( state : * mut c_void , k : * const c_char ) ;
@@ -18,8 +46,18 @@ pub unsafe fn lua_getglobal(state: *mut c_void, k: *const c_char) {
1846 lua_getfield ( state, -10002 /* LUA_GLOBALSINDEX */ , k) ;
1947}
2048
49+ #[ cfg( any( feature = "lua52" , feature = "lua53" , feature = "lua54" ) ) ]
50+ pub unsafe fn lua_pcall (
51+ state : * mut c_void ,
52+ nargs : c_int ,
53+ nresults : c_int ,
54+ errfunc : c_int ,
55+ ) -> c_int {
56+ lua_pcallk ( state, nargs, nresults, errfunc, 0 , std:: ptr:: null ( ) )
57+ }
58+
2159#[ test]
22- fn lua_works ( ) {
60+ fn test_lua ( ) {
2361 use std:: { ptr, slice} ;
2462 unsafe {
2563 let state = luaL_newstate ( ) ;
@@ -46,7 +84,7 @@ fn lua_works() {
4684}
4785
4886#[ test]
49- fn unicode_identifiers ( ) {
87+ fn test_unicode_identifiers ( ) {
5088 unsafe {
5189 let state = luaL_newstate ( ) ;
5290 let code = "local 😀 = 0\0 " ;
@@ -57,3 +95,27 @@ fn unicode_identifiers() {
5795 assert_ne ! ( 0 , ret) ;
5896 }
5997}
98+
99+ #[ test]
100+ fn test_exceptions ( ) {
101+ use std:: { ptr, slice, str} ;
102+ unsafe {
103+ let state = luaL_newstate ( ) ;
104+ assert ! ( state != ptr:: null_mut( ) ) ;
105+
106+ unsafe extern "C-unwind" fn it_panics ( state : * mut c_void ) -> c_int {
107+ luaL_error ( state, "exception!\0 " . as_ptr ( ) . cast ( ) )
108+ }
109+
110+ lua_pushcclosure ( state, it_panics, 0 ) ;
111+ let result = lua_pcall ( state, 0 , 0 , 0 ) ;
112+ assert_eq ! ( result, 2 ) ; // LUA_ERRRUN
113+ let s = {
114+ let mut len: c_long = 0 ;
115+ let version_ptr = lua_tolstring ( state, -1 , & mut len) ;
116+ let s = slice:: from_raw_parts ( version_ptr as * const u8 , len as usize ) ;
117+ str:: from_utf8 ( s) . unwrap ( )
118+ } ;
119+ assert_eq ! ( s, "exception!" ) ;
120+ }
121+ }
0 commit comments