Skip to content

Commit

Permalink
lib: luvit: bind to new uv_ref/uv_unref signatures
Browse files Browse the repository at this point in the history
uv_ref and uv_unref now work on uv_handle_t not uv_loop_t so replace all
calls to native.unref and native.ref with Stream/handle operations.

This is a WIP. It simply gets the luvit repl working again- tests do not
pass. Also, in order for this to work on OSX it needs a libuv PR

    https://github.com/joyent/libuv/pull/491/files
  • Loading branch information
philips committed Jul 10, 2012
1 parent bf4e8ac commit b5320f9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 31 deletions.
8 changes: 4 additions & 4 deletions lib/luvit/luvit.lua
Expand Up @@ -151,11 +151,11 @@ OS_BINDING.clock = OLD_OS.clock
-- These shouldn't hold open the event loop
if OS_BINDING.type() ~= "win32" then
native.activateSignalHandler(constants.SIGPIPE)
native.unref()
-- native.unref()
native.activateSignalHandler(constants.SIGINT)
native.unref()
-- native.unref()
native.activateSignalHandler(constants.SIGTERM)
native.unref()
-- native.unref()
end
local traceback = require('debug').traceback
Expand Down Expand Up @@ -306,7 +306,7 @@ assert(xpcall(function ()

if interactive or showrepl then
if OS_BINDING.type() == "win32" then
native.ref()
--native.ref()
end
repl.start()
end
Expand Down
32 changes: 19 additions & 13 deletions lib/luvit/uv.lua
Expand Up @@ -49,6 +49,12 @@ uv.Stream = Stream
-- Stream:shutdown(callback)
Stream.shutdown = native.shutdown

-- Stream:ref()
Stream.ref = native.ref

-- Stream:unref()
Stream.unref = native.unref

-- Stream:listen(callback)
Stream.listen = native.listen

Expand Down Expand Up @@ -172,17 +178,17 @@ Pipe.bind = native.pipeBind
Pipe.connect = native.pipeConnect

function Pipe:pause()
native.unref()
self:unref()
self:readStop()
end

function Pipe:pauseNoRef()
native.unref()
self:unref()
self:readStopNoRef()
end

function Pipe:resume()
native.ref()
self:ref()
self:readStart()
end

Expand All @@ -204,19 +210,19 @@ Tty.getWinsize = native.ttyGetWinsize
Tty.resetMode = native.ttyResetMode

function Tty:pause()
native.unref()
self:unref()
self:readStop()
end

-- TODO: The readStop() implementation assumes a reference is being held. This
-- will go away with a libuv upgrade.
function Tty:pauseNoRef()
native.unref()
self:unref()
self:readStopNoRef()
end

function Tty:resume()
native.ref()
self:ref()
self:readStart()
end

Expand All @@ -232,16 +238,16 @@ function Timer:initialize()
-- uv_timer_init adds a loop reference. (That is, it calls uv_ref.) This
-- is not the behavior we want in Luvit. Timers should not increase the
-- ref count of the loop except when active.
native.unref()
self:unref()
end

function Timer:_update()
local was_active = self._active
self._active = native.timerGetActive(self)
if was_active == false and self._active == true then
native.ref()
self:ref()
elseif was_active == true and self._active == false then
native.unref()
self:unref()
end
end

Expand All @@ -254,7 +260,7 @@ end
function Timer:close()
Handle.close(self)
if self._active == false then
native.ref()
self:ref()
end
end

Expand Down Expand Up @@ -288,14 +294,14 @@ uv.createWriteableStdioStream = function(fd)
local fd_type = native.handleType(fd);
if (fd_type == "TTY") then
local tty = Tty:new(fd)
native.unref()
tty:unref()
return tty
elseif (fd_type == "FILE") then
return fs.SyncWriteStream:new(fd)
elseif (fd_type == "NAMED_PIPE") then
local pipe = Pipe:new(nil)
pipe:open(fd)
native.unref()
pipe:unref()
return pipe
else
error("Unknown stream file type " .. fd)
Expand All @@ -318,7 +324,7 @@ uv.createReadableStdioStream = function(fd)

-- unref the event loop so that we don't block unless the user
-- wants stdin. This follows node's logic.
stdin:pauseNoRef()
-- stdin:pauseNoRef()

return stdin
end
Expand Down
2 changes: 2 additions & 0 deletions src/luv.c
Expand Up @@ -38,6 +38,8 @@ static const luaL_reg luv_f[] = {

/* Handle functions */
{"close", luv_close},
{"ref", luv_ref},
{"unref", luv_unref},
{"setHandler", luv_set_handler},

/* UDP functions */
Expand Down
12 changes: 12 additions & 0 deletions src/luv_handle.c
Expand Up @@ -89,6 +89,18 @@ int luv_close (lua_State* L) {
return 0;
}

int luv_ref(lua_State* L) {
uv_handle_t* handle = luv_checkudata(L, 1, "handle");
uv_ref(handle);
return 0;
}

int luv_unref(lua_State* L) {
uv_handle_t* handle = luv_checkudata(L, 1, "handle");
uv_unref(handle);
return 0;
}

int luv_set_handler(lua_State* L) {
const char* name;
luv_checkudata(L, 1, "handle");
Expand Down
3 changes: 3 additions & 0 deletions src/luv_handle.h
Expand Up @@ -39,4 +39,7 @@ void luv_on_close(uv_handle_t* handle);
int luv_close (lua_State* L);
int luv_set_handler(lua_State* L);

int luv_ref(lua_State* L);
int luv_unref(lua_State* L);

#endif
10 changes: 0 additions & 10 deletions src/luv_misc.c
Expand Up @@ -216,16 +216,6 @@ int luv_run(lua_State* L) {
return 0;
}

int luv_ref (lua_State* L) {
uv_ref(luv_get_loop(L));
return 0;
}

int luv_unref(lua_State* L) {
uv_unref(luv_get_loop(L));
return 0;
}

int luv_update_time(lua_State* L) {
uv_update_time(luv_get_loop(L));
return 0;
Expand Down
2 changes: 0 additions & 2 deletions src/luv_misc.h
Expand Up @@ -25,8 +25,6 @@

int luv_activate_signal_handler(lua_State* L);
int luv_run(lua_State* L);
int luv_ref (lua_State* L);
int luv_unref(lua_State* L);
int luv_update_time(lua_State* L);
int luv_now(lua_State* L);
int luv_hrtime(lua_State* L);
Expand Down
2 changes: 2 additions & 0 deletions src/luv_process.c
Expand Up @@ -103,9 +103,11 @@ int luv_spawn(lua_State* L) {

options.env = env ? env : luv_os_environ();
options.cwd = cwd;
#if 0
options.stdin_stream = stdin_stream;
options.stdout_stream = stdout_stream;
options.stderr_stream = stderr_stream;
#endif

/* Create the userdata */
handle = luv_create_process(L);
Expand Down
2 changes: 0 additions & 2 deletions src/utils.c
Expand Up @@ -158,8 +158,6 @@ const char* luv_handle_type_to_string(uv_handle_type type) {
case UV_CHECK: return "CHECK";
case UV_IDLE: return "IDLE";
case UV_ASYNC: return "ASYNC";
case UV_ARES_TASK: return "ARES_TASK";
case UV_ARES_EVENT: return "ARES_EVENT";
case UV_PROCESS: return "PROCESS";
case UV_FS_EVENT: return "FS_EVENT";
default: return "UNKNOWN_HANDLE";
Expand Down

0 comments on commit b5320f9

Please sign in to comment.