diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-11-16 12:55:58 +0000 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-11-16 12:55:58 +0000 |
commit | 5043447f230d0be2a4dde9512cb1aae1501a91a9 (patch) | |
tree | d0611a3661f7da97eb9e78c2934cbaf17621077b /mlua-sys/src | |
parent | b879abc418475a375065c2156ef46c9c1aeaff80 (diff) | |
download | mlua-5043447f230d0be2a4dde9512cb1aae1501a91a9.zip |
Support Luau buffer type and and library.
Buffer is an object that represents a fixed-size mutable block of memory and added to Luau 0.601.
See https://luau-lang.org/library#buffer-library for more details.
Diffstat (limited to 'mlua-sys/src')
-rw-r--r-- | mlua-sys/src/luau/lauxlib.rs | 47 | ||||
-rw-r--r-- | mlua-sys/src/luau/lua.rs | 9 | ||||
-rw-r--r-- | mlua-sys/src/luau/lualib.rs | 2 |
3 files changed, 57 insertions, 1 deletions
diff --git a/mlua-sys/src/luau/lauxlib.rs b/mlua-sys/src/luau/lauxlib.rs index 4095155..73788af 100644 --- a/mlua-sys/src/luau/lauxlib.rs +++ b/mlua-sys/src/luau/lauxlib.rs @@ -52,6 +52,8 @@ extern "C-unwind" { pub fn luaL_newmetatable_(L: *mut lua_State, tname: *const c_char) -> c_int; pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void; + pub fn luaL_checkbuffer(L: *mut lua_State, narg: c_int, len: *mut usize) -> *mut c_void; + pub fn luaL_where(L: *mut lua_State, lvl: c_int); #[link_name = "luaL_errorL"] @@ -152,5 +154,48 @@ pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) { } // -// TODO: Generic Buffer Manipulation +// Generic Buffer Manipulation // + +/// Buffer size used for on-stack string operations. This limit depends on native stack size. +pub const LUA_BUFFERSIZE: usize = 512; + +#[repr(C)] +pub struct luaL_Strbuf { + p: *mut c_char, // current position in buffer + end: *mut c_char, // end of the current buffer + L: *mut lua_State, + storage: *mut c_void, // TString + buffer: [c_char; LUA_BUFFERSIZE], +} + +// For compatibility +pub type luaL_Buffer = luaL_Strbuf; + +extern "C-unwind" { + pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Strbuf); + pub fn luaL_buffinitsize(L: *mut lua_State, B: *mut luaL_Strbuf, size: usize) -> *mut c_char; + pub fn luaL_prepbuffsize(B: *mut luaL_Strbuf, size: usize) -> *mut c_char; + pub fn luaL_addlstring(B: *mut luaL_Strbuf, s: *const c_char, l: usize); + pub fn luaL_addvalue(B: *mut luaL_Strbuf); + pub fn luaL_addvalueany(B: *mut luaL_Strbuf, idx: c_int); + pub fn luaL_pushresult(B: *mut luaL_Strbuf); + pub fn luaL_pushresultsize(B: *mut luaL_Strbuf, size: usize); +} + +pub unsafe fn luaL_addchar(B: *mut luaL_Strbuf, c: c_char) { + if (*B).p >= (*B).end { + luaL_prepbuffsize(B, 1); + } + *(*B).p = c; + (*B).p = (*B).p.add(1); +} + +pub unsafe fn luaL_addstring(B: *mut luaL_Strbuf, s: *const c_char) { + // Calculate length of s + let mut len = 0; + while *s.add(len) != 0 { + len += 1; + } + luaL_addlstring(B, s, len); +} diff --git a/mlua-sys/src/luau/lua.rs b/mlua-sys/src/luau/lua.rs index 996f62e..2da5690 100644 --- a/mlua-sys/src/luau/lua.rs +++ b/mlua-sys/src/luau/lua.rs @@ -55,6 +55,7 @@ pub const LUA_TTABLE: c_int = 6; pub const LUA_TFUNCTION: c_int = 7; pub const LUA_TUSERDATA: c_int = 8; pub const LUA_TTHREAD: c_int = 9; +pub const LUA_TBUFFER: c_int = 10; /// Guaranteed number of Lua stack slots available to a C function. pub const LUA_MINSTACK: c_int = 20; @@ -147,6 +148,7 @@ extern "C-unwind" { pub fn lua_touserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void; pub fn lua_userdatatag(L: *mut lua_State, idx: c_int) -> c_int; pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State; + pub fn lua_tobuffer(L: *mut lua_State, idx: c_int, len: *mut usize) -> *mut c_void; pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void; // @@ -181,6 +183,8 @@ extern "C-unwind" { pub fn lua_newuserdatatagged(L: *mut lua_State, sz: usize, tag: c_int) -> *mut c_void; pub fn lua_newuserdatadtor(L: *mut lua_State, sz: usize, dtor: lua_Udestructor) -> *mut c_void; + pub fn lua_newbuffer(L: *mut lua_State, sz: usize) -> *mut c_void; + // // Get functions (Lua -> stack) // @@ -373,6 +377,11 @@ pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] +pub unsafe fn lua_isbuffer(L: *mut lua_State, n: c_int) -> c_int { + (lua_type(L, n) == LUA_TBUFFER) as c_int +} + +#[inline(always)] pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int { (lua_type(L, n) == LUA_TNONE) as c_int } diff --git a/mlua-sys/src/luau/lualib.rs b/mlua-sys/src/luau/lualib.rs index 5593f8f..96864a0 100644 --- a/mlua-sys/src/luau/lualib.rs +++ b/mlua-sys/src/luau/lualib.rs @@ -9,6 +9,7 @@ pub const LUA_TABLIBNAME: &str = "table"; pub const LUA_OSLIBNAME: &str = "os"; pub const LUA_STRLIBNAME: &str = "string"; pub const LUA_BITLIBNAME: &str = "bit32"; +pub const LUA_BUFFERLIBNAME: &str = "buffer"; pub const LUA_UTF8LIBNAME: &str = "utf8"; pub const LUA_MATHLIBNAME: &str = "math"; pub const LUA_DBLIBNAME: &str = "debug"; @@ -20,6 +21,7 @@ extern "C-unwind" { pub fn luaopen_os(L: *mut lua_State) -> c_int; pub fn luaopen_string(L: *mut lua_State) -> c_int; pub fn luaopen_bit32(L: *mut lua_State) -> c_int; + pub fn luaopen_buffer(L: *mut lua_State) -> c_int; pub fn luaopen_utf8(L: *mut lua_State) -> c_int; pub fn luaopen_math(L: *mut lua_State) -> c_int; pub fn luaopen_debug(L: *mut lua_State) -> c_int; |