fallback.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.9 1994/11/21 18:22:58 roberto Stab 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. 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}},
  25. {"index", {LUA_T_CFUNCTION, indexFB}},
  26. {"gettable", {LUA_T_CFUNCTION, gettableFB}},
  27. {"arith", {LUA_T_CFUNCTION, arithFB}},
  28. {"order", {LUA_T_CFUNCTION, orderFB}},
  29. {"concat", {LUA_T_CFUNCTION, concatFB}},
  30. {"settable", {LUA_T_CFUNCTION, gettableFB}},
  31. {"gc", {LUA_T_CFUNCTION, GDFB}},
  32. {"function", {LUA_T_CFUNCTION, funcFB}}
  33. };
  34. #define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
  35. void luaI_setfallback (void)
  36. {
  37. int i;
  38. char *name = lua_getstring(lua_getparam(1));
  39. lua_Object func = lua_getparam(2);
  40. if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
  41. {
  42. lua_pushnil();
  43. return;
  44. }
  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_pushnil();
  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_reportbug("indexed expression not a table");
  72. }
  73. static void arithFB (void)
  74. {
  75. lua_reportbug("unexpected type at conversion to number");
  76. }
  77. static void concatFB (void)
  78. {
  79. lua_reportbug("unexpected type at conversion to string");
  80. }
  81. static void orderFB (void)
  82. {
  83. lua_reportbug("unexpected type at comparison");
  84. }
  85. static void GDFB (void) { }
  86. static void funcFB (void)
  87. {
  88. lua_reportbug("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;
  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. tag(&lockArray[ref]) = LUA_T_NIL;
  127. }
  128. Object *luaI_getlocked (int ref)
  129. {
  130. return &lockArray[ref];
  131. }
  132. void luaI_travlock (void (*fn)(Object *))
  133. {
  134. Word i;
  135. for (i=0; i<lockSize; i++)
  136. fn(&lockArray[i]);
  137. }