New function 'luaL_openselectedlibs'

Makes it easier to start Lua with only some standard libraries.
This commit is contained in:
Roberto Ierusalimschy
2022-12-07 15:12:52 -03:00
parent 0270c204c2
commit d738c8d18b
7 changed files with 85 additions and 83 deletions

View File

@@ -1039,10 +1039,12 @@ assert(a == nil and b == 2) -- 2 == run-time error
a, b, c = T.doremote(L1, "return a+")
assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
T.loadlib(L1)
T.loadlib(L1, 2) -- load only 'package'
a, b, c = T.doremote(L1, [[
string = require'string'
a = require'_G'; assert(a == _G and require("_G") == a)
local initialG = _G -- not loaded yet
local a = require'_G'; assert(a == _G and require("_G") == a)
assert(initialG == nil and io == nil) -- now we have 'assert'
io = require'io'; assert(type(io.read) == "function")
assert(require("io") == io)
a = require'table'; assert(type(a.insert) == "function")
@@ -1056,7 +1058,7 @@ T.closestate(L1);
L1 = T.newstate()
T.loadlib(L1)
T.loadlib(L1, 0)
T.doremote(L1, "a = {}")
T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
settable -3]])
@@ -1436,10 +1438,10 @@ end
do -- garbage collection with no extra memory
local L = T.newstate()
T.loadlib(L)
T.loadlib(L, 1 | 2) -- load _G and 'package'
local res = (T.doremote(L, [[
_ENV = require"_G"
local T = require"T"
_ENV = _G
assert(string == nil)
local a = {}
for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
local stsize, stuse = T.querystr()

View File

@@ -694,7 +694,7 @@ else
T.testC(state, "settop 0")
T.loadlib(state)
T.loadlib(state, 1 | 2) -- load _G and 'package'
assert(T.doremote(state, [[
coroutine = require'coroutine';