Skip to content

Commit

Permalink
many details (most by lhf).
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed Mar 3, 2000
1 parent f7840a3 commit 3c9d999
Show file tree
Hide file tree
Showing 32 changed files with 280 additions and 293 deletions.
39 changes: 20 additions & 19 deletions lapi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.71 2000/02/08 16:34:31 roberto Exp roberto $
** $Id: lapi.c,v 1.72 2000/02/22 17:54:16 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -41,9 +41,10 @@ const TObject *luaA_protovalue (const TObject *o) {
}


void luaA_checkCparams (lua_State *L, int nParams) {
if (nParams > L->top-L->Cstack.base)
lua_error(L, "API error - wrong number of arguments in C2lua stack");
void luaA_checkCargs (lua_State *L, int nargs) {
if (nargs > L->top-L->Cstack.base)
luaL_verror(L, "Lua API error - "
"expected at least %d arguments in C2lua stack", nargs);
}


Expand All @@ -70,7 +71,7 @@ static void top2LC (lua_State *L, int n) {


lua_Object lua_pop (lua_State *L) {
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
return luaA_putObjectOnTop(L);
}

Expand Down Expand Up @@ -103,34 +104,34 @@ lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {

lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
TObject *method;
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
method = L->top-1;
if ((ttype(method) != LUA_T_NIL) && (*lua_type(L, method) != 'f'))
lua_error(L, "API error - tag method must be a function or nil");
lua_error(L, "Lua API error - tag method must be a function or nil");
luaT_settagmethod(L, tag, event, method);
return luaA_putObjectOnTop(L);
}


lua_Object lua_seterrormethod (lua_State *L) {
lua_Object temp;
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
temp = lua_getglobal(L, "_ERRORMESSAGE");
lua_setglobal(L, "_ERRORMESSAGE");
return temp;
}


lua_Object lua_gettable (lua_State *L) {
luaA_checkCparams(L, 2);
luaA_checkCargs(L, 2);
luaV_gettable(L, L->top--);
return luaA_putObjectOnTop(L);
}


lua_Object lua_rawgettable (lua_State *L) {
lua_Object res;
luaA_checkCparams(L, 2);
luaA_checkCargs(L, 2);
if (ttype(L->top-2) != LUA_T_ARRAY)
lua_error(L, "indexed expression not a table in rawgettable");
res = luaA_putluaObject(L, luaH_get(L, avalue(L->top-2), L->top-1));
Expand All @@ -141,15 +142,15 @@ lua_Object lua_rawgettable (lua_State *L) {

void lua_settable (lua_State *L) {
StkId top;
luaA_checkCparams(L, 3);
luaA_checkCargs(L, 3);
top = L->top;
luaV_settable(L, top-3, top);
L->top = top-3; /* pop table, index, and value */
}


void lua_rawsettable (lua_State *L) {
luaA_checkCparams(L, 3);
luaA_checkCargs(L, 3);
luaV_rawsettable(L, L->top-3);
}

Expand All @@ -176,14 +177,14 @@ lua_Object lua_rawgetglobal (lua_State *L, const char *name) {


void lua_setglobal (lua_State *L, const char *name) {
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
luaV_setglobal(L, luaS_assertglobalbyname(L, name), L->top--);
}


void lua_rawsetglobal (lua_State *L, const char *name) {
GlobalVar *gv = luaS_assertglobalbyname(L, name);
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
gv->value = *(--L->top);
}

Expand Down Expand Up @@ -296,8 +297,8 @@ void lua_pushstring (lua_State *L, const char *s) {

void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
if (fn == NULL)
lua_error(L, "API error - attempt to push a NULL Cfunction");
luaA_checkCparams(L, n);
lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
luaA_checkCargs(L, n);
ttype(L->top) = LUA_T_CPROTO;
fvalue(L->top) = fn;
incr_top;
Expand All @@ -321,7 +322,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {

void lua_pushobject (lua_State *L, lua_Object o) {
if (o == LUA_NOOBJECT)
lua_error(L, "API error - attempt to push a NOOBJECT");
lua_error(L, "Lua API error - attempt to push a NOOBJECT");
*L->top = *o;
incr_top;
}
Expand All @@ -339,7 +340,7 @@ int lua_tag (lua_State *L, lua_Object o) {


void lua_settag (lua_State *L, int tag) {
luaA_checkCparams(L, 1);
luaA_checkCargs(L, 1);
luaT_realtag(L, tag);
switch (ttype(L->top-1)) {
case LUA_T_ARRAY:
Expand Down Expand Up @@ -406,7 +407,7 @@ int luaA_next (lua_State *L, const Hash *t, int i) {

int lua_next (lua_State *L, lua_Object t, int i) {
if (ttype(t) != LUA_T_ARRAY)
lua_error(L, "API error - object is not a table in `lua_next'");
lua_error(L, "Lua API error - object is not a table in `lua_next'");
i = luaA_next(L, avalue(t), i);
top2LC(L, (i==0) ? 0 : 2);
return i;
Expand Down
4 changes: 2 additions & 2 deletions lapi.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lapi.h,v 1.12 1999/12/23 18:19:57 roberto Exp roberto $
** $Id: lapi.h,v 1.13 2000/01/19 12:00:45 roberto Exp roberto $
** Auxiliary functions from Lua API
** See Copyright Notice in lua.h
*/
Expand All @@ -11,7 +11,7 @@
#include "lobject.h"


void luaA_checkCparams (lua_State *L, int nParams);
void luaA_checkCargs (lua_State *L, int nargs);
const TObject *luaA_protovalue (const TObject *o);
void luaA_pushobject (lua_State *L, const TObject *o);
GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g);
Expand Down
8 changes: 6 additions & 2 deletions lbuffer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lbuffer.c,v 1.10 1999/11/10 15:40:46 roberto Exp roberto $
** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
Expand All @@ -19,10 +19,14 @@
-------------------------------------------------------*/


/*
** amount of extra space (pre)allocated when buffer is reallocated
*/
#define EXTRABUFF 32


#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(L, size)
#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \
Openspace(L, size)

static void Openspace (lua_State *L, int size) {
L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
Expand Down
53 changes: 32 additions & 21 deletions lbuiltin.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
/*
** $Id: lbuiltin.c,v 1.92 2000/01/19 16:50:30 roberto Exp roberto $
** $Id: lbuiltin.c,v 1.93 2000/02/22 18:12:46 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/


/*
** =========================================================================
** All built-in functions are public (i.e. not static) and are named luaB_f,
** where f is the function name in Lua. So, if you do not need all these
** functions, you may register manually only the ones that you need.
** =========================================================================
*/


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -91,7 +100,7 @@ static Hash *gettable (lua_State *L, int arg) {


/*
** If your system does not support "stderr", redefine this function, or
** If your system does not support `stderr', redefine this function, or
** redefine _ERRORMESSAGE so that it won't need _ALERT.
*/
void luaB__ALERT (lua_State *L) {
Expand All @@ -116,9 +125,9 @@ void luaB__ERRORMESSAGE (lua_State *L) {


/*
** If your system does not support "stdout", you can just remove this function.
** If you need, you can define your own "print" function, following this
** model but changing "fputs" to put the strings at a proper place
** If your system does not support `stdout', you can just remove this function.
** If you need, you can define your own `print' function, following this
** model but changing `fputs' to put the strings at a proper place
** (a console window or a log file, for instance).
*/
#ifndef MAXPRINT
Expand Down Expand Up @@ -174,17 +183,17 @@ void luaB_error (lua_State *L) {
}

void luaB_setglobal (lua_State *L) {
const char *n = luaL_check_string(L, 1);
const char *name = luaL_check_string(L, 1);
lua_Object value = luaL_nonnullarg(L, 2);
lua_pushobject(L, value);
lua_setglobal(L, n);
lua_setglobal(L, name);
}

void luaB_rawsetglobal (lua_State *L) {
const char *n = luaL_check_string(L, 1);
const char *name = luaL_check_string(L, 1);
lua_Object value = luaL_nonnullarg(L, 2);
lua_pushobject(L, value);
lua_rawsetglobal(L, n);
lua_rawsetglobal(L, name);
}

void luaB_getglobal (lua_State *L) {
Expand Down Expand Up @@ -236,7 +245,7 @@ void luaB_settagmethod (lua_State *L) {
"function or nil expected");
#ifndef LUA_COMPAT_GC
if (strcmp(event, "gc") == 0 && tag != LUA_T_NIL)
lua_error(L, "cannot set this tag method from Lua");
lua_error(L, "cannot set this `gc' tag method from Lua");
#endif
lua_pushobject(L, nf);
lua_pushobject(L, lua_settagmethod(L, tag, event));
Expand Down Expand Up @@ -325,7 +334,7 @@ void luaB_call (lua_State *L) {
return; /* return nil to signal the error */
}
else
lua_error(L, NULL);
lua_error(L, NULL); /* propagate error without additional messages */
}
else { /* no errors */
if (strchr(options, 'p')) { /* pack results? */
Expand All @@ -340,22 +349,22 @@ void luaB_call (lua_State *L) {

void luaB_nextvar (lua_State *L) {
lua_Object o = luaL_nonnullarg(L, 1);
TaggedString *g;
TaggedString *name;
if (ttype(o) == LUA_T_NIL)
g = NULL;
name = NULL;
else {
luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "variable name expected");
g = tsvalue(o);
name = tsvalue(o);
}
if (!luaA_nextvar(L, g))
if (!luaA_nextvar(L, name))
lua_pushnil(L);
}


void luaB_next (lua_State *L) {
const Hash *a = gettable(L, 1);
lua_Object k = luaL_nonnullarg(L, 2);
int i; /* will get first element after `i' */
int i; /* `luaA_next' gets first element after `i' */
if (ttype(k) == LUA_T_NIL)
i = 0; /* get first */
else {
Expand Down Expand Up @@ -390,7 +399,8 @@ void luaB_tostring (lua_State *L) {
sprintf(buff, "function: %p", o->value.f);
break;
case LUA_T_USERDATA:
sprintf(buff, "userdata: %p", o->value.ts->u.d.value);
sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
o->value.ts->u.d.tag);
break;
case LUA_T_NIL:
lua_pushstring(L, "nil");
Expand Down Expand Up @@ -435,7 +445,7 @@ void luaB_foreachi (lua_State *L) {
luaD_call(L, L->top-3, 1);
if (ttype(L->top-1) != LUA_T_NIL)
return;
L->top--;
L->top--; /* remove nil result */
}
}

Expand Down Expand Up @@ -499,7 +509,7 @@ void luaB_tinsert (lua_State *L) {
pos = n+1;
}
luaV_setn(L, a, n+1); /* a.n = n+1 */
for ( ;n>=pos; n--)
for (; n>=pos; n--)
luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */
luaH_setint(L, a, pos, v); /* a[pos] = v */
}
Expand All @@ -521,6 +531,7 @@ void luaB_tremove (lua_State *L) {
/*
** {======================================================
** Quicksort
** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.)
*/

static void swap (lua_State *L, Hash *a, int i, int j) {
Expand Down Expand Up @@ -602,7 +613,7 @@ void luaB_sort (lua_State *L) {
lua_Object func = lua_getparam(L, 2);
luaL_arg_check(L, func == LUA_NOOBJECT || lua_isfunction(L, func), 2,
"function expected");
luaD_checkstack(L, 4); /* for Pivot, f, a, b (sort_comp) */
luaD_checkstack(L, 4); /* for pivot, f, a, b (sort_comp) */
auxsort(L, a, 1, n, func);
lua_pushobject(L, t);
}
Expand Down Expand Up @@ -640,7 +651,7 @@ static const struct luaL_reg builtin_funcs[] = {
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
/* "Extra" functions */
/* "Extra" functions */
{"assert", luaB_assert},
{"foreach", luaB_foreach},
{"foreachi", luaB_foreachi},
Expand Down
Loading

0 comments on commit 3c9d999

Please sign in to comment.