diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-03-01 12:29:43 +0000 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-03-21 01:08:47 +0000 |
commit | 32124b31a05a25c0146864661f3a34a8abb1a7d6 (patch) | |
tree | ea34992510c0553a434793b01abcfa37db5e288c /src/ffi/luau | |
parent | 3e5f8e7bb8bb2822629593ecbd316f0fda7b4de8 (diff) | |
download | mlua-32124b31a05a25c0146864661f3a34a8abb1a7d6.zip |
Move chunks structs to a new module.
Add Luau Compiler interface to compile sources.
Diffstat (limited to 'src/ffi/luau')
-rw-r--r-- | src/ffi/luau/compat.rs | 2 | ||||
-rw-r--r-- | src/ffi/luau/luacode.rs | 21 |
2 files changed, 20 insertions, 3 deletions
diff --git a/src/ffi/luau/compat.rs b/src/ffi/luau/compat.rs index 9094822..db9c55c 100644 --- a/src/ffi/luau/compat.rs +++ b/src/ffi/luau/compat.rs @@ -380,7 +380,7 @@ pub unsafe fn luaL_loadbufferx( } if chunk_is_text { - let data = luau_compile(data, size, ptr::null_mut(), &mut size); + let data = luau_compile_(data, size, ptr::null_mut(), &mut size); let ok = luau_load(L, name, data, size, 0) == 0; free(data as *mut c_void); if !ok { diff --git a/src/ffi/luau/luacode.rs b/src/ffi/luau/luacode.rs index 3f24b65..571db4c 100644 --- a/src/ffi/luau/luacode.rs +++ b/src/ffi/luau/luacode.rs @@ -1,6 +1,7 @@ //! Contains definitions from `luacode.h`. -use std::os::raw::{c_char, c_int}; +use std::os::raw::{c_char, c_int, c_void}; +use std::slice; #[repr(C)] pub struct lua_CompileOptions { @@ -13,10 +14,26 @@ pub struct lua_CompileOptions { } extern "C" { - pub fn luau_compile( + #[link_name = "luau_compile"] + pub fn luau_compile_( source: *const c_char, size: usize, options: *mut lua_CompileOptions, outsize: *mut usize, ) -> *mut c_char; + + fn free(p: *mut c_void); +} + +pub unsafe fn luau_compile(source: &[u8], mut options: lua_CompileOptions) -> Vec<u8> { + let mut outsize = 0; + let data_ptr = luau_compile_( + source.as_ptr() as *const c_char, + source.len(), + &mut options, + &mut outsize, + ); + let data = slice::from_raw_parts(data_ptr as *mut u8, outsize).to_vec(); + free(data_ptr as *mut c_void); + data } |