-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use { | ||
crate::pd_func_caller, | ||
alloc::string::String, | ||
anyhow::{anyhow, Error}, | ||
core::ptr, | ||
crankstart_sys::{ctypes, lua_CFunction}, | ||
cstr_core::{CStr, CString}, | ||
}; | ||
|
||
static mut LUA: Lua = Lua(ptr::null_mut()); | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Lua(*const crankstart_sys::playdate_lua); | ||
|
||
impl Lua { | ||
pub(crate) fn new(file: *const crankstart_sys::playdate_lua) { | ||
unsafe { | ||
LUA = Lua(file); | ||
} | ||
} | ||
|
||
pub fn get() -> Self { | ||
unsafe { LUA.clone() } | ||
} | ||
|
||
pub fn add_function(&self, f: lua_CFunction, name: &str) -> Result<(), Error> { | ||
let c_name = CString::new(name).map_err(Error::msg)?; | ||
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut(); | ||
pd_func_caller!((*self.0).addFunction, f, c_name.as_ptr(), &mut out_err)?; | ||
if out_err != ptr::null_mut() { | ||
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() }; | ||
Err(anyhow!(err_msg)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn get_arg_string(&self, pos: i32) -> Result<String, Error> { | ||
let c_arg_string = pd_func_caller!((*self.0).getArgString, pos as ctypes::c_int)?; | ||
unsafe { | ||
let arg_string = CStr::from_ptr(c_arg_string).to_string_lossy().into_owned(); | ||
Ok(arg_string) | ||
} | ||
} | ||
} |