lua_load* replaced by a simple lua_load

This commit is contained in:
Roberto Ierusalimschy
2002-06-03 14:46:34 -03:00
parent ff91b355f4
commit 35a22ed1ab
6 changed files with 55 additions and 184 deletions

122
lapi.c
View File

@@ -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 ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stdio.h>
#include <string.h> #include <string.h>
#include "lua.h" #include "lua.h"
@@ -31,11 +30,6 @@ const char lua_ident[] =
"$URL: www.lua.org $\n"; "$URL: www.lua.org $\n";
#ifndef lua_filerror
#include <errno.h>
#define lua_fileerror (strerror(errno))
#endif
#ifndef api_check #ifndef api_check
#define api_check(L, o) ((void)1) #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, ...) { LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
const char *ret; const char *ret;
va_list argp; va_list argp;
lua_lock(L);
va_start(argp, fmt); va_start(argp, fmt);
ret = lua_pushvfstring(L, fmt, argp); ret = luaO_pushvfstring(L, fmt, argp);
va_end(argp); va_end(argp);
lua_unlock(L);
return ret; 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) { LUA_API int lua_load (lua_State *L, lua_Getblock getblock, void *ud,
if (filename == NULL) filename = "stdin"; int binary, const char *chunkname) {
lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
return LUA_ERRFILE;
}
LUA_API int lua_loadfile (lua_State *L, const char *filename) {
ZIO z; ZIO z;
int fnindex;
int status; int status;
int bin; /* flag for file mode */ lua_lock(L);
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); if (!chunkname) chunkname = "?";
if (f == NULL) return errfile(L, filename); /* unable to open file */ luaZ_init(&z, getblock, ud, chunkname);
bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]); status = luaD_protectedparser(L, &z, binary);
if (bin && f != stdin) { lua_unlock(L);
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);
return status; 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 ** Garbage-collection functions
*/ */
@@ -722,7 +683,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
lua_lock(L); lua_lock(L);
api_checknelems(L, n); api_checknelems(L, n);
if (n >= 2) { 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); L->top -= (n-1);
luaC_checkGC(L); 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);
}
/* }====================================================== */

4
ldo.c
View File

@@ -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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -420,7 +420,6 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
struct SParser p; struct SParser p;
lu_mem old_blocks; lu_mem old_blocks;
int status; int status;
lua_lock(L);
p.z = z; p.bin = bin; p.z = z; p.bin = bin;
/* before parsing, give a (good) chance to GC */ /* before parsing, give a (good) chance to GC */
if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold) 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); setobj(L->top++, &p.err);
lua_assert(status != LUA_ERRRUN); lua_assert(status != LUA_ERRRUN);
} }
lua_unlock(L);
return status; return status;
} }

20
lua.h
View File

@@ -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 ** Lua - An Extensible Extension Language
** Tecgraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** Tecgraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org ** e-mail: info@lua.org
@@ -51,6 +51,11 @@ typedef struct lua_State lua_State;
typedef int (*lua_CFunction) (lua_State *L); typedef int (*lua_CFunction) (lua_State *L);
/*
** function for loading Lua code
*/
typedef const char * (*lua_Getblock) (void *ud, size_t *size);
/* /*
** basic types ** 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 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_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_load (lua_State *L, lua_Getblock getblock, void *ud,
LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size, int binary, const char *chunkname);
const char *name);
/* /*
@@ -249,11 +253,6 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
** compatibility macros and functions ** 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); 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 */ /* formats for Lua numbers */
#ifndef LUA_NUMBER_SCAN #ifndef LUA_NUMBER_SCAN
#define LUA_NUMBER_SCAN "%lf" #define LUA_NUMBER_SCAN "%lf"

View File

@@ -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 ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -19,7 +19,6 @@ int luaU_endianness (void);
/* definitions for headers of binary files */ /* definitions for headers of binary files */
#define VERSION 0x41 /* last format change was in 4.1 */ #define VERSION 0x41 /* last format change was in 4.1 */
#define VERSION0 0x41 /* last major 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 */ /* a multiple of PI for testing native format */
/* multiplying by 1E8 gives non-trivial integer values */ /* multiplying by 1E8 gives non-trivial integer values */

56
lzio.c
View File

@@ -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 ** a generic input stream interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stdio.h>
#include <string.h> #include <string.h>
#include "lua.h" #include "lua.h"
#include "llimits.h"
#include "lzio.h" #include "lzio.h"
/* ----------------------------------------------------- memory buffers --- */ int luaZ_fill (ZIO *z) {
size_t size;
static int zmfilbuf (ZIO* z) { const char *buff = z->getblock(z->ud, &size);
(void)z; /* to avoid warnings */ if (buff == NULL || size == 0) return EOZ;
return EOZ; z->n = size - 1;
} z->p = buff;
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;
return *(z->p++); return *(z->p++);
} }
ZIO* zFopen (ZIO* z, FILE* f, const char *name) { void luaZ_init (ZIO *z, lua_Getblock getblock, void *ud, const char *name) {
if (f==NULL) return NULL; z->getblock = getblock;
z->n = 0; z->ud = ud;
z->p = z->buffer;
z->filbuf = zffilbuf;
z->u = f;
z->name = name; z->name = name;
return z; z->n = 0;
z->p = NULL;
} }
/* --------------------------------------------------------------- read --- */ /* --------------------------------------------------------------- read --- */
size_t zread (ZIO *z, void *b, size_t n) { size_t luaZ_zread (ZIO *z, void *b, size_t n) {
while (n) { while (n) {
size_t m; size_t m;
if (z->n == 0) { if (z->n == 0) {
if (z->filbuf(z) == EOZ) if (luaZ_fill(z) == EOZ)
return n; /* return number of missing bytes */ return n; /* return number of missing bytes */
else { else {
++z->n; /* filbuf removed first byte; put back it */ ++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; return 0;
} }

34
lzio.h
View File

@@ -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 ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -8,43 +8,37 @@
#ifndef lzio_h #ifndef lzio_h
#define lzio_h #define lzio_h
#include <stdio.h> #include "lua.h"
/* For Lua only */ /* For Lua only */
#define zFopen luaZ_Fopen #define zread luaZ_zread
#define zmopen luaZ_mopen
#define zread luaZ_read
#define EOZ (-1) /* end of stream */ #define EOZ (-1) /* end of stream */
typedef struct zio ZIO; typedef struct zio ZIO;
ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */ #define zgetc(z) (((z)->n--)>0 ? \
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */ cast(int, cast(unsigned char, *(z)->p++)) : \
luaZ_fill(z))
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
#define zname(z) ((z)->name) #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 ------------------ */ /* --------- Private Part ------------------ */
#ifndef ZBSIZE
#define ZBSIZE 256 /* buffer size */
#endif
struct zio { struct zio {
size_t n; /* bytes still unread */ size_t n; /* bytes still unread */
const unsigned char* p; /* current position in buffer */ const char *p; /* current position in buffer */
int (*filbuf)(ZIO* z); lua_Getblock getblock;
void* u; /* additional data */ void* ud; /* additional data */
const char *name; const char *name;
unsigned char buffer[ZBSIZE]; /* buffer */
}; };
int luaZ_fill (ZIO *z);
#endif #endif