1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-17 15:57:12 +00:00

Fix janet_opt* api.

Inverted conditional made behavior incorrect. These
were not used in the core library, so were not tested.
This commit is contained in:
Calvin Rose
2019-11-09 09:39:14 -06:00
parent 6591e7636d
commit 1f55d40a10
3 changed files with 62 additions and 12 deletions

View File

@@ -115,15 +115,15 @@ static Janet cfun_rng_int(int32_t argc, Janet *argv) {
uint32_t word = janet_rng_u32(rng) >> 1;
return janet_wrap_integer(word);
} else {
int32_t max = janet_getinteger(argv, 1);
if (max <= 0) return janet_wrap_number(0);
int32_t max = janet_optnat(argv, argc, 1, INT32_MAX);
if (max == 0) return janet_wrap_number(0.0);
uint32_t modulo = (uint32_t) max;
uint32_t bad = UINT32_MAX % modulo;
uint32_t maxgen = INT32_MAX;
uint32_t maxword = maxgen - (maxgen % modulo);
uint32_t word;
do {
word = janet_rng_u32(rng);
} while (word > UINT32_MAX - bad);
word >>= 1;
word = janet_rng_u32(rng) >> 1;
} while (word > maxword);
return janet_wrap_integer(word % modulo);
}
}