lobject.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. ** $Id: lobject.c,v 1.45 2000/08/11 16:17:28 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include "lua.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. /*
  13. ** you can use the fact that the 3rd letter or each name is always different
  14. ** (e-m-r-b-n-l) to compare and switch these strings
  15. */
  16. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  17. "userdata", "number", "string", "table", "function", "function", "nil",
  18. "function", "function"
  19. };
  20. const TObject luaO_nilobject = {TAG_NIL, {NULL}};
  21. /*
  22. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  23. */
  24. lint32 luaO_power2 (lint32 n) {
  25. lint32 p = MINPOWER2;
  26. while (p<=n) p<<=1;
  27. return p;
  28. }
  29. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  30. if (ttype(t1) != ttype(t2)) return 0;
  31. switch (ttype(t1)) {
  32. case TAG_NUMBER:
  33. return nvalue(t1) == nvalue(t2);
  34. case TAG_STRING: case TAG_USERDATA:
  35. return tsvalue(t1) == tsvalue(t2);
  36. case TAG_TABLE:
  37. return hvalue(t1) == hvalue(t2);
  38. case TAG_CCLOSURE: case TAG_LCLOSURE:
  39. return clvalue(t1) == clvalue(t2);
  40. default:
  41. LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
  42. return 1; /* TAG_NIL */
  43. }
  44. }
  45. char *luaO_openspace (lua_State *L, size_t n) {
  46. if (n > L->Mbuffsize) {
  47. luaM_reallocvector(L, L->Mbuffer, n, char);
  48. L->Mbuffsize = n;
  49. }
  50. return L->Mbuffer;
  51. }
  52. static double expten (unsigned int e) {
  53. double exp = 10.0;
  54. double res = 1.0;
  55. for (; e; e>>=1) {
  56. if (e & 1) res *= exp;
  57. exp *= exp;
  58. }
  59. return res;
  60. }
  61. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  62. double a = 0.0;
  63. int point = 0; /* number of decimal digits */
  64. int sig;
  65. while (isspace((unsigned char)*s)) s++;
  66. sig = 0;
  67. switch (*s) {
  68. case '-': sig = 1; /* go through */
  69. case '+': s++;
  70. }
  71. if (! (isdigit((unsigned char)*s) ||
  72. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  73. return 0; /* not (at least one digit before or after the point) */
  74. while (isdigit((unsigned char)*s))
  75. a = 10.0*a + (*(s++)-'0');
  76. if (*s == '.') {
  77. s++;
  78. while (isdigit((unsigned char)*s)) {
  79. a = 10.0*a + (*(s++)-'0');
  80. point++;
  81. }
  82. }
  83. if (sig) a = -a;
  84. if (*s == 'e' || *s == 'E') {
  85. int e = 0;
  86. s++;
  87. sig = 0;
  88. switch (*s) {
  89. case '-': sig = 1; /* go through */
  90. case '+': s++;
  91. }
  92. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  93. do {
  94. e = 10*e + (*(s++)-'0');
  95. } while (isdigit((unsigned char)*s));
  96. if (sig) e = -e;
  97. point -= e;
  98. }
  99. while (isspace((unsigned char)*s)) s++;
  100. if (*s != '\0') return 0; /* invalid trailing characters? */
  101. if (point != 0) {
  102. if (point > 0) a /= expten(point);
  103. else a *= expten(-point);
  104. }
  105. *result = a;
  106. return 1;
  107. }