1
0
mirror of https://github.com/janet-lang/janet synced 2024-10-31 22:16:16 +00:00

Allow method calls on typed arrays.

This commit is contained in:
Calvin Rose 2019-08-05 18:49:21 -05:00
parent ca5dce5d9f
commit 850a2d7f79
2 changed files with 14 additions and 1 deletions

View File

@ -107,7 +107,7 @@ Janet janet_getmethod(const uint8_t *method, const JanetMethod *methods) {
return janet_wrap_cfunction(methods->cfun);
methods++;
}
janet_panicf("unknown method %S invoked", method);
janet_panicf("unknown method :%S invoked", method);
return janet_wrap_nil();
}

View File

@ -159,10 +159,14 @@ static void ta_view_unmarshal(void *p, JanetMarshalContext *ctx) {
view->as.u8 = view->buffer->data + offset;
}
static JanetMethod tarray_view_methods[];
static Janet ta_getter(void *p, Janet key) {
Janet value;
size_t index, i;
JanetTArrayView *array = p;
if (janet_checktype(key, JANET_KEYWORD))
return janet_getmethod(janet_unwrap_keyword(key), tarray_view_methods);
if (!janet_checksize(key)) janet_panic("expected size as key");
index = (size_t) janet_unwrap_number(key);
i = index * array->stride;
@ -551,6 +555,15 @@ static const JanetReg ta_cfuns[] = {
{NULL, NULL, NULL}
};
static JanetMethod tarray_view_methods[] = {
{"length", cfun_typed_array_size},
{"properties", cfun_typed_array_properties},
{"copy-bytes", cfun_typed_array_copy_bytes},
{"swap-bytes", cfun_typed_array_swap_bytes},
{"slice", cfun_typed_array_slice},
{NULL, NULL}
};
/* Module entry point */
void janet_lib_typed_array(JanetTable *env) {
janet_core_cfuns(env, NULL, ta_cfuns);