Skip to content

Commit e3f34f3

Browse files
committed
Correct C return type for lua_error
The definition of lua_error in all of Lua 5.1 through 5.4 says lua_error returns int, but the Rust definition of the same function treats it as void (because it's known not to return). This causes link-time errors when building for wasm targets because the wasm linker is aware of function return types and errors out if they differ between definition and declaration.
1 parent b16f389 commit e3f34f3

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

mlua-sys/src/lua51/lua.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,22 @@ extern "C-unwind" {
228228
//
229229
#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
230230
extern "C-unwind" {
231-
pub fn lua_error(L: *mut lua_State) -> !;
231+
#[link_name = "lua_error"]
232+
fn lua_error_(L: *mut lua_State) -> c_int;
232233
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
233234
pub fn lua_concat(L: *mut lua_State, n: c_int);
234235
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
235236
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
236237
}
237238

239+
// lua_error does not return but is declared to return int, and Rust translates
240+
// ! to void which can cause link-time errors if the platform linker is aware
241+
// of return types and requires they match (for example: wasm does this).
242+
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
243+
lua_error_(L);
244+
unreachable!();
245+
}
246+
238247
//
239248
// Some useful macros (implemented as Rust functions)
240249
//

mlua-sys/src/lua52/lua.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,23 @@ extern "C-unwind" {
308308
//
309309
// Miscellaneous functions
310310
//
311-
pub fn lua_error(L: *mut lua_State) -> !;
311+
#[link_name = "lua_error"]
312+
fn lua_error_(L: *mut lua_State) -> c_int;
312313
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
313314
pub fn lua_concat(L: *mut lua_State, n: c_int);
314315
pub fn lua_len(L: *mut lua_State, idx: c_int);
315316
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
316317
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
317318
}
318319

320+
// lua_error does not return but is declared to return int, and Rust translates
321+
// ! to void which can cause link-time errors if the platform linker is aware
322+
// of return types and requires they match (for example: wasm does this).
323+
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
324+
lua_error_(L);
325+
unreachable!();
326+
}
327+
319328
//
320329
// Some useful macros (implemented as Rust functions)
321330
//

mlua-sys/src/lua53/lua.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ extern "C-unwind" {
314314
//
315315
// Miscellaneous functions
316316
//
317-
pub fn lua_error(L: *mut lua_State) -> !;
317+
#[link_name = "lua_error"]
318+
fn lua_error_(L: *mut lua_State) -> c_int;
318319
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
319320
pub fn lua_concat(L: *mut lua_State, n: c_int);
320321
pub fn lua_len(L: *mut lua_State, idx: c_int);
@@ -323,6 +324,14 @@ extern "C-unwind" {
323324
pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
324325
}
325326

327+
// lua_error does not return but is declared to return int, and Rust translates
328+
// ! to void which can cause link-time errors if the platform linker is aware
329+
// of return types and requires they match (for example: wasm does this).
330+
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
331+
lua_error_(L);
332+
unreachable!();
333+
}
334+
326335
//
327336
// Some useful macros (implemented as Rust functions)
328337
//

mlua-sys/src/lua54/lua.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ extern "C-unwind" {
343343
//
344344
// Miscellaneous functions
345345
//
346-
pub fn lua_error(L: *mut lua_State) -> !;
346+
#[link_name = "lua_error"]
347+
fn lua_error_(L: *mut lua_State) -> c_int;
347348
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
348349
pub fn lua_concat(L: *mut lua_State, n: c_int);
349350
pub fn lua_len(L: *mut lua_State, idx: c_int);
@@ -355,6 +356,14 @@ extern "C-unwind" {
355356
pub fn lua_closeslot(L: *mut lua_State, idx: c_int);
356357
}
357358

359+
// lua_error does not return but is declared to return int, and Rust translates
360+
// ! to void which can cause link-time errors if the platform linker is aware
361+
// of return types and requires they match (for example: wasm does this).
362+
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
363+
lua_error_(L);
364+
unreachable!();
365+
}
366+
358367
//
359368
// Some useful macros (implemented as Rust functions)
360369
//

0 commit comments

Comments
 (0)