1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Use janet_getnat when non-negative integer needed.

This commit is contained in:
Calvin Rose 2022-05-05 18:24:53 -05:00
parent a9d4d2bfa3
commit b3aed13567
5 changed files with 8 additions and 4 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Fix bug in peg `thru` and `to` combinators.
- Fix printing issue in `doc` macro. - Fix printing issue in `doc` macro.
- Numerous updates to function docstrings - Numerous updates to function docstrings
- Add `defdyn` aliases for various dynamic bindings used in core. - Add `defdyn` aliases for various dynamic bindings used in core.

View File

@ -136,8 +136,7 @@ JANET_CORE_FN(cfun_array_new_filled,
"(array/new-filled count &opt value)", "(array/new-filled count &opt value)",
"Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array.") { "Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array.") {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 2);
int32_t count = janet_getinteger(argv, 0); int32_t count = janet_getnat(argv, 0);
if (count < 0) janet_panic("expected positive integer");
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil(); Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
JanetArray *array = janet_array(count); JanetArray *array = janet_array(count);
for (int32_t i = 0; i < count; i++) { for (int32_t i = 0; i < count; i++) {

View File

@ -300,8 +300,7 @@ JANET_CORE_FN(cfun_table_new,
"entries going into a table on creation, extra memory allocation " "entries going into a table on creation, extra memory allocation "
"can be avoided. Returns the new table.") { "can be avoided. Returns the new table.") {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
int32_t cap = janet_getinteger(argv, 0); int32_t cap = janet_getnat(argv, 0);
if (cap < 0) janet_panic("expected positive integer");
return janet_wrap_table(janet_table(cap)); return janet_wrap_table(janet_table(cap));
} }

View File

@ -258,6 +258,7 @@ int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len) {
/* Calculate next power of 2. May overflow. If n is 0, /* Calculate next power of 2. May overflow. If n is 0,
* will return 0. */ * will return 0. */
int32_t janet_tablen(int32_t n) { int32_t janet_tablen(int32_t n) {
if (n < 0) return 0;
n |= n >> 1; n |= n >> 1;
n |= n >> 2; n |= n >> 2;
n |= n >> 4; n |= n >> 4;

View File

@ -76,5 +76,9 @@
(def text "1800-10-818-9-818 16/12\n17/12 19/12\n20/12 11/01") (def text "1800-10-818-9-818 16/12\n17/12 19/12\n20/12 11/01")
(assert (deep= (peg/match pattern text) (peg/match alt-pattern text)) "to/thru bug #971") (assert (deep= (peg/match pattern text) (peg/match alt-pattern text)) "to/thru bug #971")
(assert-error
"table rawget regression"
(table/new -1))
(end-suite) (end-suite)