fallback.c 3.7 KB

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