lib.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // use std::{collections::HashMap, hash::Hash};
  2. use std::io::Cursor;
  3. use ldb::LDb;
  4. use buffer::Buffer;
  5. use lua_shared as lua;
  6. use lua_shared::lua_State;
  7. mod buffer;
  8. mod ldb;
  9. mod ltree;
  10. #[macro_export]
  11. macro_rules! check_slice {
  12. ($state:ident, $index:tt) => {{
  13. let mut len = 0;
  14. let str_ptr = lua_shared::Lchecklstring($state, $index, &mut len);
  15. std::slice::from_raw_parts(str_ptr, len)
  16. }};
  17. }
  18. #[macro_export]
  19. macro_rules! insert_function {
  20. ($state:ident, $name:expr, $func:expr) => {
  21. lua_shared::pushfunction($state, $func);
  22. lua_shared::setfield($state, -2, lua::cstr!($name));
  23. };
  24. }
  25. #[no_mangle]
  26. unsafe extern "C" fn gmod13_open(state: lua_State) -> i32 {
  27. lua::createtable(state, 0, 1);
  28. insert_function!(state, "Open", LDb::l_open);
  29. insert_function!(state, "Buffer", Buffer::l_new);
  30. lua::pushstring(state, lua::cstr!("Sled 0.34.7"));
  31. lua::setfield(state, -2, lua::cstr!("_VERSION"));
  32. lua::setfield(state, lua::GLOBALSINDEX, lua::cstr!("sled"));
  33. match lua::loadx(state, &mut Cursor::new(include_str!("lib.lua")), lua::cstr!("@includes/modules/lsled.lua"), lua::cstr!("t")) {
  34. Ok(_) => match lua::pcall(state, 0, 0, 0) {
  35. lua::Status::RuntimeError |
  36. lua::Status::MemoryError |
  37. lua::Status::Error => {lua::error(state);},
  38. _ => {}
  39. },
  40. Err(lua::LError::MemoryError(e)) |
  41. Err(lua::LError::SyntaxError(e)) => {
  42. lua::pushlstring(state, e.as_ptr(), e.as_bytes().len());
  43. lua::error(state);
  44. },
  45. _ => {}
  46. }
  47. 0
  48. }
  49. #[no_mangle]
  50. unsafe extern "C" fn gmod13_close(_state: lua_State) -> i32 {
  51. 0
  52. }