Skip to content

Commit f5d8c78

Browse files
committed
Update wasm code
Add exception test (useful for wasm)
1 parent c233a2b commit f5d8c78

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
lua: [lua54, lua53, lua52, lua51]
100100
steps:
101101
- uses: actions/checkout@v3
102-
- uses: dtolnay/rust-toolchain@stable
102+
- uses: dtolnay/rust-toolchain@nightly
103103
with:
104104
target: wasm32-unknown-emscripten
105105
- name: Install emscripten

src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,13 @@ impl Build {
118118

119119
for file in fs::read_dir(&source_dir).unwrap() {
120120
let file = file.unwrap();
121-
let filename = file.file_name().to_string_lossy().to_string();
121+
let filename = file.file_name();
122+
let filename = filename.to_str().unwrap();
122123
let src_file = source_dir.join(file.file_name());
123124
let dst_file = cpp_source_dir.join(file.file_name());
124125

125126
let mut content = fs::read(src_file).unwrap();
126-
// ljumptab.h only contains definitions and will cause errors when wrapping with
127-
// 'extern "C"'
128-
if filename.ends_with(".h") && !["ljumptab.h"].contains(&filename.as_str()) {
127+
if ["lauxlib.h", "lua.h", "lualib.h"].contains(&filename) {
129128
content.splice(0..0, b"extern \"C\" {\n".to_vec());
130129
content.extend(b"\n}".to_vec())
131130
}

testcrate/src/lib.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)