luatcc.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "libtcc.h"
  2. int lua_createtccstate(Lua_State* L)
  3. {
  4. TCCState* s=tcc_new();
  5. if (!s) {
  6. luaL_error(L,"Could not create tcc state");
  7. return 0;
  8. }
  9. tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
  10. lua_pushlightuserdata(L,s);
  11. return 1;
  12. }
  13. int lua_compile(Lua_State* L)
  14. {
  15. int n=lua_gettop(L);
  16. if(n<2)
  17. {
  18. luaL_error(L,"error arguments count (expected 2 arguments)");
  19. return 0;
  20. }
  21. if(!lua_isuserdata(L,1))
  22. {
  23. luaL_error(L,"argument 1 must be userdata");
  24. return 0;
  25. }
  26. if(!lua_isstring(L,2))
  27. {
  28. luaL_error(L,"argument 2 must be string");
  29. return 0;
  30. }
  31. if(tcc_compile_string((TCCState*)lua_touserdata(L,1),lua_tostring(L,2))==-1)
  32. {
  33. luaL_error(L,"compile error");
  34. return 0;
  35. }
  36. if (tcc_relocate(s,TCC_RELOCATE_AUTO)<0)
  37. {
  38. luaL_error(L,"relocate error");
  39. return 0;
  40. }
  41. return 0;
  42. }
  43. int lua_addsymbol(Lua_State* L)
  44. {
  45. int n=lua_gettop(L);
  46. if(n<3)
  47. {
  48. luaL_error(L,"error arguments count (expected 3 arguments)");
  49. return 0;
  50. }
  51. if(!lua_isuserdata(L,1))
  52. {
  53. luaL_error(L,"argument 1 must be userdata");
  54. return 0;
  55. }
  56. if(!lua_isstring(L,2))
  57. {
  58. luaL_error(L,"argument 2 must be string");
  59. return 0;
  60. }
  61. if(!lua_isuserdata(L,3)&&!lua_isnumber(L,3))
  62. {
  63. luaL_error(L,"argument 3 must be pointer");
  64. return 0;
  65. }
  66. if(!lua_isuserdata(L,3))
  67. tcc_add_symbol((TCCState*)lua_touserdata(L,1),lua_tostring(L,2),lua_touserdata(L,3));
  68. if(!lua_isnumber(L,3))
  69. tcc_add_symbol((TCCState*)lua_touserdata(L,1),lua_tostring(L,2),lua_tointeger(L,3));
  70. return 0;
  71. }
  72. int lua_getsymbol(Lua_State* L)
  73. {
  74. int n=lua_gettop(L);
  75. if(n<2)
  76. {
  77. luaL_error(L,"error arguments count (expected 2 arguments)");
  78. return 0;
  79. }
  80. if(!lua_isuserdata(L,1))
  81. {
  82. luaL_error(L,"argument 1 must be userdata");
  83. return 0;
  84. }
  85. if(!lua_isstring(L,2))
  86. {
  87. luaL_error(L,"argument 2 must be string");
  88. return 0;
  89. }
  90. void* smb=tcc_get_symbol((TCCState*)lua_touserdata(L,1),lua_tostring(L,2));
  91. lua_pushlightuserdata(L,smb);
  92. return 1;
  93. }
  94. int lua_deletetccstate(Lua_State* L)
  95. {
  96. int n=lua_gettop(L);
  97. if(n<1)
  98. {
  99. luaL_error(L,"error arguments count (expected 1 argument)");
  100. return 0;
  101. }
  102. if(!lua_isuserdata(L,1))
  103. {
  104. luaL_error(L,"argument 1 must be userdata");
  105. return 0;
  106. }
  107. tcc_delete((TCCState*)lua_touserdata(L,1));
  108. return 0;
  109. }