Quellcode durchsuchen

Now you can "reopen" db multiple times! (no)

IVogel vor 2 Jahren
Ursprung
Commit
4297b8f3b4
3 geänderte Dateien mit 33 neuen und 2 gelöschten Zeilen
  1. 7 2
      Cargo.toml
  2. 10 0
      src/lib.lua
  3. 16 0
      src/lib.rs

+ 7 - 2
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "lsled"
-version = "0.6.0"
+version = "0.6.1"
 edition = "2021"
 
 [lib]
@@ -11,4 +11,9 @@ lua-shared = {git = "http://git.thetha.wtf/ivogel/lua-shared.git"}
 sled = "0.34.7"
 bincode = "1.3.3"
 serde = "1.0.139"
-paste = "1.0"
+paste = "1.0"
+
+[profile.release]
+lto = true
+codegen-units = 1
+strip = true

+ 10 - 0
src/lib.lua

@@ -0,0 +1,10 @@
+local sled_open = sled.Open
+local cache = setmetatable({}, {__mode = "v"})
+
+function sled.Open(name)
+    local db = cache[name]
+    if db then return db end
+    db = sled_open(name)
+    cache[name] = db
+    return db
+end

+ 16 - 0
src/lib.rs

@@ -1,5 +1,7 @@
 // use std::{collections::HashMap, hash::Hash};
 
+use std::io::Cursor;
+
 use ldb::LDb;
 use buffer::Buffer;
 use lua_shared as lua;
@@ -34,6 +36,20 @@ unsafe extern "C" fn gmod13_open(state: lua_State) -> i32 {
     lua::pushstring(state, lua::cstr!("Sled 0.34.7"));
     lua::setfield(state, -2, lua::cstr!("_VERSION"));
     lua::setfield(state, lua::GLOBALSINDEX, lua::cstr!("sled"));
+    match lua::loadx(state, &mut Cursor::new(include_str!("lib.lua")), lua::cstr!("@includes/modules/lsled.lua"), lua::cstr!("t")) {
+        Ok(_) => match lua::pcall(state, 0, 0, 0) {
+            lua::Status::RuntimeError |
+            lua::Status::MemoryError  |
+            lua::Status::Error => {lua::error(state);},
+            _ => {}
+        },
+        Err(lua::LError::MemoryError(e)) | 
+        Err(lua::LError::SyntaxError(e)) => {
+            lua::pushlstring(state, e.as_ptr(), e.as_bytes().len());
+            lua::error(state);
+        },
+        _ => {}
+    }
     0
 }