2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-07-02 01:51:16 +00:00
|
|
|
*
|
2017-04-18 02:40:39 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2020-01-18 23:55:07 +00:00
|
|
|
#include "util.h"
|
2020-04-24 21:18:31 +00:00
|
|
|
#include "state.h"
|
|
|
|
#include "gc.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2020-04-24 21:18:31 +00:00
|
|
|
JANET_THREAD_LOCAL JanetTraversalNode *janet_vm_traversal = NULL;
|
|
|
|
JANET_THREAD_LOCAL JanetTraversalNode *janet_vm_traversal_top = NULL;
|
|
|
|
JANET_THREAD_LOCAL JanetTraversalNode *janet_vm_traversal_base = NULL;
|
|
|
|
|
|
|
|
static void push_traversal_node(void *lhs, void *rhs) {
|
|
|
|
JanetTraversalNode node;
|
|
|
|
node.self = (JanetGCObject *) lhs;
|
|
|
|
node.other = (JanetGCObject *) rhs;
|
|
|
|
node.index = 0;
|
|
|
|
node.index2 = 0;
|
|
|
|
if (janet_vm_traversal + 1 >= janet_vm_traversal_top) {
|
|
|
|
size_t oldsize = janet_vm_traversal - janet_vm_traversal_base;
|
|
|
|
size_t newsize = 2 * oldsize + 1;
|
|
|
|
if (newsize < 128) {
|
|
|
|
newsize = 128;
|
|
|
|
}
|
|
|
|
JanetTraversalNode *tn = realloc(janet_vm_traversal_base, newsize * sizeof(JanetTraversalNode));
|
|
|
|
if (tn == NULL) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
janet_vm_traversal_base = tn;
|
|
|
|
janet_vm_traversal_top = janet_vm_traversal_base + newsize;
|
|
|
|
janet_vm_traversal = janet_vm_traversal_base + oldsize;
|
|
|
|
}
|
|
|
|
*(++janet_vm_traversal) = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used for travsersing structs and tuples without recursion */
|
|
|
|
static int traversal_next(Janet *x, Janet *y) {
|
|
|
|
JanetTraversalNode *t = janet_vm_traversal;
|
|
|
|
while (t && t > janet_vm_traversal_base) {
|
|
|
|
JanetGCObject *self = t->self;
|
|
|
|
JanetTupleHead *tself = (JanetTupleHead *)self;
|
|
|
|
JanetStructHead *sself = (JanetStructHead *)self;
|
|
|
|
JanetGCObject *other = t->other;
|
|
|
|
JanetTupleHead *tother = (JanetTupleHead *)other;
|
|
|
|
JanetStructHead *sother = (JanetStructHead *)other;
|
|
|
|
if ((self->flags & JANET_MEM_TYPEBITS) == JANET_MEMORY_TUPLE) {
|
|
|
|
/* Node is a tuple at index t->index */
|
|
|
|
if (t->index < tself->length) {
|
|
|
|
int32_t index = t->index++;
|
|
|
|
*x = tself->data[index];
|
|
|
|
*y = tother->data[index];
|
|
|
|
janet_vm_traversal = t;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Node is a struct at index t->index: if t->index2 is true, we should return the values. */
|
|
|
|
if (t->index2) {
|
|
|
|
t->index2 = 0;
|
|
|
|
int32_t index = t->index++;
|
|
|
|
*x = sself->data[index].value;
|
|
|
|
*y = sother->data[index].value;
|
|
|
|
janet_vm_traversal = t;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (int32_t i = t->index; i < sself->capacity; i++) {
|
|
|
|
t->index2 = 1;
|
|
|
|
*x = sself->data[t->index].key;
|
|
|
|
*y = sother->data[t->index].key;
|
|
|
|
janet_vm_traversal = t;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t--;
|
|
|
|
}
|
|
|
|
janet_vm_traversal = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2018-09-06 02:18:42 +00:00
|
|
|
* Define a number of functions that can be used internally on ANY Janet.
|
2017-11-01 21:53:43 +00:00
|
|
|
*/
|
|
|
|
|
2020-01-18 23:55:07 +00:00
|
|
|
Janet janet_next(Janet ds, Janet key) {
|
|
|
|
JanetType t = janet_type(ds);
|
|
|
|
switch (t) {
|
|
|
|
default:
|
|
|
|
janet_panicf("expected iterable type, got %v", ds);
|
|
|
|
case JANET_TABLE:
|
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *start;
|
|
|
|
int32_t cap;
|
|
|
|
if (t == JANET_TABLE) {
|
|
|
|
JanetTable *tab = janet_unwrap_table(ds);
|
|
|
|
cap = tab->capacity;
|
|
|
|
start = tab->data;
|
|
|
|
} else {
|
|
|
|
JanetStruct st = janet_unwrap_struct(ds);
|
|
|
|
cap = janet_struct_capacity(st);
|
|
|
|
start = st;
|
|
|
|
}
|
|
|
|
const JanetKV *end = start + cap;
|
|
|
|
const JanetKV *kv = janet_checktype(key, JANET_NIL)
|
|
|
|
? start
|
|
|
|
: janet_dict_find(start, cap, key) + 1;
|
|
|
|
while (kv < end) {
|
|
|
|
if (!janet_checktype(kv->key, JANET_NIL)) return kv->key;
|
|
|
|
kv++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_KEYWORD:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_BUFFER:
|
|
|
|
case JANET_ARRAY:
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
int32_t i;
|
|
|
|
if (janet_checktype(key, JANET_NIL)) {
|
|
|
|
i = 0;
|
|
|
|
} else if (janet_checkint(key)) {
|
|
|
|
i = janet_unwrap_integer(key) + 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int32_t len;
|
|
|
|
if (t == JANET_BUFFER) {
|
|
|
|
len = janet_unwrap_buffer(ds)->count;
|
|
|
|
} else if (t == JANET_ARRAY) {
|
|
|
|
len = janet_unwrap_array(ds)->count;
|
|
|
|
} else if (t == JANET_TUPLE) {
|
|
|
|
len = janet_tuple_length(janet_unwrap_tuple(ds));
|
|
|
|
} else {
|
|
|
|
len = janet_string_length(janet_unwrap_string(ds));
|
|
|
|
}
|
|
|
|
if (i < len && i >= 0) {
|
|
|
|
return janet_wrap_integer(i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-01-19 00:09:20 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstract abst = janet_unwrap_abstract(ds);
|
|
|
|
const JanetAbstractType *at = janet_abstract_type(abst);
|
|
|
|
if (NULL == at->next) break;
|
|
|
|
return at->next(abst, key);
|
|
|
|
}
|
2020-01-18 23:55:07 +00:00
|
|
|
}
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:57:36 +00:00
|
|
|
/* Compare two abstract values */
|
|
|
|
static int janet_compare_abstract(JanetAbstract xx, JanetAbstract yy) {
|
|
|
|
if (xx == yy) return 0;
|
|
|
|
const JanetAbstractType *xt = janet_abstract_type(xx);
|
|
|
|
const JanetAbstractType *yt = janet_abstract_type(yy);
|
|
|
|
if (xt != yt) {
|
|
|
|
return xt > yt ? 1 : -1;
|
|
|
|
}
|
|
|
|
if (xt->compare == NULL) {
|
|
|
|
return xx > yy ? 1 : -1;
|
|
|
|
}
|
|
|
|
return xt->compare(xx, yy);
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_equals(Janet x, Janet y) {
|
2020-04-24 21:18:31 +00:00
|
|
|
janet_vm_traversal = janet_vm_traversal_base;
|
|
|
|
do {
|
|
|
|
if (janet_type(x) != janet_type(y)) return 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
switch (janet_type(x)) {
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_NIL:
|
|
|
|
break;
|
2019-03-13 18:50:25 +00:00
|
|
|
case JANET_BOOLEAN:
|
2020-04-24 21:18:31 +00:00
|
|
|
if (janet_unwrap_boolean(x) != janet_unwrap_boolean(y)) return 0;
|
2019-03-13 18:50:25 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_NUMBER:
|
2020-04-24 21:18:31 +00:00
|
|
|
if (janet_unwrap_number(x) != janet_unwrap_number(y)) return 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
case JANET_STRING:
|
2020-04-24 21:18:31 +00:00
|
|
|
if (!janet_string_equal(janet_unwrap_string(x), janet_unwrap_string(y))) return 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2019-12-28 20:57:36 +00:00
|
|
|
case JANET_ABSTRACT:
|
2020-04-24 21:18:31 +00:00
|
|
|
if (janet_compare_abstract(janet_unwrap_abstract(x), janet_unwrap_abstract(y))) return 0;
|
2019-12-28 20:57:36 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
default:
|
2020-04-24 21:18:31 +00:00
|
|
|
if (janet_unwrap_pointer(x) != janet_unwrap_pointer(y)) return 0;
|
|
|
|
break;
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
const Janet *t1 = janet_unwrap_tuple(x);
|
|
|
|
const Janet *t2 = janet_unwrap_tuple(y);
|
|
|
|
if (t1 == t2) break;
|
|
|
|
if (janet_tuple_hash(t1) != janet_tuple_hash(t2)) return 0;
|
|
|
|
if (janet_tuple_length(t1) != janet_tuple_length(t2)) return 0;
|
|
|
|
push_traversal_node(janet_tuple_head(t1), janet_tuple_head(t2));
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2020-04-24 21:18:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *s1 = janet_unwrap_struct(x);
|
|
|
|
const JanetKV *s2 = janet_unwrap_struct(y);
|
|
|
|
if (s1 == s2) break;
|
|
|
|
if (janet_struct_hash(s1) != janet_struct_hash(s2)) return 0;
|
|
|
|
if (janet_struct_length(s1) != janet_struct_length(s2)) return 0;
|
|
|
|
push_traversal_node(janet_struct_head(s1), janet_struct_head(s2));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
2020-04-24 21:18:31 +00:00
|
|
|
} while (traversal_next(&x, &y));
|
|
|
|
return 1;
|
2019-02-20 01:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes a hash value for a function */
|
|
|
|
int32_t janet_hash(Janet x) {
|
|
|
|
int32_t hash = 0;
|
|
|
|
switch (janet_type(x)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = 0;
|
|
|
|
break;
|
2019-03-13 18:50:25 +00:00
|
|
|
case JANET_BOOLEAN:
|
|
|
|
hash = janet_unwrap_boolean(x);
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_KEYWORD:
|
|
|
|
hash = janet_string_hash(janet_unwrap_string(x));
|
2017-11-27 19:03:34 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TUPLE:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = janet_tuple_hash(janet_unwrap_tuple(x));
|
2018-01-04 02:36:10 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRUCT:
|
2019-02-20 01:51:34 +00:00
|
|
|
hash = janet_struct_hash(janet_unwrap_struct(x));
|
2017-11-27 19:03:34 +00:00
|
|
|
break;
|
2019-12-28 20:57:36 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstract xx = janet_unwrap_abstract(x);
|
|
|
|
const JanetAbstractType *at = janet_abstract_type(xx);
|
|
|
|
if (at->hash != NULL) {
|
|
|
|
hash = at->hash(xx, janet_abstract_size(xx));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
2017-04-15 20:05:59 +00:00
|
|
|
default:
|
2019-02-20 01:51:34 +00:00
|
|
|
/* TODO - test performance with different hash functions */
|
|
|
|
if (sizeof(double) == sizeof(void *)) {
|
|
|
|
/* Assuming 8 byte pointer */
|
|
|
|
uint64_t i = janet_u64(x);
|
|
|
|
hash = (int32_t)(i & 0xFFFFFFFF);
|
|
|
|
/* Get a bit more entropy by shifting the low bits out */
|
|
|
|
hash >>= 3;
|
|
|
|
hash ^= (int32_t)(i >> 32);
|
|
|
|
} else {
|
|
|
|
/* Assuming 4 byte pointer (or smaller) */
|
|
|
|
hash = (int32_t)((char *)janet_unwrap_pointer(x) - (char *)0);
|
|
|
|
hash >>= 2;
|
|
|
|
}
|
2017-04-15 20:05:59 +00:00
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Compares x to y. If they are equal returns 0. If x is less, returns -1.
|
2017-02-09 20:02:59 +00:00
|
|
|
* If y is less, returns 1. All types are comparable
|
2019-12-28 20:57:36 +00:00
|
|
|
* and should have strict ordering, excepts NaNs. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_compare(Janet x, Janet y) {
|
2020-04-24 21:18:31 +00:00
|
|
|
janet_vm_traversal = janet_vm_traversal_base;
|
|
|
|
do {
|
|
|
|
JanetType tx = janet_type(x);
|
|
|
|
JanetType ty = janet_type(y);
|
|
|
|
if (tx != ty) return tx < ty ? -1 : 1;
|
|
|
|
switch (tx) {
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2020-04-24 21:18:31 +00:00
|
|
|
break;
|
|
|
|
case JANET_BOOLEAN: {
|
|
|
|
int diff = janet_unwrap_boolean(x) - janet_unwrap_boolean(y);
|
|
|
|
if (diff) return diff;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-28 20:57:36 +00:00
|
|
|
case JANET_NUMBER: {
|
|
|
|
double xx = janet_unwrap_number(x);
|
|
|
|
double yy = janet_unwrap_number(y);
|
2020-04-24 21:18:31 +00:00
|
|
|
if (xx == yy) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return (xx < yy) ? -1 : 1;
|
|
|
|
}
|
2019-12-28 20:57:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2020-04-24 21:18:31 +00:00
|
|
|
case JANET_KEYWORD: {
|
|
|
|
int diff = janet_string_compare(janet_unwrap_string(x), janet_unwrap_string(y));
|
|
|
|
if (diff) return diff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
int diff = janet_compare_abstract(janet_unwrap_abstract(x), janet_unwrap_abstract(y));
|
|
|
|
if (diff) return diff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (janet_unwrap_pointer(x) == janet_unwrap_pointer(y)) {
|
|
|
|
break;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2020-04-24 21:18:31 +00:00
|
|
|
return janet_unwrap_pointer(x) > janet_unwrap_pointer(y) ? 1 : -1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2020-04-24 21:18:31 +00:00
|
|
|
}
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
const Janet *lhs = janet_unwrap_tuple(x);
|
|
|
|
const Janet *rhs = janet_unwrap_tuple(y);
|
|
|
|
int32_t llen = janet_tuple_length(lhs);
|
|
|
|
int32_t rlen = janet_tuple_length(rhs);
|
|
|
|
if (llen < rlen) return -1;
|
|
|
|
if (llen > rlen) return 1;
|
|
|
|
push_traversal_node(janet_tuple_head(lhs), janet_tuple_head(rhs));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *lhs = janet_unwrap_struct(x);
|
|
|
|
const JanetKV *rhs = janet_unwrap_struct(y);
|
|
|
|
int32_t llen = janet_struct_capacity(lhs);
|
|
|
|
int32_t rlen = janet_struct_capacity(rhs);
|
|
|
|
int32_t lhash = janet_struct_hash(lhs);
|
|
|
|
int32_t rhash = janet_struct_hash(rhs);
|
|
|
|
if (llen < rlen) return -1;
|
|
|
|
if (llen > rlen) return 1;
|
|
|
|
if (lhash < rhash) return -1;
|
|
|
|
if (lhash > rhash) return 1;
|
|
|
|
push_traversal_node(janet_struct_head(lhs), janet_struct_head(rhs));
|
|
|
|
break;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2020-04-24 21:18:31 +00:00
|
|
|
} while (traversal_next(&x, &y));
|
|
|
|
return 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
|
2019-11-08 23:35:27 +00:00
|
|
|
static int32_t getter_checkint(Janet key, int32_t max) {
|
|
|
|
if (!janet_checkint(key)) goto bad;
|
|
|
|
int32_t ret = janet_unwrap_integer(key);
|
|
|
|
if (ret < 0) goto bad;
|
|
|
|
if (ret >= max) goto bad;
|
|
|
|
return ret;
|
|
|
|
bad:
|
|
|
|
janet_panicf("expected integer key in range [0, %d), got %v", max, key);
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
/* Gets a value and returns. Can panic. */
|
2019-12-03 03:15:08 +00:00
|
|
|
Janet janet_in(Janet ds, Janet key) {
|
2018-12-27 18:05:29 +00:00
|
|
|
Janet value;
|
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-06 16:56:40 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
break;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRUCT:
|
|
|
|
value = janet_struct_get(janet_unwrap_struct(ds), key);
|
|
|
|
break;
|
|
|
|
case JANET_TABLE:
|
|
|
|
value = janet_table_get(janet_unwrap_table(ds), key);
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ARRAY: {
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t index = getter_checkint(key, array->count);
|
|
|
|
value = array->data[index];
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_TUPLE: {
|
|
|
|
const Janet *tuple = janet_unwrap_tuple(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t len = janet_tuple_length(tuple);
|
|
|
|
value = tuple[getter_checkint(key, len)];
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t index = getter_checkint(key, buffer->count);
|
|
|
|
value = janet_wrap_integer(buffer->data[index]);
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_KEYWORD: {
|
|
|
|
const uint8_t *str = janet_unwrap_string(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t index = getter_checkint(key, janet_string_length(str));
|
|
|
|
value = janet_wrap_integer(str[index]);
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->get) {
|
2019-12-09 03:50:33 +00:00
|
|
|
if (!(type->get)(janet_unwrap_abstract(ds), key, &value))
|
|
|
|
janet_panicf("key %v not found in %v ", key, ds);
|
2019-02-20 01:51:34 +00:00
|
|
|
} else {
|
2019-03-27 01:32:51 +00:00
|
|
|
janet_panicf("no getter for %v ", ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-01-05 05:33:20 +00:00
|
|
|
return value;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:15:08 +00:00
|
|
|
Janet janet_get(Janet ds, Janet key) {
|
2019-12-02 02:49:51 +00:00
|
|
|
JanetType t = janet_type(ds);
|
|
|
|
switch (t) {
|
|
|
|
default:
|
|
|
|
return janet_wrap_nil();
|
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_KEYWORD: {
|
|
|
|
if (!janet_checkint(key)) return janet_wrap_nil();
|
|
|
|
int32_t index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0) return janet_wrap_nil();
|
|
|
|
const uint8_t *str = janet_unwrap_string(ds);
|
|
|
|
if (index >= janet_string_length(str)) return janet_wrap_nil();
|
|
|
|
return janet_wrap_integer(str[index]);
|
|
|
|
}
|
|
|
|
case JANET_ABSTRACT: {
|
2019-12-09 03:50:33 +00:00
|
|
|
Janet value;
|
2019-12-02 02:49:51 +00:00
|
|
|
void *abst = janet_unwrap_abstract(ds);
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(abst);
|
|
|
|
if (!type->get) return janet_wrap_nil();
|
2019-12-09 03:50:33 +00:00
|
|
|
if ((type->get)(abst, key, &value))
|
|
|
|
return value;
|
|
|
|
return janet_wrap_nil();
|
2019-12-02 02:49:51 +00:00
|
|
|
}
|
|
|
|
case JANET_ARRAY:
|
2019-12-04 00:58:21 +00:00
|
|
|
case JANET_TUPLE:
|
|
|
|
case JANET_BUFFER: {
|
2019-12-02 02:49:51 +00:00
|
|
|
if (!janet_checkint(key)) return janet_wrap_nil();
|
|
|
|
int32_t index = janet_unwrap_integer(key);
|
|
|
|
if (index < 0) return janet_wrap_nil();
|
|
|
|
if (t == JANET_ARRAY) {
|
|
|
|
JanetArray *a = janet_unwrap_array(ds);
|
|
|
|
if (index >= a->count) return janet_wrap_nil();
|
|
|
|
return a->data[index];
|
2019-12-04 00:58:21 +00:00
|
|
|
} else if (t == JANET_BUFFER) {
|
|
|
|
JanetBuffer *b = janet_unwrap_buffer(ds);
|
|
|
|
if (index >= b->count) return janet_wrap_nil();
|
|
|
|
return janet_wrap_integer(b->data[index]);
|
2019-12-02 02:49:51 +00:00
|
|
|
} else {
|
|
|
|
const Janet *t = janet_unwrap_tuple(ds);
|
|
|
|
if (index >= janet_tuple_length(t)) return janet_wrap_nil();
|
|
|
|
return t[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case JANET_TABLE: {
|
|
|
|
return janet_table_get(janet_unwrap_table(ds), key);
|
|
|
|
}
|
|
|
|
case JANET_STRUCT: {
|
|
|
|
const JanetKV *st = janet_unwrap_struct(ds);
|
|
|
|
return janet_struct_get(st, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
Janet janet_getindex(Janet ds, int32_t index) {
|
2018-12-27 18:05:29 +00:00
|
|
|
Janet value;
|
2019-01-05 05:33:20 +00:00
|
|
|
if (index < 0) janet_panic("expected non-negative index");
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-06 16:56:40 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, ds);
|
2019-01-05 05:33:20 +00:00
|
|
|
break;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2018-12-27 18:05:29 +00:00
|
|
|
if (index >= janet_string_length(janet_unwrap_string(ds))) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(janet_unwrap_string(ds)[index]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_ARRAY:
|
|
|
|
if (index >= janet_unwrap_array(ds)->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_unwrap_array(ds)->data[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_BUFFER:
|
|
|
|
if (index >= janet_unwrap_buffer(ds)->count) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_wrap_integer(janet_unwrap_buffer(ds)->data[index]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_TUPLE:
|
|
|
|
if (index >= janet_tuple_length(janet_unwrap_tuple(ds))) {
|
|
|
|
value = janet_wrap_nil();
|
|
|
|
} else {
|
|
|
|
value = janet_unwrap_tuple(ds)[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JANET_TABLE:
|
|
|
|
value = janet_table_get(janet_unwrap_table(ds), janet_wrap_integer(index));
|
|
|
|
break;
|
|
|
|
case JANET_STRUCT:
|
|
|
|
value = janet_struct_get(janet_unwrap_struct(ds), janet_wrap_integer(index));
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
JanetAbstractType *type = (JanetAbstractType *)janet_abstract_type(janet_unwrap_abstract(ds));
|
|
|
|
if (type->get) {
|
2019-12-09 03:50:33 +00:00
|
|
|
if (!(type->get)(janet_unwrap_abstract(ds), janet_wrap_integer(index), &value))
|
|
|
|
value = janet_wrap_nil();
|
2019-02-20 01:51:34 +00:00
|
|
|
} else {
|
2019-03-27 01:32:51 +00:00
|
|
|
janet_panicf("no getter for %v ", ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-01-05 05:33:20 +00:00
|
|
|
return value;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
int32_t janet_length(Janet x) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(x)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, x);
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
2019-01-03 00:41:07 +00:00
|
|
|
case JANET_KEYWORD:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_string_length(janet_unwrap_string(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_ARRAY:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_array(x)->count;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_BUFFER:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_buffer(x)->count;
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TUPLE:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_tuple_length(janet_unwrap_tuple(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_STRUCT:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_struct_length(janet_unwrap_struct(x));
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TABLE:
|
2019-01-05 05:33:20 +00:00
|
|
|
return janet_unwrap_table(x)->count;
|
2019-09-08 20:36:21 +00:00
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
Janet argv[1] = { x };
|
|
|
|
Janet len = janet_mcall("length", 1, argv);
|
|
|
|
if (!janet_checkint(len))
|
|
|
|
janet_panicf("invalid integer length %v", len);
|
|
|
|
return janet_unwrap_integer(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Janet janet_lengthv(Janet x) {
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
default:
|
|
|
|
janet_panicf("expected %T, got %v", JANET_TFLAG_LENGTHABLE, x);
|
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_KEYWORD:
|
|
|
|
return janet_wrap_integer(janet_string_length(janet_unwrap_string(x)));
|
|
|
|
case JANET_ARRAY:
|
|
|
|
return janet_wrap_integer(janet_unwrap_array(x)->count);
|
|
|
|
case JANET_BUFFER:
|
|
|
|
return janet_wrap_integer(janet_unwrap_buffer(x)->count);
|
|
|
|
case JANET_TUPLE:
|
|
|
|
return janet_wrap_integer(janet_tuple_length(janet_unwrap_tuple(x)));
|
|
|
|
case JANET_STRUCT:
|
|
|
|
return janet_wrap_integer(janet_struct_length(janet_unwrap_struct(x)));
|
|
|
|
case JANET_TABLE:
|
|
|
|
return janet_wrap_integer(janet_unwrap_table(x)->count);
|
|
|
|
case JANET_ABSTRACT: {
|
|
|
|
Janet argv[1] = { x };
|
|
|
|
return janet_mcall("length", 1, argv);
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
void janet_putindex(Janet ds, int32_t index, Janet value) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v",
|
2019-02-20 01:51:34 +00:00
|
|
|
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
|
|
|
case JANET_ARRAY: {
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
|
|
|
if (index >= array->count) {
|
|
|
|
janet_array_ensure(array, index + 1, 2);
|
|
|
|
array->count = index + 1;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
array->data[index] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
|
|
|
if (!janet_checkint(value))
|
|
|
|
janet_panicf("can only put integers in buffers, got %v", value);
|
|
|
|
if (index >= buffer->count) {
|
|
|
|
janet_buffer_ensure(buffer, index + 1, 2);
|
|
|
|
buffer->count = index + 1;
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-11-08 23:35:27 +00:00
|
|
|
buffer->data[index] = (uint8_t)(janet_unwrap_integer(value) & 0xFF);
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_TABLE: {
|
|
|
|
JanetTable *table = janet_unwrap_table(ds);
|
|
|
|
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 {
|
2019-03-27 01:32:51 +00:00
|
|
|
janet_panicf("no setter for %v ", ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 05:33:20 +00:00
|
|
|
void janet_put(Janet ds, Janet key, Janet value) {
|
2018-12-27 18:05:29 +00:00
|
|
|
switch (janet_type(ds)) {
|
|
|
|
default:
|
2019-01-05 05:33:20 +00:00
|
|
|
janet_panicf("expected %T, got %v",
|
2019-02-20 01:51:34 +00:00
|
|
|
JANET_TFLAG_ARRAY | JANET_TFLAG_BUFFER | JANET_TFLAG_TABLE, ds);
|
|
|
|
case JANET_ARRAY: {
|
|
|
|
JanetArray *array = janet_unwrap_array(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
2019-02-20 01:51:34 +00:00
|
|
|
if (index >= array->count) {
|
|
|
|
janet_array_setcount(array, index + 1);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
array->data[index] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(ds);
|
2019-11-08 23:35:27 +00:00
|
|
|
int32_t index = getter_checkint(key, INT32_MAX - 1);
|
2019-02-20 01:51:34 +00:00
|
|
|
if (!janet_checkint(value))
|
|
|
|
janet_panicf("can only put integers in buffers, got %v", value);
|
|
|
|
if (index >= buffer->count) {
|
|
|
|
janet_buffer_setcount(buffer, index + 1);
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
buffer->data[index] = (uint8_t)(janet_unwrap_integer(value) & 0xFF);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
case JANET_TABLE:
|
|
|
|
janet_table_put(janet_unwrap_table(ds), key, value);
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
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 {
|
2019-03-27 01:32:51 +00:00
|
|
|
janet_panicf("no setter for %v ", ds);
|
2019-02-19 23:41:21 +00:00
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-12-27 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|