From b3aed13567c1a77d474b48b05575508223095659 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 5 May 2022 18:24:53 -0500 Subject: [PATCH] Use janet_getnat when non-negative integer needed. --- CHANGELOG.md | 1 + src/core/array.c | 3 +-- src/core/table.c | 3 +-- src/core/util.c | 1 + test/suite0011.janet | 4 ++++ 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 427193e0..bbab5ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Fix bug in peg `thru` and `to` combinators. - Fix printing issue in `doc` macro. - Numerous updates to function docstrings - Add `defdyn` aliases for various dynamic bindings used in core. diff --git a/src/core/array.c b/src/core/array.c index a93e8faa..ee7d9d22 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -136,8 +136,7 @@ JANET_CORE_FN(cfun_array_new_filled, "(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.") { janet_arity(argc, 1, 2); - int32_t count = janet_getinteger(argv, 0); - if (count < 0) janet_panic("expected positive integer"); + int32_t count = janet_getnat(argv, 0); Janet x = (argc == 2) ? argv[1] : janet_wrap_nil(); JanetArray *array = janet_array(count); for (int32_t i = 0; i < count; i++) { diff --git a/src/core/table.c b/src/core/table.c index c7235f64..546f9b8e 100644 --- a/src/core/table.c +++ b/src/core/table.c @@ -300,8 +300,7 @@ JANET_CORE_FN(cfun_table_new, "entries going into a table on creation, extra memory allocation " "can be avoided. Returns the new table.") { janet_fixarity(argc, 1); - int32_t cap = janet_getinteger(argv, 0); - if (cap < 0) janet_panic("expected positive integer"); + int32_t cap = janet_getnat(argv, 0); return janet_wrap_table(janet_table(cap)); } diff --git a/src/core/util.c b/src/core/util.c index 4d3f3e92..e5126a6f 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -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, * will return 0. */ int32_t janet_tablen(int32_t n) { + if (n < 0) return 0; n |= n >> 1; n |= n >> 2; n |= n >> 4; diff --git a/test/suite0011.janet b/test/suite0011.janet index 38b43b66..34dd6c34 100644 --- a/test/suite0011.janet +++ b/test/suite0011.janet @@ -76,5 +76,9 @@ (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-error + "table rawget regression" + (table/new -1)) + (end-suite)