fallback.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.16 1995/10/17 11:52:38 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. };
  35. #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
  36. void luaI_setfallback (void)
  37. {
  38. int i;
  39. char *name = lua_getstring(lua_getparam(1));
  40. lua_Object func = lua_getparam(2);
  41. if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
  42. lua_error("incorrect argument to function `setfallback'");
  43. for (i=0; i<N_FB; i++)
  44. {
  45. if (strcmp(luaI_fallBacks[i].kind, name) == 0)
  46. {
  47. luaI_pushobject(&luaI_fallBacks[i].function);
  48. luaI_fallBacks[i].function = *luaI_Address(func);
  49. return;
  50. }
  51. }
  52. /* name not found */
  53. lua_error("incorrect argument to function `setfallback'");
  54. }
  55. static void errorFB (void)
  56. {
  57. lua_Object o = lua_getparam(1);
  58. if (lua_isstring(o))
  59. fprintf (stderr, "lua: %s\n", lua_getstring(o));
  60. else
  61. fprintf(stderr, "lua: unknown error\n");
  62. }
  63. static void indexFB (void)
  64. {
  65. lua_pushnil();
  66. }
  67. static void gettableFB (void)
  68. {
  69. lua_error("indexed expression not a table");
  70. }
  71. static void arithFB (void)
  72. {
  73. lua_error("unexpected type at conversion to number");
  74. }
  75. static void concatFB (void)
  76. {
  77. lua_error("unexpected type at conversion to string");
  78. }
  79. static void orderFB (void)
  80. {
  81. lua_error("unexpected type at comparison");
  82. }
  83. static void GDFB (void) { }
  84. static void funcFB (void)
  85. {
  86. lua_error("call expression not a function");
  87. }
  88. /*
  89. ** Lock routines
  90. */
  91. static Object *lockArray = NULL;
  92. static Word lockSize = 0;
  93. int luaI_lock (Object *object)
  94. {
  95. Word i;
  96. Word oldSize;
  97. if (tag(object) == LUA_T_NIL)
  98. return -1;
  99. for (i=0; i<lockSize; i++)
  100. if (tag(&lockArray[i]) == LUA_T_NIL)
  101. {
  102. lockArray[i] = *object;
  103. return i;
  104. }
  105. /* no more empty spaces */
  106. oldSize = lockSize;
  107. if (lockArray == NULL)
  108. {
  109. lockSize = 10;
  110. lockArray = newvector(lockSize, Object);
  111. }
  112. else
  113. {
  114. lockSize = 3*oldSize/2 + 5;
  115. lockArray = growvector(lockArray, lockSize, Object);
  116. }
  117. for (i=oldSize; i<lockSize; i++)
  118. tag(&lockArray[i]) = LUA_T_NIL;
  119. lockArray[oldSize] = *object;
  120. return oldSize;
  121. }
  122. void lua_unlock (int ref)
  123. {
  124. tag(&lockArray[ref]) = LUA_T_NIL;
  125. }
  126. Object *luaI_getlocked (int ref)
  127. {
  128. return &lockArray[ref];
  129. }
  130. void luaI_travlock (int (*fn)(Object *))
  131. {
  132. Word i;
  133. for (i=0; i<lockSize; i++)
  134. fn(&lockArray[i]);
  135. }
  136. char *luaI_travfallbacks (int (*fn)(Object *))
  137. {
  138. Word i;
  139. for (i=0; i<N_FB; i++)
  140. if (fn(&luaI_fallBacks[i].function))
  141. return luaI_fallBacks[i].kind;
  142. return NULL;
  143. }