userdata#
A userdata value is a block of memory owned by C code,
exposed to Lua through the C API. The operator meets userdata
inside hosts that wrap their own objects: a Wireshark Pinfo,
an Nmap Target, a LuaSocket socket, an OpenResty
request.
The operator does not create userdata in pure Lua; they call into the host and treat the returned value as opaque.
local socket = require("socket")
local s = socket.tcp() -- s is a userdata
print(type(s)) --> userdata
Userdata can carry a metatable (set by the host) so it behaves like an object with methods.
s:connect("example.com", 80)
s:send("GET / HTTP/1.0\r\n\r\n")
s:close()