Roberto Ierusalimschy 31 years ago
parent
commit
aa7b1fcec4
2 changed files with 74 additions and 21 deletions
  1. 6 4
      lua.h
  2. 68 17
      opcode.c

+ 6 - 4
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.2 1994/11/04 10:47:49 roberto Exp roberto $
+** $Id: lua.h,v 3.3 1994/11/07 16:34:44 roberto Exp $
 */
 
 
@@ -29,6 +29,8 @@ typedef enum
 typedef void (*lua_CFunction) (void);
 typedef unsigned int lua_Object;
 
+lua_Object     lua_setfallback		(char *name, lua_CFunction fallback);
+
 void           lua_error		(char *s);
 int            lua_dofile 		(char *filename);
 int            lua_dostring 		(char *string);
@@ -54,7 +56,7 @@ lua_Object     lua_getglobal 		(char *name);
 int            lua_storeglobal		(char *name);
 
 int            lua_storesubscript	(void);
-lua_Object     lua_getIndex         	(void);
+lua_Object     lua_getsubscript         (void);
 
 int            lua_type 		(lua_Object object);
 
@@ -65,8 +67,8 @@ int            lua_type 		(lua_Object object);
 
 #define lua_call(f)           lua_callfunction(lua_getglobal(f))
 
-#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_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
+#define lua_getfield(o,f)   (lua_pushobject(o), lua_pushstring(f), lua_getsubscript())
 
 #define lua_isnil(_)            (lua_type(_)==LUA_T_NIL)
 #define lua_isnumber(_)         (lua_type(_)==LUA_T_NUMBER)

+ 68 - 17
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 3.3 1994/11/07 15:20:56 roberto Exp $";
+char *rcs_opcode="$Id: opcode.c,v 3.4 1994/11/07 16:34:44 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -99,7 +99,7 @@ void luaI_setfallback (void)
   {
     if (strcmp(fallBacks[i].kind, name) == 0)
     {
-      lua_pushobject(Ref(&fallBacks[i].function));
+      luaI_pushobject(&fallBacks[i].function);
       fallBacks[i].function = *Address(func);
       return;
     }
@@ -245,6 +245,12 @@ static void adjust_top (Object *newtop)
 }
 
 
+static void adjustC (int nParams)
+{
+  adjust_top(stack+CBase+nParams);
+}
+
+
 /*
 ** Call a C function. CBase will point to the top of the stack,
 ** and CnResults is the number of parameters. Returns an index
@@ -297,7 +303,7 @@ static void do_call (Object *func, int base, int nResults, int whereRes)
 
 /*
 ** Function to index a table. Receives the table at top-2 and the index
-** at top-1. Remove them from stack and push the result.
+** at top-1.
 */
 static void pushsubscript (void)
 {
@@ -432,6 +438,51 @@ int lua_dostring (char *string)
 }
 
 
+/*
+** API: set a function as a fallback
+*/
+lua_Object lua_setfallback (char *name, lua_CFunction fallback)
+{
+  static Object func = {LUA_T_CFUNCTION, luaI_setfallback};
+  adjustC(0);
+  lua_pushstring(name);
+  lua_pushcfunction(fallback);
+  do_protectedrun(&func, 1);
+  return (Ref(top-1));
+}
+
+
+/* 
+** API: receives on the stack the table and the index.
+** returns the value.
+*/
+lua_Object lua_getsubscript (void)
+{
+  static Byte code[2] = {PUSHINDEXED, RETCODE0};
+  int status;
+  Object func;
+  tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
+  adjustC(2);
+  status = do_protectedrun(&func, 1);
+  if (status == 0)
+    return (Ref(top-1));
+  else
+    return 0;
+}
+
+/* 
+** API: receives on the stack the table, the index, and the new value.
+*/
+int lua_storesubscript (void)
+{
+  static Byte code[2] = {STOREINDEXED, RETCODE0};
+  Object func;
+  tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
+  adjustC(3);
+  return(do_protectedrun(&func, 0));
+}
+
+
 /*
 ** Get a parameter, returning the object handle or 0 on error.
 ** 'number' must be 1 to get the first parameter.
@@ -501,11 +552,23 @@ lua_Object lua_getglobal (char *name)
 {
  int n = lua_findsymbol(name);
  if (n < 0) return 0;
- *(top-1) = s_object(n);
- top++;
+ *(top++) = s_object(n);
  return Ref(top-1);
 }
 
+/*
+** Store top of the stack at a global variable array field.
+** Return 1 on error, 0 on success.
+*/
+int lua_storeglobal (char *name)
+{
+ int n = lua_findsymbol (name);
+ if (n < 0) return 1;
+ adjustC(1);
+ s_object(n) = *(--top);
+ return 0;
+}
+
 /*
 ** Push a nil object
 */
@@ -576,18 +639,6 @@ void luaI_pushobject (Object *o)
  *top++ = *o;
 }
 
-/*
-** Store top of the stack at a global variable array field.
-** Return 1 on error, 0 on success.
-*/
-int lua_storeglobal (char *name)
-{
- int n = lua_findsymbol (name);
- if (n < 0) return 1;
- s_object(n) = *(--top);
- return 0;
-}
-
 int lua_type (lua_Object o)
 {
   if (o == 0)