aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2017-03-30 12:43:21 +0200
committerMike Pall <mike>2017-03-30 12:45:14 +0200
commitde97b9d52bbc42effeaf1180764053a912526873 (patch)
treebfc8e1bc405c8d97ee958eb9750eb23d6a636e3b
parentdc320ca70f2c5bb3977b82853bcee6dad2523d01 (diff)
downloadluajit-de97b9d52bbc42effeaf1180764053a912526873.tar.gz
luajit-de97b9d52bbc42effeaf1180764053a912526873.tar.bz2
luajit-de97b9d52bbc42effeaf1180764053a912526873.zip
Add some more changes and extensions from Lua 5.2.
Contributed by François Perrad.
-rw-r--r--doc/extensions.html15
-rw-r--r--src/lauxlib.h8
-rw-r--r--src/lib_aux.c57
-rw-r--r--src/lib_package.c22
-rw-r--r--src/luaconf.h2
5 files changed, 64 insertions, 40 deletions
diff --git a/doc/extensions.html b/doc/extensions.html
index 87d4da24..93432953 100644
--- a/doc/extensions.html
+++ b/doc/extensions.html
@@ -312,6 +312,20 @@ indexes for varargs.</li>
312<li><tt>debug.getupvalue()</tt> and <tt>debug.setupvalue()</tt> handle 312<li><tt>debug.getupvalue()</tt> and <tt>debug.setupvalue()</tt> handle
313C&nbsp;functions.</li> 313C&nbsp;functions.</li>
314<li><tt>debug.upvalueid()</tt> and <tt>debug.upvaluejoin()</tt>.</li> 314<li><tt>debug.upvalueid()</tt> and <tt>debug.upvaluejoin()</tt>.</li>
315<li>Lua/C API extensions:
316<tt>lua_upvalueid()</tt>
317<tt>lua_upvaluejoin()</tt>
318<tt>lua_loadx()</tt>
319<tt>luaL_fileresult()</tt>
320<tt>luaL_execresult()</tt>
321<tt>luaL_loadfilex()</tt>
322<tt>luaL_loadbufferx()</tt>
323<tt>luaL_traceback()</tt>
324<tt>luaL_setfuncs()</tt>
325<tt>luaL_pushmodule()</tt>
326<tt>luaL_newlibtable()</tt>
327<tt>luaL_newlib()</tt>
328</li>
315<li>Command line option <tt>-E</tt>.</li> 329<li>Command line option <tt>-E</tt>.</li>
316<li>Command line checks <tt>__tostring</tt> for errors.</li> 330<li>Command line checks <tt>__tostring</tt> for errors.</li>
317</ul> 331</ul>
@@ -338,6 +352,7 @@ exit status.</li>
338<li><tt>debug.getuservalue()</tt> and <tt>debug.setuservalue()</tt>.</li> 352<li><tt>debug.getuservalue()</tt> and <tt>debug.setuservalue()</tt>.</li>
339<li>Remove <tt>math.mod()</tt>, <tt>string.gfind()</tt>.</li> 353<li>Remove <tt>math.mod()</tt>, <tt>string.gfind()</tt>.</li>
340<li><tt>package.searchers</tt>.</li> 354<li><tt>package.searchers</tt>.</li>
355<li><tt>module()</tt> returns the module table.</li>
341</ul> 356</ul>
342<p> 357<p>
343Note: this provides only partial compatibility with Lua 5.2 at the 358Note: this provides only partial compatibility with Lua 5.2 at the
diff --git a/src/lauxlib.h b/src/lauxlib.h
index 91ff4ea9..a8280c20 100644
--- a/src/lauxlib.h
+++ b/src/lauxlib.h
@@ -85,6 +85,9 @@ LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
85 const char *name, const char *mode); 85 const char *name, const char *mode);
86LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, 86LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
87 int level); 87 int level);
88LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
89LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
90 int sizehint);
88 91
89 92
90/* 93/*
@@ -114,6 +117,11 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
114 117
115#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 118#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
116 119
120/* From Lua 5.2. */
121#define luaL_newlibtable(L, l) \
122 lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
123#define luaL_newlib(L, l) (luaL_newlibtable(L, l), luaL_setfuncs(L, l, 0))
124
117/* 125/*
118** {====================================================== 126** {======================================================
119** Generic Buffer manipulation 127** Generic Buffer manipulation
diff --git a/src/lib_aux.c b/src/lib_aux.c
index 4e137949..c40565c3 100644
--- a/src/lib_aux.c
+++ b/src/lib_aux.c
@@ -107,38 +107,36 @@ LUALIB_API const char *luaL_findtable(lua_State *L, int idx,
107static int libsize(const luaL_Reg *l) 107static int libsize(const luaL_Reg *l)
108{ 108{
109 int size = 0; 109 int size = 0;
110 for (; l->name; l++) size++; 110 for (; l && l->name; l++) size++;
111 return size; 111 return size;
112} 112}
113 113
114LUALIB_API void luaL_pushmodule(lua_State *L, const char *modname, int sizehint)
115{
116 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
117 lua_getfield(L, -1, modname);
118 if (!lua_istable(L, -1)) {
119 lua_pop(L, 1);
120 if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, sizehint) != NULL)
121 lj_err_callerv(L, LJ_ERR_BADMODN, modname);
122 lua_pushvalue(L, -1);
123 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table. */
124 }
125 lua_remove(L, -2); /* Remove _LOADED table. */
126}
127
114LUALIB_API void luaL_openlib(lua_State *L, const char *libname, 128LUALIB_API void luaL_openlib(lua_State *L, const char *libname,
115 const luaL_Reg *l, int nup) 129 const luaL_Reg *l, int nup)
116{ 130{
117 lj_lib_checkfpu(L); 131 lj_lib_checkfpu(L);
118 if (libname) { 132 if (libname) {
119 int size = libsize(l); 133 luaL_pushmodule(L, libname, libsize(l));
120 /* check whether lib already exists */ 134 lua_insert(L, -(nup + 1)); /* Move module table below upvalues. */
121 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
122 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
123 if (!lua_istable(L, -1)) { /* not found? */
124 lua_pop(L, 1); /* remove previous result */
125 /* try global variable (and create one if it does not exist) */
126 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
127 lj_err_callerv(L, LJ_ERR_BADMODN, libname);
128 lua_pushvalue(L, -1);
129 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
130 }
131 lua_remove(L, -2); /* remove _LOADED table */
132 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
133 }
134 for (; l->name; l++) {
135 int i;
136 for (i = 0; i < nup; i++) /* copy upvalues to the top */
137 lua_pushvalue(L, -nup);
138 lua_pushcclosure(L, l->func, nup);
139 lua_setfield(L, -(nup+2), l->name);
140 } 135 }
141 lua_pop(L, nup); /* remove upvalues */ 136 if (l)
137 luaL_setfuncs(L, l, nup);
138 else
139 lua_pop(L, nup); /* Remove upvalues. */
142} 140}
143 141
144LUALIB_API void luaL_register(lua_State *L, const char *libname, 142LUALIB_API void luaL_register(lua_State *L, const char *libname,
@@ -147,6 +145,19 @@ LUALIB_API void luaL_register(lua_State *L, const char *libname,
147 luaL_openlib(L, libname, l, 0); 145 luaL_openlib(L, libname, l, 0);
148} 146}
149 147
148LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
149{
150 luaL_checkstack(L, nup, "too many upvalues");
151 for (; l->name; l++) {
152 int i;
153 for (i = 0; i < nup; i++) /* Copy upvalues to the top. */
154 lua_pushvalue(L, -nup);
155 lua_pushcclosure(L, l->func, nup);
156 lua_setfield(L, -(nup + 2), l->name);
157 }
158 lua_pop(L, nup); /* Remove upvalues. */
159}
160
150LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, 161LUALIB_API const char *luaL_gsub(lua_State *L, const char *s,
151 const char *p, const char *r) 162 const char *p, const char *r)
152{ 163{
diff --git a/src/lib_package.c b/src/lib_package.c
index b7655c6b..552db305 100644
--- a/src/lib_package.c
+++ b/src/lib_package.c
@@ -489,29 +489,19 @@ static void modinit(lua_State *L, const char *modname)
489static int lj_cf_package_module(lua_State *L) 489static int lj_cf_package_module(lua_State *L)
490{ 490{
491 const char *modname = luaL_checkstring(L, 1); 491 const char *modname = luaL_checkstring(L, 1);
492 int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ 492 int lastarg = (int)(L->top - L->base);
493 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 493 luaL_pushmodule(L, modname, 1);
494 lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
495 if (!lua_istable(L, -1)) { /* not found? */
496 lua_pop(L, 1); /* remove previous result */
497 /* try global variable (and create one if it does not exist) */
498 if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
499 lj_err_callerv(L, LJ_ERR_BADMODN, modname);
500 lua_pushvalue(L, -1);
501 lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
502 }
503 /* check whether table already has a _NAME field */
504 lua_getfield(L, -1, "_NAME"); 494 lua_getfield(L, -1, "_NAME");
505 if (!lua_isnil(L, -1)) { /* is table an initialized module? */ 495 if (!lua_isnil(L, -1)) { /* Module already initialized? */
506 lua_pop(L, 1); 496 lua_pop(L, 1);
507 } else { /* no; initialize it */ 497 } else {
508 lua_pop(L, 1); 498 lua_pop(L, 1);
509 modinit(L, modname); 499 modinit(L, modname);
510 } 500 }
511 lua_pushvalue(L, -1); 501 lua_pushvalue(L, -1);
512 setfenv(L); 502 setfenv(L);
513 dooptions(L, loaded - 1); 503 dooptions(L, lastarg);
514 return 0; 504 return LJ_52;
515} 505}
516 506
517static int lj_cf_package_seeall(lua_State *L) 507static int lj_cf_package_seeall(lua_State *L)
diff --git a/src/luaconf.h b/src/luaconf.h
index 6486c003..0c70b145 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -79,7 +79,7 @@
79#define LUA_IGMARK "-" 79#define LUA_IGMARK "-"
80#define LUA_PATH_CONFIG \ 80#define LUA_PATH_CONFIG \
81 LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \ 81 LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \
82 LUA_EXECDIR "\n" LUA_IGMARK 82 LUA_EXECDIR "\n" LUA_IGMARK "\n"
83 83
84/* Quoting in error messages. */ 84/* Quoting in error messages. */
85#define LUA_QL(x) "'" x "'" 85#define LUA_QL(x) "'" x "'"