Skip to content
Myr

docs: clarify ownership of jv arguments and results (#3627)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The ownership comment in `jv.h` currently says every `jv_*` function except `jv_copy` consumes its input. Several accessors and object-iteration functions borrow their arguments instead, which makes that guidance misleading for callers and language bindings. Document the borrowing exceptions beside their declarations, including numeric helpers and `jv_show`. Clarify that returned `jv` values are owned, while the pointers returned by `jv_string_value` and `jv_number_get_literal` belong to their input values. This changes comments only.

authored by JUSHUANGHUI LI · committed by GitHub · parent 6aeafff · Browse files · 1 file+15 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−2 removed

  • other:+15 added−2 removed

1 file+15 added−2 removed

modified:src/jv.h+15 added-2 removed
41 unmodified lines
42 } u;43} jv;4445/*46 * All jv_* functions consume (decref) input and produce (incref) output47 * Except jv_copy46 * Unless noted otherwise, functions consume their jv arguments. Returned jv47 * values are owned. Pass jv_copy(value) to retain a value across a consuming48 * call, and release each owned value with jv_free() when no longer needed.49 * Borrowed arguments are not consumed; the caller retains ownership.50 * Returned jv values are owned even when the arguments are borrowed.51 */5253/* These functions borrow their jv arguments. */54jv_kind jv_get_kind(jv);55const char* jv_kind_name(jv_kind);56static int jv_is_valid(jv x) { return jv_get_kind(x) != JV_KIND_INVALID; }5758/* Borrows its argument and returns a new reference to the same value. */59jv jv_copy(jv);60void jv_free(jv);6162/* Borrows its argument. */63int jv_get_refcnt(jv);6465int jv_equal(jv, jv);66int jv_identical(jv, jv);
10 unmodified lines
77jv jv_bool(int);7879jv jv_number(double);80jv jv_number_with_literal(const char*);81/* These functions borrow their jv arguments. */82double jv_number_value(jv);83int jv_is_integer(jv);84jv jv_number_abs(jv);85jv jv_number_negate(jv);8687/* Borrows its argument. */88int jv_number_has_literal(jv);89/* Borrows its argument; the returned string belongs to the number. */90const char* jv_number_get_literal(jv);9192jv jv_array(void);93jv jv_array_sized(int);
41 unmodified lines
135jv jv_string_empty(int len);136int jv_string_length_bytes(jv);137int jv_string_length_codepoints(jv);138unsigned long jv_string_hash(jv);139/* Borrows its argument; the returned string belongs to the value. */140const char* jv_string_value(jv);141jv jv_string_indexes(jv j, jv k);142jv jv_string_slice(jv j, int start, int end);143jv jv_string_concat(jv, jv);
15 unmodified lines
159int jv_object_length(jv object);160jv jv_object_merge(jv, jv);161jv jv_object_merge_recursive(jv, jv);162163/* Object iteration borrows the object; returned keys and values are owned. */164int jv_object_iter(jv);165int jv_object_iter_next(jv, int);166int jv_object_iter_valid(jv, int);167jv jv_object_iter_key(jv, int);
55 unmodified lines
223 JV_OBJECT_2, JV_OBJECT_1)(__VA_ARGS__)224225226227/* Borrows its argument. */228int jv_get_refcnt(jv);229230enum jv_print_flags {231 JV_PRINT_PRETTY = 1,
11 unmodified lines
243#define JV_PRINT_INDENT_FLAGS(n) \244 ((n) < 0 || (n) > 7 ? JV_PRINT_TAB | JV_PRINT_PRETTY : (n) << 8 | JV_PRINT_PRETTY)245void jv_dumpf(jv, FILE *f, int flags);246void jv_dump(jv, int flags);247/* Borrows its argument, unlike jv_dump() and jv_dumpf(). */248void jv_show(jv, int flags);249jv jv_dump_string(jv, int flags);250char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize);251
62 unmodified lines