| 4865 | } |
| 4866 | |
| 4867 | static int db_bootstrap(Lua L) |
| 4868 | { |
| 4869 | luaL_checkudata(L, 1, dbtypes.db); |
| 4870 | lua_pop(L, 1); |
| 4871 | |
| 4872 | lua_Debug ar = {0}; |
| 4873 | const int frame = 2; // 0 -> db_bootstrap , 1 -> comdb2_main, 2 -> SP |
| 4874 | if (lua_getstack(L, frame, &ar) == 0) return 0; |
| 4875 | |
| 4876 | lua_getglobal(L, "_SP"); |
| 4877 | lua_newtable(L); |
| 4878 | lua_setfield(L, -2, "cdb2_func_refs"); |
| 4879 | lua_newtable(L); |
| 4880 | lua_setfield(L, -2, "cdb2_func_names"); |
| 4881 | |
| 4882 | lua_getfield(L, 1, "cdb2_func_refs"); |
| 4883 | lua_getfield(L, 1, "cdb2_func_names"); |
| 4884 | |
| 4885 | // Stack: |
| 4886 | // 3: table for lookup by name |
| 4887 | // 2: table for lookup by ref |
| 4888 | // 1: _SP{} |
| 4889 | |
| 4890 | int n = 1; // 1st local variable |
| 4891 | const char *name; |
| 4892 | while ((name = lua_getlocal(L, &ar, n++)) != NULL) { |
| 4893 | if (!lua_isfunction(L, -1)) { |
| 4894 | lua_pop(L, 1); |
| 4895 | continue; |
| 4896 | } |
| 4897 | lua_pushvalue(L, -1); // push copy of func |
| 4898 | lua_setfield(L, 3, name); // _SP.cdb2_func_names[funcname] = func |
| 4899 | lua_pushstring(L, name); |
| 4900 | lua_settable(L, 2); // _SP.cdb2_func_refs[func] = funcname |
| 4901 | } |
| 4902 | lua_settop(L, 0); |
| 4903 | |
| 4904 | return 0; |
| 4905 | } |
| 4906 | |
| 4907 | static int db_ctrace(Lua L) |
| 4908 | { |
nothing calls this directly
no test coverage detected