summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-23 00:52:43 +0800
committerruki <[email protected]>2022-07-23 00:52:43 +0800
commitbf3a38003ae5b93010dd0d8b70155bf1b1ee305a (patch)
treef924fe448c67d490acc0af6b180496efb6bb916e
parent6de3254bd605d481b07b4f498b58c4896752089b (diff)
impl fwatcher.wait
-rw-r--r--core/src/xmake/fwatcher/wait.c23
-rw-r--r--core/src/xmake/io/poller_wait.c17
-rw-r--r--xmake/core/base/fwatcher.lua7
-rw-r--r--xmake/core/sandbox/modules/import/core/base/fwatcher.lua5
4 files changed, 48 insertions, 4 deletions
diff --git a/core/src/xmake/fwatcher/wait.c b/core/src/xmake/fwatcher/wait.c
index 66bc0eed4..f8bbd6b78 100644
--- a/core/src/xmake/fwatcher/wait.c
+++ b/core/src/xmake/fwatcher/wait.c
@@ -48,7 +48,26 @@ tb_int_t xm_fwatcher_wait(lua_State* lua)
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(fwatcher, 0);
- // save result: ok
- lua_pushboolean(lua, tb_true);
+ // get the timeout
+ tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2);
+
+ // wait fwatcher event
+ tb_fwatcher_event_t event;
+ tb_long_t ok = tb_fwatcher_wait(fwatcher, &event, timeout);
+
+ // save result
+ lua_pushinteger(lua, ok);
+ if (ok > 0)
+ {
+ lua_newtable(lua);
+ lua_pushstring(lua, "path");
+ lua_pushstring(lua, event.filepath);
+ lua_settable(lua, -3);
+
+ lua_pushstring(lua, "type");
+ lua_pushinteger(lua, event.event);
+ lua_settable(lua, -3);
+ return 2;
+ }
return 1;
}
diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c
index 008f1366e..7389faacf 100644
--- a/core/src/xmake/io/poller_wait.c
+++ b/core/src/xmake/io/poller_wait.c
@@ -54,7 +54,22 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref
if (priv) lua_pushstring(g_lua, (tb_char_t const*)priv);
else lua_pushlightuserdata(g_lua, object->ref.ptr);
lua_rawseti(g_lua, -2, 2);
- lua_pushinteger(g_lua, (tb_int_t)events);
+ if (object->type == TB_POLLER_OBJECT_FWATCHER)
+ {
+ lua_newtable(g_lua);
+ tb_fwatcher_event_t* event = (tb_fwatcher_event_t*)events;
+ if (event)
+ {
+ lua_pushstring(g_lua, "path");
+ lua_pushstring(g_lua, event->filepath);
+ lua_settable(g_lua, -3);
+
+ lua_pushstring(g_lua, "type");
+ lua_pushinteger(g_lua, event->event);
+ lua_settable(g_lua, -3);
+ }
+ }
+ else lua_pushinteger(g_lua, (tb_int_t)events);
lua_rawseti(g_lua, -2, 3);
lua_rawseti(g_lua, -2, ++g_events_count);
}
diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua
index cec65f02e..9de827ae0 100644
--- a/xmake/core/base/fwatcher.lua
+++ b/xmake/core/base/fwatcher.lua
@@ -34,6 +34,11 @@ fwatcher._remove = fwatcher._remove or fwatcher.remove
fwatcher._wait = fwatcher._wait or fwatcher.wait
fwatcher._close = fwatcher._close or fwatcher.close
+-- the fwatcher event type, @see tbox/platform/fwatcher.h
+fwatcher.ET_MODIFY = 1
+fwatcher.ET_CREATE = 2
+fwatcher.ET_DELETE = 4
+
-- get cdata of fwatcher
function _instance:cdata()
local cdata = self._CDATA
@@ -89,7 +94,7 @@ end
--
-- @param timeout the timeout
--
--- @return ok, event
+-- @return ok, event, e.g {type = fwatcher.ET_MODIFY, path = "/tmp"}
--
function _instance:wait(timeout)
diff --git a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua
index bf8710b00..af3aa4151 100644
--- a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua
+++ b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua
@@ -25,6 +25,11 @@ local sandbox_core_base_fwatcher = sandbox_core_base_fwatcher or {}
local fwatcher = require("base/fwatcher")
local raise = require("sandbox/modules/raise")
+-- the fwatcher event type
+sandbox_core_base_fwatcher.ET_MODIFY = fwatcher.ET_MODIFY
+sandbox_core_base_fwatcher.ET_CREATE = fwatcher.ET_CREATE
+sandbox_core_base_fwatcher.ET_DELETE = fwatcher.ET_DELETE
+
-- add watch directory
function sandbox_core_base_fwatcher.add(watchdir, opt)
local ok, errors = fwatcher.add(watchdir, opt)