aboutsummaryrefslogtreecommitdiff
path: root/src/3rdparty/luax/luax.h
blob: 7218e3a9af243af54b0b3892b4e059211e3aa52d (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* Copyright (C) 2018 chai
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* include following 3 files before this
*    lua.h
*    lauxlib.h
*    lualib.h
*/
#ifndef __LUAX_H
#define __LUAX_H
// only for lua 5.1
#if LUA_VERSION_NUM == 501 

#define LUAX_VERSION "0.1.0"

#define luax_newstate luaL_newstate 
#define luax_close lua_close
#define luax_openlibs luaL_openlibs
// load chunk but dont run it 
#define luax_loadbuffer luaL_loadbuffer
#define luax_dostring luaL_dostring
#define luax_pcall lua_pcall 
#define luax_setglobal lua_setglobal
#define luax_setglobali(L, i, name)\
    lua_pushvalue(L, i);\
    lua_setglobal(L, name);
#define luax_pop lua_pop
#define luax_remove lua_remove
#define luax_newtable lua_newtable
#define luax_getglobal lua_getglobal

#define luax_clearstack(L) lua_settop(L, 0)
/**
* 
*/
#define luax_setglobalstring(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n))

/**
* Get number of args 
*/
#define luax_gettop lua_gettop

#define luax_gettable lua_gettable

#define luax_settable lua_settable

/**
* Check userdata type.
*/
#define luax_checktype luaL_checkudata
#define luax_checknumber luaL_checknumber
#define luax_checkinteger luaL_checkinteger
#define luax_checkstring luaL_checkstring
#define luax_checklstring luaL_checklstring
#define luax_touserdata lua_touserdata
#define luax_tolightuserdata lua_touserdata
//#define luax_checkbool luaL_checkinteger
inline bool luax_checkbool(lua_State *L, int numArg)               
{
    bool b = false;
    if (lua_type(L, numArg) == LUA_TBOOLEAN)
    {
        b = lua_toboolean(L, numArg);
    }
    else
    {
        luaL_typerror(L, numArg, lua_typename(L, LUA_TBOOLEAN));
    }
    return b;
}

/**
* Oprating tables. 
*/
/* get value and leaves it on top of stack */
#define luax_rawgetnumber(L, i, k) (lua_rawgeti(L,i, k), lua_tonumber(L, -1))
inline float luax_rawgetnumberthenpop(lua_State* L, int i, int k)
{
    float n = luax_rawgetnumber(L, i, k);
    luax_pop(L, 1);
    return n;
}

#define luax_rawgeti lua_rawgeti

/**
* 
*/
#define luax_typerror luaL_typerror

/**
* Error checking
*/
#define luax_error luaL_error

inline void luax_errorf(lua_State* L, const char* fmt, ...)
{
    const int FORMAT_MSG_BUFFER_SIZE = 2048;
    char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
    va_list args;
    va_start(args, fmt);
    vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args);
    va_end(args);
    //luax_getglobal(L, "jin");
    //luax_setfieldstring(L, "error", err);
    luax_error(L, err);
}

/**
* Push value on the top of stack.
*/
#define luax_pushnumber  lua_pushnumber
#define luax_pushstring  lua_pushstring 
#define luax_pushlstring lua_pushlstring
#define luax_pushinteger lua_pushinteger
#define luax_pushboolean lua_pushboolean
#define luax_pushlightuserdata lua_pushlightuserdata
#define luax_pushnil lua_pushnil
#define luax_pushvalue lua_pushvalue
#define luax_pushliteral lua_pushliteral
#define luax_pushcfunction lua_pushcfunction //lua_pushcclosure(L, (f), 0)
#define luax_pushcclosure lua_pushcclosure

#define luax_setmetatable lua_setmetatable

//inline void luax_pushuserdata(lua_State* L, void* p)
//{
//    /**
//    * https://stackoverflow.com/questions/15038796/lua-c-api-push-existing-pointers
//    */
//    void** box = (void**)lua_newuserdata(L, sizeof(p));
//    *box = p;
//
//}

#define luax_rawseti lua_rawseti

/**
* Set field
*/
#define luax_setfield lua_setfield
#define luax_setfield_(T, L, k, v)\
   do { lua_push##T(L, v); lua_setfield(L, -2, k); } while (0)

#define luax_setfieldnumber(L, k, v) luax_setfield_(number, L, k, v)
#define luax_setfieldinteger(L, k, v) luax_setfield_(integer, L, k, v)
#define luax_setfieldstring(L, k, v) luax_setfield_(string, L, k, v)
#define luax_setfieldbool(L, k, v)   luax_setfield_(boolean, L, k, v)
#define luax_setfieldudata(L, k, v)  luax_setfield_(lightuserdata, L, k, v)
#define luax_setfieldcfunc(L, k, v)  luax_setfield_(cfunction, L, k, v)
#define luax_setfieldfstring(L, k, ...)\
   do { lua_pushfstring(L, __VA_ARGS__); lua_setfield(L, -2, k); } while (0)

/**
* If nosuch field push a nil at the top of stack.
*/
#define luax_getfield(L, I, N) lua_getfield(L, I, N)
inline float luax_getfieldnumber(lua_State* L, int I, const char* N)
{
    luax_getfield(L, I, N); 
    float n = luax_checknumber(L, -1); 
    return n;
}
inline int luax_getfieldinteger(lua_State* L, int I, const char* N)
{
    luax_getfield(L, I, N);
    int bin = luax_checkinteger(L, -1);
    return bin;
}
inline const char* luax_getfieldstring(lua_State* L, int I, const char* N)
{
    luax_getfield(L, I, N);
    const char* str = luax_checkstring(L, -1);
    return str;
}
inline char luax_getfieldbool(lua_State* L, int I, const char* N)
{
    luax_getfield(L, I, N);
    char bin = lua_toboolean(L, -1);
    return bin;
}
/**
*
*/
#define luax_call lua_call

/**
* Set raw 
*/
#define luax_setraw(T, L, idx, i, v)\
    (lua_push##T(L, v), lua_rawseti(L, idx, i))

#define luax_setrawstring(L, idx, i, v) luax_setraw(string, L, idx, i, v)
#define luax_setrawnumber(L, idx, i, v) luax_setraw(number, L, idx, i, v)
#define luax_setrawbool(L, idx, i, v) luax_setraw(boolean, L, idx, i, v)

/**
* 
*/
#define luax_optboolean(L, i, x)\
  (!lua_isnoneornil(L, i) ? lua_toboolean(L, i) : (x))
#define luax_optudata(L, i, name, x)\
  (!lua_isnoneornil(L, i) ? luaL_checkudata(L, i, name) : (x))
#define luax_optnumber luaL_optnumber
#define luax_optinteger luaL_optinteger

inline int luax_newlib(lua_State* L, const luaL_Reg* f)
{
    lua_createtable(L, 0, sizeof(f));
    for (; f && f->name; ++f)
    {
        lua_pushcfunction(L, f->func);
        lua_setfield(L, -2, f->name);
    }
    return 1; // leave lib table on top of stack
}

/**
* Register a userdefined lua type with given type name.
*/
inline void luax_newtype(lua_State* L, const char* tname, const luaL_Reg* f)
{
    luaL_newmetatable(L, tname);

    // m.__index = m
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");

    if (f != NULL)
        luaL_register(L, NULL, f);
		
    lua_pop(L, 1); // Pops metatable.
}

/**
* Instantiate of a type.
*/
inline void* luax_newinstance(lua_State* L, const char* tname, int size)
{
    void* p = lua_newuserdata(L, size);

    luaL_newmetatable(L, tname);
    lua_setmetatable(L, -2);
    
    return p;
}

/**
* require a module
*/
inline int luax_require(lua_State* L, const char* mname, lua_CFunction openf, int glb) 
{
    openf(L); 
    if (glb)
    {
        lua_setglobal(L, mname);
        return 0; 
    }
    return 1;
}

/**
* Return 1 if match. 
*/
inline int luax_istype(lua_State* L, int idx, const char* tname)
{
    if (lua_getmetatable(L, idx))
    {
        /* get metatable called tname */
        lua_getfield(L, LUA_REGISTRYINDEX, tname); 
        if (lua_rawequal(L, -1, -2))
        {
            lua_pop(L, 2); // pop both metatables
            return 1; 
        }
        lua_pop(L, 2);
        return 0; 
    }
    /* value at idx has no metatable */
    return 0;
}

#define luax_is(T, L, i) (lua_is##T(L, i))
#define luax_isnumber(L, i) luax_is(number, L, i)
#define luax_isstring(L, i) luax_is(string, L, i)
#define luax_istable(L, i) luax_is(table, L, i)
#define luax_isnil(L, i) luax_is(nil, L, i)
#define luax_isboolean(L, i) luax_is(boolean, L, i)
#define luax_isfunction(L, i) luax_is(function, L, i)
#define luax_isuserdata lua_isuserdata
#define luax_islightuserdata lua_islightuserdata
inline int luax_isinteger(lua_State* L, int i)
{
    if (!luax_isnumber(L, i))
        return 0;
    return lua_tonumber(L, i) == lua_tointeger(L, i);
}
inline int luax_isfloat(lua_State* L, int i)
{
    if (!luax_isnumber(L, i))
        return 0;
    return lua_tonumber(L, i) != lua_tointeger(L, i);
}

#define luax_isnumberstrict(L, i) (lua_type(L, i) == LUA_TNUMBER)
#define luax_isstringstrict(L, i) (lua_type(L, i) == LUA_TSTRING)
#define luax_isbooleanstrict(L, i) (lua_type(L, i) == LUA_TBOOLEAN)
#define luax_istablestrict(L, i) (lua_type(L, i) == LUA_TTABLE)
#define luax_isnilstrict(L, i) (lua_type(L, i) == LUA_TNIL)
inline int luax_isintegerstrict(lua_State* L, int i)
{
    if (!luax_isnumberstrict(L, i))
        return 0;
    return lua_tonumber(L, i) == lua_tointeger(L, i);
}
inline int luax_isfloatstrict(lua_State* L, int i)
{
    if (!luax_isnumberstrict(L, i))
        return 0;
    return lua_tonumber(L, i) != lua_tointeger(L, i);
}

/**
* To userdata. 
*/
inline void* luax_toudata(lua_State* L, int idx)
{
    void* p = lua_touserdata(L, idx); 
    if (p == NULL)
        luaL_typerror(L, idx, "userdata"); // if p is not userdata
    return p; 
}

/**
* Get table index size. 
*/
inline int luax_tableidxlen(lua_State* L, int i)
{
    return lua_objlen(L, i);
}

/**
* Get table hash size
inline int luax_tbalehashlen(lua_State* L, int i)
{

}
*/

/**
* Get table hash and index size
inline int luax_tablelen(lua_State* L, int i)
{

}
*/

/* create a global tbale and stay it on the top of stack */
#define luax_globaltable(L, name)\
lua_newtable(L);\
lua_pushvalue(L, 1);\
lua_setglobal(L, name);

inline int luax_tableinsert(lua_State * L, int tindex, int vindex, int pos)
{
    if (tindex < 0)
        tindex = lua_gettop(L) + 1 + tindex;
    if (vindex < 0)
        vindex = lua_gettop(L) + 1 + vindex;
    if (pos == -1)
    {
        lua_pushvalue(L, vindex);
        lua_rawseti(L, tindex, lua_objlen(L, tindex) + 1);
        return 0;
    }
    else if (pos < 0)
        pos = lua_objlen(L, tindex) + 1 + pos;
    for (int i = lua_objlen(L, tindex) + 1; i > pos; --i)
    {
        lua_rawgeti(L, tindex, i - 1);
        lua_rawseti(L, tindex, i);
    }
    lua_pushvalue(L, vindex);
    lua_rawseti(L, tindex, pos);
    return 0;
}

/**
* Add the package loader to the package.loaders table.
*/
inline int luax_registersearcher(lua_State * L, lua_CFunction f, int pos)
{
    lua_getglobal(L, "package");

    if (lua_isnil(L, -1))
        return luaL_error(L, "Can't register searcher: package table does not exist.");

    lua_getfield(L, -1, "loaders");

    if (lua_isnil(L, -1))
        return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");

    lua_pushcfunction(L, f);
    luax_tableinsert(L, -2, -1, pos);
    lua_pop(L, 3);
    return 0;
}

typedef struct luax_Str
{
    const char* name;
    const char* value;
} luax_Str;

inline void luax_setfieldstrings(lua_State* L, const luax_Str* strs)
{
    for (int i = 0; strs[i].name != 0; ++i)
    {
        luax_setfieldstring(L, strs[i].name, strs[i].value);
    }
}

typedef struct luax_Num
{
    const char* name; 
    float number;
};

inline void luax_setfieldnumbers(lua_State* L, const luax_Num* strs)
{
    for (int i = 0; strs[i].name != 0; ++i)
    {
        luax_setfieldnumber(L, strs[i].name, strs[i].number);
    }
}

typedef luaL_Reg luax_Reg;

#define luax_ref luaL_ref

#define luax_unref luaL_unref

#endif // #if LUA_VERSION_NUM == 501 

#endif // __LUAX_H