Files
ReSkate/External/luau/Common/include/Luau/HashUtil.h
T
AyeZeeBB 6d542536e1 Plugins: sandboxed Luau plugins in mods, phase 1 (the host, and the base, console and storage APIs)
A mod with a reskate-plugin.json is a plugin: Luau scripts that run in their own VM, with only
the reskate API ReSkate registers for them. Luau 0.741 is vendored in External/luau (the VM and
compiler only: no native code generation, no loadstring, io or C modules).

- Limits: 64 MB of memory, 50 ms a call, 2 ms a tick on average (over it for 3 s stops it),
  more than 5 errors in 10 s stops it, 50 log lines a second. A plugin over a limit cannot catch
  it in a pcall and carry on.
- API: reskate.log, on (load, unload, tick, level_loaded, level_unloading), timers, the level,
  its own package's files and JSON, require inside the package; console.command and print
  ("console"); storage get, set, add and keys ("storage"), 1 MB in
  %LOCALAPPDATA%\ReSkate\Plugins\<mod>\storage.json.
- Found every 2 s among the mods mods.json leaves on: switching a mod, or changing a plugin's
  files, loads, unloads or reloads it without a restart. A package holding a program is refused.
- plugin list, commands, reload, off and on in the console. A plugin cannot take a name of
  ReSkate's own commands.
- Tests: DINGOSDK_BUILD_PLUGIN_TESTS, including plugins written to break the sandbox.
2026-10-19 11:55:27 -05:00

63 lines
1.9 KiB
C++

#pragma once
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include <type_traits>
#include <utility>
namespace Luau
{
struct DenseHashPointer
{
size_t operator()(const void* key) const
{
// The idea to use this hash function was suggested here originally: https://maskray.me/blog/2026-06-07-recent-llvm-hash-table-improvements
// Hash function implementation is detailed here: https://github.com/MaskRay/llvm-project/blob/main/llvm/include/llvm/ADT/DenseMapInfo.h
// This hash produces better scattering for arena allocated types, because the pointers usually share the higher order bits.
// When inserting lots of keys, quadratic probing is not enough to save DenseHash, although it usually takes many more elements,
// before it becomes a problem
uint64_t u = static_cast<uint64_t>(uintptr_t(key));
u *= 0xbf58476d1ce4e5b9u;
u ^= u >> 31;
// On 32-bit platforms uint64_t to size_t is a narrowing, so we need
// to static cast here.
return static_cast<size_t>(u);
}
};
namespace detail
{
template<typename T>
using DenseHashDefault = std::conditional_t<std::is_pointer_v<T>, DenseHashPointer, std::hash<T>>;
} // namespace detail
inline void hashCombine(size_t& seed, size_t hash)
{
// Golden Ratio constant used for better hash scattering
// See https://softwareengineering.stackexchange.com/a/402543
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T1, typename T2, typename H1 = detail::DenseHashDefault<T1>, typename H2 = detail::DenseHashDefault<T2>>
struct PairHash
{
std::size_t operator()(const std::pair<T1, T2>& p) const noexcept
{
std::size_t seed = 0;
hashCombine(seed, h1(p.first));
hashCombine(seed, h2(p.second));
return seed;
}
private:
H1 h1;
H2 h2;
};
} // namespace Luau