Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lobject.c
blob0225e2d8cc750fc0755b7259dd6df38fde0085d2
1 /*
2 ** $Id: lobject.c,v 1.19 1999/04/13 19:28:49 roberto Exp $
3 ** Some generic functions over Lua objects
4 ** See Copyright Notice in lua.h
5 */
7 #include <ctype.h>
8 #include <stdlib.h>
10 #include "lobject.h"
11 #include "lua.h"
14 char *luaO_typenames[] = { /* ORDER LUA_T */
15 "userdata", "number", "string", "table", "function", "function",
16 "nil", "function", "mark", "mark", "mark", "line", NULL
20 TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
24 /* hash dimensions values */
25 static long dimensions[] =
26 {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
27 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
28 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
31 int luaO_redimension (int oldsize)
33 int i;
34 for (i=0; dimensions[i]<MAX_INT; i++) {
35 if (dimensions[i] > oldsize)
36 return dimensions[i];
38 lua_error("tableEM");
39 return 0; /* to avoid warnings */
43 int luaO_equalval (TObject *t1, TObject *t2) {
44 switch (ttype(t1)) {
45 case LUA_T_NIL: return 1;
46 case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
47 case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
48 case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
49 case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
50 case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
51 case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
52 default:
53 LUA_INTERNALERROR("invalid type");
54 return 0; /* UNREACHABLE */
59 void luaO_insertlist (GCnode *root, GCnode *node)
61 node->next = root->next;
62 root->next = node;
63 node->marked = 0;
67 #ifdef OLD_ANSI
68 void luaO_memup (void *dest, void *src, int size) {
69 while (size--)
70 ((char *)dest)[size]=((char *)src)[size];
73 void luaO_memdown (void *dest, void *src, int size) {
74 int i;
75 for (i=0; i<size; i++)
76 ((char *)dest)[i]=((char *)src)[i];
78 #endif
82 static double expten (unsigned int e) {
83 double exp = 10.0;
84 double res = 1.0;
85 for (; e; e>>=1) {
86 if (e & 1) res *= exp;
87 exp *= exp;
89 return res;
93 double luaO_str2d (char *s) { /* LUA_NUMBER */
94 double a = 0.0;
95 int point = 0;
96 while (isdigit((unsigned char)*s)) {
97 a = 10.0*a + (*(s++)-'0');
99 if (*s == '.') {
100 s++;
101 while (isdigit((unsigned char)*s)) {
102 a = 10.0*a + (*(s++)-'0');
103 point++;
106 if (toupper((unsigned char)*s) == 'E') {
107 int e = 0;
108 int sig = 1;
109 s++;
110 if (*s == '-') {
111 s++;
112 sig = -1;
114 else if (*s == '+') s++;
115 if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */
116 do {
117 e = 10*e + (*(s++)-'0');
118 } while (isdigit((unsigned char)*s));
119 point -= sig*e;
121 while (isspace((unsigned char)*s)) s++;
122 if (*s != '\0') return -1; /* invalid trailing characters? */
123 if (point > 0)
124 a /= expten(point);
125 else if (point < 0)
126 a *= expten(-point);
127 return a;