mirror of
https://github.com/janet-lang/janet
synced 2024-12-27 17:00:27 +00:00
Add call function pointer to abstract types.
This will allow better JITs, FFIs, DSLs, etc.
This commit is contained in:
parent
8db68c04c4
commit
e179f26d50
@ -208,9 +208,18 @@ static void vm_do_trace(JanetFunction *func) {
|
|||||||
static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
|
static Janet call_nonfn(JanetFiber *fiber, Janet callee) {
|
||||||
int32_t argn = fiber->stacktop - fiber->stackstart;
|
int32_t argn = fiber->stacktop - fiber->stackstart;
|
||||||
Janet ds, key;
|
Janet ds, key;
|
||||||
|
JanetType t = janet_type(callee);
|
||||||
|
if (t == JANET_ABSTRACT) {
|
||||||
|
JanetAbstract abst = janet_unwrap_abstract(callee);
|
||||||
|
const JanetAbstractType *at = janet_abstract_type(abst);
|
||||||
|
if (NULL != at->call) {
|
||||||
|
fiber->stacktop = fiber->stackstart;
|
||||||
|
return at->call(abst, argn, fiber->data + fiber->stackstart);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (argn != 1) janet_panicf("%v called with %d arguments, possibly expected 1", callee, argn);
|
if (argn != 1) janet_panicf("%v called with %d arguments, possibly expected 1", callee, argn);
|
||||||
if (janet_checktypes(callee, JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY |
|
if ((1 << t) & (JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY |
|
||||||
JANET_TFLAG_STRING | JANET_TFLAG_BUFFER | JANET_TFLAG_ABSTRACT)) {
|
JANET_TFLAG_STRING | JANET_TFLAG_BUFFER | JANET_TFLAG_ABSTRACT)) {
|
||||||
ds = callee;
|
ds = callee;
|
||||||
key = fiber->data[fiber->stackstart];
|
key = fiber->data[fiber->stackstart];
|
||||||
} else {
|
} else {
|
||||||
|
@ -908,6 +908,7 @@ struct JanetAbstractType {
|
|||||||
int (*compare)(void *lhs, void *rhs);
|
int (*compare)(void *lhs, void *rhs);
|
||||||
int32_t (*hash)(void *p, size_t len);
|
int32_t (*hash)(void *p, size_t len);
|
||||||
Janet(*next)(void *p, Janet key);
|
Janet(*next)(void *p, Janet key);
|
||||||
|
Janet(*call)(void *p, int32_t argc, Janet *argv);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Some macros to let us add extra types to JanetAbstract types without
|
/* Some macros to let us add extra types to JanetAbstract types without
|
||||||
@ -924,7 +925,8 @@ struct JanetAbstractType {
|
|||||||
#define JANET_ATEND_TOSTRING NULL,JANET_ATEND_COMPARE
|
#define JANET_ATEND_TOSTRING NULL,JANET_ATEND_COMPARE
|
||||||
#define JANET_ATEND_COMPARE NULL,JANET_ATEND_HASH
|
#define JANET_ATEND_COMPARE NULL,JANET_ATEND_HASH
|
||||||
#define JANET_ATEND_HASH NULL,JANET_ATEND_NEXT
|
#define JANET_ATEND_HASH NULL,JANET_ATEND_NEXT
|
||||||
#define JANET_ATEND_NEXT
|
#define JANET_ATEND_NEXT NULL,JANET_ATEND_CALL
|
||||||
|
#define JANET_ATEND_CALL
|
||||||
|
|
||||||
struct JanetReg {
|
struct JanetReg {
|
||||||
const char *name;
|
const char *name;
|
||||||
|
Loading…
Reference in New Issue
Block a user