@@ -122,14 +122,19 @@ Janet janet_array_peek(JanetArray *array) {
122122
123123/* C Functions */
124124
125- static Janet cfun_array_new (int32_t argc , Janet * argv ) {
125+ JANET_CORE_FN (cfun_array_new ,
126+ "(array/new capacity)" ,
127+ "Creates a new empty array with a pre-allocated capacity. The same as "
128+ "(array) but can be more efficient if the maximum size of an array is known." ) {
126129 janet_fixarity (argc , 1 );
127130 int32_t cap = janet_getinteger (argv , 0 );
128131 JanetArray * array = janet_array (cap );
129132 return janet_wrap_array (array );
130133}
131134
132- static Janet cfun_array_new_filled (int32_t argc , Janet * argv ) {
135+ JANET_CORE_FN (cfun_array_new_filled ,
136+ "(array/new-filled count &opt value)" ,
137+ "Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array." ) {
133138 janet_arity (argc , 1 , 2 );
134139 int32_t count = janet_getinteger (argv , 0 );
135140 Janet x = (argc == 2 ) ? argv [1 ] : janet_wrap_nil ();
@@ -141,7 +146,10 @@ static Janet cfun_array_new_filled(int32_t argc, Janet *argv) {
141146 return janet_wrap_array (array );
142147}
143148
144- static Janet cfun_array_fill (int32_t argc , Janet * argv ) {
149+ JANET_CORE_FN (cfun_array_fill ,
150+ "(array/fill arr &opt value)" ,
151+ "Replace all elements of an array with `value` (defaulting to nil) without changing the length of the array. "
152+ "Returns the modified array." ) {
145153 janet_arity (argc , 1 , 2 );
146154 JanetArray * array = janet_getarray (argv , 0 );
147155 Janet x = (argc == 2 ) ? argv [1 ] : janet_wrap_nil ();
@@ -151,19 +159,26 @@ static Janet cfun_array_fill(int32_t argc, Janet *argv) {
151159 return argv [0 ];
152160}
153161
154- static Janet cfun_array_pop (int32_t argc , Janet * argv ) {
162+ JANET_CORE_FN (cfun_array_pop ,
163+ "(array/pop arr)" ,
164+ "Remove the last element of the array and return it. If the array is empty, will return nil. Modifies "
165+ "the input array." ) {
155166 janet_fixarity (argc , 1 );
156167 JanetArray * array = janet_getarray (argv , 0 );
157168 return janet_array_pop (array );
158169}
159170
160- static Janet cfun_array_peek (int32_t argc , Janet * argv ) {
171+ JANET_CORE_FN (cfun_array_peek ,
172+ "(array/peek arr)" ,
173+ "Returns the last element of the array. Does not modify the array." ) {
161174 janet_fixarity (argc , 1 );
162175 JanetArray * array = janet_getarray (argv , 0 );
163176 return janet_array_peek (array );
164177}
165178
166- static Janet cfun_array_push (int32_t argc , Janet * argv ) {
179+ JANET_CORE_FN (cfun_array_push ,
180+ "(array/push arr x)" ,
181+ "Insert an element in the end of an array. Modifies the input array and returns it." ) {
167182 janet_arity (argc , 1 , -1 );
168183 JanetArray * array = janet_getarray (argv , 0 );
169184 if (INT32_MAX - argc + 1 <= array -> count ) {
@@ -176,7 +191,12 @@ static Janet cfun_array_push(int32_t argc, Janet *argv) {
176191 return argv [0 ];
177192}
178193
179- static Janet cfun_array_ensure (int32_t argc , Janet * argv ) {
194+ JANET_CORE_FN (cfun_array_ensure ,
195+ "(array/ensure arr capacity growth)" ,
196+ "Ensures that the memory backing the array is large enough for `capacity` "
197+ "items at the given rate of growth. Capacity and growth must be integers. "
198+ "If the backing capacity is already enough, then this function does nothing. "
199+ "Otherwise, the backing memory will be reallocated so that there is enough space." ) {
180200 janet_fixarity (argc , 3 );
181201 JanetArray * array = janet_getarray (argv , 0 );
182202 int32_t newcount = janet_getinteger (argv , 1 );
@@ -186,7 +206,13 @@ static Janet cfun_array_ensure(int32_t argc, Janet *argv) {
186206 return argv [0 ];
187207}
188208
189- static Janet cfun_array_slice (int32_t argc , Janet * argv ) {
209+ JANET_CORE_FN (cfun_array_slice ,
210+ "(array/slice arrtup &opt start end)" ,
211+ "Takes a slice of array or tuple from `start` to `end`. The range is half open, "
212+ "[start, end). Indexes can also be negative, indicating indexing from the "
213+ "end of the array. By default, `start` is 0 and `end` is the length of the array. "
214+ "Note that index -1 is synonymous with index `(length arrtup)` to allow a full "
215+ "negative slice range. Returns a new array." ) {
190216 JanetView view = janet_getindexed (argv , 0 );
191217 JanetRange range = janet_getslice (argc , argv );
192218 JanetArray * array = janet_array (range .end - range .start );
@@ -196,7 +222,12 @@ static Janet cfun_array_slice(int32_t argc, Janet *argv) {
196222 return janet_wrap_array (array );
197223}
198224
199- static Janet cfun_array_concat (int32_t argc , Janet * argv ) {
225+ JANET_CORE_FN (cfun_array_concat ,
226+ "(array/concat arr & parts)" ,
227+ "Concatenates a variable number of arrays (and tuples) into the first argument, "
228+ "which must be an array. If any of the parts are arrays or tuples, their elements will "
229+ "be inserted into the array. Otherwise, each part in `parts` will be appended to `arr` in order. "
230+ "Return the modified array `arr`." ) {
200231 int32_t i ;
201232 janet_arity (argc , 1 , -1 );
202233 JanetArray * array = janet_getarray (argv , 0 );
@@ -219,7 +250,12 @@ static Janet cfun_array_concat(int32_t argc, Janet *argv) {
219250 return janet_wrap_array (array );
220251}
221252
222- static Janet cfun_array_insert (int32_t argc , Janet * argv ) {
253+ JANET_CORE_FN (cfun_array_insert ,
254+ "(array/insert arr at & xs)" ,
255+ "Insert all `xs` into array `arr` at index `at`. `at` should be an integer between "
256+ "0 and the length of the array. A negative value for `at` will index backwards from "
257+ "the end of the array, such that inserting at -1 appends to the array. "
258+ "Returns the array." ) {
223259 size_t chunksize , restsize ;
224260 janet_arity (argc , 2 , -1 );
225261 JanetArray * array = janet_getarray (argv , 0 );
@@ -245,7 +281,12 @@ static Janet cfun_array_insert(int32_t argc, Janet *argv) {
245281 return argv [0 ];
246282}
247283
248- static Janet cfun_array_remove (int32_t argc , Janet * argv ) {
284+ JANET_CORE_FN (cfun_array_remove ,
285+ "(array/remove arr at &opt n)" ,
286+ "Remove up to `n` elements starting at index `at` in array `arr`. `at` can index from "
287+ "the end of the array with a negative index, and `n` must be a non-negative integer. "
288+ "By default, `n` is 1. "
289+ "Returns the array." ) {
249290 janet_arity (argc , 2 , 3 );
250291 JanetArray * array = janet_getarray (argv , 0 );
251292 int32_t at = janet_getinteger (argv , 1 );
@@ -270,7 +311,9 @@ static Janet cfun_array_remove(int32_t argc, Janet *argv) {
270311 return argv [0 ];
271312}
272313
273- static Janet cfun_array_trim (int32_t argc , Janet * argv ) {
314+ JANET_CORE_FN (cfun_array_trim ,
315+ "(array/trim arr)" ,
316+ "Set the backing capacity of an array to its current length. Returns the modified array." ) {
274317 janet_fixarity (argc , 1 );
275318 JanetArray * array = janet_getarray (argv , 0 );
276319 if (array -> count ) {
@@ -290,103 +333,33 @@ static Janet cfun_array_trim(int32_t argc, Janet *argv) {
290333 return argv [0 ];
291334}
292335
293- static Janet cfun_array_clear (int32_t argc , Janet * argv ) {
336+ JANET_CORE_FN (cfun_array_clear ,
337+ "(array/clear arr)" ,
338+ "Empties an array, setting it's count to 0 but does not free the backing capacity. "
339+ "Returns the modified array." ) {
294340 janet_fixarity (argc , 1 );
295341 JanetArray * array = janet_getarray (argv , 0 );
296342 array -> count = 0 ;
297343 return argv [0 ];
298344}
299345
300- static const JanetReg array_cfuns [] = {
301- {
302- "array/new" , cfun_array_new ,
303- JDOC ("(array/new capacity)\n\n"
304- "Creates a new empty array with a pre-allocated capacity. The same as "
305- "(array) but can be more efficient if the maximum size of an array is known." )
306- },
307- {
308- "array/new-filled" , cfun_array_new_filled ,
309- JDOC ("(array/new-filled count &opt value)\n\n"
310- "Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array." )
311- },
312- {
313- "array/fill" , cfun_array_fill ,
314- JDOC ("(array/fill arr &opt value)\n\n"
315- "Replace all elements of an array with `value` (defaulting to nil) without changing the length of the array. "
316- "Returns the modified array." )
317- },
318- {
319- "array/pop" , cfun_array_pop ,
320- JDOC ("(array/pop arr)\n\n"
321- "Remove the last element of the array and return it. If the array is empty, will return nil. Modifies "
322- "the input array." )
323- },
324- {
325- "array/peek" , cfun_array_peek ,
326- JDOC ("(array/peek arr)\n\n"
327- "Returns the last element of the array. Does not modify the array." )
328- },
329- {
330- "array/push" , cfun_array_push ,
331- JDOC ("(array/push arr x)\n\n"
332- "Insert an element in the end of an array. Modifies the input array and returns it." )
333- },
334- {
335- "array/ensure" , cfun_array_ensure ,
336- JDOC ("(array/ensure arr capacity growth)\n\n"
337- "Ensures that the memory backing the array is large enough for `capacity` "
338- "items at the given rate of growth. Capacity and growth must be integers. "
339- "If the backing capacity is already enough, then this function does nothing. "
340- "Otherwise, the backing memory will be reallocated so that there is enough space." )
341- },
342- {
343- "array/slice" , cfun_array_slice ,
344- JDOC ("(array/slice arrtup &opt start end)\n\n"
345- "Takes a slice of array or tuple from `start` to `end`. The range is half open, "
346- "[start, end). Indexes can also be negative, indicating indexing from the "
347- "end of the array. By default, `start` is 0 and `end` is the length of the array. "
348- "Note that index -1 is synonymous with index `(length arrtup)` to allow a full "
349- "negative slice range. Returns a new array." )
350- },
351- {
352- "array/concat" , cfun_array_concat ,
353- JDOC ("(array/concat arr & parts)\n\n"
354- "Concatenates a variable number of arrays (and tuples) into the first argument, "
355- "which must be an array. If any of the parts are arrays or tuples, their elements will "
356- "be inserted into the array. Otherwise, each part in `parts` will be appended to `arr` in order. "
357- "Return the modified array `arr`." )
358- },
359- {
360- "array/insert" , cfun_array_insert ,
361- JDOC ("(array/insert arr at & xs)\n\n"
362- "Insert all `xs` into array `arr` at index `at`. `at` should be an integer between "
363- "0 and the length of the array. A negative value for `at` will index backwards from "
364- "the end of the array, such that inserting at -1 appends to the array. "
365- "Returns the array." )
366- },
367- {
368- "array/remove" , cfun_array_remove ,
369- JDOC ("(array/remove arr at &opt n)\n\n"
370- "Remove up to `n` elements starting at index `at` in array `arr`. `at` can index from "
371- "the end of the array with a negative index, and `n` must be a non-negative integer. "
372- "By default, `n` is 1. "
373- "Returns the array." )
374- },
375- {
376- "array/trim" , cfun_array_trim ,
377- JDOC ("(array/trim arr)\n\n"
378- "Set the backing capacity of an array to its current length. Returns the modified array." )
379- },
380- {
381- "array/clear" , cfun_array_clear ,
382- JDOC ("(array/clear arr)\n\n"
383- "Empties an array, setting it's count to 0 but does not free the backing capacity. "
384- "Returns the modified array." )
385- },
386- {NULL , NULL , NULL }
387- };
388-
389346/* Load the array module */
390347void janet_lib_array (JanetTable * env ) {
391- janet_core_cfuns (env , NULL , array_cfuns );
348+ JanetRegExt array_cfuns [] = {
349+ JANET_CORE_REG ("array/new" , cfun_array_new ),
350+ JANET_CORE_REG ("array/new-filled" , cfun_array_new_filled ),
351+ JANET_CORE_REG ("array/fill" , cfun_array_fill ),
352+ JANET_CORE_REG ("array/pop" , cfun_array_pop ),
353+ JANET_CORE_REG ("array/peek" , cfun_array_peek ),
354+ JANET_CORE_REG ("array/push" , cfun_array_push ),
355+ JANET_CORE_REG ("array/ensure" , cfun_array_ensure ),
356+ JANET_CORE_REG ("array/slice" , cfun_array_slice ),
357+ JANET_CORE_REG ("array/concat" , cfun_array_concat ),
358+ JANET_CORE_REG ("array/insert" , cfun_array_insert ),
359+ JANET_CORE_REG ("array/remove" , cfun_array_remove ),
360+ JANET_CORE_REG ("array/trim" , cfun_array_trim ),
361+ JANET_CORE_REG ("array/clear" , cfun_array_clear ),
362+ JANET_REG_END
363+ };
364+ janet_core_cfuns_ext (env , NULL , array_cfuns );
392365}
0 commit comments