lobject.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. ** $Id: lobject.c,v 1.60 2001/01/24 15:45:33 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 "ldo.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
  17. /*
  18. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  19. */
  20. luint32 luaO_power2 (luint32 n) {
  21. luint32 p = MINPOWER2;
  22. while (p<=n) p<<=1;
  23. return p;
  24. }
  25. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  26. if (ttype(t1) != ttype(t2)) return 0;
  27. switch (ttype(t1)) {
  28. case LUA_TNUMBER:
  29. return nvalue(t1) == nvalue(t2);
  30. case LUA_TSTRING: case LUA_TUSERDATA:
  31. return tsvalue(t1) == tsvalue(t2);
  32. case LUA_TTABLE:
  33. return hvalue(t1) == hvalue(t2);
  34. case LUA_TFUNCTION:
  35. return clvalue(t1) == clvalue(t2);
  36. default:
  37. lua_assert(ttype(t1) == LUA_TNIL);
  38. return 1; /* LUA_TNIL */
  39. }
  40. }
  41. char *luaO_openspace (lua_State *L, size_t n) {
  42. if (n > G(L)->Mbuffsize) {
  43. luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, n, char);
  44. G(L)->Mbuffsize = n;
  45. }
  46. return G(L)->Mbuffer;
  47. }
  48. int luaO_str2d (const char *s, lua_Number *result) { /* LUA_NUMBER */
  49. char *endptr;
  50. lua_Number res = lua_str2number(s, &endptr);
  51. if (endptr == s) return 0; /* no conversion */
  52. while (isspace((unsigned char)*endptr)) endptr++;
  53. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  54. *result = res;
  55. return 1;
  56. }
  57. /* maximum length of a string format for `luaO_verror' */
  58. #define MAX_VERROR 280
  59. /* this function needs to handle only '%d' and '%.XXs' formats */
  60. void luaO_verror (lua_State *L, const char *fmt, ...) {
  61. va_list argp;
  62. char buff[MAX_VERROR]; /* to hold formatted message */
  63. va_start(argp, fmt);
  64. vsprintf(buff, fmt, argp);
  65. va_end(argp);
  66. luaD_error(L, buff);
  67. }
  68. void luaO_chunkid (char *out, const char *source, int bufflen) {
  69. if (*source == '=') {
  70. strncpy(out, source+1, bufflen); /* remove first char */
  71. out[bufflen-1] = '\0'; /* ensures null termination */
  72. }
  73. else {
  74. if (*source == '@') {
  75. int l;
  76. source++; /* skip the `@' */
  77. bufflen -= sizeof("file `...%s'");
  78. l = strlen(source);
  79. if (l>bufflen) {
  80. source += (l-bufflen); /* get last part of file name */
  81. sprintf(out, "file `...%.99s'", source);
  82. }
  83. else
  84. sprintf(out, "file `%.99s'", source);
  85. }
  86. else {
  87. int len = strcspn(source, "\n"); /* stop at first newline */
  88. bufflen -= sizeof("string \"%.*s...\"");
  89. if (len > bufflen) len = bufflen;
  90. if (source[len] != '\0') { /* must truncate? */
  91. strcpy(out, "string \"");
  92. out += strlen(out);
  93. strncpy(out, source, len);
  94. strcpy(out+len, "...\"");
  95. }
  96. else
  97. sprintf(out, "string \"%.99s\"", source);
  98. }
  99. }
  100. }