1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-04 02:52:59 +00:00

Merge pull request #415 from leafgarland/fix_numarray_example

Fix numarray example to work with jpm and latest Janet c-api
This commit is contained in:
Calvin Rose 2020-05-25 14:04:58 -04:00 committed by GitHub
commit b33fdc1674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 15 deletions

View File

@ -23,7 +23,7 @@ static int num_array_gc(void *p, size_t s) {
return 0; return 0;
} }
Janet num_array_get(void *p, Janet key); int num_array_get(void *p, Janet key, Janet *out);
void num_array_put(void *p, Janet key, Janet value); void num_array_put(void *p, Janet key, Janet value);
static const JanetAbstractType num_array_type = { static const JanetAbstractType num_array_type = {
@ -31,7 +31,8 @@ static const JanetAbstractType num_array_type = {
num_array_gc, num_array_gc,
NULL, NULL,
num_array_get, num_array_get,
num_array_put num_array_put,
JANET_ATEND_PUT
}; };
static Janet num_array_new(int32_t argc, Janet *argv) { static Janet num_array_new(int32_t argc, Janet *argv) {
@ -81,21 +82,20 @@ static const JanetMethod methods[] = {
{NULL, NULL} {NULL, NULL}
}; };
Janet num_array_get(void *p, Janet key) { int num_array_get(void *p, Janet key, Janet *out) {
size_t index; size_t index;
Janet value;
num_array *array = (num_array *)p; num_array *array = (num_array *)p;
if (janet_checktype(key, JANET_KEYWORD)) if (janet_checktype(key, JANET_KEYWORD))
return janet_getmethod(janet_unwrap_keyword(key), methods); return janet_getmethod(janet_unwrap_keyword(key), methods, out);
if (!janet_checkint(key)) if (!janet_checkint(key))
janet_panic("expected integer key"); janet_panic("expected integer key");
index = (size_t)janet_unwrap_integer(key); index = (size_t)janet_unwrap_integer(key);
if (index >= array->size) { if (index >= array->size) {
value = janet_wrap_nil(); return 0;
} else { } else {
value = janet_wrap_number(array->data[index]); *out = janet_wrap_number(array->data[index]);
} }
return value; return 1;
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {

View File

@ -0,0 +1,7 @@
(declare-project
:name "numarray"
:description "Example c lib with abstract type")
(declare-native
:name "numarray"
:source @["numarray.c"])

View File

@ -1,10 +1,4 @@
(import cook) (import build/numarray)
(cook/make-native
:name "numarray"
:source @["numarray.c"])
(import build/numarray :as numarray)
(def a (numarray/new 30)) (def a (numarray/new 30))
(print (get a 20)) (print (get a 20))