aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 14:36:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 14:36:32 -0300
commit7631c29b2f1d57866e2ab885aedc19c29e93a653 (patch)
tree115c2557d506e60a2da055b93a1791cc95b1d0f4
parenta9dc7c88283a8046ad40c592f9e626d93e8e14a1 (diff)
downloadlua-7631c29b2f1d57866e2ab885aedc19c29e93a653.tar.gz
lua-7631c29b2f1d57866e2ab885aedc19c29e93a653.tar.bz2
lua-7631c29b2f1d57866e2ab885aedc19c29e93a653.zip
comand-line option '-l' creates global with given name after
requiring module
-rw-r--r--lua.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/lua.c b/lua.c
index b2add022..6f80550b 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.189 2010/03/13 03:57:46 roberto Exp roberto $ 2** $Id: lua.c,v 1.190 2010/04/14 15:14:21 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -167,14 +167,14 @@ static int traceback (lua_State *L) {
167} 167}
168 168
169 169
170static int docall (lua_State *L, int narg, int clear) { 170static int docall (lua_State *L, int narg, int nres) {
171 int status; 171 int status;
172 int base = lua_gettop(L) - narg; /* function index */ 172 int base = lua_gettop(L) - narg; /* function index */
173 lua_pushcfunction(L, traceback); /* push traceback function */ 173 lua_pushcfunction(L, traceback); /* push traceback function */
174 lua_insert(L, base); /* put it under chunk and args */ 174 lua_insert(L, base); /* put it under chunk and args */
175 globalL = L; /* to be available to 'laction' */ 175 globalL = L; /* to be available to 'laction' */
176 signal(SIGINT, laction); 176 signal(SIGINT, laction);
177 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); 177 status = lua_pcall(L, narg, nres, base);
178 signal(SIGINT, SIG_DFL); 178 signal(SIGINT, SIG_DFL);
179 lua_remove(L, base); /* remove traceback function */ 179 lua_remove(L, base); /* remove traceback function */
180 return status; 180 return status;
@@ -206,22 +206,31 @@ static int getargs (lua_State *L, char **argv, int n) {
206 206
207static int dofile (lua_State *L, const char *name) { 207static int dofile (lua_State *L, const char *name) {
208 int status = luaL_loadfile(L, name); 208 int status = luaL_loadfile(L, name);
209 if (status == LUA_OK) status = docall(L, 0, 1); 209 if (status == LUA_OK) status = docall(L, 0, 0);
210 return report(L, status); 210 return report(L, status);
211} 211}
212 212
213 213
214static int dostring (lua_State *L, const char *s, const char *name) { 214static int dostring (lua_State *L, const char *s, const char *name) {
215 int status = luaL_loadbuffer(L, s, strlen(s), name); 215 int status = luaL_loadbuffer(L, s, strlen(s), name);
216 if (status == LUA_OK) status = docall(L, 0, 1); 216 if (status == LUA_OK) status = docall(L, 0, 0);
217 return report(L, status); 217 return report(L, status);
218} 218}
219 219
220 220
221static int dolibrary (lua_State *L, const char *name) { 221static int dolibrary (lua_State *L, const char *name) {
222 lua_getglobal(L, "require"); 222 int status;
223 lua_pushglobaltable(L);
224 lua_getfield(L, -1, "require");
223 lua_pushstring(L, name); 225 lua_pushstring(L, name);
224 return report(L, docall(L, 1, 1)); 226 status = docall(L, 1, 1);
227 if (status == LUA_OK) {
228 lua_setfield(L, -2, name); /* global[name] = require return */
229 lua_pop(L, 1); /* remove global table */
230 }
231 else
232 lua_remove(L, -2); /* remove global table (below error msg.) */
233 return report(L, status);
225} 234}
226 235
227 236
@@ -297,7 +306,7 @@ static void dotty (lua_State *L) {
297 const char *oldprogname = progname; 306 const char *oldprogname = progname;
298 progname = NULL; 307 progname = NULL;
299 while ((status = loadline(L)) != -1) { 308 while ((status = loadline(L)) != -1) {
300 if (status == LUA_OK) status = docall(L, 0, 0); 309 if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
301 report(L, status); 310 report(L, status);
302 if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ 311 if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
303 luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); 312 luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
@@ -326,7 +335,7 @@ static int handle_script (lua_State *L, char **argv, int n) {
326 status = luaL_loadfile(L, fname); 335 status = luaL_loadfile(L, fname);
327 lua_insert(L, -(narg+1)); 336 lua_insert(L, -(narg+1));
328 if (status == LUA_OK) 337 if (status == LUA_OK)
329 status = docall(L, narg, 0); 338 status = docall(L, narg, LUA_MULTRET);
330 else 339 else
331 lua_pop(L, narg); 340 lua_pop(L, narg);
332 return report(L, status); 341 return report(L, status);