Skip to content

Commit 5d24510

Browse files
committed
Exporting DJB hash to Lua.
1 parent c72c775 commit 5d24510

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/lua/extra.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "lua/luawrapper.h"
2929
#include "support/file.h"
3030
#include "support/strings-helpers.h"
31+
#include "support/djbhash.h"
3132
#include "support/zip.h"
3233

3334
namespace {
@@ -81,6 +82,30 @@ PCSX::File* load(std::string_view name, std::string_view from, bool inArchives =
8182
return new PCSX::PosixFile(absolutePath);
8283
}
8384

85+
uint64_t djbHash(const char* str, size_t len) {
86+
return PCSX::djb::hash(str, len);
87+
}
88+
89+
template <typename T, size_t S>
90+
void registerSymbol(PCSX::Lua L, const char (&name)[S], const T ptr) {
91+
L.push<S>(name);
92+
L.push((void*)ptr);
93+
L.settable();
94+
}
95+
96+
#define REGISTER(L, s) registerSymbol(L, #s, s)
97+
98+
void registerAllSymbols(PCSX::Lua L) {
99+
L.getfieldtable("_CLIBS", LUA_REGISTRYINDEX);
100+
L.push("SUPPORT_EXTRA");
101+
L.newtable();
102+
103+
REGISTER(L, djbHash);
104+
105+
L.settable();
106+
L.pop();
107+
}
108+
84109
} // namespace
85110

86111
PCSX::ZipArchive& PCSX::LuaFFI::addArchive(Lua L, IO<File> file) {
@@ -138,6 +163,7 @@ void PCSX::LuaFFI::open_extra(Lua L) {
138163
static const char* extra = (
139164
#include "lua/extra.lua"
140165
);
166+
registerAllSymbols(L);
141167
L.load(pprint, "third_party:pprint.lua/pprint.lua");
142168
L.load(pprint_internals, "third_party:pprint.lua/pprint-internals.lua");
143169
L.load(reflectFFI, "third_party:ffi-reflect/reflect.lua");

src/lua/extra.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
-- along with this program; if not, write to the
1616
-- Free Software Foundation, Inc.,
1717
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
19+
ffi.cdef [[
20+
uint64_t djbHash(const char* str, size_t len);
21+
]]
22+
23+
local C = ffi.load 'SUPPORT_EXTRA'
24+
1825
Support.extra = {
1926

2027
loadfile = function(name) return loadstring(Support._internal.loadfile(name), '@' .. name) end,
@@ -33,5 +40,8 @@ Support.extra = {
3340
error('FFI call failed in ' .. name .. ': ' .. ret)
3441
end,
3542

43+
djbHash = function(str)
44+
return C.djbHash(str, #str)
45+
end,
3646
}
3747
-- )EOF"

0 commit comments

Comments
 (0)