1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-27 00:40:26 +00:00

Remove check for function calls to enable all types,

even nil. Now any value can be called as a function, usually
looking itself up in an associative data structure.
This commit is contained in:
Calvin Rose 2019-01-06 11:56:40 -05:00
parent 6f3bc3d577
commit 2a79d2e749
3 changed files with 5 additions and 10 deletions

View File

@ -150,7 +150,7 @@ Janet janet_get(Janet ds, Janet key) {
Janet value; Janet value;
switch (janet_type(ds)) { switch (janet_type(ds)) {
default: default:
janet_panicf("get: expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds); janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
value = janet_wrap_nil(); value = janet_wrap_nil();
break; break;
case JANET_STRUCT: case JANET_STRUCT:
@ -226,7 +226,7 @@ Janet janet_getindex(Janet ds, int32_t index) {
if (index < 0) janet_panic("expected non-negative index"); if (index < 0) janet_panic("expected non-negative index");
switch (janet_type(ds)) { switch (janet_type(ds)) {
default: default:
janet_panicf("get: expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds); janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
value = janet_wrap_nil(); value = janet_wrap_nil();
break; break;
case JANET_STRING: case JANET_STRING:

View File

@ -223,10 +223,6 @@ static void *op_lookup[255] = {
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;
if (!janet_checktypes(callee, JANET_TFLAG_FUNCLIKE)) {
janet_panicf("attempted to call %v, expected %T", callee,
JANET_TFLAG_CALLABLE);
}
if (argn != 1) janet_panicf("%v called with arity %d, expected 1", callee, argn); 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)) {
ds = callee; ds = callee;
@ -566,7 +562,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) {
if (janet_fiber_funcframe(fiber, func)) { if (janet_fiber_funcframe(fiber, func)) {
int32_t n = fiber->stacktop - fiber->stackstart; int32_t n = fiber->stacktop - fiber->stackstart;
janet_panicf("%v called with %d argument%s, expected %d", janet_panicf("%v called with %d argument%s, expected %d",
callee, n, n > 1 ? "s" : "", func->def->arity); callee, n, n == 1 ? "s" : "", func->def->arity);
} }
stack = fiber->data + fiber->frame; stack = fiber->data + fiber->frame;
pc = func->def->bytecode; pc = func->def->bytecode;
@ -594,9 +590,10 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) {
if (janet_checktype(callee, JANET_FUNCTION)) { if (janet_checktype(callee, JANET_FUNCTION)) {
func = janet_unwrap_function(callee); func = janet_unwrap_function(callee);
if (janet_fiber_funcframe_tail(fiber, func)) { if (janet_fiber_funcframe_tail(fiber, func)) {
janet_stack_frame(fiber->data + fiber->frame)->pc = pc;
int32_t n = fiber->stacktop - fiber->stackstart; int32_t n = fiber->stacktop - fiber->stackstart;
janet_panicf("%v called with %d argument%s, expected %d", janet_panicf("%v called with %d argument%s, expected %d",
callee, n, n > 1 ? "s" : "", func->def->arity); callee, n, n == 1 ? "s" : "", func->def->arity);
} }
stack = fiber->data + fiber->frame; stack = fiber->data + fiber->frame;
pc = func->def->bytecode; pc = func->def->bytecode;

View File

@ -323,8 +323,6 @@ typedef enum JanetType {
#define JANET_TFLAG_DICTIONARY (JANET_TFLAG_TABLE | JANET_TFLAG_STRUCT) #define JANET_TFLAG_DICTIONARY (JANET_TFLAG_TABLE | JANET_TFLAG_STRUCT)
#define JANET_TFLAG_LENGTHABLE (JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY) #define JANET_TFLAG_LENGTHABLE (JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY)
#define JANET_TFLAG_CALLABLE (JANET_TFLAG_FUNCTION | JANET_TFLAG_CFUNCTION) #define JANET_TFLAG_CALLABLE (JANET_TFLAG_FUNCTION | JANET_TFLAG_CFUNCTION)
#define JANET_TFLAG_FUNCLIKE (JANET_TFLAG_CALLABLE | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY | \
JANET_TFLAG_KEYWORD | JANET_TFLAG_SYMBOL)
/* We provide three possible implementations of Janets. The preferred /* We provide three possible implementations of Janets. The preferred
* nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the * nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the