aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/lua/lib.cpp
blob: 1315d1d1aa342cec917236a3c15d340a5e76aff1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <library/cpp/archive/yarchive.h>

#include <util/generic/buffer.h>
#include <util/generic/yexception.h>
#include <util/generic/singleton.h>

#include <util/system/hostname.h>
#include <util/memory/blob.h>
#include <util/stream/output.h>
#include <util/stream/buffer.h>

//lua code, with hooks
struct lua_State;

static void RegisterYandexHooks(lua_State* l);
static int loader_Common(lua_State* L);

#define LUA_LIB
#define luaall_c

#define ltable_c    // for luai_hashnum under Visual Studio

#include "lobject.c"
#include "lstate.c"
#include "lgc.c"
#include "lvm.c"
#include "lauxlib.c"
#include "ltm.c"
#include "lcode.c"
#include "lapi.c"
#include "lmathlib.c"
#include "lparser.c"
#include "ldblib.c"
#include "ldebug.c"
#include "lzio.c"
#include "loslib.c"
#include "ltable.c"
#include "lmem.c"
#include "liolib.c"
#include "lstring.c"
#include "lundump.c"
#include "linit.c"
#include "llex.c"
#include "lopcodes.c"
#include "lstrlib.c"
#include "lfunc.c"
#include "ldo.c"
#include "lbaselib.c"
#include "ldump.c"
#include "ltablib.c"
#include "loadlib.c"
#include "lutf8lib.c"

#if LUA_VERSION_NUM >= 502
    #include "lbitlib.c"
    #include "lctype.c"
    #include "lcorolib.c"
#endif

//end of lua code

//yandex stuff
namespace {
    static const unsigned char data[] = {
         #include "common.inc"
    };

    class TLuaArchive: public TArchiveReader {
    public:
        inline TLuaArchive()
            : TArchiveReader(TBlob::NoCopy(data, sizeof(data)))
        {
        }
    };

    static inline TArchiveReader& Archive() {
        return *Singleton<TLuaArchive>();
    }

    static inline bool OpenInternalModule(const char* key, TBuffer& buf) {
        try {
            TAutoPtr<IInputStream> in(Archive().ObjectByKey(key));
            TBufferOutput out(buf);

            TransferData(in.Get(), &out);

            return true;
        } catch (...) {
        }

        return false;
    }
}

#if LUA_VERSION_NUM == 502
static int loader_Common(lua_State* L) {
    const char* extname;
    char filename[256];

    extname = luaL_checkstring(L, 1);
    snprintf(filename, 256, "/%s.lua", extname);

    TBuffer buf;
    if (!OpenInternalModule(filename, buf)) {
        return 1;
    }

    if (luaL_loadbuffer(L, buf.Data(), buf.Size(), filename) != 0) {
        return 1;
    }

    lua_pushstring(L, filename);

    return 2;
}
#endif

#if LUA_VERSION_NUM == 501
static int loader_Common(lua_State* L) {
    const char* extname;
    char filename[256];

    extname = luaL_checkstring(L, 1);
    snprintf(filename, 256, "/%s.lua", extname);

    TBuffer buf;
    if (!OpenInternalModule(filename, buf)) {
        loaderror(L, extname);

        return 1;
    }

    if (luaL_loadbuffer(L, buf.Data(), buf.Size(), filename) != 0) {
        loaderror(L, filename);
    }

    return 1;  /* library loaded successfully */
}
#endif

static int OsHostName(lua_State* l) {
    lua_pushstring(l, HostName().data());

    return 1;
}

static int OsPutEnv(lua_State* l) {
    const char* env = luaL_checkstring(l, 1);
    int res = putenv(strdup(env));
    lua_pushnumber(l, res);

    return 1;
}

static const luaL_Reg YANDEX_OS_LIBS[] = {
    {"hostname", OsHostName},
    {"putenv", OsPutEnv},
    {0, 0}
};

LUALIB_API int RegisterYandexOsHooks(lua_State* l) {
    luaL_register(l, LUA_OSLIBNAME, YANDEX_OS_LIBS);

    return 1;
}

static void RegisterYandexHooks(lua_State* l) {
    lua_pushcfunction(l, RegisterYandexOsHooks);
    lua_pushstring(l, "RegisterYandexOsHooks");
    lua_call(l, 1, 0);
}