Skip to content

Commit

Permalink
Add comments to luv_check_{handle,stream} explaining how they work (#602
Browse files Browse the repository at this point in the history
)
  • Loading branch information
squeek502 committed May 31, 2022
1 parent c51e705 commit 26401e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ static uv_handle_t* luv_check_handle(lua_State* L, int index) {
if (!(udata = lua_touserdata(L, index))) { goto fail; }
if (!(handle = *(uv_handle_t**) udata)) { goto fail; }
if (!handle->data) { goto fail; }
// "uv_handle" in the registry is a table structured like so:
// {
// [<uv_aync metatable>] = true,
// [<uv_check metatable>] = true,
// ...
// }
// so to check that the value at the index is a "uv_handle",
// we get its metatable and check that we get `true` back
// when looking the metatable up in the "uv_handle" table.
lua_getfield(L, LUA_REGISTRYINDEX, "uv_handle");
lua_getmetatable(L, index < 0 ? index - 1 : index);
lua_rawget(L, -2);
Expand Down
9 changes: 9 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ static uv_stream_t* luv_check_stream(lua_State* L, int index) {
if (!(udata = lua_touserdata(L, index))) { goto fail; }
if (!(handle = *(uv_stream_t**) udata)) { goto fail; }
if (!handle->data) { goto fail; }
// "uv_stream" in the registry is a table structured like so:
// {
// [<uv_pipe metatable>] = true,
// [<uv_tcp metatable>] = true,
// [<uv_tty metatable>] = true,
// }
// so to check that the value at the index is a "uv_stream",
// we get its metatable and check that we get `true` back
// when looking the metatable up in the "uv_stream" table.
lua_getfield(L, LUA_REGISTRYINDEX, "uv_stream");
lua_getmetatable(L, index < 0 ? index - 1 : index);
lua_rawget(L, -2);
Expand Down

0 comments on commit 26401e1

Please sign in to comment.