summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-03 14:46:34 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-03 14:46:34 -0300
commit35a22ed1ab15fabb9404d0184165baa1d52394f1 (patch)
tree2dc281a4897de0eac364fa15695d11db030cd079
parentff91b355f451c7b7fc286d8ca9bc8613096f497d (diff)
downloadlua-github-35a22ed1ab15fabb9404d0184165baa1d52394f1.tar.gz
lua_load* replaced by a simple lua_load
-rw-r--r--lapi.c122
-rw-r--r--ldo.c4
-rw-r--r--lua.h20
-rw-r--r--lundump.h3
-rw-r--r--lzio.c56
-rw-r--r--lzio.h34
6 files changed, 55 insertions, 184 deletions
diff --git a/lapi.c b/lapi.c
index 9ad290f7..7e88dc03 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,11 +1,10 @@
/*
-** $Id: lapi.c,v 1.192 2002/05/16 18:39:46 roberto Exp roberto $
+** $Id: lapi.c,v 1.193 2002/05/27 20:35:40 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
-#include <stdio.h>
#include <string.h>
#include "lua.h"
@@ -31,11 +30,6 @@ const char lua_ident[] =
"$URL: www.lua.org $\n";
-#ifndef lua_filerror
-#include <errno.h>
-#define lua_fileerror (strerror(errno))
-#endif
-
#ifndef api_check
#define api_check(L, o) ((void)1)
@@ -368,9 +362,11 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
const char *ret;
va_list argp;
+ lua_lock(L);
va_start(argp, fmt);
- ret = lua_pushvfstring(L, fmt, argp);
+ ret = luaO_pushvfstring(L, fmt, argp);
va_end(argp);
+ lua_unlock(L);
return ret;
}
@@ -567,54 +563,19 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
}
-static int errfile (lua_State *L, const char *filename) {
- if (filename == NULL) filename = "stdin";
- lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
- return LUA_ERRFILE;
-}
-
-
-LUA_API int lua_loadfile (lua_State *L, const char *filename) {
+LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud,
+ int binary, const char *chunkname) {
ZIO z;
- int fnindex;
int status;
- int bin; /* flag for file mode */
- FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
- if (f == NULL) return errfile(L, filename); /* unable to open file */
- bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
- if (bin && f != stdin) {
- fclose(f);
- f = fopen(filename, "rb"); /* reopen in binary mode */
- if (f == NULL) return errfile(L, filename); /* unable to reopen file */
- }
- if (filename == NULL)
- lua_pushliteral(L, "=stdin");
- else
- lua_pushfstring(L, "@%s", filename);
- fnindex = lua_gettop(L); /* stack index of file name */
- luaZ_Fopen(&z, f, lua_tostring(L, fnindex));
- status = luaD_protectedparser(L, &z, bin);
- lua_remove(L, fnindex);
- if (ferror(f)) {
- if (status == 0) lua_pop(L, 1); /* remove chunk */
- return errfile(L, filename);
- }
- if (f != stdin)
- fclose(f);
+ lua_lock(L);
+ if (!chunkname) chunkname = "?";
+ luaZ_init(&z, getblock, ud, chunkname);
+ status = luaD_protectedparser(L, &z, binary);
+ lua_unlock(L);
return status;
}
-LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
- const char *name) {
- ZIO z;
- if (!name) name = "?";
- luaZ_mopen(&z, buff, size, name);
- return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
-}
-
-
-
/*
** Garbage-collection functions
*/
@@ -722,7 +683,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
lua_lock(L);
api_checknelems(L, n);
if (n >= 2) {
- luaV_strconc(L, n, L->top - L->ci->base - 1);
+ luaV_concat(L, n, L->top - L->ci->base - 1);
L->top -= (n-1);
luaC_checkGC(L);
}
@@ -763,62 +724,3 @@ LUA_API int lua_pushupvalues (lua_State *L) {
}
-
-/*
-** {======================================================
-** compatibility code
-** =======================================================
-*/
-
-
-static void callalert (lua_State *L, int status) {
- if (status != 0) {
- int top = lua_gettop(L);
- lua_getglobal(L, "_ALERT");
- lua_insert(L, -2);
- lua_pcall(L, 1, 0, 0);
- lua_settop(L, top-1);
- }
-}
-
-
-LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
- int status;
- int errpos = lua_gettop(L) - nargs;
- lua_getglobal(L, "_ERRORMESSAGE");
- lua_insert(L, errpos); /* put below function and args */
- status = lua_pcall(L, nargs, nresults, errpos);
- lua_remove(L, errpos);
- callalert(L, status);
- return status;
-}
-
-static int aux_do (lua_State *L, int status) {
- if (status == 0) { /* parse OK? */
- int err = lua_gettop(L);
- lua_getglobal(L, "_ERRORMESSAGE");
- lua_insert(L, err);
- status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
- lua_remove(L, err); /* remove error function */
- }
- callalert(L, status);
- return status;
-}
-
-
-LUA_API int lua_dofile (lua_State *L, const char *filename) {
- return aux_do(L, lua_loadfile(L, filename));
-}
-
-
-LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
- const char *name) {
- return aux_do(L, lua_loadbuffer(L, buff, size, name));
-}
-
-
-LUA_API int lua_dostring (lua_State *L, const char *str) {
- return lua_dobuffer(L, str, strlen(str), str);
-}
-
-/* }====================================================== */
diff --git a/ldo.c b/ldo.c
index c11c5639..ee9dd249 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.c,v 1.176 2002/05/16 18:39:46 roberto Exp roberto $
+** $Id: ldo.c,v 1.177 2002/05/27 20:35:40 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -420,7 +420,6 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
struct SParser p;
lu_mem old_blocks;
int status;
- lua_lock(L);
p.z = z; p.bin = bin;
/* before parsing, give a (good) chance to GC */
if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
@@ -437,7 +436,6 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
setobj(L->top++, &p.err);
lua_assert(status != LUA_ERRRUN);
}
- lua_unlock(L);
return status;
}
diff --git a/lua.h b/lua.h
index 710e0c4d..6277b98c 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
/*
-** $Id: lua.h,v 1.133 2002/05/16 18:39:46 roberto Exp roberto $
+** $Id: lua.h,v 1.134 2002/05/23 20:29:05 roberto Exp roberto $
** Lua - An Extensible Extension Language
** Tecgraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org
@@ -51,6 +51,11 @@ typedef struct lua_State lua_State;
typedef int (*lua_CFunction) (lua_State *L);
+/*
+** function for loading Lua code
+*/
+typedef const char * (*lua_Getblock) (void *ud, size_t *size);
+
/*
** basic types
@@ -176,9 +181,8 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex);
*/
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf);
-LUA_API int lua_loadfile (lua_State *L, const char *filename);
-LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
- const char *name);
+LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud,
+ int binary, const char *chunkname);
/*
@@ -249,11 +253,6 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
** compatibility macros and functions
*/
-LUA_API int lua_call (lua_State *L, int nargs, int nresults);
-LUA_API int lua_dofile (lua_State *L, const char *filename);
-LUA_API int lua_dostring (lua_State *L, const char *str);
-LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
- const char *name);
LUA_API int lua_pushupvalues (lua_State *L);
@@ -292,6 +291,9 @@ LUA_API int lua_pushupvalues (lua_State *L);
** =======================================================================
*/
+/* binary files start with <esc>Lua */
+#define LUA_SIGNATURE "\033Lua"
+
/* formats for Lua numbers */
#ifndef LUA_NUMBER_SCAN
#define LUA_NUMBER_SCAN "%lf"
diff --git a/lundump.h b/lundump.h
index b82f0b8c..6c647acf 100644
--- a/lundump.h
+++ b/lundump.h
@@ -1,5 +1,5 @@
/*
-** $Id: lundump.h,v 1.23 2001/06/28 13:55:17 lhf Exp lhf $
+** $Id: lundump.h,v 1.23 2001/07/12 19:34:03 roberto Exp roberto $
** load pre-compiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -19,7 +19,6 @@ int luaU_endianness (void);
/* definitions for headers of binary files */
#define VERSION 0x41 /* last format change was in 4.1 */
#define VERSION0 0x41 /* last major change was in 4.1 */
-#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
/* a multiple of PI for testing native format */
/* multiplying by 1E8 gives non-trivial integer values */
diff --git a/lzio.c b/lzio.c
index 6bbe6982..3e3610c9 100644
--- a/lzio.c
+++ b/lzio.c
@@ -1,69 +1,44 @@
/*
-** $Id: lzio.c,v 1.15 2001/11/28 20:13:13 roberto Exp roberto $
+** $Id: lzio.c,v 1.16 2002/04/29 12:37:41 roberto Exp roberto $
** a generic input stream interface
** See Copyright Notice in lua.h
*/
-
-#include <stdio.h>
#include <string.h>
#include "lua.h"
+#include "llimits.h"
#include "lzio.h"
-/* ----------------------------------------------------- memory buffers --- */
-
-static int zmfilbuf (ZIO* z) {
- (void)z; /* to avoid warnings */
- return EOZ;
-}
-
-
-ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
- if (b==NULL) return NULL;
- z->n = size;
- z->p = (const unsigned char *)b;
- z->filbuf = zmfilbuf;
- z->u = NULL;
- z->name = name;
- return z;
-}
-
-
-/* -------------------------------------------------------------- FILEs --- */
-
-static int zffilbuf (ZIO* z) {
- size_t n;
- if (feof((FILE *)z->u)) return EOZ;
- n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
- if (n==0) return EOZ;
- z->n = n-1;
- z->p = z->buffer;
+int luaZ_fill (ZIO *z) {
+ size_t size;
+ const char *buff = z->getblock(z->ud, &size);
+ if (buff == NULL || size == 0) return EOZ;
+ z->n = size - 1;
+ z->p = buff;
return *(z->p++);
}
-ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
- if (f==NULL) return NULL;
- z->n = 0;
- z->p = z->buffer;
- z->filbuf = zffilbuf;
- z->u = f;
+void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name) {
+ z->getblock = getblock;
+ z->ud = ud;
z->name = name;
- return z;
+ z->n = 0;
+ z->p = NULL;
}
/* --------------------------------------------------------------- read --- */
-size_t zread (ZIO *z, void *b, size_t n) {
+size_t luaZ_zread (ZIO *z, void *b, size_t n) {
while (n) {
size_t m;
if (z->n == 0) {
- if (z->filbuf(z) == EOZ)
+ if (luaZ_fill(z) == EOZ)
return n; /* return number of missing bytes */
else {
++z->n; /* filbuf removed first byte; put back it */
@@ -79,3 +54,4 @@ size_t zread (ZIO *z, void *b, size_t n) {
}
return 0;
}
+
diff --git a/lzio.h b/lzio.h
index 283fdc38..9ffbbf8a 100644
--- a/lzio.h
+++ b/lzio.h
@@ -1,5 +1,5 @@
/*
-** $Id: lzio.h,v 1.8 2001/03/26 14:31:49 roberto Exp roberto $
+** $Id: lzio.h,v 1.9 2002/04/29 12:37:41 roberto Exp roberto $
** Buffered streams
** See Copyright Notice in lua.h
*/
@@ -8,43 +8,37 @@
#ifndef lzio_h
#define lzio_h
-#include <stdio.h>
-
+#include "lua.h"
/* For Lua only */
-#define zFopen luaZ_Fopen
-#define zmopen luaZ_mopen
-#define zread luaZ_read
+#define zread luaZ_zread
#define EOZ (-1) /* end of stream */
typedef struct zio ZIO;
-ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
-ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
-
-size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
+#define zgetc(z) (((z)->n--)>0 ? \
+ cast(int, cast(unsigned char, *(z)->p++)) : \
+ luaZ_fill(z))
-#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
#define zname(z) ((z)->name)
+void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name);
+size_t luaZ_zread (ZIO* z, void* b, size_t n); /* read next n bytes */
/* --------- Private Part ------------------ */
-#ifndef ZBSIZE
-#define ZBSIZE 256 /* buffer size */
-#endif
-
struct zio {
- size_t n; /* bytes still unread */
- const unsigned char* p; /* current position in buffer */
- int (*filbuf)(ZIO* z);
- void* u; /* additional data */
+ size_t n; /* bytes still unread */
+ const char *p; /* current position in buffer */
+ lua_Getblock getblock;
+ void* ud; /* additional data */
const char *name;
- unsigned char buffer[ZBSIZE]; /* buffer */
};
+int luaZ_fill (ZIO *z);
+
#endif