aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-03 17:11:41 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-03 17:11:41 -0300
commitad7103ea3aed7f40a5cf7055af253b34320134bc (patch)
tree978a750ba9c4cb990ece1fe6280f3806acbe6fce
parent0079e0f57ce2bd4dc40b9b7c5831c58764a7938f (diff)
downloadlua-ad7103ea3aed7f40a5cf7055af253b34320134bc.tar.gz
lua-ad7103ea3aed7f40a5cf7055af253b34320134bc.tar.bz2
lua-ad7103ea3aed7f40a5cf7055af253b34320134bc.zip
lua_load* defined in auxlib (and so renamed to luaL_load*)
-rw-r--r--lauxlib.c206
-rw-r--r--lauxlib.h12
-rw-r--r--lbaselib.c12
-rw-r--r--ltests.c8
-rw-r--r--lua.c26
5 files changed, 219 insertions, 45 deletions
diff --git a/lauxlib.c b/lauxlib.c
index b7b82661..af38c670 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,33 +1,36 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.70 2002/05/15 18:57:44 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.71 2002/05/16 18:39:46 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <ctype.h>
8#include <stdarg.h> 9#include <stdarg.h>
9#include <stdio.h> 10#include <stdio.h>
10#include <string.h> 11#include <string.h>
11 12
13#ifndef lua_filerror
14#include <errno.h>
15#define lua_fileerror (strerror(errno))
16#endif
17
18
12/* This file uses only the official API of Lua. 19/* This file uses only the official API of Lua.
13** Any function declared here could be written as an application function. 20** Any function declared here could be written as an application function.
14** With care, these functions can be used by other libraries.
15*/ 21*/
16 22
17#include "lua.h" 23#include "lua.h"
18 24
19#include "lauxlib.h" 25#include "lauxlib.h"
20#include "luadebug.h" 26#include "luadebug.h"
21#include "lualib.h"
22 27
23 28
24LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { 29/*
25 int i; 30** {======================================================
26 for (i=0; list[i]; i++) 31** Error-report functions
27 if (strcmp(list[i], name) == 0) 32** =======================================================
28 return i; 33*/
29 return -1; /* name not found */
30}
31 34
32 35
33LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { 36LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
@@ -54,6 +57,33 @@ static void tag_error (lua_State *L, int narg, int tag) {
54} 57}
55 58
56 59
60LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
61 lua_Debug ar;
62 const char *msg;
63 va_list argp;
64 va_start(argp, fmt);
65 msg = lua_pushvfstring(L, fmt, argp);
66 va_end(argp);
67 if (lua_getstack(L, 1, &ar)) { /* check calling function */
68 lua_getinfo(L, "Snl", &ar);
69 if (ar.currentline > 0)
70 lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg);
71 }
72 return lua_errorobj(L);
73}
74
75/* }====================================================== */
76
77
78LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
79 int i;
80 for (i=0; list[i]; i++)
81 if (strcmp(list[i], name) == 0)
82 return i;
83 return -1; /* name not found */
84}
85
86
57LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) { 87LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
58 if (!lua_checkstack(L, space)) 88 if (!lua_checkstack(L, space))
59 luaL_verror(L, "stack overflow (%s)", mes); 89 luaL_verror(L, "stack overflow (%s)", mes);
@@ -143,21 +173,6 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
143} 173}
144 174
145 175
146LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
147 lua_Debug ar;
148 const char *msg;
149 va_list argp;
150 va_start(argp, fmt);
151 msg = lua_pushvfstring(L, fmt, argp);
152 va_end(argp);
153 if (lua_getstack(L, 1, &ar)) { /* check calling function */
154 lua_getinfo(L, "Snl", &ar);
155 if (ar.currentline > 0)
156 lua_pushfstring(L, "%s:%d: %s", ar.short_src, ar.currentline, msg);
157 }
158 return lua_errorobj(L);
159}
160
161 176
162/* 177/*
163** {====================================================== 178** {======================================================
@@ -284,3 +299,144 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
284 } 299 }
285} 300}
286 301
302
303/*
304** {======================================================
305** Load functions
306** =======================================================
307*/
308
309typedef struct LoadF {
310 FILE *f;
311 char buff[LUAL_BUFFERSIZE];
312} LoadF;
313
314
315static const char *getF (void *ud, size_t *size) {
316 LoadF *lf = (LoadF *)ud;
317 *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
318 return (*size > 0) ? lf->buff : NULL;
319}
320
321
322static int errfile (lua_State *L, const char *filename) {
323 if (filename == NULL) filename = "stdin";
324 lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
325 return LUA_ERRFILE;
326}
327
328
329LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
330 LoadF lf;
331 int status;
332 int c;
333 int old_top = lua_gettop(L);
334 lf.f = (filename == NULL) ? stdin : fopen(filename, "r");
335 if (lf.f == NULL) return errfile(L, filename); /* unable to open file */
336 c = ungetc(getc(lf.f), lf.f);
337 if (!(isspace(c) || isprint(c)) && lf.f != stdin) { /* binary file? */
338 fclose(lf.f);
339 lf.f = fopen(filename, "rb"); /* reopen in binary mode */
340 if (lf.f == NULL) return errfile(L, filename); /* unable to reopen file */
341 }
342 if (filename == NULL)
343 lua_pushliteral(L, "=stdin");
344 else
345 lua_pushfstring(L, "@%s", filename);
346 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
347 lua_remove(L, old_top+1); /* remove filename from stack */
348 if (ferror(lf.f)) {
349 lua_settop(L, old_top); /* ignore results from `lua_load' */
350 return errfile(L, filename);
351 }
352 if (lf.f != stdin)
353 fclose(lf.f);
354 return status;
355}
356
357
358typedef struct LoadS {
359 const char *s;
360 size_t size;
361} LoadS;
362
363
364static const char *getS (void *ud, size_t *size) {
365 LoadS *ls = (LoadS *)ud;
366 if (ls->size == 0) return NULL;
367 *size = ls->size;
368 ls->size = 0;
369 return ls->s;
370}
371
372
373LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
374 const char *name) {
375 LoadS ls;
376 ls.s = buff;
377 ls.size = size;
378 return lua_load(L, getS, &ls, name);
379}
380
381/* }====================================================== */
382
383
384/*
385** {======================================================
386** compatibility code
387** =======================================================
388*/
389
390
391static void callalert (lua_State *L, int status) {
392 if (status != 0) {
393 int top = lua_gettop(L);
394 lua_getglobal(L, "_ALERT");
395 lua_insert(L, -2);
396 lua_pcall(L, 1, 0, 0);
397 lua_settop(L, top-1);
398 }
399}
400
401
402LUALIB_API int lua_call (lua_State *L, int nargs, int nresults) {
403 int status;
404 int errpos = lua_gettop(L) - nargs;
405 lua_getglobal(L, "_ERRORMESSAGE");
406 lua_insert(L, errpos); /* put below function and args */
407 status = lua_pcall(L, nargs, nresults, errpos);
408 lua_remove(L, errpos);
409 callalert(L, status);
410 return status;
411}
412
413
414static int aux_do (lua_State *L, int status) {
415 if (status == 0) { /* parse OK? */
416 int err = lua_gettop(L);
417 lua_getglobal(L, "_ERRORMESSAGE");
418 lua_insert(L, err);
419 status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
420 lua_remove(L, err); /* remove error function */
421 }
422 callalert(L, status);
423 return status;
424}
425
426
427LUALIB_API int lua_dofile (lua_State *L, const char *filename) {
428 return aux_do(L, luaL_loadfile(L, filename));
429}
430
431
432LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
433 const char *name) {
434 return aux_do(L, luaL_loadbuffer(L, buff, size, name));
435}
436
437
438LUALIB_API int lua_dostring (lua_State *L, const char *str) {
439 return lua_dobuffer(L, str, strlen(str), str);
440}
441
442/* }====================================================== */
diff --git a/lauxlib.h b/lauxlib.h
index f8c0235e..227e77a6 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.46 2002/05/06 19:05:10 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.47 2002/05/16 18:39:46 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -51,6 +51,10 @@ LUALIB_API int luaL_findstring (const char *name,
51LUALIB_API int luaL_ref (lua_State *L, int t); 51LUALIB_API int luaL_ref (lua_State *L, int t);
52LUALIB_API void luaL_unref (lua_State *L, int t, int ref); 52LUALIB_API void luaL_unref (lua_State *L, int t, int ref);
53 53
54LUALIB_API int luaL_loadfile (lua_State *L, const char *filename);
55LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
56 const char *name);
57
54 58
55 59
56/* 60/*
@@ -113,6 +117,12 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B);
113#define luaL_checktype luaL_check_type 117#define luaL_checktype luaL_check_type
114#define luaL_checkany luaL_check_any 118#define luaL_checkany luaL_check_any
115 119
120LUALIB_API int lua_call (lua_State *L, int nargs, int nresults);
121LUALIB_API int lua_dofile (lua_State *L, const char *filename);
122LUALIB_API int lua_dostring (lua_State *L, const char *str);
123LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
124 const char *name);
125
116 126
117#endif 127#endif
118 128
diff --git a/lbaselib.c b/lbaselib.c
index 9c711c8d..88c8db67 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.74 2002/05/16 18:39:46 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.75 2002/05/16 19:09:19 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -174,7 +174,7 @@ static int luaB_nexti (lua_State *L) {
174 else { /* `for' step */ 174 else { /* `for' step */
175 i++; /* next value */ 175 i++; /* next value */
176 lua_pushnumber(L, i); 176 lua_pushnumber(L, i);
177 lua_rawgeti(L, 1, i); 177 lua_rawgeti(L, 1, (int)i);
178 return (lua_isnil(L, -1)) ? 0 : 2; 178 return (lua_isnil(L, -1)) ? 0 : 2;
179 } 179 }
180} 180}
@@ -194,20 +194,20 @@ static int luaB_loadstring (lua_State *L) {
194 size_t l; 194 size_t l;
195 const char *s = luaL_check_lstr(L, 1, &l); 195 const char *s = luaL_check_lstr(L, 1, &l);
196 const char *chunkname = luaL_opt_string(L, 2, s); 196 const char *chunkname = luaL_opt_string(L, 2, s);
197 return passresults(L, lua_loadbuffer(L, s, l, chunkname)); 197 return passresults(L, luaL_loadbuffer(L, s, l, chunkname));
198} 198}
199 199
200 200
201static int luaB_loadfile (lua_State *L) { 201static int luaB_loadfile (lua_State *L) {
202 const char *fname = luaL_opt_string(L, 1, NULL); 202 const char *fname = luaL_opt_string(L, 1, NULL);
203 return passresults(L, lua_loadfile(L, fname)); 203 return passresults(L, luaL_loadfile(L, fname));
204} 204}
205 205
206 206
207static int luaB_assert (lua_State *L) { 207static int luaB_assert (lua_State *L) {
208 luaL_check_any(L, 1); 208 luaL_check_any(L, 1);
209 if (!lua_toboolean(L, 1)) 209 if (!lua_toboolean(L, 1))
210 return luaL_verror(L, "assertion failed! %s", luaL_opt_string(L, 2, "")); 210 return luaL_verror(L, "%s", luaL_opt_string(L, 2, "assertion failed!"));
211 lua_settop(L, 1); 211 lua_settop(L, 1);
212 return 1; 212 return 1;
213} 213}
@@ -349,7 +349,7 @@ static int luaB_require (lua_State *L) {
349 lua_settop(L, 3); /* reset stack position */ 349 lua_settop(L, 3); /* reset stack position */
350 if ((path = pushnextpath(L, path)) == NULL) break; 350 if ((path = pushnextpath(L, path)) == NULL) break;
351 pushcomposename(L); 351 pushcomposename(L);
352 status = lua_loadfile(L, lua_tostring(L, -1)); /* try to load it */ 352 status = luaL_loadfile(L, lua_tostring(L, -1)); /* try to load it */
353 } 353 }
354 } 354 }
355 switch (status) { 355 switch (status) {
diff --git a/ltests.c b/ltests.c
index 02b74eb2..c348a87c 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.121 2002/05/13 13:10:04 roberto Exp roberto $ 2** $Id: ltests.c,v 1.122 2002/05/16 14:59:49 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -397,7 +397,7 @@ static int doonnewstack (lua_State *L) {
397 lua_State *L1 = lua_newthread(L); 397 lua_State *L1 = lua_newthread(L);
398 size_t l; 398 size_t l;
399 const char *s = luaL_check_lstr(L, 1, &l); 399 const char *s = luaL_check_lstr(L, 1, &l);
400 int status = lua_loadbuffer(L1, s, l, s); 400 int status = luaL_loadbuffer(L1, s, l, s);
401 if (status == 0) 401 if (status == 0)
402 status = lua_pcall(L1, 0, 0, 0); 402 status = lua_pcall(L1, 0, 0, 0);
403 lua_pushnumber(L, status); 403 lua_pushnumber(L, status);
@@ -641,10 +641,10 @@ static int testC (lua_State *L) {
641 else if EQ("loadstring") { 641 else if EQ("loadstring") {
642 size_t sl; 642 size_t sl;
643 const char *s = luaL_check_lstr(L, getnum, &sl); 643 const char *s = luaL_check_lstr(L, getnum, &sl);
644 lua_loadbuffer(L, s, sl, s); 644 luaL_loadbuffer(L, s, sl, s);
645 } 645 }
646 else if EQ("loadfile") { 646 else if EQ("loadfile") {
647 lua_loadfile(L, luaL_check_string(L, getnum)); 647 luaL_loadfile(L, luaL_check_string(L, getnum));
648 } 648 }
649 else if EQ("setmetatable") { 649 else if EQ("setmetatable") {
650 lua_setmetatable(L, getnum); 650 lua_setmetatable(L, getnum);
diff --git a/lua.c b/lua.c
index 621abb83..5e1f4d5a 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.87 2002/05/16 19:09:19 roberto Exp roberto $ 2** $Id: lua.c,v 1.88 2002/05/23 19:43:04 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*/
@@ -107,16 +107,23 @@ static void print_usage (void) {
107} 107}
108 108
109 109
110static int l_panic (lua_State *l) {
111 (void)l;
112 fputs("unable to recover; exiting\n", stderr);
113 return 0;
114}
115
116
110static void print_version (void) { 117static void print_version (void) {
111 printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT); 118 printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
112} 119}
113 120
114 121
115static void assign (char *arg) { 122static void assign (const char *arg) {
116 char *eq = strchr(arg, '='); 123 char *eq = strchr(arg, '='); /* arg is `name=value'; find the `=' */
117 *eq = '\0'; /* spilt `arg' in two strings (name & value) */ 124 lua_pushlstring(L, arg, eq-arg); /* push name */
118 lua_pushstring(L, eq+1); 125 lua_pushstring(L, eq+1); /* push value */
119 lua_setglobal(L, arg); 126 lua_settable(L, LUA_GLOBALSINDEX); /* _G.name = value */
120} 127}
121 128
122 129
@@ -158,12 +165,12 @@ static int docall (int status) {
158 165
159 166
160static int file_input (const char *name) { 167static int file_input (const char *name) {
161 return docall(lua_loadfile(L, name)); 168 return docall(luaL_loadfile(L, name));
162} 169}
163 170
164 171
165static int dostring (const char *s, const char *name) { 172static int dostring (const char *s, const char *name) {
166 return docall(lua_loadbuffer(L, s, strlen(s), name)); 173 return docall(luaL_loadbuffer(L, s, strlen(s), name));
167} 174}
168 175
169 176
@@ -231,7 +238,7 @@ static int load_string (void) {
231 firstline = 0; 238 firstline = 0;
232 push_line(buffer); 239 push_line(buffer);
233 lua_concat(L, lua_gettop(L)); 240 lua_concat(L, lua_gettop(L));
234 status = lua_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); 241 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
235 } while (incomplete(status)); /* repeat loop to get rest of `line' */ 242 } while (incomplete(status)); /* repeat loop to get rest of `line' */
236 save_line(lua_tostring(L, 1)); 243 save_line(lua_tostring(L, 1));
237 lua_remove(L, 1); 244 lua_remove(L, 1);
@@ -363,6 +370,7 @@ int main (int argc, char *argv[]) {
363 int toclose = 0; 370 int toclose = 0;
364 (void)argc; /* to avoid warnings */ 371 (void)argc; /* to avoid warnings */
365 L = lua_open(); /* create state */ 372 L = lua_open(); /* create state */
373 lua_setpanicf(L, l_panic);
366 LUA_USERINIT(L); /* open libraries */ 374 LUA_USERINIT(L); /* open libraries */
367 register_own(argv); /* create own function */ 375 register_own(argv); /* create own function */
368 status = handle_luainit(); 376 status = handle_luainit();