Browse Source

new API: lua_Object now is an integer

Roberto Ierusalimschy 31 years ago
parent
commit
d95a8b3121
6 changed files with 112 additions and 141 deletions
  1. 11 11
      hash.c
  2. 12 21
      inout.c
  3. 12 16
      lua.h
  4. 64 81
      opcode.c
  5. 3 2
      opcode.h
  6. 10 10
      table.c

+ 11 - 11
hash.c

@@ -3,7 +3,7 @@
 ** hash manager for lua
 */
 
-char *rcs_hash="$Id: hash.c,v 2.12 1994/11/03 22:20:15 roberto Exp $";
+char *rcs_hash="$Id: hash.c,v 2.13 1994/11/07 15:19:51 roberto Exp roberto $";
 
 #include <string.h>
 #include <stdlib.h>
@@ -293,29 +293,29 @@ static void hashnext (Hash *t, int i)
    return;
   }
  }
- lua_pushobject(ref(node(t,i)));
- lua_pushobject(val(node(t,i)));
+ luaI_pushobject(ref(node(t,i)));
+ luaI_pushobject(val(node(t,i)));
 }
 
 void lua_next (void)
 {
  Hash   *t;
- Object *o = lua_getparam (1);
- Object *r = lua_getparam (2);
- if (o == NULL || r == NULL)
+ lua_Object o = lua_getparam(1);
+ lua_Object r = lua_getparam(2);
+ if (o == 0 || r == 0)
    lua_error ("too few arguments to function `next'");
- if (lua_getparam (3) != NULL)
+ if (lua_getparam(3) != 0)
    lua_error ("too many arguments to function `next'");
- if (tag(o) != LUA_T_ARRAY)
+ if (!lua_istable(o))
    lua_error ("first argument of function `next' is not a table");
- t = avalue(o);
- if (tag(r) == LUA_T_NIL)
+ t = avalue(luaI_Address(o));
+ if (lua_isnil(r))
  {
   hashnext(t, 0);
  }
  else
  {
-  int h = present (t, r);
+  int h = present (t, luaI_Address(r));
   hashnext(t, h+1);
  }
 }

+ 12 - 21
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.6 1994/11/02 20:29:39 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.7 1994/11/03 22:34:29 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,15 +32,6 @@ static int nfuncstack=0;
 
 static FILE *fp;
 static char *st;
-static void (*usererror) (char *s);
-
-/*
-** Function to set user function to handle errors.
-*/
-void lua_errorfunction (void (*fn) (char *s))
-{
- usererror = fn;
-}
 
 /*
 ** Function to get the next character from the input file
@@ -202,16 +193,16 @@ void lua_internaldofile (void)
 void lua_print (void)
 {
  int i=1;
- Object *obj;
- while ((obj=lua_getparam (i++)) != NULL)
+ lua_Object obj;
+ while ((obj=lua_getparam (i++)) != 0)
  {
-  if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber (obj));
-  else if (lua_isstring(obj))    printf("%s\n",lua_getstring (obj));
-  else if (lua_isfunction(obj))  printf("function: %p\n",bvalue(obj));
-  else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj)
+  if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber(obj));
+  else if (lua_isstring(obj))    printf("%s\n",lua_getstring(obj));
+  else if (lua_isfunction(obj))  printf("function: %p\n",bvalue(luaI_Address(obj)));
+  else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
 );
-  else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata (obj));
-  else if (lua_istable(obj))     printf("table: %p\n",avalue(obj));
+  else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata(obj));
+  else if (lua_istable(obj))     printf("table: %p\n",avalue(luaI_Address(obj)));
   else if (lua_isnil(obj))       printf("nil\n");
   else                           printf("invalid value to print\n");
  }
@@ -223,10 +214,10 @@ void lua_print (void)
 */
 void luaI_type (void)
 {
-  Object *o = lua_getparam(1);
-  if (o == NULL)
+  lua_Object o = lua_getparam(1);
+  if (o == 0)
     lua_error("no parameter to function 'type'");
-  switch (tag(o))
+  switch (lua_type(o))
   {
     case LUA_T_NIL :
       lua_pushstring("nil");

+ 12 - 16
lua.h

@@ -2,7 +2,7 @@
 ** LUA - Linguagem para Usuarios de Aplicacao
 ** Grupo de Tecnologia em Computacao Grafica
 ** TeCGraf - PUC-Rio
-** $Id: lua.h,v 3.1 1994/11/02 20:30:53 roberto Exp roberto $
+** $Id: lua.h,v 3.2 1994/11/04 10:47:49 roberto Exp roberto $
 */
 
 
@@ -21,56 +21,52 @@ typedef enum
  LUA_T_FUNCTION,
  LUA_T_CFUNCTION,
  LUA_T_USERDATA
-} Type;
+} lua_Type;
  
 
 /* Public Part */
 
 typedef void (*lua_CFunction) (void);
-typedef struct Object *lua_Object;
+typedef unsigned int lua_Object;
 
-#define lua_register(n,f)	(lua_pushcfunction(f), lua_storeglobal(n))
-
-void           lua_errorfunction    	(void (*fn) (char *s));
 void           lua_error		(char *s);
 int            lua_dofile 		(char *filename);
 int            lua_dostring 		(char *string);
 int            lua_callfunction		(lua_Object function);
 
 lua_Object     lua_getparam 		(int number);
+#define	       lua_getresult		lua_getparam
+
 float          lua_getnumber 		(lua_Object object);
 char          *lua_getstring 		(lua_Object object);
 char 	      *lua_copystring 		(lua_Object object);
 lua_CFunction  lua_getcfunction 	(lua_Object object);
 void          *lua_getuserdata  	(lua_Object object);
-void          *lua_gettable  	        (lua_Object object);
-lua_Object     lua_getfield         	(lua_Object object, char *field);
-lua_Object     lua_getindexed         	(lua_Object object, float index);
-lua_Object     lua_getglobal 		(char *name);
 
 int 	       lua_pushnil 		(void);
 int            lua_pushnumber 		(float n);
 int            lua_pushstring 		(char *s);
 int            lua_pushcfunction	(lua_CFunction fn);
 int            lua_pushuserdata     	(void *u);
-int            lua_pushtable     	(void *t);
-int            lua_pushsubscript	(void);
 int            lua_pushobject       	(lua_Object object);
 
+lua_Object     lua_getglobal 		(char *name);
 int            lua_storeglobal		(char *name);
-int            lua_storefield 		(lua_Object object, char *field);
-int            lua_storeindexed 	(lua_Object object, float index);
+
 int            lua_storesubscript	(void);
+lua_Object     lua_getIndex         	(void);
 
 int            lua_type 		(lua_Object object);
 
 
 /* for lua 1.1 */
 
+#define lua_register(n,f)	(lua_pushcfunction(f), lua_storeglobal(n))
+
 #define lua_call(f)           lua_callfunction(lua_getglobal(f))
 
-#define         lua_getindexed(o,n)     (lua_pushnumber(n), lua_getIndex(o))
-#define         lua_getfield(o,f)       (lua_pushstring(f), lua_getIndex(o))
+#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getIndex())
+#define lua_getfield(o,f)   (lua_pushobject(o), lua_pushstring(f), lua_getIndex())
 
 #define lua_isnil(_)            (lua_type(_)==LUA_T_NIL)
 #define lua_isnumber(_)         (lua_type(_)==LUA_T_NUMBER)

+ 64 - 81
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 3.2 1994/11/04 10:47:49 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.3 1994/11/07 15:20:56 roberto Exp $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,6 +32,12 @@ static Object *stack = NULL;
 static Object *top = NULL;
 
 
+/* macros to convert from lua_Object to (Object *) and back */
+ 
+#define Address(lo)     ((lo)+stack-1)
+#define Ref(st)         ((st)-stack+1)
+ 
+
 static int CBase = 0;   /* when Lua calls C or C calls Lua, points to the */
                         /* first slot after the last parameter. */
 static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
@@ -44,6 +50,12 @@ static int lua_execute (Byte *pc, int base);
 static void do_call (Object *func, int base, int nResults, int whereRes);
 
 
+Object *luaI_Address (lua_Object o)
+{
+  return Address(o);
+}
+
+
 /*
 ** Fallbacks
 */
@@ -87,8 +99,8 @@ void luaI_setfallback (void)
   {
     if (strcmp(fallBacks[i].kind, name) == 0)
     {
-      lua_pushobject(&fallBacks[i].function);
-      fallBacks[i].function = *func;
+      lua_pushobject(Ref(&fallBacks[i].function));
+      fallBacks[i].function = *Address(func);
       return;
     }
   }
@@ -96,6 +108,7 @@ void luaI_setfallback (void)
   lua_pushnil();
 }
 
+
 /*
 ** Error messages
 */
@@ -373,12 +386,12 @@ static int do_protectedrun (Object *function, int nResults)
 /*
 ** Execute the given lua function. Return 0 on success or 1 on error.
 */
-int lua_callfunction (Object *function)
+int lua_callfunction (lua_Object function)
 {
   if (function == NULL)
     return 1;
   else
-    return do_protectedrun (function, MULT_RET);
+    return do_protectedrun (Address(function), MULT_RET);
 }
 
 
@@ -420,73 +433,77 @@ int lua_dostring (char *string)
 
 
 /*
-** Get a parameter, returning the object handle or NULL on error.
+** Get a parameter, returning the object handle or 0 on error.
 ** 'number' must be 1 to get the first parameter.
 */
-Object *lua_getparam (int number)
+lua_Object lua_getparam (int number)
 {
- if (number <= 0 || number > CnResults) return NULL;
- return (stack+(CBase-CnResults+number-1));
+ if (number <= 0 || number > CnResults) return 0;
+ /* Ref(stack+(CBase-CnResults+number-1)) ==
+    stack+(CBase-CnResults+number-1)-stack+1 == */
+ return CBase-CnResults+number;
 }
 
 /*
 ** Given an object handle, return its number value. On error, return 0.0.
 */
-real lua_getnumber (Object *object)
+real lua_getnumber (lua_Object object)
 {
- if (object == NULL || tag(object) == LUA_T_NIL) return 0.0;
- if (tonumber (object)) return 0.0;
- else                   return (nvalue(object));
+ if (object == 0 || tag(Address(object)) == LUA_T_NIL) return 0.0;
+ if (tonumber (Address(object))) return 0.0;
+ else                   return (nvalue(Address(object)));
 }
 
 /*
 ** Given an object handle, return its string pointer. On error, return NULL.
 */
-char *lua_getstring (Object *object)
+char *lua_getstring (lua_Object object)
 {
- if (object == NULL || tag(object) == LUA_T_NIL) return NULL;
- if (tostring (object)) return NULL;
- else                   return (svalue(object));
+ if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
+ if (tostring (Address(object))) return NULL;
+ else return (svalue(Address(object)));
 }
 
 /*
 ** Given an object handle, return a copy of its string. On error, return NULL.
 */
-char *lua_copystring (Object *object)
+char *lua_copystring (lua_Object object)
 {
- if (object == NULL || tag(object) == LUA_T_NIL) return NULL;
- if (tostring (object)) return NULL;
- else                   return (strdup(svalue(object)));
+ if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
+ if (tostring (Address(object))) return NULL;
+ else return (strdup(svalue(Address(object))));
 }
 
 /*
 ** Given an object handle, return its cfuntion pointer. On error, return NULL.
 */
-lua_CFunction lua_getcfunction (Object *object)
+lua_CFunction lua_getcfunction (lua_Object object)
 {
- if (object == NULL) return NULL;
- if (tag(object) != LUA_T_CFUNCTION) return NULL;
- else                            return (fvalue(object));
+ if (object == 0) return NULL;
+ if (tag(Address(object)) != LUA_T_CFUNCTION) return NULL;
+ else return (fvalue(Address(object)));
 }
 
 /*
 ** Given an object handle, return its user data. On error, return NULL.
 */
-void *lua_getuserdata (Object *object)
+void *lua_getuserdata (lua_Object object)
 {
- if (object == NULL) return NULL;
- if (tag(object) != LUA_T_USERDATA) return NULL;
- else                           return (uvalue(object));
+ if (object == 0) return NULL;
+ if (tag(Address(object)) != LUA_T_USERDATA) return NULL;
+ else return (uvalue(Address(object)));
 }
 
 /*
 ** Get a global object. Return the object handle or NULL on error.
 */
-Object *lua_getglobal (char *name)
+lua_Object lua_getglobal (char *name)
 {
  int n = lua_findsymbol(name);
- if (n < 0) return NULL;
- return &s_object(n);
+ if (n < 0) return 0;
+ *(top-1) = s_object(n);
+ top++;
+ return Ref(top-1);
 }
 
 /*
@@ -541,15 +558,24 @@ int lua_pushuserdata (void *u)
 }
 
 /*
-** Push an object to stack.
+** Push a lua_Object to stack.
 */
-int lua_pushobject (Object *o)
+int lua_pushobject (lua_Object o)
 {
  lua_checkstack(top-stack+1);
- *top++ = *o;
+ *top++ = *Address(o);
  return 0;
 }
 
+/*
+** Push an object on the stack.
+*/
+void luaI_pushobject (Object *o)
+{
+ lua_checkstack(top-stack+1);
+ *top++ = *o;
+}
+
 /*
 ** Store top of the stack at a global variable array field.
 ** Return 1 on error, 0 on success.
@@ -558,60 +584,16 @@ int lua_storeglobal (char *name)
 {
  int n = lua_findsymbol (name);
  if (n < 0) return 1;
- if (top-stack <= CBase) return 1;
  s_object(n) = *(--top);
  return 0;
 }
 
-
-/*
-** Store top of the stack at an array field. Return 1 on error, 0 on success.
-*/
-int lua_storefield (lua_Object object, char *field)
-{
- if (tag(object) != LUA_T_ARRAY)
-  return 1;
- else
- {
-  Object ref, *h;
-  tag(&ref) = LUA_T_STRING;
-  svalue(&ref) = lua_createstring(field);
-  h = lua_hashdefine(avalue(object), &ref);
-  if (h == NULL) return 1;
-  if (tag(top-1) == LUA_T_MARK) return 1;
-  *h = *(--top);
- }
- return 0;
-}
-
-
-/*
-** Store top of the stack at an array index. Return 1 on error, 0 on success.
-*/
-int lua_storeindexed (lua_Object object, float index)
-{
- if (tag(object) != LUA_T_ARRAY)
-  return 1;
- else
- {
-  Object ref, *h;
-  tag(&ref) = LUA_T_NUMBER;
-  nvalue(&ref) = index;
-  h = lua_hashdefine(avalue(object), &ref);
-  if (h == NULL) return 1;
-  if (tag(top-1) == LUA_T_MARK) return 1;
-  *h = *(--top);
- }
- return 0;
-}
-
-
 int lua_type (lua_Object o)
 {
-  if (o == NULL)
+  if (o == 0)
     return LUA_T_NIL;
   else
-    return tag(o);
+    return tag(Address(o));
 }
 
 
@@ -1063,3 +1045,4 @@ static int lua_execute (Byte *pc, int base)
  }
 }
 
+

+ 3 - 2
opcode.h

@@ -1,6 +1,6 @@
 /*
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.3 1994/11/06 15:35:04 roberto Exp $
+** $Id: opcode.h,v 3.4 1994/11/07 15:20:56 roberto Exp roberto $
 */
 
 #ifndef opcode_h
@@ -152,7 +152,6 @@ typedef struct
                               code.m.c3 = *pc++; code.m.c4 = *pc++;}
  
 
-
 /* Exported functions */
 char   *lua_strdup (char *l);
 
@@ -162,5 +161,7 @@ int     yylex (void);		        /* from "lex.c" module */
 Byte   *lua_parse      (void); 		/* from "lua.stx" module */
 void    lua_travstack (void (*fn)(Object *));
 void    luaI_setfallback (void);
+Object *luaI_Address (lua_Object o);
+void	luaI_pushobject (Object *o);
 
 #endif

+ 10 - 10
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.10 1994/11/03 22:33:40 roberto Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.11 1994/11/04 17:20:00 roberto Exp roberto $";
 
 #include <stdlib.h>
 #include <string.h>
@@ -259,23 +259,23 @@ char *lua_filename (void)
 void lua_nextvar (void)
 {
  char *varname, *next;
- Object *o = lua_getparam (1);
- if (o == NULL)
+ lua_Object o = lua_getparam(1);
+ if (o == 0)
  { lua_error ("too few arguments to function `nextvar'"); return; }
- if (lua_getparam (2) != NULL)
+ if (lua_getparam(2) != NULL)
  { lua_error ("too many arguments to function `nextvar'"); return; }
- if (tag(o) == LUA_T_NIL)
+ if (lua_isnil(o))
  {
-  varname = 0;
+  varname = NULL;
  }
- else if (tag(o) != LUA_T_STRING) 
+ else if (!lua_isstring(o))
  { 
   lua_error ("incorrect argument to function `nextvar'"); 
   return;
  }
  else
  {
-  varname = svalue(o);
+  varname = lua_getstring(o);
  }
  next = lua_varnext(varname);
  if (next == NULL)
@@ -288,7 +288,7 @@ void lua_nextvar (void)
   Object name;
   tag(&name) = LUA_T_STRING;
   svalue(&name) = next;
-  if (lua_pushobject (&name)) return;
-  if (lua_pushobject (&s_object(indexstring(next)))) return;
+  luaI_pushobject(&name);
+  luaI_pushobject(&s_object(indexstring(next)));
  }
 }