fallback.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.5 1994/11/10 17:36:54 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include "mem.h"
  8. #include "fallback.h"
  9. #include "opcode.h"
  10. #include "inout.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. /*
  20. ** Warning: This list must be in the same order as the #define's
  21. */
  22. struct FB luaI_fallBacks[] = {
  23. {"error", {LUA_T_CFUNCTION, errorFB}},
  24. {"index", {LUA_T_CFUNCTION, indexFB}},
  25. {"gettable", {LUA_T_CFUNCTION, gettableFB}},
  26. {"arith", {LUA_T_CFUNCTION, arithFB}},
  27. {"order", {LUA_T_CFUNCTION, orderFB}},
  28. {"concat", {LUA_T_CFUNCTION, concatFB}},
  29. {"unminus", {LUA_T_CFUNCTION, arithFB}},
  30. {"settable", {LUA_T_CFUNCTION, gettableFB}},
  31. {"gc", {LUA_T_CFUNCTION, GDFB}}
  32. };
  33. #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
  34. void luaI_setfallback (void)
  35. {
  36. int i;
  37. char *name = lua_getstring(lua_getparam(1));
  38. lua_Object func = lua_getparam(2);
  39. if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
  40. {
  41. lua_pushnil();
  42. return;
  43. }
  44. for (i=0; i<N_FB; i++)
  45. {
  46. if (strcmp(luaI_fallBacks[i].kind, name) == 0)
  47. {
  48. luaI_pushobject(&luaI_fallBacks[i].function);
  49. luaI_fallBacks[i].function = *luaI_Address(func);
  50. return;
  51. }
  52. }
  53. /* name not found */
  54. lua_pushnil();
  55. }
  56. static void errorFB (void)
  57. {
  58. lua_Object o = lua_getparam(1);
  59. if (lua_isstring(o))
  60. fprintf (stderr, "lua: %s\n", lua_getstring(o));
  61. else
  62. fprintf(stderr, "lua: unknown error\n");
  63. }
  64. static void indexFB (void)
  65. {
  66. lua_pushnil();
  67. }
  68. static void gettableFB (void)
  69. {
  70. lua_reportbug("indexed expression not a table");
  71. }
  72. static void arithFB (void)
  73. {
  74. lua_reportbug("unexpected type at conversion to number");
  75. }
  76. static void concatFB (void)
  77. {
  78. lua_reportbug("unexpected type at conversion to string");
  79. }
  80. static void orderFB (void)
  81. {
  82. lua_reportbug("unexpected type at comparison");
  83. }
  84. static void GDFB (void) { }
  85. /*
  86. ** Lock routines
  87. */
  88. static Object *lockArray = NULL;
  89. static int lockSize = 0;
  90. int lua_lock (lua_Object object)
  91. {
  92. int i;
  93. int oldSize;
  94. if (lua_isnil(object))
  95. return -1;
  96. for (i=0; i<lockSize; i++)
  97. if (tag(&lockArray[i]) == LUA_T_NIL)
  98. {
  99. lockArray[i] = *luaI_Address(object);
  100. return i;
  101. }
  102. /* no more empty spaces */
  103. oldSize = lockSize;
  104. if (lockArray == NULL)
  105. {
  106. lockSize = 10;
  107. lockArray = newvector(lockSize, Object);
  108. }
  109. else
  110. {
  111. lockSize = 3*oldSize/2 + 5;
  112. lockArray = growvector(lockArray, lockSize, Object);
  113. }
  114. for (i=oldSize; i<lockSize; i++)
  115. tag(&lockArray[i]) = LUA_T_NIL;
  116. lockArray[oldSize] = *luaI_Address(object);
  117. return oldSize;
  118. }
  119. void lua_unlock (int ref)
  120. {
  121. tag(&lockArray[ref]) = LUA_T_NIL;
  122. }
  123. Object *luaI_getlocked (int ref)
  124. {
  125. return &lockArray[ref];
  126. }
  127. void luaI_travlock (void (*fn)(Object *))
  128. {
  129. int i;
  130. for (i=0; i<lockSize; i++)
  131. fn(&lockArray[i]);
  132. }