Lua Reference Manual Comments

lua-users home
wiki

此页面包含对 Lua 参考手册 [1](除非另有说明,版本为 5.1)内容的评论和注解。内容可以包括评论、用法示例以及澄清手册内容的链接,包括 API 中特定函数的更高级或微妙的方面。此页面上的内容顺序应大致遵循手册中的内容顺序。另请参阅官方 Lua 5.1 参考手册勘误 [2]

LuaTutorial 中有一些类似的页面,但本页面并非教程,其中一些类似页面的内容不一定以教程格式组织,可能更适合放在这里。本页面在性质和目的上与 MySQL 参考手册中的用户评论相似 [3]

2 - The Language

2.4.5 - For Statement

数字 for 语句中出现的求值列表实际上并非列表,最后一个值会被截断。因此,您不能写 for i = f(x) do ... end,其中 f(x) 返回 2, 4, 1。这与非数字 for 不同,后者不会截断。(由 RiciLake નોંધ)

2.8 - Metatables

此处未列出一些元方法。这些包括 __gc__mode__metatable

请注意,__tostring 仅由 tostring 调用,而在自动转换为字符串时不会调用。__gc 仅在完整的用户数据上调用(这也是在 Lua 中实现的对象的 RAII 方法不起作用的原因之一)。(Beginning Lua Programming 第 268 页有一个关于某些元方法适用性的有用图表。)

3 - The Application Program Interface

lua_getfenv

“索引”指的是堆栈上的索引,而不是堆栈级别(如 getfenv 的情况)。至少有两位用户指出参考手册中这一点不清楚。您可以使用 lua_getinfo"f" 参数来获取给定堆栈级别的函数。

luaL_dostring

通过隐含......发生错误时,将错误推入堆栈并返回错误代码(LUA_ERRSYNTAX 或 LUA_ERRMEM)。成功时,返回 0 并将函数返回值推入堆栈。

如果您不希望返回值堆积在堆栈上,请使用此方法

(luaL_loadstring(L, str) || lua_pcall(L, 0, 0, 0))

luaL_loadfile

通过隐含......发生错误时,将错误推入堆栈并返回错误代码(LUA_ERRSYNTAX、LUA_ERRMEM 或 LUA_ERRFILE)。成功时,返回 0 并将函数返回值推入堆栈。

如果您不希望返回值堆积在堆栈上,请使用此方法

(luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))

luaL_register

此函数在插入表时会调用元方法,但未对此进行文档说明。

5 - Standard Libraries

在“目前,Lua 拥有以下标准库”之后,应该列出“协程操作。”

5.1 - Basic Functions

ipairs

请参阅 GeneralizedPairsAndIpairs,其中提供了一个会调用元方法的重新实现。

next

请参阅 GeneralizedPairsAndIpairs,其中提供了一个会调用元方法的重新实现。

pairs

请参阅 GeneralizedPairsAndIpairs,其中提供了一个会调用元方法的重新实现。

select

尽管未明确说明,但传递给 select 的负数索引表示相对于 ... 末尾的偏移量。超出范围的索引会引发错误。从 luaB_select 中可以看出,这显然是预期的行为。下面提供了测试套件来演示这一点。

function test(...) return select(-1, ...) end
function test2(...) return select(-2, ...) end

assert(not pcall(function() assert(test() == nil) end))
assert(test(1) == 1)
assert(test(1,2) == 2)
assert(test(1,2,3) == 3)

local a,b,c = test2(1,2)
assert(a == 1 and b == 2 and c == nil)
local a,b,c = test2(1,2,3)
assert(a == 2 and b == 3 and c == nil)

xpcall

为什么 xpcall 不像 pcall 那样接受函数参数?以下代码由 RiciLake 建议。

/** better_xpcall(errfunc, func, ...) */
static int l_better_xpcall (lua_State *L) {
  luaL_checktype(L, 1, LUA_TFUNCTION);
  luaL_checkany(L, 2);
  lua_pushboolean(L, 0 == lua_pcall(L, lua_gettop(L)-2, LUA_MULTRET, 1));
  lua_replace(L, 1);
  return lua_gettop(L);
}

请注意,与标准 xpcall 相比,errfuncfunc 参数的顺序已更改。Sergey Rozhenko 的以下代码与标准 xpcall 兼容。

static int luaMy_xpcall (lua_State *L) {
  luaL_checktype(L, 2, LUA_TFUNCTION);

  // switch function & error function
  lua_pushvalue(L, 1);
  lua_pushvalue(L, 2);
  lua_replace(L, 1);
  lua_replace(L, 2);

  // call
  lua_pushboolean(L, 0 == lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET, 1));
  lua_replace(L, 1);
  return lua_gettop(L);
}

5.2 - Coroutine Manipulation

另请参阅 Beginning Lua Programming 中的“Chapter 9: Handling Events Naturally with Coroutines”和“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 9: Coroutines”,或 CoroutinesTutorial

5.3 - Modules

另请参阅 Beginning Lua Programming 中的“Chapter 7: Using Modules”和“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 15: Modules and Packages”,或 ModulesTutorial

5.4 - String Manipulation

gmatchgsub 中的 g 显然代表“global”(全局),意味着会处理所有匹配项,而不仅仅是第一个(Beginning Lua Programming,p.186)。它可能受到 Perl 的 /g“global match”(全局匹配)修饰符的启发 [5]

gsub 这个名称是从 AWK 沿用下来的,早在 Lua 2.5 时期。--lhf

另请参阅 Beginning Lua Programming 中的“Chapter 5: Using Strings”和“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 20: The String Library”,或 LuaTypesTutorial + StringsTutorial + StringLibraryTutorial

string.dump

Beginning Lua Programming(p.302)指出 string.dump 有一个未文档说明的(可能随时更改的)行为:“可以转储带有 upvalues 的函数,但在转储版本中,所有 upvalues 都将对该函数私有(即使原始函数与其他函数共享它们),并且在函数内部赋值之前它们将是 nil。(在 Lua 5.0 中,如果给定一个带有 upvalues 的函数,string.dump 会触发错误。)”

5.5 - Table Manipulation

另请参阅 Beginning Lua Programming 中的“Chapter 4: Working with Tables”和“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 19: The Table Library”,或 TableLibraryTutorial

5.6 - Mathematical Functions

另请参阅 Beginning Lua Programming 中的“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 18: The Mathematical Library”,或 MathLibraryTutorial

math.atan2

[6] [7]

5.7 - Input and Output Facilities

另请参阅 Beginning Lua Programming 中的“Chapter 5: Using Strings”和“Chapter 11: Exploring Lua's Libraries”,或 Programming in Lua, Second Edition [4] 中的“Chapter 21: The I/O Library”。

file:close

另请参阅对应的 C fclose 函数 [8]

file:flush

另请参阅对应的 C fflush 函数 [9]

file:read

.

file:seek

另请参阅对应的 C fseek 函数 [10]

file:setvbuf

另请参阅对应的 C setvfbuf 函数 [11]

file:write

.

io.open

另请参阅对应的 C fopen 函数 [12]。这有助于理解模式。

io.read

文本可以更精确地说“相当于 io.input():read(...)”(而不是“io.input():read”——另请参阅 io.write)。

io.write

文本可以更精确地说“相当于 io.input():write(...)”(而不是“io.input():write”——另请参阅 io.read)。

5.8 - Operating System Facilities

另请参阅 Beginning Lua Programming 中的“Chapter 11: Exploring Lua's Libraries”(重复)和“Chapter 11: Exploring Lua's Libraries”,Programming in Lua, Second Edition [4] 中的“Chapter 22: The Operating System Library”,以及 OsLibraryTutorial

os.clock

另请参阅对应的 C clock 函数 [13]

os.date

另请参阅对应的 C date 函数 [14]

对于可移植的 os.date("%z") 替换,请参阅 TimeZone

os.difftime

另请参阅对应的 C difftime 函数 [15]

os.execute

另请参阅对应的 C system 函数 [16]

在 Windows 中,您可以通过在命令前加上“start ”来通过 os.execute 运行非阻塞进程。在类 Unix 系统中,您可以将命令后缀为“&”。当然,这两种方法都不可移植。示例

os.execute("start notepad")  -- Windows
os.execute("emacs &")  -- UNIX

os.exit

另请参阅对应的 C exit 函数 [17]

os.getenv

另请参阅对应的 C getenv 函数 [18]

Lua 标准库不提供对 POSIX 定义的 C setenv 函数的访问,因为它未在 ASCI C 中定义。

os.remove

另请参阅对应的 C remove 函数 [19]

os.rename

另请参阅对应的 C rename 函数 [20]

os.setlocale

另请参阅对应的 C setlocale 函数 [21] 和 Wikipedia:Locale [22]

各种函数和操作受当前区域设置的影响。这些包括:os.datestring.lowerstring.upper、字符串比较和模式匹配。

os.time

另请参阅对应的 C time 函数 [23]

os.tmpname

另请参阅对应的 C tmpnam 函数 [24]

5.9 - The Debug Library

另请参阅 Beginning Lua Programming 中的“Chapter 11: Exploring Lua's Libraries”或 Programming in Lua, Second Edition [4] 中的“Chapter 23: The Debug Library”。

debug.getfenv

注意:getfenv 函数与 debug.getfenv 不完全相同。

debug.getlocal

相关备注:LuaList:2007-01/msg00214.html

示例:StringInterpolation

debug.getmetatable

此函数类似于 getmetatable,不同之处在于它在检索元表时会忽略 __metatable 元方法。此函数可以替代地称为 rawgetmetatable。由于某些对象可能设置了 __metatable 元方法以防止客户端访问元表(出于安全考虑),您可能希望阻止访问 debug.getmetatable

debug.setmetatable

此函数类似于 setmetatable,但它可以作用于表以外的对象。您甚至可以将其用于 nil

未文档说明的注意事项: 该函数在成功时返回 true,在失败时返回 false,尽管目前似乎总是返回 true。(注意:未文档说明的行为在将来可能会更改。)

7 - Incompatibilities with the Previous Version

另请参阅 MigratingToFiveOneLuaFiveFeaturesLuaFiveAlphaToBeta

8 - The Complete Syntax of Lua

另请参阅 LuaGrammar

其他作者笔记和用户评论

我认为“3. The Application Program Interface”一章最好放在后面,这样那些仅通过 Lua 语言而不是 C 使用 Lua 的人(这包括大多数用户)就不必过多地关注它了。这是一个更高级的主题。--DavidManura

5.1 手册的替代浏览器:我不喜欢 Lua 手册全部都在一页上的方式,因此我为自己制作了一个小浏览器/搜索器,我一直都在使用它,并且想与他人分享:[Luai] --McFin

以上页面已对照 5.1.3 参考手册进行了审查。--DavidManura


RecentChanges · preferences
编辑 · 历史
最后编辑时间:2010 年 11 月 5 日上午 4:04 GMT (差异)