lobject.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ** $Id: lobject.c,v 1.50 2000/10/02 20:10:55 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 lua_Type luaO_typearr[] = { /* ORDER LUA_T */
  16. LUA_TUSERDATA, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE,
  17. LUA_TFUNCTION, LUA_TFUNCTION, LUA_TNIL
  18. };
  19. const TObject luaO_nilobject = {TAG_NIL, {NULL}};
  20. /*
  21. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  22. */
  23. lint32 luaO_power2 (lint32 n) {
  24. lint32 p = MINPOWER2;
  25. while (p<=n) p<<=1;
  26. return p;
  27. }
  28. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  29. if (ttype(t1) != ttype(t2)) return 0;
  30. switch (ttype(t1)) {
  31. case TAG_NUMBER:
  32. return nvalue(t1) == nvalue(t2);
  33. case TAG_STRING: case TAG_USERDATA:
  34. return tsvalue(t1) == tsvalue(t2);
  35. case TAG_TABLE:
  36. return hvalue(t1) == hvalue(t2);
  37. case TAG_CCLOSURE: case TAG_LCLOSURE:
  38. return clvalue(t1) == clvalue(t2);
  39. default:
  40. LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
  41. return 1; /* TAG_NIL */
  42. }
  43. }
  44. char *luaO_openspace (lua_State *L, size_t n) {
  45. if (n > L->Mbuffsize) {
  46. luaM_reallocvector(L, L->Mbuffer, n, char);
  47. L->nblocks += (n - L->Mbuffsize)*sizeof(char);
  48. L->Mbuffsize = n;
  49. }
  50. return L->Mbuffer;
  51. }
  52. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  53. char *endptr;
  54. Number res = lua_str2number(s, &endptr);
  55. if (endptr == s) return 0; /* no conversion */
  56. while (isspace((unsigned char)*endptr)) endptr++;
  57. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  58. *result = res;
  59. return 1;
  60. }
  61. /* this function needs to handle only '%d' and '%.XXs' formats */
  62. void luaO_verror (lua_State *L, const char *fmt, ...) {
  63. va_list argp;
  64. char buff[600]; /* to hold formated message */
  65. va_start(argp, fmt);
  66. vsprintf(buff, fmt, argp);
  67. va_end(argp);
  68. lua_error(L, buff);
  69. }
  70. #define EXTRALEN sizeof("string \"...\"0")
  71. void luaO_chunkid (char *out, const char *source, int len) {
  72. if (*source == '(') {
  73. strncpy(out, source+1, len-1); /* remove first char */
  74. out[len-1] = '\0'; /* make sure `out' has an end */
  75. out[strlen(out)-1] = '\0'; /* remove last char */
  76. }
  77. else {
  78. len -= EXTRALEN;
  79. if (*source == '@')
  80. sprintf(out, "file `%.*s'", len, source+1);
  81. else {
  82. const char *b = strchr(source , '\n'); /* stop at first new line */
  83. int lim = (b && (b-source)<len) ? b-source : len;
  84. sprintf(out, "string \"%.*s\"", lim, source);
  85. strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
  86. }
  87. }
  88. }