Skip to content

Commit 151722e

Browse files
committed
fix filesystem, fix pop
1 parent 6edf342 commit 151722e

File tree

7 files changed

+37
-16
lines changed

7 files changed

+37
-16
lines changed

resources/debugger.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- empty file

resources/testmain.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ local counter = 0
1010
taskpad:push("hello world", "hello world2", counter)
1111
]])
1212
while true do
13-
print(d:bpop())
13+
local a, err, c, d = d:pop()
14+
if a then
15+
print(err, c, d)
16+
else
17+
print("sleep 1000")
18+
thread.sleep(1000)
19+
end
1420
end

src/bee/lua_filesystem.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,30 @@ fn path_constructor(_: &Lua, path: String) -> LuaResult<LuaFilePath> {
162162
Ok(LuaFilePath::new(path))
163163
}
164164

165-
fn status(_: &Lua, path: String) -> LuaResult<()> {
165+
fn status(_: &Lua, path: LuaFilePath) -> LuaResult<()> {
166166
let _ = path;
167167
// Implementation for status function
168168
Ok(())
169169
}
170170

171-
fn exists(_: &Lua, path: String) -> LuaResult<bool> {
172-
Ok(std::path::Path::new(&path).exists())
171+
fn exists(_: &Lua, path: LuaFilePath) -> LuaResult<bool> {
172+
Ok(std::path::Path::new(&path.path).exists())
173173
}
174174

175-
fn is_directory(_: &Lua, path: String) -> LuaResult<bool> {
176-
Ok(std::path::Path::new(&path).is_dir())
175+
fn is_directory(_: &Lua, path: LuaFilePath) -> LuaResult<bool> {
176+
Ok(std::path::Path::new(&path.path).is_dir())
177177
}
178178

179-
fn is_regular_file(_: &Lua, path: String) -> LuaResult<bool> {
180-
Ok(std::path::Path::new(&path).is_file())
179+
fn is_regular_file(_: &Lua, path: LuaFilePath) -> LuaResult<bool> {
180+
Ok(std::path::Path::new(&path.path).is_file())
181181
}
182182

183-
fn file_size(_: &Lua, path: String) -> LuaResult<u64> {
184-
Ok(std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0))
183+
fn file_size(_: &Lua, path: LuaFilePath) -> LuaResult<u64> {
184+
Ok(std::fs::metadata(&path.path).map(|m| m.len()).unwrap_or(0))
185185
}
186186

187-
fn create_directory(_: &Lua, path: String) -> LuaResult<()> {
188-
std::fs::create_dir(&path)?;
187+
fn create_directory(_: &Lua, path: LuaFilePath) -> LuaResult<()> {
188+
std::fs::create_dir(&path.path)?;
189189
Ok(())
190190
}
191191

@@ -354,7 +354,17 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
354354
"temp_directory_path",
355355
lua.create_function(temp_directory_path)?,
356356
)?;
357-
// exports.set("pairs", lua.create_function(pairs_ctor)?)?;
357+
exports.set("pairs", lua.create_function(|lua, path: LuaFilePath| -> LuaResult<_> {
358+
let table = lua.create_table()?;
359+
for entry in std::fs::read_dir(&path.path)? {
360+
let entry = entry?;
361+
let path = entry.path();
362+
let path = LuaFilePath::new(path.to_str().unwrap_or("").to_string());
363+
table.set(path.clone(), true)?;
364+
}
365+
let next = lua.globals().get::<mlua::Function>("next").unwrap();
366+
Ok((next, table, mlua::Nil))
367+
})?)?;
358368
// exports.set("pairs_r", lua.create_function(pairs_r_ctor)?)?;
359369

360370
Ok(exports)

src/bee/lua_thread.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ impl UserData for LuaChannel {
8888
Ok(())
8989
});
9090

91-
methods.add_async_method("pop", |lua, this, ()| async move {
91+
methods.add_method("pop", |lua, this, ()| {
9292
let id = this.id;
9393
let opt_receiver = { ChannelMgr.lock().unwrap().get_receiver(id) };
9494
if let Some(receiver) = opt_receiver {
95-
let data = receiver.lock().unwrap().recv().await;
96-
if let Some(data) = data {
95+
let data = receiver.lock().unwrap().try_recv();
96+
if let Ok(data) = data {
9797
let lua_seri_unpack = lua.globals().get::<LuaFunction>("lua_seri_unpack")?;
9898
let mut returns = lua_seri_unpack.call::<mlua::MultiValue>(data).unwrap();
9999
returns.insert(0, mlua::Value::Boolean(true));

src/lua_preload.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use mlua::{lua_State, prelude::*};
22
use crate::bee;
33
use crate::lua_seri;
4+
use crate::override_lua;
45

56
extern "C-unwind" {
67
fn luaopen_lpeglabel(lua: *mut lua_State) -> i32;
@@ -50,6 +51,8 @@ pub fn lua_preload(lua: &Lua) -> LuaResult<()> {
5051
// lua_seri
5152
lua_seri::register_lua_seri(&lua)?;
5253

54+
// override
55+
5356
add_package_path(
5457
&lua,
5558
"resources/?.lua;resources/?/init.lua;resources/script/?.lua;resources/script/?/init.lua",

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod bee;
22
mod lua_seri;
33
mod lua_preload;
4+
mod override_lua;
45

56
#[macro_use]
67
extern crate lazy_static;

src/override_lua/mod.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)