fallback.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.24 1996/04/22 18:00:37 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "mem.h"
  9. #include "fallback.h"
  10. #include "opcode.h"
  11. #include "lua.h"
  12. #include "table.h"
  13. static void errorFB (void);
  14. static void indexFB (void);
  15. static void gettableFB (void);
  16. static void arithFB (void);
  17. static void concatFB (void);
  18. static void orderFB (void);
  19. static void GDFB (void);
  20. static void funcFB (void);
  21. /*
  22. ** Warning: This list must be in the same order as the #define's
  23. */
  24. struct FB luaI_fallBacks[] = {
  25. {"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
  26. {"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
  27. {"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
  28. {"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
  29. {"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
  30. {"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
  31. {"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
  32. {"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
  33. {"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
  34. /* no fixed number of params or results */
  35. {"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
  36. /* same default behavior of index FB */
  37. };
  38. #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
  39. void luaI_setfallback (void)
  40. {
  41. int i;
  42. char *name = lua_getstring(lua_getparam(1));
  43. lua_Object func = lua_getparam(2);
  44. if (name == NULL || !lua_isfunction(func))
  45. lua_error("incorrect argument to function `setfallback'");
  46. for (i=0; i<N_FB; i++)
  47. {
  48. if (strcmp(luaI_fallBacks[i].kind, name) == 0)
  49. {
  50. luaI_pushobject(&luaI_fallBacks[i].function);
  51. luaI_fallBacks[i].function = *luaI_Address(func);
  52. return;
  53. }
  54. }
  55. /* name not found */
  56. lua_error("incorrect argument to function `setfallback'");
  57. }
  58. static void errorFB (void)
  59. {
  60. lua_Object o = lua_getparam(1);
  61. if (lua_isstring(o))
  62. fprintf (stderr, "lua: %s\n", lua_getstring(o));
  63. else
  64. fprintf(stderr, "lua: unknown error\n");
  65. }
  66. static void indexFB (void)
  67. {
  68. lua_pushnil();
  69. }
  70. static void gettableFB (void)
  71. {
  72. lua_error("indexed expression not a table");
  73. }
  74. static void arithFB (void)
  75. {
  76. lua_error("unexpected type at conversion to number");
  77. }
  78. static void concatFB (void)
  79. {
  80. lua_error("unexpected type at conversion to string");
  81. }
  82. static void orderFB (void)
  83. {
  84. lua_error("unexpected type at comparison");
  85. }
  86. static void GDFB (void) { }
  87. static void funcFB (void)
  88. {
  89. lua_error("call expression not a function");
  90. }
  91. /*
  92. ** Reference routines
  93. */
  94. static struct ref {
  95. Object o;
  96. enum {LOCK, HOLD, FREE, COLLECTED} status;
  97. } *refArray = NULL;
  98. static int refSize = 0;
  99. int luaI_ref (Object *object, int lock)
  100. {
  101. int i;
  102. int oldSize;
  103. if (tag(object) == LUA_T_NIL)
  104. return -1; /* special ref for nil */
  105. for (i=0; i<refSize; i++)
  106. if (refArray[i].status == FREE)
  107. goto found;
  108. /* no more empty spaces */
  109. oldSize = refSize;
  110. refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
  111. for (i=oldSize; i<refSize; i++)
  112. refArray[i].status = FREE;
  113. i = oldSize;
  114. found:
  115. refArray[i].o = *object;
  116. refArray[i].status = lock ? LOCK : HOLD;
  117. return i;
  118. }
  119. void lua_unref (int ref)
  120. {
  121. if (ref >= 0 && ref < refSize)
  122. refArray[ref].status = FREE;
  123. }
  124. Object *luaI_getref (int ref)
  125. {
  126. static Object nul = {LUA_T_NIL, {0}};
  127. if (ref == -1)
  128. return &nul;
  129. if (ref >= 0 && ref < refSize &&
  130. (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
  131. return &refArray[ref].o;
  132. else
  133. return NULL;
  134. }
  135. void luaI_travlock (int (*fn)(Object *))
  136. {
  137. int i;
  138. for (i=0; i<refSize; i++)
  139. if (refArray[i].status == LOCK)
  140. fn(&refArray[i].o);
  141. }
  142. void luaI_invalidaterefs (void)
  143. {
  144. int i;
  145. for (i=0; i<refSize; i++)
  146. if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
  147. refArray[i].status = COLLECTED;
  148. }
  149. char *luaI_travfallbacks (int (*fn)(Object *))
  150. {
  151. int i;
  152. for (i=0; i<N_FB; i++)
  153. if (fn(&luaI_fallBacks[i].function))
  154. return luaI_fallBacks[i].kind;
  155. return NULL;
  156. }