Update janet_getmethod to better match new get api.

This commit is contained in:
Calvin Rose 2019-12-09 18:45:05 -06:00
parent 8ecf359bbe
commit 8700a407ce
7 changed files with 13 additions and 17 deletions

View File

@ -103,13 +103,15 @@ type janet_opt##name(const Janet *argv, int32_t argc, int32_t n, int32_t dflt_le
return janet_get##name(argv, n); \
}
Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods) {
int janet_getmethod(const uint8_t *method, const JanetMethod *methods, Janet *out) {
while (methods->name) {
if (!janet_cstrcmp(method, methods->name))
return janet_wrap_cfunction(methods->cfun);
if (!janet_cstrcmp(method, methods->name)) {
*out = janet_wrap_cfunction(methods->cfun);
return 1;
}
methods++;
}
return janet_wrap_nil();
return 0;
}
DEFINE_GETTER(number, NUMBER, double)

View File

@ -355,16 +355,14 @@ static int it_s64_get(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD))
return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), it_s64_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), it_s64_methods, out);
}
static int it_u64_get(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD))
return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), it_u64_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), it_u64_methods, out);
}
static const JanetReg it_cfuns[] = {

View File

@ -357,8 +357,7 @@ static int io_file_get(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD))
return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), io_file_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), io_file_methods, out);
}
FILE *janet_dynfile(const char *name, FILE *def) {

View File

@ -199,8 +199,7 @@ static const JanetMethod rng_methods[] = {
static int janet_rng_get(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), rng_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), rng_methods, out);
}
/* Get a random number */

View File

@ -1058,8 +1058,7 @@ static const JanetMethod parser_methods[] = {
static int parserget(void *p, Janet key, Janet *out) {
(void) p;
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
*out = janet_getmethod(janet_unwrap_keyword(key), parser_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), parser_methods, out);
}
static const JanetReg parse_cfuns[] = {

View File

@ -170,8 +170,7 @@ static int ta_getter(void *p, Janet key, Janet *out) {
size_t index, i;
JanetTArrayView *array = p;
if (janet_checktype(key, JANET_KEYWORD)) {
*out = janet_getmethod(janet_unwrap_keyword(key), tarray_view_methods);
return !janet_checktype(*out, JANET_NIL);
return janet_getmethod(janet_unwrap_keyword(key), tarray_view_methods, out);
}
if (!janet_checksize(key)) janet_panic("expected size as key");
index = (size_t) janet_unwrap_number(key);

View File

@ -1355,7 +1355,7 @@ JANET_NO_RETURN JANET_API void janet_panic_abstract(Janet x, int32_t n, const Ja
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 Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods);
JANET_API int janet_getmethod(const uint8_t *method, const JanetMethod *methods, Janet *out);
JANET_API double janet_getnumber(const Janet *argv, int32_t n);
JANET_API JanetArray *janet_getarray(const Janet *argv, int32_t n);