📜  Lua-标准库

📅  最后修改于: 2020-10-16 05:20:54             🧑  作者: Mango


Lua标准库提供了丰富的功能集,这些功能可以直接通过C API实现,并且是用Lua编程语言内置的。这些库提供Lua编程语言内的服务,以及文件和db操作之类的外部服务。

内置在官方C API中的这些标准库作为单独的C模块提供。它包括以下内容-

  • 基本库,其中包括协程子库
  • 模块库
  • 字符串操作
  • 表操作
  • 数学库
  • 文件输入输出
  • 操作系统设施
  • 调试工具

基础图书馆

在整个教程中,我们在各种主题下使用了基础库。下表提供了相关页面的链接,并列出了本Lua教程各个部分所涵盖的功能。

Sr.No. Library / Method & Purpose
1

Error Handling

Includes error handling functions like assert, error as explained in Lua – Error Handling.

2

Memory Management

Includes the automatic memory management functions related to garbage collection as explained in Lua – Garbage Collection.

3

dofile ([filename])

It opens the file and executes the contents of the file as a chunk. If no parameter is passed, then this function executes the contents of standard input. The errors will be propagated to the caller.

4

_G

Thus is the global variable that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable.

5

getfenv ([f])

Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level − Level 1 is the function calling getfenv. If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.

6

getmetatable (object)

If object does not have a metatable, returns nil. Otherwise, if the object’s metatable has a “__metatable” field, returns the associated value. Otherwise, returns the metatable of the given object.

7

ipairs (t)

This functions fetches the indices and values of tables.

8

load (func [, chunkname])

Loads a chunk using function func to get its pieces. Each call to func must return a string that concatenates with previous results.

9

loadfile ([filename]))

Similar to load, but gets the chunk from file filename or from the standard input, if no file name is given.

10

loadstring (string [, chunkname])

Similar to load, but gets the chunk from the given string.

11

next (table [, index])

Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. next returns the next index of the table and its associated value.

12

pairs (t)

Suspends the running coroutine. The parameter passed to this method acts as additional return values to the resume function.

13

print (…)

Suspends the running coroutine. The parameter passed to this method acts as additional return values to the resume function.

14

rawequal (v1, v2)

Checks whether v1 is equal to v2, without invoking any metamethod. Returns a boolean.

15

rawget (table, index)

Gets the real value of table[index], without invoking any metamethod. table must be a table; index may be any value.

16

rawset (table, index, value)

Sets the real value of table[index] to value, without invoking any metamethod. table must be a table, index any value different from nil, and value any Lua value. This function returns table.

17

select (index, …)

If index is a number, returns all arguments after argument number index. Otherwise, index must be the string “#”, and select returns the total number of extra arguments it received.

18

setfenv (f, table)

Sets the environment to be used by the given function. f can be a Lua function or a number that specifies the function at that stack level − Level 1 is the function calling setfenv. setfenv returns the given function. As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values.

19

setmetatable (table, metatable)

Sets the metatable for the given table. (You cannot change the metatable of other types from Lua, only from C.) If metatable is nil, removes the metatable of the given table. If the original metatable has a “__metatable” field, raises an error. This function returns table.

20

tonumber (e [, base])

Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.

21

tostring (e)

Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format.

22

type (v)

Returns the type of its only argument, coded as a string. The possible results of this function are “nil” (a string, not the value nil), “number”, “string”, “boolean”, “table”, “function”, “thread”, and “userdata”.

23

unpack (list [, i [, j]])

Returns the elements from the given table.

24

_VERSION

A global variable (not a function) that holds a string containing the current interpreter version. The current contents of this variable is “Lua 5.1”.

25

Coroutines

Includes the coroutine manipulation functions as explained in Lua – Coroutines.

模块库

模块库提供了在Lua中加载模块的基本功能。它直接在全局环境中导出一项函数:要求。其他所有内容都以表包的形式导出。有关模块库的详细信息在上一章“ Lua-模块”教程中进行了说明。

字符串操作

Lua提供了丰富的字符串操作函数集。早期的Lua-Strings教程对此进行了详细介绍。

表操作

Lua几乎所有操作都依赖于表。先前的Lua-Tables教程详细介绍了此内容。

文件输入输出

我们在编程中经常需要数据存储工具,这是由Lua中用于文件I / O的标准库函数提供的。在更早的Lua-文件I / O教程中对此进行了讨论。

调试工具

Lua提供了一个调试库,该库提供了所有原始函数供我们创建自己的调试器。在更早的Lua-调试教程中对此进行了讨论。