fallback.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 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))
  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 int lockSize = 0;
  95. int luaI_lock (Object *object)
  96. {
  97. int i;
  98. int 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. lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD);
  110. for (i=oldSize; i<lockSize; i++)
  111. tag(&lockArray[i]) = LUA_T_NIL;
  112. lockArray[oldSize] = *object;
  113. return oldSize;
  114. }
  115. void lua_unlock (int ref)
  116. {
  117. if (ref >= 0 && ref < lockSize)
  118. tag(&lockArray[ref]) = LUA_T_NIL;
  119. }
  120. Object *luaI_getlocked (int ref)
  121. {
  122. static Object nul = {LUA_T_NIL, {0}};
  123. if (ref >= 0 && ref < lockSize)
  124. return &lockArray[ref];
  125. else
  126. return &nul;
  127. }
  128. void luaI_travlock (int (*fn)(Object *))
  129. {
  130. int i;
  131. for (i=0; i<lockSize; i++)
  132. fn(&lockArray[i]);
  133. }
  134. char *luaI_travfallbacks (int (*fn)(Object *))
  135. {
  136. int i;
  137. for (i=0; i<N_FB; i++)
  138. if (fn(&luaI_fallBacks[i].function))
  139. return luaI_fallBacks[i].kind;
  140. return NULL;
  141. }