-
Notifications
You must be signed in to change notification settings - Fork 1
/
tally.c
79 lines (67 loc) · 1.52 KB
/
tally.c
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
/* tally.c — support library for Tally */
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifndef DEVEL
#define APP_ID "ca.vlacroix.Tally"
#else
#define APP_ID "ca.vlacroix.Tally.Devel"
#endif
#define APP_VER "0.4.1"
static int
lua_get_is_devel(lua_State *L)
{
#ifdef DEVEL
lua_pushboolean(L, 1);
#else
lua_pushboolean(L, 0);
#endif
return 1;
}
static int
lua_get_app_id(lua_State *L)
{
lua_pushstring(L, APP_ID);
return 1;
}
static int
lua_get_app_ver(lua_State *L)
{
lua_pushstring(L, APP_VER);
return 1;
}
static const luaL_Reg tallylib[] = {
{ "get_is_devel", lua_get_is_devel },
{ "get_app_id", lua_get_app_id },
{ "get_app_ver", lua_get_app_ver },
{ NULL, NULL },
};
extern char _binary_tally_bytecode_start[];
extern char _binary_tally_bytecode_end[];
int
main()
{
lua_State *L;
size_t tally_bytecode_len;
int lua_result;
L = luaL_newstate();
luaL_openlibs(L);
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_remove(L, -2);
lua_pushstring(L, "tallylib");
luaL_newlib(L, tallylib);
lua_settable(L, -3);
lua_remove(L, -1);
tally_bytecode_len = ((size_t)_binary_tally_bytecode_end) - ((size_t)_binary_tally_bytecode_start);
lua_result = luaL_loadbuffer(L, _binary_tally_bytecode_start, tally_bytecode_len, APP_ID);
switch (lua_result) {
case LUA_OK:
lua_call(L, 0, 0);
return 0;
default:
/* FIXME: Handle each error case individually. */
fprintf(stderr, "An unrecoverable error occurred when loading Tally, preventing the program from starting.\n");
return lua_result;
}
}