示例工程 31-luajit-and-ffi

31-luajit-and-ffi/02-ffi-struct/main.lua

lua · 0.5 KB

-- @runtimes luajit
--- 演示:FFI struct 布局与字段访问。
local M = {}
 
--- @return table { size = integer, x = number, y = number }
function M.run(log)
	log = log or print
	local ffi = require("ffi")
	ffi.cdef("typedef struct { double x; double y; } Point;")
	local point = ffi.new("Point", 1.5, 2.5)
	log(("size=%d x=%.1f y=%.1f"):format(ffi.sizeof("Point"), point.x, point.y))
	return { size = ffi.sizeof("Point"), x = point.x, y = point.y }
end
if not ... then
	M.run()
end
return M