Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments to luv_check_{handle,stream} explaining how they work #602

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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