fix: missing Random:seed method
This commit is contained in:
parent
1f3b7f828d
commit
1350910d28
@ -674,6 +674,9 @@ function __vc_create_random_methods(random_methods)
|
|||||||
local buffer = nil
|
local buffer = nil
|
||||||
local buffer_size = 64
|
local buffer_size = 64
|
||||||
|
|
||||||
|
local seed_func = random_methods.seed
|
||||||
|
local random_func = random_methods.random
|
||||||
|
|
||||||
function random_methods:bytes(n)
|
function random_methods:bytes(n)
|
||||||
local bytes = Bytearray(n)
|
local bytes = Bytearray(n)
|
||||||
for i=1,n do
|
for i=1,n do
|
||||||
@ -682,9 +685,14 @@ function __vc_create_random_methods(random_methods)
|
|||||||
return bytes
|
return bytes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function random_methods:seed(x)
|
||||||
|
seed_func(self, x)
|
||||||
|
buffer = nil
|
||||||
|
end
|
||||||
|
|
||||||
function random_methods:random(a, b)
|
function random_methods:random(a, b)
|
||||||
if not buffer or index > #buffer then
|
if not buffer or index > #buffer then
|
||||||
buffer = self:generate(buffer_size)
|
buffer = random_func(self, buffer_size)
|
||||||
index = 1
|
index = 1
|
||||||
end
|
end
|
||||||
local value = buffer[index]
|
local value = buffer[index]
|
||||||
|
|||||||
@ -22,7 +22,7 @@ static int l_random(lua::State* L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_generate(lua::State* L) {
|
static int l_bytes(lua::State* L) {
|
||||||
size_t size = lua::tointeger(L, 1);
|
size_t size = lua::tointeger(L, 1);
|
||||||
|
|
||||||
auto randomEngine = util::seeded_random_engine(random_device);
|
auto randomEngine = util::seeded_random_engine(random_device);
|
||||||
@ -40,7 +40,7 @@ static int l_uuid(lua::State* L) {
|
|||||||
|
|
||||||
const luaL_Reg randomlib[] = {
|
const luaL_Reg randomlib[] = {
|
||||||
{"random", lua::wrap<l_random>},
|
{"random", lua::wrap<l_random>},
|
||||||
{"bytes", lua::wrap<l_generate>},
|
{"bytes", lua::wrap<l_bytes>},
|
||||||
{"uuid", lua::wrap<l_uuid>},
|
{"uuid", lua::wrap<l_uuid>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
using namespace lua;
|
using namespace lua;
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
static int l_generate(lua::State* L) {
|
static int l_random(lua::State* L) {
|
||||||
std::uniform_int_distribution<> dist(0, std::numeric_limits<int>::max());
|
std::uniform_int_distribution<> dist(0, std::numeric_limits<int>::max());
|
||||||
|
|
||||||
auto& rng = require_userdata<LuaRandom>(L, 1).rng;
|
auto& rng = require_userdata<LuaRandom>(L, 1).rng;
|
||||||
@ -20,6 +20,11 @@ static int l_generate(lua::State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_seed(lua::State* L) {
|
||||||
|
require_userdata<LuaRandom>(L, 1).rng = std::mt19937(lua::touinteger(L, 2));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_meta_meta_call(lua::State* L) {
|
static int l_meta_meta_call(lua::State* L) {
|
||||||
integer_t seed;
|
integer_t seed;
|
||||||
if (lua::isnoneornil(L, 1)) {
|
if (lua::isnoneornil(L, 1)) {
|
||||||
@ -35,8 +40,10 @@ int LuaRandom::createMetatable(lua::State* L) {
|
|||||||
|
|
||||||
requireglobal(L, "__vc_create_random_methods");
|
requireglobal(L, "__vc_create_random_methods");
|
||||||
createtable(L, 0, 0);
|
createtable(L, 0, 0);
|
||||||
pushcfunction(L, wrap<l_generate>);
|
pushcfunction(L, wrap<l_random>);
|
||||||
setfield(L, "generate");
|
setfield(L, "random");
|
||||||
|
pushcfunction(L, wrap<l_seed>);
|
||||||
|
setfield(L, "seed");
|
||||||
call(L, 1, 1);
|
call(L, 1, 1);
|
||||||
|
|
||||||
setfield(L, "__index");
|
setfield(L, "__index");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user