summaryrefslogtreecommitdiff
path: root/src/lua/lua_database.cpp
blob: 545dcd312e7c0a2593b0b7fc8643440c398268f6 (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "lua_database.hpp"

#include <memory>
#include <string>

#include "lua.hpp"

#include "esp_log.h"
#include "lauxlib.h"
#include "lua.h"
#include "lvgl.h"

#include "database.hpp"
#include "event_queue.hpp"
#include "index.hpp"
#include "property.hpp"
#include "service_locator.hpp"
#include "ui_events.hpp"

namespace lua {

[[maybe_unused]] static constexpr char kTag[] = "lua_db";

static constexpr char kDbIndexMetatable[] = "db_index";
static constexpr char kDbRecordMetatable[] = "db_record";
static constexpr char kDbIteratorMetatable[] = "db_record";

static auto indexes(lua_State* state) -> int {
  Bridge* instance = Bridge::Get(state);

  lua_newtable(state);

  auto db = instance->services().database().lock();
  if (!db) {
    return 1;
  }

  for (const auto& i : db->GetIndexes()) {
    database::IndexInfo** data = reinterpret_cast<database::IndexInfo**>(
        lua_newuserdata(state, sizeof(uintptr_t)));
    luaL_setmetatable(state, kDbIndexMetatable);
    *data = new database::IndexInfo{i};
    lua_rawseti(state, -2, i.id);
  }

  return 1;
}

static const struct luaL_Reg kDatabaseFuncs[] = {{"indexes", indexes},
                                                 {NULL, NULL}};

static auto db_iterate(lua_State* state) -> int {
  database::Iterator* it = *reinterpret_cast<database::Iterator**>(
      lua_touserdata(state, lua_upvalueindex(1)));

  auto res = it->Next();
  if (res) {
    database::IndexRecord** record = reinterpret_cast<database::IndexRecord**>(
        lua_newuserdata(state, sizeof(uintptr_t)));
    *record = new database::IndexRecord(*res);
    luaL_setmetatable(state, kDbRecordMetatable);
    return 1;
  }
  return 0;
}

static auto db_iterator_gc(lua_State* state) -> int {
  database::Iterator** it = reinterpret_cast<database::Iterator**>(
      luaL_checkudata(state, 1, kDbIteratorMetatable));
  if (it != NULL) {
    delete *it;
  }
  return 0;
}

static auto push_iterator(
    lua_State* state,
    std::variant<database::Continuation, database::IndexInfo> val) -> void {
  Bridge* instance = Bridge::Get(state);
  database::Iterator** data = reinterpret_cast<database::Iterator**>(
      lua_newuserdata(state, sizeof(uintptr_t)));
  std::visit(
      [&](auto&& arg) {
        *data = new database::Iterator(instance->services().database(), arg);
      },
      val);
  luaL_setmetatable(state, kDbIteratorMetatable);
  lua_pushcclosure(state, db_iterate, 1);
}

static auto record_text(lua_State* state) -> int {
  database::IndexRecord* data = *reinterpret_cast<database::IndexRecord**>(
      luaL_checkudata(state, 1, kDbRecordMetatable));
  lua_pushstring(state,
                 data->text().value_or("[tell jacqueline u saw this]").c_str());
  return 1;
}

static auto record_contents(lua_State* state) -> int {
  database::IndexRecord* data = *reinterpret_cast<database::IndexRecord**>(
      luaL_checkudata(state, 1, kDbRecordMetatable));

  if (data->track()) {
    lua_pushinteger(state, *data->track());
  } else {
    push_iterator(state, data->Expand(1).value());
  }

  return 1;
}

static auto record_gc(lua_State* state) -> int {
  database::IndexRecord** data = reinterpret_cast<database::IndexRecord**>(
      luaL_checkudata(state, 1, kDbRecordMetatable));
  if (data != NULL) {
    delete *data;
  }
  return 0;
}

static const struct luaL_Reg kDbRecordFuncs[] = {{"title", record_text},
                                                 {"contents", record_contents},
                                                 {"__tostring", record_text},
                                                 {"__gc", record_gc},
                                                 {NULL, NULL}};

static auto index_name(lua_State* state) -> int {
  database::IndexInfo** data = reinterpret_cast<database::IndexInfo**>(
      luaL_checkudata(state, 1, kDbIndexMetatable));
  if (data == NULL) {
    return 0;
  }
  lua_pushstring(state, (*data)->name.c_str());
  return 1;
}

static auto index_iter(lua_State* state) -> int {
  database::IndexInfo** data = reinterpret_cast<database::IndexInfo**>(
      luaL_checkudata(state, 1, kDbIndexMetatable));
  if (data == NULL) {
    return 0;
  }
  push_iterator(state, **data);
  return 1;
}

static auto index_gc(lua_State* state) -> int {
  database::IndexInfo** data = reinterpret_cast<database::IndexInfo**>(
      luaL_checkudata(state, 1, kDbIndexMetatable));
  if (data != NULL) {
    delete *data;
  }
  return 0;
}

static const struct luaL_Reg kDbIndexFuncs[] = {{"name", index_name},
                                                {"iter", index_iter},
                                                {"__tostring", index_name},
                                                {"__gc", index_gc},
                                                {NULL, NULL}};

static auto lua_database(lua_State* state) -> int {
  // Metatable for indexes
  luaL_newmetatable(state, kDbIndexMetatable);

  lua_pushliteral(state, "__index");
  lua_pushvalue(state, -2);
  lua_settable(state, -3);  // metatable.__index = metatable

  // Add member funcs to the metatable.
  luaL_setfuncs(state, kDbIndexFuncs, 0);

  luaL_newmetatable(state, kDbIteratorMetatable);
  lua_pushliteral(state, "__gc");
  lua_pushcfunction(state, db_iterator_gc);
  lua_settable(state, -3);

  luaL_newmetatable(state, kDbRecordMetatable);
  lua_pushliteral(state, "__index");
  lua_pushvalue(state, -2);
  lua_settable(state, -3);  // metatable.__index = metatable
  luaL_setfuncs(state, kDbRecordFuncs, 0);

  luaL_newlib(state, kDatabaseFuncs);
  return 1;
}

auto RegisterDatabaseModule(lua_State* s) -> void {
  luaL_requiref(s, "database", lua_database, true);
  lua_pop(s, 1);
}

}  // namespace lua