From 4306e6e97818919f829266d336fbdc05271286c2 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Fri, 26 May 2023 23:52:48 +0100 Subject: Add `Value::to_string()` method similar to `luaL_tolstring`. It uses `__tostring` metamethod if set. Closes #279 --- mlua-sys/src/lua51/compat.rs | 10 +++++----- mlua-sys/src/lua52/compat.rs | 10 +++++----- mlua-sys/src/lua53/lauxlib.rs | 8 +++++++- mlua-sys/src/luau/compat.rs | 10 +++++----- 4 files changed, 22 insertions(+), 16 deletions(-) (limited to 'mlua-sys/src') diff --git a/mlua-sys/src/lua51/compat.rs b/mlua-sys/src/lua51/compat.rs index c67dbe0..5216d33 100644 --- a/mlua-sys/src/lua51/compat.rs +++ b/mlua-sys/src/lua51/compat.rs @@ -486,10 +486,10 @@ pub unsafe fn luaL_traceback( lua_concat(L, lua_gettop(L) - top); } -pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char { +pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) -> *const c_char { + idx = lua_absindex(L, idx); if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { - let t = lua_type(L, idx); - match t { + match lua_type(L, idx) { LUA_TNIL => { lua_pushliteral(L, "nil"); } @@ -503,7 +503,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> lua_pushliteral(L, "true"); } } - _ => { + t => { let tt = luaL_getmetafield(L, idx, cstr!("__name")); let name = if tt == LUA_TSTRING { lua_tostring(L, -1) @@ -512,7 +512,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> }; lua_pushfstring(L, cstr!("%s: %p"), name, lua_topointer(L, idx)); if tt != LUA_TNIL { - lua_replace(L, -2); + lua_replace(L, -2); // remove '__name' } } }; diff --git a/mlua-sys/src/lua52/compat.rs b/mlua-sys/src/lua52/compat.rs index b834ab1..9a75a01 100644 --- a/mlua-sys/src/lua52/compat.rs +++ b/mlua-sys/src/lua52/compat.rs @@ -205,10 +205,10 @@ pub unsafe fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_in } } -pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char { +pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) -> *const c_char { + idx = lua_absindex(L, idx); if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { - let t = lua_type(L, idx); - match t { + match lua_type(L, idx) { LUA_TNIL => { lua_pushliteral(L, "nil"); } @@ -222,7 +222,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> lua_pushliteral(L, "true"); } } - _ => { + t => { let tt = luaL_getmetafield(L, idx, cstr!("__name")); let name = if tt == LUA_TSTRING { lua_tostring(L, -1) @@ -231,7 +231,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> }; lua_pushfstring(L, cstr!("%s: %p"), name, lua_topointer(L, idx)); if tt != LUA_TNIL { - lua_replace(L, -2); + lua_replace(L, -2); // remove '__name' } } }; diff --git a/mlua-sys/src/lua53/lauxlib.rs b/mlua-sys/src/lua53/lauxlib.rs index cc83c61..0d1679f 100644 --- a/mlua-sys/src/lua53/lauxlib.rs +++ b/mlua-sys/src/lua53/lauxlib.rs @@ -25,7 +25,8 @@ extern "C" { pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int; pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int; - pub fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char; + #[link_name = "luaL_tolstring"] + pub fn luaL_tolstring_(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char; pub fn luaL_argerror(L: *mut lua_State, arg: c_int, extramsg: *const c_char) -> c_int; pub fn luaL_checklstring(L: *mut lua_State, arg: c_int, l: *mut usize) -> *const c_char; pub fn luaL_optlstring( @@ -167,6 +168,11 @@ pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) { lua::lua_getfield(L, lua::LUA_REGISTRYINDEX, n); } +#[inline(always)] +pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char { + luaL_tolstring_(L, lua::lua_absindex(L, idx), len) +} + // luaL_opt would be implemented here but it is undocumented, so it's omitted #[inline(always)] diff --git a/mlua-sys/src/luau/compat.rs b/mlua-sys/src/luau/compat.rs index 043245a..f08882d 100644 --- a/mlua-sys/src/luau/compat.rs +++ b/mlua-sys/src/luau/compat.rs @@ -436,10 +436,10 @@ pub unsafe fn luaL_traceback( lua_concat(L, lua_gettop(L) - top); } -pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char { +pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) -> *const c_char { + idx = lua_absindex(L, idx); if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { - let t = lua_type(L, idx); - match t { + match lua_type(L, idx) { LUA_TNIL => { lua_pushliteral(L, "nil"); } @@ -453,7 +453,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> lua_pushliteral(L, "true"); } } - _ => { + t => { let tt = luaL_getmetafield(L, idx, cstr!("__name")); let name = if tt == LUA_TSTRING { lua_tostring(L, -1) @@ -462,7 +462,7 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> }; lua_pushfstring(L, cstr!("%s: %p"), name, lua_topointer(L, idx)); if tt != LUA_TNIL { - lua_replace(L, -2); + lua_replace(L, -2); // remove '__name' } } }; -- cgit v1.2.3