math
-- math.abs
local x = -5
local abs_x = math.abs(x) -- 计算 x 的绝对值
print(abs_x) -- 输出 5-- math.acos
local x = 0.5
local acos_x = math.acos(x) -- 计算 x 的反余弦值
print(acos_x) -- 输出 1.0471975511966 (弧度)-- math.asin
local x = 0.5
local asin_x = math.asin(x) -- 计算 x 的反正弦值
print(asin_x) -- 输出 0.5235987755983 (弧度)-- math.atan
local x = 1
local atan_x = math.atan(x) -- 计算 x 的反正切值
print(atan_x) -- 输出 0.78539816339745 (弧度)-- math.atan2
local y, x = 2, 2
local atan2_yx = math.atan2(y, x) -- 计算 y/x 的反正切值
print(atan2_yx) -- 输出 0.78539816339745 (弧度)-- math.ceil
local x = 5.3
local ceil_x = math.ceil(x) -- 对 x 向上取整
print(ceil_x) -- 输出 6-- math.cos
local angle = math.pi / 3
local cos_angle = math.cos(angle) -- 计算角度的余弦值
print(cos_angle) -- 输出 0.5-- math.cosh
local x = 1
local cosh_x = math.cosh(x) -- 计算 x 的双曲余弦值
print(cosh_x) -- 输出 1.5430806348152-- math.deg
local rad = math.pi / 3
local deg = math.deg(rad) -- 将弧度转换为角度
print(deg) -- 输出 60-- math.exp
local x = 1
local exp_x = math.exp(x) -- 计算 e 的 x 次幂
print(exp_x) -- 输出 2.718281828459-- math.floor
local x = 5.7
local floor_x = math.floor(x) -- 对 x 向下取整
print(floor_x) -- 输出 5-- math.fmod
local x, y = 7, 3
local remainder = math.fmod(x, y) -- 计算 x 除以 y 的余数
print(remainder) -- 输出 1-- math.frexp
local x = 8
local m, e = math.frexp(x) -- 将 x 分解为尾数 m 和指数 e
print(m, e) -- 输出 0.5, 4-- math.huge
local huge_num = math.huge -- 表示正无穷大
print(huge_num) -- 输出 inf-- math.ldexp
local m, e = 0.5, 4
local x = math.ldexp(m, e) -- 根据尾数 m 和指数 e 计算结果
print(x) -- 输出 8-- math.log
local x = 8
local log_x = math.log(x) -- 计算 x 的自然对数
print(log_x) -- 输出 2.0794415416798-- math.max
local max_val = math.max(5, 8, 1, 3, 7) -- 计算参数中的最大值
print(max_val) -- 输出 8-- math.maxinteger
local max_int = math.maxinteger -- 表示整数的最大值
print(max_int) -- 输出 9223372036854775807-- math.min
local min_val = math.min(5, 8, 1, 3, 7) -- 计算参数中的最小值
print(min_val) -- 输出 1-- math.mininteger
local min_int = math.mininteger -- 表示整数的最小值
print(min_int) -- 输出 -9223372036854775808-- math.modf
local x = 5.7
local integral, fractional = math.modf(x) -- 将 x 分解为整数部分和小数部分
print(integral, fractional) -- 输出 5, 0.7-- math.pi
local pi_val = math.pi -- 圆周率的近似值
print(pi_val) -- 输出 3.1415926535898-- math.rad
local deg = 60
local rad = math.rad(deg) -- 将角度转换为弧度
print(rad) -- 输出 1.0471975511966-- math.random
local random_num = math.random(1, 10) -- 生成一个 [1, 10] 区间内的随机整数
print(random_num)-- math.randomseed
math.randomseed(os.time()) -- 使用当前时间作为随机数种子-- math.sin
local angle = math.pi / 3
local sin_angle = math.sin(angle) -- 计算角度的正弦值
print(sin_angle) -- 输出 0.86602540378444-- math.sinh
local x = 1
local sinh_x = math.sinh(x) -- 计算 x 的双曲正弦值
print(sinh_x) -- 输出 1.1752011936438-- math.sqrt
local x = 9
local sqrt_x = math.sqrt(x) -- 计算 x 的平方根
print(sqrt_x) -- 输出 3-- math.tan
local angle = math.pi / 4
local tan_angle = math.tan(angle) -- 计算角度的正切值
print(tan_angle) -- 输出 0.99999999999999-- math.tanh
local x = 1
local tanh_x = math.tanh(x) -- 计算 x 的双曲正切值
print(tanh_x) -- 输出 0.76159415595576-- math.tointeger
local x = 5.7
local int_x = math.tointeger(x) -- 将 x 转换为整数(如果 x 是整数,则返回 x,否则返回 nil)
print(int_x) -- 输出 nil-- math.type
local x = 5.7
local y = 3
local type_x = math.type(x) -- 检查 x 的类型("float" 或 "integer")
local type_y = math.type(y) -- 检查 y 的类型("float" 或 "integer")
print(type_x) -- 输出 "float"
print(type_y) -- 输出 "integer"-- math.ult
local x, y = 3, 5
local result = math.ult(x, y) -- 检查 x 是否严格小于 y(无符号整数比较)
print(result) -- 输出 true
string
-- string.byte
local str = "hello"
local byte_val = string.byte(str, 1) -- 获取第 1 个字符的 ASCII 码
print(byte_val) -- 输出 104,即 "h" 的 ASCII 码-- string.char
local char_val = string.char(104) -- 根据 ASCII 码获取字符
print(char_val) -- 输出 "h"-- string.dump
local function example() print("Hello, World!") end
local binary_str = string.dump(example) -- 获取函数的二进制表示
print(binary_str)-- string.find
local s = "hello, world"
local start_pos, end_pos = string.find(s, "world") -- 在字符串中查找子串 "world"
print(start_pos, end_pos) -- 输出 8 12-- string.format
local formatted_str = string.format("Hello, %s! You are %d years old.", "John", 25) -- 格式化字符串
print(formatted_str) -- 输出 "Hello, John! You are 25 years old."-- string.gmatch
local s = "I am learning Lua"
for word in string.gmatch(s, "%a+") do -- 使用迭代器函数逐个提取单词print(word)
end-- string.gsub
local s = "The quick brown dog jumps over the lazy dog"
local new_s = string.gsub(s, "dog", "fox") -- 将所有 "dog" 替换为 "fox"
print(new_s) -- 输出 "The quick brown fox jumps over the lazy fox"-- string.len
local s = "hello"
local length = string.len(s) -- 获取字符串长度
print(length) -- 输出 5-- string.lower
local s = "HELLO"
local lower_s = string.lower(s) -- 将字符串转换为小写
print(lower_s) -- 输出 "hello"-- string.match
local s = "My phone number is 123-456-7890"
local phone_number = string.match(s, "%d%d%d%-%d%d%d%-%d%d%d%d") -- 提取电话号码
print(phone_number) -- 输出 "123-456-7890"-- string.pack
local packed_data = string.pack(">i4", 42) -- 以大端序整数格式打包数据
print(packed_data)-- string.packsize
local size = string.packsize(">i4") -- 获取大端序整数格式的数据大小
print(size) -- 输出 4-- string.rep
local s = "hello"
local repeated_s = string.rep(s, 3, " ") -- 重复字符串 3 次,每次之间用空格分隔
print(repeated_s) -- 输出 "hello hello hello"-- string.reverse
local s = "hello"
local reversed_s = string.reverse(s) -- 反转字符串
print(reversed_s) -- 输出 "olleh"-- string.sub
local s = "hello, world"
local sub_s = string.sub(s, 1, 5) -- 截取子串,从第 1 个字符到第 5 个字符
print(sub_s) -- 输出 "hello"-- string.unpack
local packed_data = string.pack(">i4", 42)
local unpacked_data = string.unpack(">i4", packed_data) -- 从打包数据中解包整数
print(unpacked_data) -- 输出 "42"-- string.upper
local s = "hello"
local upper_s = string.upper(s) -- 将字符串转换为大写
print(upper_s) -- 输出 "HELLO"
table
-- table.concat
local t = {"apple", "banana", "cherry"}
local joined_str = table.concat(t, ", ") -- 将表中的元素连接成一个字符串,以 ", " 分隔
print(joined_str) -- 输出 "apple, banana, cherry"-- table.insert
local t = {10, 20, 30}
table.insert(t, 2, 15) -- 在表的第 2 个位置插入元素 15
for _, v in ipairs(t) do print(v) end -- 输出 10, 15, 20, 30-- table.move
local t1 = {1, 2, 3, 4, 5}
local t2 = {6, 7, 8, 9, 10}
table.move(t1, 1, 5, 6, t2) -- 将 t1 的前 5 个元素移动到 t2 的第 6 个位置开始
for _, v in ipairs(t2) do print(v) end -- 输出 6, 7, 8, 9, 10, 1, 2, 3, 4, 5-- table.pack
local t = table.pack("apple", "banana", "cherry") -- 将多个值打包成一个表
for _, v in ipairs(t) do print(v) end -- 输出 "apple", "banana", "cherry"-- table.remove
local t = {"apple", "banana", "cherry"}
table.remove(t, 2) -- 移除表中第 2 个元素
for _, v in ipairs(t) do print(v) end -- 输出 "apple", "cherry"-- table.sort
local t = {5, 8, 1, 3, 7}
table.sort(t) -- 将表中的元素进行升序排序
for _, v in ipairs(t) do print(v) end -- 输出 1, 3, 5, 7, 8-- table.unpack
local t = {"apple", "banana", "cherry"}
local a, b, c = table.unpack(t) -- 解包表中的元素,将它们赋值给变量
print(a, b, c) -- 输出 "apple", "banana", "cherry"
metamethods
__add:定义加法操作符的行为。
local mt = {__add = function(a, b)return {value = a.value + b.value}end
}
local a = {value = 10}
local b = {value = 20}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a + b
print(result.value) -- 输出 30
__band:定义按位与操作符的行为。
local mt = {__band = function(a, b)return {value = a.value & b.value}end
}
local a = {value = 0x0F}
local b = {value = 0x33}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a & b
print(result.value) -- 输出 3
__bnot:定义按位非操作符的行为。
local mt = {__bnot = function(a)return {value = ~a.value}end
}
local a = {value = 0x0F}
setmetatable(a, mt)
local result = ~a
print(result.value) -- 输出 -16
__bor:定义按位或操作符的行为。
local mt = {__bor = function(a, b)return {value = a.value | b.value}end
}
local a = {value = 0x0F}
local b = {value = 0x33}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a | b
print(result.value) -- 输出 63
__bxor:定义按位异或操作符的行为。
local mt = {__bxor = function(a, b)return {value = a.value ~ b.value}end
}
local a = {value = 0x0F}
local b = {value = 0x33}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a ~ b
print(result.value) -- 输出 60
__call:定义调用表作为函数时的行为。
local mt = {__call = function(t, arg)return t.value * argend
}
local a = {value = 10}
setmetatable(a, mt)
local result = a(5) -- 调用 a 作为函数
print(result) -- 输出 50
__close:在 Lua 5.4 中引入,定义在执行器(finalizer)中调用的函数。
local mt = {__close = function(t)print("Closing", t.value)end
}
local function newResource(value)return setmetatable({value = value}, mt)
end
local function test()local r = newResource("Resource 1")return r
end
local resource = test() -- 在离开 test 函数作用域时调用 __close 方法
__concat:定义字符串连接操作符的行为。
local mt = {__concat = function(a, b)return a.value .. b.valueend
}
local a = {value = "Hello "}
local b = {value = "World!"}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a .. b
print(result) -- 输出 "Hello World!"
__div:定义除法操作符的行为。
local mt = {__div = function(a, b)return {value = a.value / b.value}end
}
local a = {value = 10}
local b = {value = 2}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a / b
print(result.value) -- 输出 5
__eq:定义相等操作符的行为。
local mt = {__eq = function(a, b)return a.value == b.valueend
}
local a = {value = 10}
local b = {value = 10}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a == b
print(result) -- 输出 true
__gc:定义垃圾回收器操作的行为,当对象被垃圾回收器回收时调用。
local mt = {__gc = function(t)print("Object with value", t.value, "is being collected.")end
}
local function createObject(value)local obj = {value = value}setmetatable(obj, mt)
end
createObject(42) -- 在垃圾回收时调用 __gc 方法
collectgarbage() -- 强制进行垃圾回收
__idiv:定义整数除法操作符的行为。
local mt = {__idiv = function(a, b)return {value = a.value // b.value}end
}
local a = {value = 10}
local b = {value = 3}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a // b
print(result.value) -- 输出 3
__index:定义访问表中不存在的键时的行为。
local mt = {__index = function(t, key)return "default"end
}
local a = {}
setmetatable(a, mt)
print(a.some_key) -- 输出 "default"
__le:定义小于等于操作符的行为。
local mt = {__le = function(a, b)return a.value <= b.valueend
}
local a = {value = 10}
local b = {value = 20}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a <= b
print(result) -- 输出 true
__len:定义求表长度操作符的行为。
local mt = {__len = function(t)return t.valueend
}
local a = {value = 5}
setmetatable(a, mt)
local length = #a
print(length) -- 输出 5
__lt:定义小于操作符的行为。
local mt = {__lt = function(a, b)return a.value < b.valueend
}
local a = {value = 10}
local b = {value = 20}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a < b
print(result) -- 输出 true
__metatable:定义获取元表的行为,以防止用户访问或修改实际的元表。
local mt = {__metatable = "This metatable is locked."
}
local a = {}
setmetatable(a, mt)
local a_mt = getmetatable(a)
print(a_mt) -- 输出 "This metatable is locked."
__mod:定义取模操作符的行为。
local mt = {__mod = function(a, b)return {value = a.value % b.value}end
}
local a = {value = 10}
local b = {value = 3}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a % b
print(result.value) -- 输出 1
__mode:定义弱表的行为,可以设置为 “k”、“v” 或 “kv”,表示表中的键、值或键值都是弱引用。
local mt = {__mode = "v"}
local cache = setmetatable({}, mt)
local function getFromCache(key)local value = cache[key]if not value thenvalue = key .. " cached"cache[key] = valueendreturn value
end
getFromCache("test") -- 在垃圾回收时,值将被释放
collectgarbage()
__mul:定义乘法操作符的行为。
local mt = {__mul = function(a, b)return {value = a.value * b.value}end
}
local a = {value = 10}
local b = {value = 2}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a * b
print(result.value) -- 输出 20
__name:在 Lua 5.3 中引入,用于定义类型名。
local mt = {__name = "MyType"
}
local a = setmetatable({}, mt)
local typeName = tostring(a)
print(typeName) -- 输出 "MyType: <address>"
__newindex:定义设置表中不存在的键时的行为。
local mt = {__newindex = function(t, key, value)print("Setting", key, "to", value)end
}
local a = {}
setmetatable(a, mt)
a.some_key = "value" -- 输出 "Setting some_key to value"
__pairs:定义使用 pairs() 函数遍历表时的行为。
local mt = {__pairs = function(t)local function iter(t, key)local nextKey, nextValue = next(t, key)if nextKey thenreturn nextKey, nextValueendendreturn iter, t, nilend
}
local a = {a = 1, b = 2, c = 3}
setmetatable(a, mt)
for k, v in pairs(a) doprint(k, v)
end
__pow:定义幂运算操作符的行为。
local mt = {__pow = function(a, b)return {value = a.value ^ b.value}end
}
local a = {value = 2}
local b = {value = 3}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a ^ b
print(result.value) -- 输出 8
__shl:定义按位左移操作符的行为。
local mt = {__shl = function(a, b)return {value = a.value << b.value}end
}
local a = {value = 0x0F}
local b = {value = 1}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a << b
print(result.value) -- 输出 30
__shr:定义按位右移操作符的行为。
local mt = {__shr = function(a, b)return {value = a.value >> b.value}end
}
local a = {value = 0x0F}
local b = {value = 1}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a >> b
print(result.value) -- 输出 7
__sub:定义减法操作符的行为。
local mt = {__sub = function(a, b)return {value = a.value - b.value}end
}
local a = {value = 10}
local b = {value = 2}
setmetatable(a, mt)
setmetatable(b, mt)
local result = a - b
print(result.value) -- 输出 8
__tostring:定义将表转换为字符串时的行为。
local mt = {__tostring = function(t)return "Object with value: " .. t.valueend
}
local a = {value = 10}
setmetatable(a, mt)
local str = tostring(a)
print(str) -- 输出 "Object with value: 10"
__unm:定义一元负操作符的行为。
local mt = {__unm = function(a)return {value = -a.value}end
}
local a = {value = 10}
setmetatable(a, mt)
local result = -a
print(result.value) -- 输出 -10