省略Lua的标准库的最佳方式? [英] Best way to omit Lua standard libraries?
问题描述
什么是删除或省略一个Lua标准库包的最佳方式?例如删除 OS库函数在某一特定环境的。有问题的项目从源文件建立的Lua,所以我的可以编辑源,虽然我宁愿做它通过如果可能的话API。
What is the best way to remove or omit a Lua standard library package? For example remove the os library functions in a particular environment. The project in question is building Lua from the source files so I can edit the source, although I would rather do it through the API if possible.
推荐答案
请查看源代码包文件 luaconf.h
容易获得大多数编译时配置,作为用于 lua_Number
的实际类型。
See the file luaconf.h
in the source kit for easy access to most compile-time configuration such as the actual type used for lua_Number
.
请查看源代码包文件 linit.c
对于那些通过调用 luaL_openlibs()$ C加载的核心库列表$ C>。
See the file linit.c
in the source kit for the list of core libraries that are loaded by calling luaL_openlibs()
.
通常的做法是将该文件复制到你的应用程序的源代码,并修改它以满足您的需求,调用该副本的 luaL_openlibs()
到位的核心版本。如果你是私人编译Lua和不链接到库的pre-生成的二进制文件之一,那么你可以找到做适合您的需要的等效的方法。
Common practice is to copy that file to your application's source, and modify it to suit your needs, calling that copy's luaL_openlibs()
in place of the core version. If you are compiling Lua privately and not linking to one of the pre-built binaries of the library, then you can find a method to do the equivalent that suits your needs.
当然,你也不需要编译或链接任何库的来源(如操作系统
在 loslib找到。 ç
),你选择离开了 luaL_openlibs()
。
Of course, you also don't need to compile or link to the sources for any library (such as os
, found in loslib.c
) that you choose to leave out of luaL_openlibs()
.
这是你可能无法离开了完全的唯一库是一个提供之类的东西对()
, ipairs()基库
, PCALL()
,的ToString()
,以及更多的可真不方便无。当移植到一个环境,其中一些是有问题的,它通常是一个好主意,在其实施密切关注lbaselib.c
,要么修剪功能,从它或它们重新实现满足您的需求。
The only library that you probably can't leave out completely is the base library that provides things like pairs()
, ipairs()
, pcall()
, tostring()
, and lots more that can be really inconvenient to do without. When porting to an environment where some of these are problematic, it is usually a good idea to look closely at its implementation in lbaselib.c
and either trim features from it or reimplement them to suit your needs.
编辑:
要包括跨preTER库的不同名单另一种方法是在所有不叫 luaL_openlibs()
。虽然为了方便提供,像所有的辅助库, luaL_openlibs()
是不是强制性的。相反,明确地打开你想要的库。
Another approach to including a different list of libraries in the interpreter is to not call luaL_openlibs()
at all. Although provided as a convenience, like all of the auxiliary library, luaL_openlibs()
is not mandatory. Instead, explicitly open just the libraries you want.
要访问这些库中,
C主机程序应该调用
luaL_openlibs
的功能,这将打开
所有的标准库。此外,
它可以通过单独打开它们
调用 luaopen_base
(用于基本
库), luaopen_package
(用于
包库), luaopen_string
(对于
串库), luaopen_table
(该表库), luaopen_math
(用于数学库),
luaopen_io
(用于I / O库)
luaopen_os
(用于操作系统
库)和 luaopen_debug
(用于
调试库)。这些功能
在 lualib.h
宣布,不应该
直接调用:你必须给他们打电话
像任何其他的Lua的C函数,例如,
通过使用 lua_call
。
To have access to these libraries, the C host program should call the
luaL_openlibs
function, which opens all standard libraries. Alternatively, it can open them individually by callingluaopen_base
(for the basic library),luaopen_package
(for the package library),luaopen_string
(for the string library),luaopen_table
(for the table library),luaopen_math
(for the mathematical library),luaopen_io
(for the I/O library),luaopen_os
(for the Operating System library), andluaopen_debug
(for the debug library). These functions are declared inlualib.h
and should not be called directly: you must call them like any other Lua C function, e.g., by usinglua_call
.
这最后一句话是偶尔的麻烦之源,因为老版本的Lua没有这个限制。每个单独的模块的 luaopen_xxx()
函数遵循由要求
函数中使用相同的协议。它应传递一个参数:包含通过该模块被称为名称的字符串。唯一的例外是基本模块,这是通过一个空字符串,因为它没有实际名称
That last sentence is occasionally the source of trouble, since older versions of Lua did not have that restriction. Each of the individual module's luaopen_xxx()
functions follows the same protocol used by the require
function. It should be passed a single argument: a string containing the name by which the module is known. The exception is the base module, which is passed an empty string because it has no actual name.
下面是创建一个新的Lua状态,只打开底座和封装库的函数:
Here's a function that creates a new Lua state and opens only the base and package libraries:
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
lua_State *CreateBasicLua() {
lua_State *L;
L = luaL_newstate();
if (L) {
lua_pushcfunction(L, luaopen_base);
lua_pushstring(L, "");
lua_call(L, 1, 0);
lua_pushcfunction(L, luaopen_package);
lua_pushstring(L, LUA_LOADLIBNAME);
lua_call(L, 1, 0);
}
return L;
}
它返回新的 lua_State
成功,或 NULL
失败。
这篇关于省略Lua的标准库的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!