lib.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <library/cpp/archive/yarchive.h>
  2. #include <util/generic/buffer.h>
  3. #include <util/generic/yexception.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/system/hostname.h>
  6. #include <util/memory/blob.h>
  7. #include <util/stream/output.h>
  8. #include <util/stream/buffer.h>
  9. //lua code, with hooks
  10. struct lua_State;
  11. static void RegisterYandexHooks(lua_State* l);
  12. static int loader_Common(lua_State* L);
  13. #define LUA_LIB
  14. #define luaall_c
  15. #define ltable_c // for luai_hashnum under Visual Studio
  16. #include "lobject.c"
  17. #include "lstate.c"
  18. #include "lgc.c"
  19. #include "lvm.c"
  20. #include "lauxlib.c"
  21. #include "ltm.c"
  22. #include "lcode.c"
  23. #include "lapi.c"
  24. #include "lmathlib.c"
  25. #include "lparser.c"
  26. #include "ldblib.c"
  27. #include "ldebug.c"
  28. #include "lzio.c"
  29. #include "loslib.c"
  30. #include "ltable.c"
  31. #include "lmem.c"
  32. #include "liolib.c"
  33. #include "lstring.c"
  34. #include "lundump.c"
  35. #include "linit.c"
  36. #include "llex.c"
  37. #include "lopcodes.c"
  38. #include "lstrlib.c"
  39. #include "lfunc.c"
  40. #include "ldo.c"
  41. #include "lbaselib.c"
  42. #include "ldump.c"
  43. #include "ltablib.c"
  44. #include "loadlib.c"
  45. #include "lutf8lib.c"
  46. #if LUA_VERSION_NUM >= 502
  47. #include "lbitlib.c"
  48. #include "lctype.c"
  49. #include "lcorolib.c"
  50. #endif
  51. //end of lua code
  52. //yandex stuff
  53. namespace {
  54. static const unsigned char data[] = {
  55. #include "common.inc"
  56. };
  57. class TLuaArchive: public TArchiveReader {
  58. public:
  59. inline TLuaArchive()
  60. : TArchiveReader(TBlob::NoCopy(data, sizeof(data)))
  61. {
  62. }
  63. };
  64. static inline TArchiveReader& Archive() {
  65. return *Singleton<TLuaArchive>();
  66. }
  67. static inline bool OpenInternalModule(const char* key, TBuffer& buf) {
  68. try {
  69. TAutoPtr<IInputStream> in(Archive().ObjectByKey(key));
  70. TBufferOutput out(buf);
  71. TransferData(in.Get(), &out);
  72. return true;
  73. } catch (...) {
  74. }
  75. return false;
  76. }
  77. }
  78. #if LUA_VERSION_NUM == 502
  79. static int loader_Common(lua_State* L) {
  80. const char* extname;
  81. char filename[256];
  82. extname = luaL_checkstring(L, 1);
  83. snprintf(filename, 256, "/%s.lua", extname);
  84. TBuffer buf;
  85. if (!OpenInternalModule(filename, buf)) {
  86. return 1;
  87. }
  88. if (luaL_loadbuffer(L, buf.Data(), buf.Size(), filename) != 0) {
  89. return 1;
  90. }
  91. lua_pushstring(L, filename);
  92. return 2;
  93. }
  94. #endif
  95. #if LUA_VERSION_NUM == 501
  96. static int loader_Common(lua_State* L) {
  97. const char* extname;
  98. char filename[256];
  99. extname = luaL_checkstring(L, 1);
  100. snprintf(filename, 256, "/%s.lua", extname);
  101. TBuffer buf;
  102. if (!OpenInternalModule(filename, buf)) {
  103. loaderror(L, extname);
  104. return 1;
  105. }
  106. if (luaL_loadbuffer(L, buf.Data(), buf.Size(), filename) != 0) {
  107. loaderror(L, filename);
  108. }
  109. return 1; /* library loaded successfully */
  110. }
  111. #endif
  112. static int OsHostName(lua_State* l) {
  113. lua_pushstring(l, HostName().data());
  114. return 1;
  115. }
  116. static int OsPutEnv(lua_State* l) {
  117. const char* env = luaL_checkstring(l, 1);
  118. int res = putenv(strdup(env));
  119. lua_pushnumber(l, res);
  120. return 1;
  121. }
  122. static const luaL_Reg YANDEX_OS_LIBS[] = {
  123. {"hostname", OsHostName},
  124. {"putenv", OsPutEnv},
  125. {0, 0}
  126. };
  127. LUALIB_API int RegisterYandexOsHooks(lua_State* l) {
  128. luaL_register(l, LUA_OSLIBNAME, YANDEX_OS_LIBS);
  129. return 1;
  130. }
  131. static void RegisterYandexHooks(lua_State* l) {
  132. lua_pushcfunction(l, RegisterYandexOsHooks);
  133. lua_pushstring(l, "RegisterYandexOsHooks");
  134. lua_call(l, 1, 0);
  135. }