Skip to content

Commit 165389b

Browse files
committed
New interface to function 'luaL_openselectedlibs'
Instead of preloading all non-loaded libraries, there is another mask to select which libraries to preload.
1 parent c8121ce commit 165389b

8 files changed

Lines changed: 80 additions & 56 deletions

File tree

linit.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222

2323
/*
24-
** Standard Libraries
24+
** Standard Libraries. (Must be listed in the same ORDER of their
25+
** respective constants LUA_<libname>K.)
2526
*/
2627
static const luaL_Reg stdlibs[] = {
2728
{LUA_GNAME, luaopen_base},
2829
{LUA_LOADLIBNAME, luaopen_package},
29-
3030
{LUA_COLIBNAME, luaopen_coroutine},
3131
{LUA_DBLIBNAME, luaopen_debug},
3232
{LUA_IOLIBNAME, luaopen_io},
@@ -35,30 +35,28 @@ static const luaL_Reg stdlibs[] = {
3535
{LUA_STRLIBNAME, luaopen_string},
3636
{LUA_TABLIBNAME, luaopen_table},
3737
{LUA_UTF8LIBNAME, luaopen_utf8},
38-
3938
{NULL, NULL}
4039
};
4140

4241

4342
/*
44-
** require selected standard libraries and add the others to the
45-
** preload table.
43+
** require and preload selected standard libraries
4644
*/
47-
LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) {
48-
int mask = 1;
45+
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
46+
int mask;
4947
const luaL_Reg *lib;
5048
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
51-
for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) {
52-
if (what & mask) { /* selected? */
49+
for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
50+
if (load & mask) { /* selected? */
5351
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
5452
lua_pop(L, 1); /* remove result from the stack */
5553
}
56-
else { /* add library to PRELOAD table */
54+
else if (preload & mask) { /* selected? */
5755
lua_pushcfunction(L, lib->func);
58-
lua_setfield(L, -2, lib->name);
56+
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
5957
}
6058
}
6159
lua_assert((mask >> 1) == LUA_UTF8LIBK);
62-
lua_pop(L, 1); // remove PRELOAD table
60+
lua_pop(L, 1); /* remove PRELOAD table */
6361
}
6462

ltests.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,9 @@ static lua_State *getstate (lua_State *L) {
12231223

12241224
static int loadlib (lua_State *L) {
12251225
lua_State *L1 = getstate(L);
1226-
int what = luaL_checkinteger(L, 2);
1227-
luaL_openselectedlibs(L1, what);
1226+
int load = luaL_checkinteger(L, 2);
1227+
int preload = luaL_checkinteger(L, 3);
1228+
luaL_openselectedlibs(L1, load, preload);
12281229
luaL_requiref(L1, "T", luaB_opentests, 0);
12291230
lua_assert(lua_type(L1, -1) == LUA_TTABLE);
12301231
/* 'requiref' should not reload module already loaded... */

lua.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ static void doREPL (lua_State *L) {
618618
/* }================================================================== */
619619

620620
#if !defined(luai_openlibs)
621-
#define luai_openlibs(L) luaL_openlibs(L)
621+
#define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
622622
#endif
623623

624624

lualib.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
/* version suffix for environment variable names */
1515
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
1616

17-
#define LUA_GK 1
17+
#define LUA_GLIBK 1
1818
LUAMOD_API int (luaopen_base) (lua_State *L);
1919

2020
#define LUA_LOADLIBNAME "package"
21-
#define LUA_LOADLIBK (LUA_GK << 1)
21+
#define LUA_LOADLIBK (LUA_GLIBK << 1)
2222
LUAMOD_API int (luaopen_package) (lua_State *L);
2323

2424

@@ -56,10 +56,10 @@ LUAMOD_API int (luaopen_utf8) (lua_State *L);
5656

5757

5858
/* open selected libraries */
59-
LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int what);
59+
LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload);
6060

6161
/* open all libraries */
62-
#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0)
62+
#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
6363

6464

6565
#endif

manual/2html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ item = function (s)
358358
local t, p = string.match(s, "^([^\n|]+)|()")
359359
if t then
360360
s = string.sub(s, p)
361-
s = Tag.b(t..": ") .. s
361+
s = Tag.b(t) ..": " .. s
362362
end
363363
return Tag.li(fixpara(s))
364364
end,

manual/manual.of

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -664,15 +664,13 @@ Values equal to or less than 100 mean the collector will not wait to
664664
start a new cycle.
665665
A value of 200 means that the collector waits for
666666
the total number of objects to double before starting a new cycle.
667-
The default value is 200.
668667

669668
The garbage-collector step size controls the
670669
size of each incremental step,
671670
specifically how many objects the interpreter creates
672671
before performing a step:
673672
A value of @M{n} means the interpreter will create
674673
approximately @M{n} objects between steps.
675-
The default value is 250.
676674

677675
The garbage-collector step multiplier
678676
controls the size of each GC step.
@@ -681,7 +679,6 @@ in each step, @M{n%} objects for each created object.
681679
Larger values make the collector more aggressive.
682680
Beware that values too small can
683681
make the collector too slow to ever finish a cycle.
684-
The default value is 200.
685682
As a special case, a zero value means unlimited work,
686683
effectively producing a non-incremental, stop-the-world collector.
687684

@@ -711,7 +708,6 @@ after the last major collection.
711708
For instance, for a multiplier of 20,
712709
the collector will do a minor collection when the number of objects
713710
gets 20% larger than the total after the last major collection.
714-
The default value is 25.
715711

716712
The minor-major multiplier controls the shift to major collections.
717713
For a multiplier @M{x},
@@ -721,7 +717,6 @@ than the total after the previous major collection.
721717
For instance, for a multiplier of 100,
722718
the collector will do a major collection when the number of old objects
723719
gets larger than twice the total after the previous major collection.
724-
The default value is 100.
725720

726721
The major-minor multiplier controls the shift back to minor collections.
727722
For a multiplier @M{x},
@@ -731,7 +726,6 @@ of the objects allocated during the last cycle.
731726
In particular, for a multiplier of 0,
732727
the collector will immediately shift back to minor collections
733728
after doing one cycle of major collections.
734-
The default value is 50.
735729

736730
}
737731

@@ -5885,13 +5879,6 @@ or @id{NULL} if there is a @x{memory allocation error}.
58855879

58865880
}
58875881

5888-
@APIEntry{void luaL_openlibs (lua_State *L);|
5889-
@apii{0,0,e}
5890-
5891-
Opens all standard Lua libraries into the given state.
5892-
5893-
}
5894-
58955882
@APIEntry{
58965883
T luaL_opt (L, func, arg, dflt);|
58975884
@apii{0,0,-}
@@ -6073,7 +6060,7 @@ and sets the call result to @T{package.loaded[modname]},
60736060
as if that function has been called through @Lid{require}.
60746061

60756062
If @id{glb} is true,
6076-
also stores the module into the global @id{modname}.
6063+
also stores the module into the global variable @id{modname}.
60776064

60786065
Leaves a copy of the module on the stack.
60796066

@@ -6290,23 +6277,61 @@ Except for the basic and the package libraries,
62906277
each library provides all its functions as fields of a global table
62916278
or as methods of its objects.
62926279

6293-
To have access to these libraries,
6294-
the @N{C host} program should call the @Lid{luaL_openlibs} function,
6295-
which opens all standard libraries.
6280+
}
6281+
6282+
6283+
@sect2{lualib-h| @title{Loading the Libraries in C code}
6284+
6285+
A @N{C host} program must explicitly load
6286+
the standard libraries into a state,
6287+
if it wants its scripts to use them.
6288+
For that,
6289+
the host program can call the function @Lid{luaL_openlibs}.
62966290
Alternatively,
6297-
the host program can open them individually by using
6298-
@Lid{luaL_requiref} to call
6299-
@defid{luaopen_base} (for the basic library),
6300-
@defid{luaopen_package} (for the package library),
6301-
@defid{luaopen_coroutine} (for the coroutine library),
6302-
@defid{luaopen_string} (for the string library),
6303-
@defid{luaopen_utf8} (for the UTF-8 library),
6304-
@defid{luaopen_table} (for the table library),
6305-
@defid{luaopen_math} (for the mathematical library),
6306-
@defid{luaopen_io} (for the I/O library),
6307-
@defid{luaopen_os} (for the operating system library),
6308-
and @defid{luaopen_debug} (for the debug library).
6309-
These functions are declared in @defid{lualib.h}.
6291+
the host can select which libraries to open,
6292+
by using @Lid{luaL_openselectedlibs}.
6293+
Both functions are defined in the header file @id{lualib.h}.
6294+
@index{lualib.h}
6295+
6296+
The stand-alone interpreter @id{lua} @see{lua-sa}
6297+
already opens all standard libraries.
6298+
6299+
@APIEntry{void luaL_openlibs (lua_State *L);|
6300+
@apii{0,0,e}
6301+
6302+
Opens all standard Lua libraries into the given state.
6303+
6304+
}
6305+
6306+
@APIEntry{void luaL_openselectedlibs (lua_State *L, int load, int preload);|
6307+
@apii{0,0,e}
6308+
6309+
Opens (loads) and preloads selected libraries into the state @id{L}.
6310+
(To @emph{preload} means to add
6311+
the library loader into the table @Lid{package.preload},
6312+
so that the library can be required later by the program.
6313+
Keep in mind that @Lid{require} itself is provided
6314+
by the @emph{package} library.
6315+
If a program does not load that library,
6316+
it will be unable to require anything.)
6317+
6318+
The integer @id{load} selects which libraries to load;
6319+
the integer @id{preload} selects which to preload, among those not loaded.
6320+
Both are masks formed by a bitwise OR of the following constants:
6321+
@description{
6322+
@item{@defid{LUA_GLIBK} | the basic library.}
6323+
@item{@defid{LUA_LOADLIBK} | the package library.}
6324+
@item{@defid{LUA_COLIBK} | the coroutine library.}
6325+
@item{@defid{LUA_STRLIBK} | the string library.}
6326+
@item{@defid{LUA_UTF8LIBK} | the UTF-8 library.}
6327+
@item{@defid{LUA_TABLIBK} | the table library.}
6328+
@item{@defid{LUA_MATHLIBK} | the mathematical library.}
6329+
@item{@defid{LUA_IOLIBK} | the I/O library.}
6330+
@item{@defid{LUA_OSLIBK} | the operating system library.}
6331+
@item{@defid{LUA_DBLIBK} | the debug library.}
6332+
}
6333+
6334+
}
63106335

63116336
}
63126337

testes/api.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ do
546546
]], source)
547547
collectgarbage()
548548
local m2 = collectgarbage"count" * 1024
549-
-- load used fewer than 350 bytes. Code alone has more than 3*N bytes,
549+
-- load used fewer than 400 bytes. Code alone has more than 3*N bytes,
550550
-- and string literal has N bytes. Both were not loaded.
551-
assert(m2 > m1 and m2 - m1 < 350)
551+
assert(m2 > m1 and m2 - m1 < 400)
552552
X = 0; code(); assert(X == N and Y == string.rep("a", N))
553553
X = nil; Y = nil
554554

@@ -1122,7 +1122,7 @@ assert(a == nil and c == 2) -- 2 == run-time error
11221122
a, b, c = T.doremote(L1, "return a+")
11231123
assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
11241124

1125-
T.loadlib(L1, 2) -- load only 'package'
1125+
T.loadlib(L1, 2, ~2) -- load only 'package', preload all others
11261126
a, b, c = T.doremote(L1, [[
11271127
string = require'string'
11281128
local initialG = _G -- not loaded yet
@@ -1141,7 +1141,7 @@ T.closestate(L1);
11411141

11421142

11431143
L1 = T.newstate()
1144-
T.loadlib(L1, 0)
1144+
T.loadlib(L1, 0, 0)
11451145
T.doremote(L1, "a = {}")
11461146
T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
11471147
settable -3]])
@@ -1524,7 +1524,7 @@ end
15241524

15251525
do -- garbage collection with no extra memory
15261526
local L = T.newstate()
1527-
T.loadlib(L, 1 | 2) -- load _G and 'package'
1527+
T.loadlib(L, 1 | 2, 0) -- load _G and 'package'
15281528
local res = (T.doremote(L, [[
15291529
_ENV = _G
15301530
assert(string == nil)

testes/coroutine.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ else
705705

706706
T.testC(state, "settop 0")
707707

708-
T.loadlib(state, 1 | 2) -- load _G and 'package'
708+
T.loadlib(state, 1 | 2, 4) -- load _G and 'package', preload 'coroutine'
709709

710710
assert(T.doremote(state, [[
711711
coroutine = require'coroutine';

0 commit comments

Comments
 (0)