aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-19 19:28:37 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-19 19:28:37 -0300
commita275d9a25b161af426696d7b73d46f91150309c9 (patch)
tree763a5694213fe30b231bc81777cec71828935696
parent7e0be1fbde80d72886e11bcbf114a8dbf6d5e1d9 (diff)
downloadlua-a275d9a25b161af426696d7b73d46f91150309c9.tar.gz
lua-a275d9a25b161af426696d7b73d46f91150309c9.tar.bz2
lua-a275d9a25b161af426696d7b73d46f91150309c9.zip
functions "lua_is..." consider coercions.
-rw-r--r--fallback.c4
-rw-r--r--inout.c46
-rw-r--r--iolib.c7
-rw-r--r--lua.h18
-rw-r--r--strlib.c18
5 files changed, 41 insertions, 52 deletions
diff --git a/fallback.c b/fallback.c
index dfbd2fd2..34d0b99c 100644
--- a/fallback.c
+++ b/fallback.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 1.20 1996/02/22 20:34:33 roberto Exp roberto $"; 6char *rcs_fallback="$Id: fallback.c,v 1.21 1996/03/04 13:29:10 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -49,7 +49,7 @@ void luaI_setfallback (void)
49 int i; 49 int i;
50 char *name = lua_getstring(lua_getparam(1)); 50 char *name = lua_getstring(lua_getparam(1));
51 lua_Object func = lua_getparam(2); 51 lua_Object func = lua_getparam(2);
52 if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func))) 52 if (name == NULL || !lua_isfunction(func))
53 lua_error("incorrect argument to function `setfallback'"); 53 lua_error("incorrect argument to function `setfallback'");
54 for (i=0; i<N_FB; i++) 54 for (i=0; i<N_FB; i++)
55 { 55 {
diff --git a/inout.c b/inout.c
index 0582522c..294d4137 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.34 1996/03/15 13:13:13 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11 11
@@ -128,21 +128,26 @@ void lua_internaldofile (void)
128static char *tostring (lua_Object obj) 128static char *tostring (lua_Object obj)
129{ 129{
130 char *buff = luaI_buffer(20); 130 char *buff = luaI_buffer(20);
131 if (lua_isstring(obj)) 131 if (lua_isstring(obj)) /* get strings and numbers */
132 return lua_getstring(obj); 132 return lua_getstring(obj);
133 if (lua_isnumber(obj)) 133 else switch(lua_type(obj))
134 sprintf(buff, "%g", lua_getnumber(obj)); 134 {
135 else if (lua_isfunction(obj)) 135 case LUA_T_FUNCTION:
136 sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf); 136 sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
137 else if (lua_iscfunction(obj)) 137 break;
138 sprintf(buff, "cfunction: %p", lua_getcfunction(obj)); 138 case LUA_T_CFUNCTION:
139 else if (lua_isuserdata(obj)) 139 sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
140 sprintf(buff, "userdata: %p", lua_getuserdata(obj)); 140 break;
141 else if (lua_istable(obj)) 141 case LUA_T_ARRAY:
142 sprintf(buff, "table: %p", avalue(luaI_Address(obj))); 142 sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
143 else if (lua_isnil(obj)) 143 break;
144 sprintf(buff, "nil"); 144 case LUA_T_NIL:
145 else buff[0] = 0; 145 sprintf(buff, "nil");
146 break;
147 default:
148 sprintf(buff, "userdata: %p", lua_getuserdata(obj));
149 break;
150 }
146 return buff; 151 return buff;
147} 152}
148 153
@@ -201,16 +206,7 @@ void lua_obj2number (void)
201{ 206{
202 lua_Object o = lua_getparam(1); 207 lua_Object o = lua_getparam(1);
203 if (lua_isnumber(o)) 208 if (lua_isnumber(o))
204 lua_pushobject(o); 209 lua_pushnumber(lua_getnumber(o));
205 else if (lua_isstring(o))
206 {
207 char c;
208 float f;
209 if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
210 lua_pushnumber(f);
211 else
212 lua_pushnil();
213 }
214 else 210 else
215 lua_pushnil(); 211 lua_pushnil();
216} 212}
diff --git a/iolib.c b/iolib.c
index ed6ead8d..a6ae51bd 100644
--- a/iolib.c
+++ b/iolib.c
@@ -3,7 +3,7 @@
3** Input/output library to LUA 3** Input/output library to LUA
4*/ 4*/
5 5
6char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 15:56:03 roberto Exp roberto $"; 6char *rcs_iolib="$Id: iolib.c,v 1.39 1996/03/14 15:55:18 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <ctype.h> 9#include <ctype.h>
@@ -422,9 +422,10 @@ static void io_write (void)
422 if (lua_getparam (2) == LUA_NOOBJECT) /* free format */ 422 if (lua_getparam (2) == LUA_NOOBJECT) /* free format */
423 { 423 {
424 lua_Object o1 = lua_getparam(1); 424 lua_Object o1 = lua_getparam(1);
425 if (lua_isnumber(o1)) 425 int t = lua_type(o1);
426 if (t == LUA_T_NUMBER)
426 status = fprintf (out, "%g", lua_getnumber(o1)) >= 0; 427 status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
427 else if (lua_isstring(o1)) 428 else if (t == LUA_T_STRING)
428 status = fprintf (out, "%s", lua_getstring(o1)) >= 0; 429 status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
429 } 430 }
430 else /* formated */ 431 else /* formated */
diff --git a/lua.h b/lua.h
index a268a176..1bfcfaf1 100644
--- a/lua.h
+++ b/lua.h
@@ -2,7 +2,7 @@
2** LUA - Linguagem para Usuarios de Aplicacao 2** LUA - Linguagem para Usuarios de Aplicacao
3** Grupo de Tecnologia em Computacao Grafica 3** Grupo de Tecnologia em Computacao Grafica
4** TeCGraf - PUC-Rio 4** TeCGraf - PUC-Rio
5** $Id: lua.h,v 3.22 1996/02/12 18:32:09 roberto Exp roberto $ 5** $Id: lua.h,v 3.23 1996/02/14 13:40:26 roberto Exp $
6*/ 6*/
7 7
8 8
@@ -52,6 +52,14 @@ void lua_endblock (void);
52lua_Object lua_getparam (int number); 52lua_Object lua_getparam (int number);
53#define lua_getresult(_) lua_getparam(_) 53#define lua_getresult(_) lua_getparam(_)
54 54
55#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
56#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
57#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
58#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
59int lua_isnumber (lua_Object object);
60int lua_isstring (lua_Object object);
61int lua_isfunction (lua_Object object);
62
55float lua_getnumber (lua_Object object); 63float lua_getnumber (lua_Object object);
56char *lua_getstring (lua_Object object); 64char *lua_getstring (lua_Object object);
57lua_CFunction lua_getcfunction (lua_Object object); 65lua_CFunction lua_getcfunction (lua_Object object);
@@ -88,14 +96,6 @@ lua_Object lua_createtable (void);
88 96
89#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA) 97#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA)
90 98
91#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
92#define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER)
93#define lua_isstring(_) (lua_type(_)==LUA_T_STRING)
94#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
95#define lua_isfunction(_) (lua_type(_)==LUA_T_FUNCTION)
96#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
97#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
98
99 99
100/* for compatibility with old versions. Avoid using these macros */ 100/* for compatibility with old versions. Avoid using these macros */
101 101
diff --git a/strlib.c b/strlib.c
index 59d855a4..93965b76 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.18 1996/02/12 18:34:44 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.19 1996/03/14 15:52:35 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -24,7 +24,7 @@ void lua_arg_error(char *funcname)
24char *lua_check_string (int numArg, char *funcname) 24char *lua_check_string (int numArg, char *funcname)
25{ 25{
26 lua_Object o = lua_getparam(numArg); 26 lua_Object o = lua_getparam(numArg);
27 if (!(lua_isstring(o) || lua_isnumber(o))) 27 if (!lua_isstring(o))
28 lua_arg_error(funcname); 28 lua_arg_error(funcname);
29 return lua_getstring(o); 29 return lua_getstring(o);
30} 30}
@@ -32,17 +32,9 @@ char *lua_check_string (int numArg, char *funcname)
32double lua_check_number (int numArg, char *funcname) 32double lua_check_number (int numArg, char *funcname)
33{ 33{
34 lua_Object o = lua_getparam(numArg); 34 lua_Object o = lua_getparam(numArg);
35 if (lua_isnumber(o)) 35 if (!lua_isnumber(o))
36 return lua_getnumber(o); 36 lua_arg_error(funcname);
37 else if (lua_isstring(o)) 37 return lua_getnumber(o);
38 {
39 float t;
40 char c;
41 if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1)
42 return t;
43 }
44 lua_arg_error(funcname);
45 return 0; /* to avoid warnings */
46} 38}
47 39
48char *luaI_addchar (int c) 40char *luaI_addchar (int c)