Line data Source code
1 : #pragma once
2 :
3 : // for inclusion at the end of the generated .cpp
4 :
5 3902 : void * new_ap_object(lua_State *L, size_t size, const char * name) {
6 3902 : void * ud = lua_newuserdata(L, size);
7 3902 : if (luaL_newmetatable(L, name)) { // metatable just created, set it up
8 163 : for (uint32_t i = 0; i < ARRAY_SIZE(ap_object_fun); i++) {
9 163 : if (strcmp(name, ap_object_fun[i].name) == 0) {
10 22 : lua_pushcfunction(L, ap_object_fun[i].func);
11 22 : lua_setfield(L, -2, "__index");
12 22 : break;
13 : }
14 : }
15 : // if no name matched, the metatable will be pointless but otherwise
16 : // valid, so it's safe to continue. since this function isn't
17 : // accessible to user code, it's probably not worth raising an error.
18 : }
19 : // correct metatable is on stack regardless of if we just set it up
20 3902 : lua_setmetatable(L, -2);
21 3902 : return ud;
22 : }
23 :
24 897641 : void set_userdata_metatable(lua_State *L, const char * name) {
25 897641 : if (luaL_newmetatable(L, name)) { // metatable just created, set it up
26 2653 : for (uint32_t i = 0; i < ARRAY_SIZE(userdata_fun); i++) {
27 2653 : if (strcmp(name, userdata_fun[i].name) == 0) {
28 169 : lua_pushcfunction(L, userdata_fun[i].func);
29 169 : lua_setfield(L, -2, "__index");
30 169 : if (userdata_fun[i].operators != nullptr) {
31 68 : luaL_setfuncs(L, userdata_fun[i].operators, 0);
32 : }
33 : break;
34 : }
35 : }
36 : // if no name matched, the metatable will be pointless but otherwise
37 : // valid, so it's safe to continue. since this function isn't
38 : // accessible to user code, it's probably not worth raising an error.
39 : }
40 : // correct metatable is on stack regardless of if we just set it up
41 897641 : lua_setmetatable(L, -2);
42 897641 : }
43 :
44 652 : static int binding_index(lua_State *L) {
45 652 : const char * name = luaL_checkstring(L, 2);
46 :
47 : bool found = false;
48 30291 : for (uint32_t i = 0; i < ARRAY_SIZE(singleton_fun); i++) {
49 30027 : if (strcmp(name, singleton_fun[i].name) == 0) {
50 388 : lua_newuserdata(L, 0);
51 388 : if (luaL_newmetatable(L, name)) { // need to create metatable
52 376 : lua_pushcfunction(L, singleton_fun[i].func);
53 376 : lua_setfield(L, -2, "__index");
54 : }
55 388 : lua_setmetatable(L, -2);
56 : found = true;
57 : break;
58 : }
59 : }
60 : if (!found) {
61 6928 : for (uint32_t i = 0; i < ARRAY_SIZE(new_userdata); i++) {
62 6799 : if (strcmp(name, new_userdata[i].name) == 0) {
63 135 : lua_pushcfunction(L, new_userdata[i].fun);
64 : found = true;
65 : break;
66 : }
67 : }
68 : }
69 517 : if (!found) {
70 : return 0;
71 : }
72 :
73 : // store found value to avoid a re-index
74 523 : lua_pushvalue(L, -2);
75 523 : lua_pushvalue(L, -2);
76 523 : lua_settable(L, -5);
77 :
78 523 : return 1;
79 : }
80 :
81 92 : void load_generated_sandbox(lua_State *L) {
82 92 : lua_createtable(L, 0, 1);
83 92 : lua_pushcfunction(L, binding_index);
84 92 : lua_setfield(L, -2, "__index");
85 92 : lua_setmetatable(L, -2);
86 92 : }
|