Ver Fonte

functions "lua_is..." consider coercions.

Roberto Ierusalimschy há 29 anos atrás
pai
commit
a275d9a25b
5 ficheiros alterados com 41 adições e 52 exclusões
  1. 2 2
      fallback.c
  2. 21 25
      inout.c
  3. 4 3
      iolib.c
  4. 9 9
      lua.h
  5. 5 13
      strlib.c

+ 2 - 2
fallback.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
  
-char *rcs_fallback="$Id: fallback.c,v 1.20 1996/02/22 20:34:33 roberto Exp roberto $";
+char *rcs_fallback="$Id: fallback.c,v 1.21 1996/03/04 13:29:10 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <string.h>
@@ -49,7 +49,7 @@ void luaI_setfallback (void)
   int i;
   char *name = lua_getstring(lua_getparam(1));
   lua_Object func = lua_getparam(2);
-  if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
+  if (name == NULL || !lua_isfunction(func))
     lua_error("incorrect argument to function `setfallback'");
   for (i=0; i<N_FB; i++)
   {

+ 21 - 25
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.34 1996/03/15 13:13:13 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
 
 #include <stdio.h>
 
@@ -128,21 +128,26 @@ void lua_internaldofile (void)
 static char *tostring (lua_Object obj)
 {
   char *buff = luaI_buffer(20);
-  if (lua_isstring(obj))
+  if (lua_isstring(obj))   /* get strings and numbers */
     return lua_getstring(obj);
-  if (lua_isnumber(obj))
-    sprintf(buff, "%g", lua_getnumber(obj));
-  else if (lua_isfunction(obj))
-    sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
-  else if (lua_iscfunction(obj))
-    sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
-  else if (lua_isuserdata(obj))
-    sprintf(buff, "userdata: %p", lua_getuserdata(obj));
-  else if (lua_istable(obj))
-    sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
-  else if (lua_isnil(obj))
-    sprintf(buff, "nil");
-  else buff[0] = 0;
+  else switch(lua_type(obj))
+  {
+    case LUA_T_FUNCTION:
+      sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
+      break;
+    case LUA_T_CFUNCTION:
+      sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
+      break;
+    case LUA_T_ARRAY:
+      sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
+      break;
+    case LUA_T_NIL:
+      sprintf(buff, "nil");
+      break;
+    default:
+      sprintf(buff, "userdata: %p", lua_getuserdata(obj));
+      break;
+  }
   return buff;
 }
 
@@ -201,16 +206,7 @@ void lua_obj2number (void)
 {
   lua_Object o = lua_getparam(1);
   if (lua_isnumber(o))
-    lua_pushobject(o);
-  else if (lua_isstring(o))
-  {
-    char c;
-    float f;
-    if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
-      lua_pushnumber(f);
-    else
-      lua_pushnil();
-  }
+    lua_pushnumber(lua_getnumber(o));
   else
     lua_pushnil();
 }

+ 4 - 3
iolib.c

@@ -3,7 +3,7 @@
 ** Input/output library to LUA
 */
 
-char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 15:56:03 roberto Exp roberto $";
+char *rcs_iolib="$Id: iolib.c,v 1.39 1996/03/14 15:55:18 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <ctype.h>
@@ -422,9 +422,10 @@ static void io_write (void)
   if (lua_getparam (2) == LUA_NOOBJECT)   /* free format */
   {
     lua_Object o1 = lua_getparam(1);
-    if (lua_isnumber(o1))
+    int t = lua_type(o1);
+    if (t == LUA_T_NUMBER)
       status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
-    else if (lua_isstring(o1))
+    else if (t == LUA_T_STRING)
       status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
   }
   else					/* formated */

+ 9 - 9
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.22 1996/02/12 18:32:09 roberto Exp roberto $
+** $Id: lua.h,v 3.23 1996/02/14 13:40:26 roberto Exp $
 */
 
 
@@ -52,6 +52,14 @@ void	       lua_endblock		(void);
 lua_Object     lua_getparam 		(int number);
 #define	       lua_getresult(_)		lua_getparam(_)
 
+#define        lua_isnil(_)             (lua_type(_)==LUA_T_NIL)
+#define        lua_istable(_)           (lua_type(_)==LUA_T_ARRAY)
+#define        lua_isuserdata(_)        (lua_type(_)>=LUA_T_USERDATA)
+#define        lua_iscfunction(_)       (lua_type(_)==LUA_T_CFUNCTION)
+int            lua_isnumber             (lua_Object object);
+int            lua_isstring             (lua_Object object);
+int            lua_isfunction           (lua_Object object);
+
 float          lua_getnumber 		(lua_Object object);
 char          *lua_getstring 		(lua_Object object);
 lua_CFunction  lua_getcfunction 	(lua_Object object);
@@ -88,14 +96,6 @@ lua_Object     lua_createtable		(void);
 
 #define lua_pushuserdata(u)     lua_pushusertag(u, LUA_T_USERDATA)
 
-#define lua_isnil(_)            (lua_type(_)==LUA_T_NIL)
-#define lua_isnumber(_)         (lua_type(_)==LUA_T_NUMBER)
-#define lua_isstring(_)         (lua_type(_)==LUA_T_STRING)
-#define lua_istable(_)          (lua_type(_)==LUA_T_ARRAY)
-#define lua_isfunction(_)       (lua_type(_)==LUA_T_FUNCTION)
-#define lua_iscfunction(_)      (lua_type(_)==LUA_T_CFUNCTION)
-#define lua_isuserdata(_)       (lua_type(_)>=LUA_T_USERDATA)
-
 
 /* for compatibility with old versions. Avoid using these macros */
 

+ 5 - 13
strlib.c

@@ -3,7 +3,7 @@
 ** String library to LUA
 */
 
-char *rcs_strlib="$Id: strlib.c,v 1.18 1996/02/12 18:34:44 roberto Exp roberto $";
+char *rcs_strlib="$Id: strlib.c,v 1.19 1996/03/14 15:52:35 roberto Exp roberto $";
 
 #include <string.h>
 #include <stdio.h>
@@ -24,7 +24,7 @@ void lua_arg_error(char *funcname)
 char *lua_check_string (int numArg, char *funcname)
 {
   lua_Object o = lua_getparam(numArg);
-  if (!(lua_isstring(o) || lua_isnumber(o)))
+  if (!lua_isstring(o))
     lua_arg_error(funcname);
   return lua_getstring(o);
 }
@@ -32,17 +32,9 @@ char *lua_check_string (int numArg, char *funcname)
 double lua_check_number (int numArg, char *funcname)
 {
   lua_Object o = lua_getparam(numArg);
-  if (lua_isnumber(o))
-    return lua_getnumber(o);
-  else if (lua_isstring(o))
-  {
-    float t;
-    char c;
-    if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1)
-      return t;
-  }
-  lua_arg_error(funcname);
-  return 0;  /* to avoid warnings */
+  if (!lua_isnumber(o))
+    lua_arg_error(funcname);
+  return lua_getnumber(o);
 }
 
 char *luaI_addchar (int c)