lobject.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ** $Id: lobject.c,v 1.47 2000/09/11 20:29:27 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. /*
  16. ** you can use the fact that the 3rd letter or each name is always different
  17. ** (e-m-r-b-n-l) to compare and switch these strings
  18. */
  19. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  20. "userdata", "number", "string", "table", "function", "function", "nil",
  21. "function", "function"
  22. };
  23. const TObject luaO_nilobject = {TAG_NIL, {NULL}};
  24. /*
  25. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  26. */
  27. lint32 luaO_power2 (lint32 n) {
  28. lint32 p = MINPOWER2;
  29. while (p<=n) p<<=1;
  30. return p;
  31. }
  32. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  33. if (ttype(t1) != ttype(t2)) return 0;
  34. switch (ttype(t1)) {
  35. case TAG_NUMBER:
  36. return nvalue(t1) == nvalue(t2);
  37. case TAG_STRING: case TAG_USERDATA:
  38. return tsvalue(t1) == tsvalue(t2);
  39. case TAG_TABLE:
  40. return hvalue(t1) == hvalue(t2);
  41. case TAG_CCLOSURE: case TAG_LCLOSURE:
  42. return clvalue(t1) == clvalue(t2);
  43. default:
  44. LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
  45. return 1; /* TAG_NIL */
  46. }
  47. }
  48. char *luaO_openspace (lua_State *L, size_t n) {
  49. if (n > L->Mbuffsize) {
  50. luaM_reallocvector(L, L->Mbuffer, n, char);
  51. L->Mbuffsize = n;
  52. }
  53. return L->Mbuffer;
  54. }
  55. static double expten (unsigned int e) {
  56. double exp = 10.0;
  57. double res = 1.0;
  58. for (; e; e>>=1) {
  59. if (e & 1) res *= exp;
  60. exp *= exp;
  61. }
  62. return res;
  63. }
  64. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  65. double a = 0.0;
  66. int point = 0; /* number of decimal digits */
  67. int sig;
  68. while (isspace((unsigned char)*s)) s++;
  69. sig = 0;
  70. switch (*s) {
  71. case '-': sig = 1; /* go through */
  72. case '+': s++;
  73. }
  74. if (! (isdigit((unsigned char)*s) ||
  75. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  76. return 0; /* not (at least one digit before or after the point) */
  77. while (isdigit((unsigned char)*s))
  78. a = 10.0*a + (*(s++)-'0');
  79. if (*s == '.') {
  80. s++;
  81. while (isdigit((unsigned char)*s)) {
  82. a = 10.0*a + (*(s++)-'0');
  83. point++;
  84. }
  85. }
  86. if (sig) a = -a;
  87. if (*s == 'e' || *s == 'E') {
  88. int e = 0;
  89. s++;
  90. sig = 0;
  91. switch (*s) {
  92. case '-': sig = 1; /* go through */
  93. case '+': s++;
  94. }
  95. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  96. do {
  97. e = 10*e + (*(s++)-'0');
  98. } while (isdigit((unsigned char)*s));
  99. if (sig) e = -e;
  100. point -= e;
  101. }
  102. while (isspace((unsigned char)*s)) s++;
  103. if (*s != '\0') return 0; /* invalid trailing characters? */
  104. if (point != 0) {
  105. if (point > 0) a /= expten(point);
  106. else a *= expten(-point);
  107. }
  108. *result = a;
  109. return 1;
  110. }
  111. /* this function needs to handle only '%d' and '%.XXXs' formats */
  112. void luaO_verror (lua_State *L, const char *fmt, ...) {
  113. char buff[500];
  114. va_list argp;
  115. va_start(argp, fmt);
  116. vsprintf(buff, fmt, argp);
  117. va_end(argp);
  118. lua_error(L, buff);
  119. }
  120. #define EXTRALEN sizeof("string \"...\"0")
  121. void luaO_chunkid (char *out, const char *source, int len) {
  122. if (*source == '(') {
  123. strncpy(out, source+1, len-1); /* remove first char */
  124. out[len-1] = '\0'; /* make sure `out' has an end */
  125. out[strlen(out)-1] = '\0'; /* remove last char */
  126. }
  127. else {
  128. len -= EXTRALEN;
  129. if (*source == '@')
  130. sprintf(out, "file `%.*s'", len, source+1);
  131. else {
  132. const char *b = strchr(source , '\n'); /* stop at first new line */
  133. int lim = (b && (b-source)<len) ? b-source : len;
  134. sprintf(out, "string \"%.*s\"", lim, source);
  135. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  136. }
  137. }
  138. }