mirror of
https://github.com/janet-lang/janet
synced 2024-11-25 09:47:17 +00:00
Add optional argument functions to c api.
These are just helpers to make parameters than can be nil with a default value easier to handle in a consitent way.
This commit is contained in:
parent
b26a7bb22a
commit
339dea9390
@ -99,6 +99,11 @@ type janet_get##name(const Janet *argv, int32_t n) { \
|
|||||||
janet_panic_type(x, n, JANET_TFLAG_##NAME); \
|
janet_panic_type(x, n, JANET_TFLAG_##NAME); \
|
||||||
} \
|
} \
|
||||||
return janet_unwrap_##name(x); \
|
return janet_unwrap_##name(x); \
|
||||||
|
} \
|
||||||
|
type janet_opt##name(const Janet *argv, int32_t argc, int32_t n, type dflt) { \
|
||||||
|
if (argc >= n) return dflt; \
|
||||||
|
if (janet_checktype(argv[n], JANET_NIL)) return dflt; \
|
||||||
|
return janet_get##name(argv, n); \
|
||||||
}
|
}
|
||||||
|
|
||||||
Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods) {
|
Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods) {
|
||||||
@ -221,11 +226,17 @@ JanetRange janet_getslice(int32_t argc, const Janet *argv) {
|
|||||||
range.start = 0;
|
range.start = 0;
|
||||||
range.end = length;
|
range.end = length;
|
||||||
} else if (argc == 2) {
|
} else if (argc == 2) {
|
||||||
range.start = janet_gethalfrange(argv, 1, length, "start");
|
range.start = janet_checktype(argv[1], JANET_NIL)
|
||||||
|
? 0
|
||||||
|
: janet_gethalfrange(argv, 1, length, "start");
|
||||||
range.end = length;
|
range.end = length;
|
||||||
} else {
|
} else {
|
||||||
range.start = janet_gethalfrange(argv, 1, length, "start");
|
range.start = janet_checktype(argv[1], JANET_NIL)
|
||||||
range.end = janet_gethalfrange(argv, 2, length, "end");
|
? 0
|
||||||
|
: janet_gethalfrange(argv, 1, length, "start");
|
||||||
|
range.end = janet_checktype(argv[2], JANET_NIL)
|
||||||
|
? length
|
||||||
|
: janet_gethalfrange(argv, 2, length, "end");
|
||||||
if (range.end < range.start)
|
if (range.end < range.start)
|
||||||
range.end = range.start;
|
range.end = range.start;
|
||||||
}
|
}
|
||||||
@ -271,6 +282,30 @@ uint64_t janet_getflags(const Janet *argv, int32_t n, const char *flags) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t janet_optinteger(const Janet *argv, int32_t argc, int32_t n, int32_t dflt) {
|
||||||
|
if (argc <= n) return dflt;
|
||||||
|
if (janet_checktype(argv[n], JANET_NIL)) return dflt;
|
||||||
|
return janet_getinteger(argv, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t janet_optinteger64(const Janet *argv, int32_t argc, int32_t n, int64_t dflt) {
|
||||||
|
if (argc <= n) return dflt;
|
||||||
|
if (janet_checktype(argv[n], JANET_NIL)) return dflt;
|
||||||
|
return janet_getinteger64(argv, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t janet_optsize(const Janet *argv, int32_t argc, int32_t n, size_t dflt) {
|
||||||
|
if (argc <= n) return dflt;
|
||||||
|
if (janet_checktype(argv[n], JANET_NIL)) return dflt;
|
||||||
|
return janet_getsize(argv, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *janet_optabstract(const Janet *argv, int32_t argc, int32_t n, const JanetAbstractType *at, void *dflt) {
|
||||||
|
if (argc <= n) return dflt;
|
||||||
|
if (janet_checktype(argv[n], JANET_NIL)) return dflt;
|
||||||
|
return janet_getabstract(argv, n, at);
|
||||||
|
}
|
||||||
|
|
||||||
/* Some definitions for function-like macros */
|
/* Some definitions for function-like macros */
|
||||||
|
|
||||||
JANET_API JanetStructHead *(janet_struct_head)(const JanetKV *st) {
|
JANET_API JanetStructHead *(janet_struct_head)(const JanetKV *st) {
|
||||||
|
@ -1333,6 +1333,7 @@ JANET_API void janet_arity(int32_t arity, int32_t min, int32_t max);
|
|||||||
JANET_API void janet_fixarity(int32_t arity, int32_t fix);
|
JANET_API void janet_fixarity(int32_t arity, int32_t fix);
|
||||||
|
|
||||||
JANET_API Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods);
|
JANET_API Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods);
|
||||||
|
|
||||||
JANET_API double janet_getnumber(const Janet *argv, int32_t n);
|
JANET_API double janet_getnumber(const Janet *argv, int32_t n);
|
||||||
JANET_API JanetArray *janet_getarray(const Janet *argv, int32_t n);
|
JANET_API JanetArray *janet_getarray(const Janet *argv, int32_t n);
|
||||||
JANET_API const Janet *janet_gettuple(const Janet *argv, int32_t n);
|
JANET_API const Janet *janet_gettuple(const Janet *argv, int32_t n);
|
||||||
@ -1360,6 +1361,27 @@ JANET_API JanetRange janet_getslice(int32_t argc, const Janet *argv);
|
|||||||
JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which);
|
JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which);
|
||||||
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
|
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
|
||||||
|
|
||||||
|
/* Optionals */
|
||||||
|
JANET_API double janet_optnumber(const Janet *argv, int32_t argc, int32_t n, double dflt);
|
||||||
|
JANET_API JanetArray *janet_optarray(const Janet *argv, int32_t argc, int32_t n, JanetArray *dflt);
|
||||||
|
JANET_API const Janet *janet_opttuple(const Janet *argv, int32_t argc, int32_t n, const Janet *dflt);
|
||||||
|
JANET_API JanetTable *janet_opttable(const Janet *argv, int32_t argc, int32_t n, JanetTable *dflt);
|
||||||
|
JANET_API const JanetKV *janet_optstruct(const Janet *argv, int32_t argc, int32_t n, const JanetKV *dflt);
|
||||||
|
JANET_API const uint8_t *janet_optstring(const Janet *argv, int32_t argc, int32_t n, const uint8_t *dflt);
|
||||||
|
JANET_API const char *janet_optcstring(const Janet *argv, int32_t argc, int32_t n, const char *dflt);
|
||||||
|
JANET_API const uint8_t *janet_optsymbol(const Janet *argv, int32_t argc, int32_t n, const uint8_t *dflt);
|
||||||
|
JANET_API const uint8_t *janet_optkeyword(const Janet *argv, int32_t argc, int32_t n, const uint8_t *dflt);
|
||||||
|
JANET_API JanetBuffer *janet_optbuffer(const Janet *argv, int32_t argc, int32_t n, JanetBuffer *dflt);
|
||||||
|
JANET_API JanetFiber *janet_optfiber(const Janet *argv, int32_t argc, int32_t n, JanetFiber *dflt);
|
||||||
|
JANET_API JanetFunction *janet_optfunction(const Janet *argv, int32_t argc, int32_t n, JanetFunction *dflt);
|
||||||
|
JANET_API JanetCFunction janet_optcfunction(const Janet *argv, int32_t argc, int32_t n, JanetCFunction dflt);
|
||||||
|
JANET_API int janet_optboolean(const Janet *argv, int32_t argc, int32_t n, int dflt);
|
||||||
|
JANET_API void *janet_optpointer(const Janet *argv, int32_t argc, int32_t n, void *dflt);
|
||||||
|
JANET_API int32_t janet_optinteger(const Janet *argv, int32_t argc, int32_t n, int32_t dflt);
|
||||||
|
JANET_API int64_t janet_optinteger64(const Janet *argv, int32_t argc, int32_t n, int64_t dflt);
|
||||||
|
JANET_API size_t janet_optsize(const Janet *argv, int32_t argc, int32_t n, size_t dflt);
|
||||||
|
JANET_API void *janet_optabstract(const Janet *argv, int32_t argc, int32_t n, const JanetAbstractType *at, void *dflt);
|
||||||
|
|
||||||
JANET_API Janet janet_dyn(const char *name);
|
JANET_API Janet janet_dyn(const char *name);
|
||||||
JANET_API void janet_setdyn(const char *name, Janet value);
|
JANET_API void janet_setdyn(const char *name, Janet value);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user