lobject.c 3.0 KB

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