1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Experimental getter/setter for abstract types

This commit is contained in:
J.-F. Cap 2019-02-05 17:14:13 +01:00
parent 98a26f5ce3
commit 960cf76eb5
6 changed files with 54 additions and 3 deletions

View File

@ -53,6 +53,8 @@ static int cfun_io_gc(void *p, size_t len);
JanetAbstractType cfun_io_filetype = {
"core/file",
cfun_io_gc,
NULL,
NULL,
NULL
};

View File

@ -612,7 +612,9 @@ static int parsergc(void *p, size_t size) {
static JanetAbstractType janet_parse_parsertype = {
"core/parser",
parsergc,
parsermark
parsermark,
NULL,
NULL
};
/* C Function parser */

View File

@ -1004,7 +1004,9 @@ static int peg_mark(void *p, size_t size) {
static JanetAbstractType peg_type = {
"core/peg",
NULL,
peg_mark
peg_mark,
NULL,
NULL
};
/* Convert Builder to Peg (Janet Abstract Value) */

View File

@ -219,6 +219,17 @@ Janet janet_get(Janet ds, Janet key) {
}
break;
}
case JANET_ABSTRACT:
{
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->get) {
value = (type->get)(janet_unwrap_abstract(ds),key);
} else {
janet_panicf("no getter for %T ", JANET_TFLAG_LENGTHABLE, ds);
value = janet_wrap_nil();
}
break;
}
}
return value;
}
@ -267,6 +278,17 @@ Janet janet_getindex(Janet ds, int32_t index) {
case JANET_STRUCT:
value = janet_struct_get(janet_unwrap_struct(ds), janet_wrap_integer(index));
break;
case JANET_ABSTRACT:
{
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->get) {
value = (type->get)(janet_unwrap_abstract(ds),janet_wrap_integer(index));
} else {
janet_panicf("no getter for %T ", JANET_TFLAG_LENGTHABLE, ds);
value = janet_wrap_nil();
}
break;
}
}
return value;
}
@ -327,6 +349,16 @@ void janet_putindex(Janet ds, int32_t index, Janet value) {
janet_table_put(table, janet_wrap_integer(index), value);
break;
}
case JANET_ABSTRACT:
{
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->put) {
(type->put)(janet_unwrap_abstract(ds),janet_wrap_integer(index),value);
} else {
janet_panicf("no setter for %T ", JANET_TFLAG_LENGTHABLE, ds);
}
break;
}
}
}
@ -367,5 +399,16 @@ void janet_put(Janet ds, Janet key, Janet value) {
case JANET_TABLE:
janet_table_put(janet_unwrap_table(ds), key, value);
break;
case JANET_ABSTRACT:
{
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
if (type->put) {
(type->put)(janet_unwrap_abstract(ds),key,value);
} else {
janet_panicf("no setter for %T ", JANET_TFLAG_LENGTHABLE, ds);
}
break;
}
}
}

View File

@ -228,7 +228,7 @@ static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
int32_t argn = fiber->stacktop - fiber->stackstart;
Janet ds, key;
if (argn != 1) janet_panicf("%v called with arity %d, expected 1", callee, argn);
if (janet_checktypes(callee, JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY)) {
if (janet_checktypes(callee, JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY | JANET_TFLAG_ABSTRACT)) {
ds = callee;
key = fiber->data[fiber->stackstart];
} else {

View File

@ -738,6 +738,8 @@ struct JanetAbstractType {
const char *name;
int (*gc)(void *data, size_t len);
int (*gcmark)(void *data, size_t len);
Janet (*get)(void *data, Janet key);
void (*put)(void *data, Janet key, Janet value);
};
/* Contains information about abstract types */