|
| 1 | +#![allow(clippy::missing_safety_doc)] |
| 2 | + |
1 | 3 | use std::os::raw::{c_char, c_int, c_long, c_void};
|
2 | 4 |
|
3 | 5 | #[repr(C)]
|
@@ -73,100 +75,100 @@ pub unsafe fn lua_getglobal(state: *mut c_void, k: *const c_char) {
|
73 | 75 | lua_getfield(state, -1002002 /* LUA_GLOBALSINDEX */, k);
|
74 | 76 | }
|
75 | 77 |
|
76 |
| -#[test] |
77 |
| -fn test_luau() { |
78 |
| - use std::{ptr, slice, str}; |
79 |
| - unsafe { |
80 |
| - let state = luaL_newstate(); |
81 |
| - assert!(state != ptr::null_mut()); |
82 |
| - |
83 |
| - // Enable JIT if supported |
84 |
| - #[cfg(not(target_os = "emscripten"))] |
85 |
| - if luau_codegen_supported() != 0 { |
86 |
| - luau_codegen_create(state); |
87 |
| - } |
88 |
| - |
89 |
| - luaL_openlibs(state); |
90 |
| - |
91 |
| - let version = { |
92 |
| - lua_getglobal(state, "_VERSION\0".as_ptr().cast()); |
93 |
| - let mut len: c_long = 0; |
94 |
| - let version_ptr = lua_tolstring(state, -1, &mut len); |
95 |
| - let s = slice::from_raw_parts(version_ptr as *const u8, len as usize); |
96 |
| - str::from_utf8(s).unwrap() |
97 |
| - }; |
98 |
| - |
99 |
| - assert_eq!(version, "Luau"); |
100 |
| - |
101 |
| - let code = "local a, b = ... return a + b\0"; |
102 |
| - let mut bytecode_size = 0; |
103 |
| - let bytecode = luau_compile( |
104 |
| - code.as_ptr().cast(), |
105 |
| - code.len() - 1, |
106 |
| - ptr::null_mut(), |
107 |
| - &mut bytecode_size, |
108 |
| - ); |
109 |
| - let result = luau_load(state, "sum\0".as_ptr().cast(), bytecode, bytecode_size, 0); |
110 |
| - assert_eq!(result, 0); |
111 |
| - free(bytecode.cast()); |
112 |
| - |
113 |
| - // Compile the function (JIT, if supported) |
114 |
| - #[cfg(not(target_os = "emscripten"))] |
115 |
| - if luau_codegen_supported() != 0 { |
116 |
| - luau_codegen_compile(state, -1); |
117 |
| - } |
| 78 | +pub unsafe fn to_string<'a>(state: *mut c_void, index: c_int) -> &'a str { |
| 79 | + let mut len: c_long = 0; |
| 80 | + let ptr = lua_tolstring(state, index, &mut len); |
| 81 | + let bytes = std::slice::from_raw_parts(ptr as *const u8, len as usize); |
| 82 | + std::str::from_utf8(bytes).unwrap() |
| 83 | +} |
118 | 84 |
|
119 |
| - // Call the loaded function |
120 |
| - lua_pushinteger(state, 123); |
121 |
| - lua_pushinteger(state, 321); |
122 |
| - lua_call(state, 2, 1); |
123 |
| - assert_eq!(lua_tointegerx(state, -1, ptr::null_mut()), 444); |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use std::ptr; |
124 | 88 |
|
125 |
| - lua_close(state); |
| 89 | + use super::*; |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_luau() { |
| 93 | + unsafe { |
| 94 | + let state = luaL_newstate(); |
| 95 | + assert!(!state.is_null()); |
| 96 | + |
| 97 | + // Enable JIT if supported |
| 98 | + #[cfg(not(target_os = "emscripten"))] |
| 99 | + if luau_codegen_supported() != 0 { |
| 100 | + luau_codegen_create(state); |
| 101 | + } |
| 102 | + |
| 103 | + luaL_openlibs(state); |
| 104 | + |
| 105 | + lua_getglobal(state, c"_VERSION".as_ptr()); |
| 106 | + let version = to_string(state, -1); |
| 107 | + |
| 108 | + assert_eq!(version, "Luau"); |
| 109 | + |
| 110 | + let code = "local a, b = ... return a + b"; |
| 111 | + let mut bytecode_size = 0; |
| 112 | + let bytecode = luau_compile( |
| 113 | + code.as_ptr().cast(), |
| 114 | + code.len(), |
| 115 | + ptr::null_mut(), |
| 116 | + &mut bytecode_size, |
| 117 | + ); |
| 118 | + let result = luau_load(state, c"sum".as_ptr(), bytecode, bytecode_size, 0); |
| 119 | + assert_eq!(result, 0); |
| 120 | + free(bytecode.cast()); |
| 121 | + |
| 122 | + // Compile the function (JIT, if supported) |
| 123 | + #[cfg(not(target_os = "emscripten"))] |
| 124 | + if luau_codegen_supported() != 0 { |
| 125 | + luau_codegen_compile(state, -1); |
| 126 | + } |
| 127 | + |
| 128 | + // Call the loaded function |
| 129 | + lua_pushinteger(state, 123); |
| 130 | + lua_pushinteger(state, 321); |
| 131 | + lua_call(state, 2, 1); |
| 132 | + assert_eq!(lua_tointegerx(state, -1, ptr::null_mut()), 444); |
| 133 | + |
| 134 | + lua_close(state); |
| 135 | + } |
126 | 136 | }
|
127 |
| -} |
128 | 137 |
|
129 |
| -#[test] |
130 |
| -fn test_metatablepointer() { |
131 |
| - use std::ptr; |
132 |
| - unsafe { |
133 |
| - let state = luaL_newstate(); |
134 |
| - assert!(state != ptr::null_mut()); |
| 138 | + #[test] |
| 139 | + fn test_metatablepointer() { |
| 140 | + unsafe { |
| 141 | + let state = luaL_newstate(); |
| 142 | + assert!(!state.is_null()); |
135 | 143 |
|
136 |
| - lua_createtable(state, 0, 0); |
137 |
| - assert!(lua_getmetatablepointer(state, -1).is_null()); |
| 144 | + lua_createtable(state, 0, 0); |
| 145 | + assert!(lua_getmetatablepointer(state, -1).is_null()); |
138 | 146 |
|
139 |
| - lua_createtable(state, 0, 0); |
140 |
| - let mt_ptr1 = lua_topointer(state, -1); |
| 147 | + lua_createtable(state, 0, 0); |
| 148 | + let mt_ptr1 = lua_topointer(state, -1); |
141 | 149 |
|
142 |
| - lua_setmetatable(state, -2); |
143 |
| - let mt_ptr2 = lua_getmetatablepointer(state, -1); |
144 |
| - assert_eq!(mt_ptr1, mt_ptr2); |
| 150 | + lua_setmetatable(state, -2); |
| 151 | + let mt_ptr2 = lua_getmetatablepointer(state, -1); |
| 152 | + assert_eq!(mt_ptr1, mt_ptr2); |
145 | 153 |
|
146 |
| - lua_close(state); |
| 154 | + lua_close(state); |
| 155 | + } |
147 | 156 | }
|
148 |
| -} |
149 | 157 |
|
150 |
| -#[test] |
151 |
| -fn test_exceptions() { |
152 |
| - use std::{ptr, slice, str}; |
153 |
| - unsafe { |
154 |
| - let state = luaL_newstate(); |
155 |
| - assert!(state != ptr::null_mut()); |
| 158 | + #[test] |
| 159 | + fn test_exceptions() { |
| 160 | + unsafe { |
| 161 | + let state = luaL_newstate(); |
| 162 | + assert!(!state.is_null()); |
156 | 163 |
|
157 |
| - unsafe extern "C-unwind" fn it_panics(state: *mut c_void) -> c_int { |
158 |
| - luaL_errorL(state, "exception!\0".as_ptr().cast()); |
159 |
| - } |
| 164 | + unsafe extern "C-unwind" fn it_panics(state: *mut c_void) -> c_int { |
| 165 | + luaL_errorL(state, "exception!\0".as_ptr().cast()); |
| 166 | + } |
160 | 167 |
|
161 |
| - lua_pushcclosurek(state, it_panics, ptr::null(), 0, ptr::null()); |
162 |
| - let result = lua_pcall(state, 0, 0, 0); |
163 |
| - assert_eq!(result, 2); // LUA_ERRRUN |
164 |
| - let s = { |
165 |
| - let mut len: c_long = 0; |
166 |
| - let version_ptr = lua_tolstring(state, -1, &mut len); |
167 |
| - let s = slice::from_raw_parts(version_ptr as *const u8, len as usize); |
168 |
| - str::from_utf8(s).unwrap() |
169 |
| - }; |
170 |
| - assert_eq!(s, "exception!"); |
| 168 | + lua_pushcclosurek(state, it_panics, ptr::null(), 0, ptr::null()); |
| 169 | + let result = lua_pcall(state, 0, 0, 0); |
| 170 | + assert_eq!(result, 2); // LUA_ERRRUN |
| 171 | + assert_eq!(to_string(state, -1), "exception!"); |
| 172 | + } |
171 | 173 | }
|
172 | 174 | }
|
0 commit comments