1
0
mirror of https://github.com/janet-lang/janet synced 2025-05-01 15:04:15 +00:00

Update example to use API.

This commit is contained in:
Calvin Rose 2019-02-05 19:49:10 -05:00
parent 74e1a3273f
commit 8343c9edd1
2 changed files with 7 additions and 14 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.
## 0.4.0 - ??
- Add methods to parser values that mirror the api.
- Add janet\_getmethod to CAPI for easier use of method like syntax.
- Add get/set to abstract types to allow them to behave more
like objects with methods.

View File

@ -75,26 +75,18 @@ void num_array_put(void *p, Janet key, Janet value) {
}
}
static const JanetReg methods[] = {
{"scale", num_array_scale,"(:scale numarray factor)"},
{"sum", num_array_sum,"(:sum numarray)"}
static const JanetMethod methods[] = {
{"scale", num_array_scale},
{"sum", num_array_sum},
{NULL, NULL}
};
Janet num_array_get(void *p, Janet key) {
size_t index;
Janet value;
num_array * array=(num_array *)p;
if (janet_checktype(key,JANET_KEYWORD)) {
const uint8_t *keyw = janet_unwrap_keyword(key);
const size_t nm = sizeof(methods)/sizeof(JanetReg);
size_t i;
for ( i=0 ; i<nm ; i++ ) {
if (!janet_cstrcmp(keyw, methods[i].name)) {
return janet_wrap_cfunction(methods[i].cfun);
}
}
janet_panicf("undefined method %s",keyw);
}
if (janet_checktype(key, JANET_KEYWORD))
return janet_getmethod(janet_unwrap_keyword(key), methods);
if (!janet_checkint(key))
janet_panic("expected integer key");
index = (size_t)janet_unwrap_integer(key);