summaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
commit9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761 (patch)
treeda8d97d954e5ffabf9ff275df725f1e0a3a5b3e6 /lua.c
parentf1fd9b5c2c21f24d25d7813f431a3495702ebea6 (diff)
downloadlua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.gz
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.bz2
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.zip
first version for new API
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c74
1 files changed, 38 insertions, 36 deletions
diff --git a/lua.c b/lua.c
index b3b8e6ea..3a50ceb2 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.44 2000/08/09 19:16:57 roberto Exp roberto $ 2** $Id: lua.c,v 1.45 2000/08/14 17:45:59 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*/
@@ -10,14 +10,14 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13#define LUA_SINGLESTATE
14
15#include "lua.h" 13#include "lua.h"
16 14
17#include "luadebug.h" 15#include "luadebug.h"
18#include "lualib.h" 16#include "lualib.h"
19 17
18
20lua_State *lua_state = NULL; 19lua_State *lua_state = NULL;
20#define L lua_state
21 21
22 22
23#ifndef PROMPT 23#ifndef PROMPT
@@ -54,10 +54,10 @@ extern void USERINIT (void);
54#else 54#else
55#define USERINIT userinit 55#define USERINIT userinit
56static void userinit (void) { 56static void userinit (void) {
57 lua_iolibopen(); 57 lua_iolibopen(L);
58 lua_strlibopen(); 58 lua_strlibopen(L);
59 lua_mathlibopen(); 59 lua_mathlibopen(L);
60 lua_dblibopen(); 60 lua_dblibopen(L);
61} 61}
62#endif 62#endif
63 63
@@ -68,10 +68,10 @@ static handler lreset (void) {
68 68
69 69
70static void lstop (void) { 70static void lstop (void) {
71 lua_setlinehook(lua_state, old_linehook); 71 lua_setlinehook(L, old_linehook);
72 lua_setcallhook(lua_state, old_callhook); 72 lua_setcallhook(L, old_callhook);
73 lreset(); 73 lreset();
74 lua_error("interrupted!"); 74 lua_error(L, "interrupted!");
75} 75}
76 76
77 77
@@ -79,15 +79,15 @@ static void laction (int i) {
79 (void)i; /* to avoid warnings */ 79 (void)i; /* to avoid warnings */
80 signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop, 80 signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
81 terminate process (default action) */ 81 terminate process (default action) */
82 old_linehook = lua_setlinehook(lua_state, (lua_Hook)lstop); 82 old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
83 old_callhook = lua_setcallhook(lua_state, (lua_Hook)lstop); 83 old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
84} 84}
85 85
86 86
87static int ldo (int (*f)(lua_State *L, const char *), const char *name) { 87static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
88 int res; 88 int res;
89 handler h = lreset(); 89 handler h = lreset();
90 res = f(lua_state, name); /* dostring | dofile */ 90 res = f(L, name); /* dostring | dofile */
91 signal(SIGINT, h); /* restore old action */ 91 signal(SIGINT, h); /* restore old action */
92 if (res == LUA_ERRMEM) { 92 if (res == LUA_ERRMEM) {
93 /* Lua gives no message in such case, so lua.c provides one */ 93 /* Lua gives no message in such case, so lua.c provides one */
@@ -122,29 +122,29 @@ static void print_version (void) {
122static void assign (char *arg) { 122static void assign (char *arg) {
123 char *eq = strchr(arg, '='); 123 char *eq = strchr(arg, '=');
124 *eq = '\0'; /* spilt `arg' in two strings (name & value) */ 124 *eq = '\0'; /* spilt `arg' in two strings (name & value) */
125 lua_pushstring(eq+1); 125 lua_pushstring(L, eq+1);
126 lua_setglobal(arg); 126 lua_setglobal(L, arg);
127} 127}
128 128
129 129
130static lua_Object getargs (char *argv[]) { 130static void getargs (char *argv[]) {
131 lua_Object args = lua_createtable();
132 int i; 131 int i;
132 lua_newtable(L);
133 for (i=0; argv[i]; i++) { 133 for (i=0; argv[i]; i++) {
134 /* arg[i] = argv[i] */ 134 /* arg[i] = argv[i] */
135 lua_pushobject(args); lua_pushnumber(i); 135 lua_pushobject(L, -1); lua_pushnumber(L, i);
136 lua_pushstring(argv[i]); lua_settable(); 136 lua_pushstring(L, argv[i]); lua_settable(L);
137 } 137 }
138 /* arg.n = maximum index in table `arg' */ 138 /* arg.n = maximum index in table `arg' */
139 lua_pushobject(args); lua_pushstring("n"); 139 lua_pushobject(L, -1); lua_pushstring(L, "n");
140 lua_pushnumber(i-1); lua_settable(); 140 lua_pushnumber(L, i-1); lua_settable(L);
141 return args;
142} 141}
143 142
144 143
145static void l_getargs (void) { 144static int l_getargs (lua_State *l) {
146 char **argv = (char **)lua_getuserdata(lua_getparam(1)); 145 char **argv = (char **)lua_touserdata(l, 1);
147 lua_pushobject(getargs(argv)); 146 getargs(argv);
147 return 1;
148} 148}
149 149
150 150
@@ -173,9 +173,11 @@ static void manual_input (int version, int prompt) {
173 while (cont) { 173 while (cont) {
174 char buffer[MAXINPUT]; 174 char buffer[MAXINPUT];
175 int i = 0; 175 int i = 0;
176 lua_beginblock();
177 if (prompt) { 176 if (prompt) {
178 const char *s = lua_getstring(lua_getglobal("_PROMPT")); 177 const char *s;
178 lua_getglobal(L, "_PROMPT");
179 s = lua_tostring(L, -1);
180 lua_settop(L, -1); /* remove global */
179 if (!s) s = PROMPT; 181 if (!s) s = PROMPT;
180 fputs(s, stdout); 182 fputs(s, stdout);
181 } 183 }
@@ -198,7 +200,7 @@ static void manual_input (int version, int prompt) {
198 } 200 }
199 buffer[i] = '\0'; 201 buffer[i] = '\0';
200 ldo(lua_dostring, buffer); 202 ldo(lua_dostring, buffer);
201 lua_endblock(); 203 lua_settop(L, 0); /* remove eventual results */
202 } 204 }
203 printf("\n"); 205 printf("\n");
204} 206}
@@ -262,8 +264,8 @@ static int handle_argv (char *argv[], struct Options *opt) {
262 print_message(); 264 print_message();
263 return EXIT_FAILURE; 265 return EXIT_FAILURE;
264 } 266 }
265 lua_pushobject(getargs(argv+i)); /* collect remaining arguments */ 267 getargs(argv+i); /* collect remaining arguments */
266 lua_setglobal("arg"); 268 lua_setglobal(L, "arg");
267 return file_input(argv[i]); /* stop scanning arguments */ 269 return file_input(argv[i]); /* stop scanning arguments */
268 } 270 }
269 case 's': { 271 case 's': {
@@ -296,9 +298,9 @@ static void getstacksize (int argc, char *argv[], struct Options *opt) {
296 298
297 299
298static void register_getargs (char *argv[]) { 300static void register_getargs (char *argv[]) {
299 lua_pushuserdata(argv); 301 lua_pushuserdata(L, argv);
300 lua_pushcclosure(l_getargs, 1); 302 lua_pushcclosure(L, l_getargs, 1);
301 lua_setglobal("getargs"); 303 lua_setglobal(L, "getargs");
302} 304}
303 305
304 306
@@ -307,12 +309,12 @@ int main (int argc, char *argv[]) {
307 int status; 309 int status;
308 opt.toclose = 0; 310 opt.toclose = 0;
309 getstacksize(argc, argv, &opt); /* handle option `-s' */ 311 getstacksize(argc, argv, &opt); /* handle option `-s' */
310 lua_state = lua_newstate(opt.stacksize, 1); /* create state */ 312 L = lua_newstate(opt.stacksize, 1); /* create state */
311 USERINIT(); /* open libraries */ 313 USERINIT(); /* open libraries */
312 register_getargs(argv); /* create `getargs' function */ 314 register_getargs(argv); /* create `getargs' function */
313 status = handle_argv(argv+1, &opt); 315 status = handle_argv(argv+1, &opt);
314 if (opt.toclose) 316 if (opt.toclose)
315 lua_close(); 317 lua_close(L);
316 return status; 318 return status;
317} 319}
318 320