fallback.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.10 1994/12/20 21:20:36 roberto Exp $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "mem.h"
  9. #include "fallback.h"
  10. #include "opcode.h"
  11. #include "inout.h"
  12. #include "lua.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}},
  26. {"index", {LUA_T_CFUNCTION, indexFB}},
  27. {"gettable", {LUA_T_CFUNCTION, gettableFB}},
  28. {"arith", {LUA_T_CFUNCTION, arithFB}},
  29. {"order", {LUA_T_CFUNCTION, orderFB}},
  30. {"concat", {LUA_T_CFUNCTION, concatFB}},
  31. {"settable", {LUA_T_CFUNCTION, gettableFB}},
  32. {"gc", {LUA_T_CFUNCTION, GDFB}},
  33. {"function", {LUA_T_CFUNCTION, funcFB}}
  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. {
  43. lua_pushnil();
  44. return;
  45. }
  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_pushnil();
  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_reportbug("indexed expression not a table");
  73. }
  74. static void arithFB (void)
  75. {
  76. lua_reportbug("unexpected type at conversion to number");
  77. }
  78. static void concatFB (void)
  79. {
  80. lua_reportbug("unexpected type at conversion to string");
  81. }
  82. static void orderFB (void)
  83. {
  84. lua_reportbug("unexpected type at comparison");
  85. }
  86. static void GDFB (void) { }
  87. static void funcFB (void)
  88. {
  89. lua_reportbug("call expression not a function");
  90. }
  91. /*
  92. ** Lock routines
  93. */
  94. static Object *lockArray = NULL;
  95. static Word lockSize = 0;
  96. int luaI_lock (Object *object)
  97. {
  98. Word i;
  99. Word oldSize;
  100. if (tag(object) == LUA_T_NIL)
  101. return -1;
  102. for (i=0; i<lockSize; i++)
  103. if (tag(&lockArray[i]) == LUA_T_NIL)
  104. {
  105. lockArray[i] = *object;
  106. return i;
  107. }
  108. /* no more empty spaces */
  109. oldSize = lockSize;
  110. if (lockArray == NULL)
  111. {
  112. lockSize = 10;
  113. lockArray = newvector(lockSize, Object);
  114. }
  115. else
  116. {
  117. lockSize = 3*oldSize/2 + 5;
  118. lockArray = growvector(lockArray, lockSize, Object);
  119. }
  120. for (i=oldSize; i<lockSize; i++)
  121. tag(&lockArray[i]) = LUA_T_NIL;
  122. lockArray[oldSize] = *object;
  123. return oldSize;
  124. }
  125. void lua_unlock (int ref)
  126. {
  127. tag(&lockArray[ref]) = LUA_T_NIL;
  128. }
  129. Object *luaI_getlocked (int ref)
  130. {
  131. return &lockArray[ref];
  132. }
  133. void luaI_travlock (void (*fn)(Object *))
  134. {
  135. Word i;
  136. for (i=0; i<lockSize; i++)
  137. fn(&lockArray[i]);
  138. }