2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 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.
|
|
|
|
*/
|
|
|
|
|
2018-12-30 19:23:52 +00:00
|
|
|
#include <inttypes.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
|
|
|
|
#ifndef JANET_AMALG
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2018-06-12 18:24:45 +00:00
|
|
|
#include "state.h"
|
2018-01-19 21:43:19 +00:00
|
|
|
#include "gc.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-04-26 14:21:03 +00:00
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Base 64 lookup table for digits */
|
2018-09-06 02:18:42 +00:00
|
|
|
const char janet_base64[65] =
|
2017-11-27 19:03:34 +00:00
|
|
|
"0123456789"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"_=";
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* The JANET value types in order. These types can be used as
|
2017-11-06 03:05:47 +00:00
|
|
|
* mnemonics instead of a bit pattern for type checking */
|
2019-01-03 00:41:07 +00:00
|
|
|
const char *const janet_type_names[16] = {
|
|
|
|
"number",
|
|
|
|
"nil",
|
|
|
|
"boolean",
|
|
|
|
"boolean",
|
|
|
|
"fiber",
|
|
|
|
"string",
|
|
|
|
"symbol",
|
|
|
|
"keyword",
|
|
|
|
"array",
|
|
|
|
"tuple",
|
|
|
|
"table",
|
|
|
|
"struct",
|
|
|
|
"buffer",
|
|
|
|
"function",
|
|
|
|
"cfunction",
|
|
|
|
"abstract"
|
2017-11-06 03:05:47 +00:00
|
|
|
};
|
2017-07-03 00:51:52 +00:00
|
|
|
|
2018-12-13 23:46:53 +00:00
|
|
|
const char *const janet_signal_names[14] = {
|
2019-01-03 00:41:07 +00:00
|
|
|
"ok",
|
|
|
|
"error",
|
|
|
|
"debug",
|
|
|
|
"yield",
|
|
|
|
"user0",
|
|
|
|
"user1",
|
|
|
|
"user2",
|
|
|
|
"user3",
|
|
|
|
"user4",
|
|
|
|
"user5",
|
|
|
|
"user6",
|
|
|
|
"user7",
|
|
|
|
"user8",
|
|
|
|
"user9"
|
2018-12-13 23:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *const janet_status_names[16] = {
|
2019-01-03 00:41:07 +00:00
|
|
|
"dead",
|
|
|
|
"error",
|
|
|
|
"debug",
|
|
|
|
"pending",
|
|
|
|
"user0",
|
|
|
|
"user1",
|
|
|
|
"user2",
|
|
|
|
"user3",
|
|
|
|
"user4",
|
|
|
|
"user5",
|
|
|
|
"user6",
|
|
|
|
"user7",
|
|
|
|
"user8",
|
|
|
|
"user9",
|
|
|
|
"new",
|
|
|
|
"alive"
|
2018-12-13 23:46:53 +00:00
|
|
|
};
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Calculate hash for string */
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janet_string_calchash(const uint8_t *str, int32_t len) {
|
2018-01-19 21:43:19 +00:00
|
|
|
const uint8_t *end = str + len;
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (str < end)
|
|
|
|
hash = (hash << 5) + hash + *str++;
|
|
|
|
return (int32_t) hash;
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:03:34 +00:00
|
|
|
/* Computes hash of an array of values */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janet_array_calchash(const Janet *array, int32_t len) {
|
|
|
|
const Janet *end = array + len;
|
2017-11-27 19:03:34 +00:00
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (array < end)
|
2018-09-06 02:18:42 +00:00
|
|
|
hash = (hash << 5) + hash + janet_hash(*array++);
|
2017-11-28 23:27:55 +00:00
|
|
|
return (int32_t) hash;
|
2017-11-27 19:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Computes hash of an array of values */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len) {
|
|
|
|
const JanetKV *end = kvs + len;
|
2018-01-05 21:17:55 +00:00
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (kvs < end) {
|
2018-09-06 02:18:42 +00:00
|
|
|
hash = (hash << 5) + hash + janet_hash(kvs->key);
|
|
|
|
hash = (hash << 5) + hash + janet_hash(kvs->value);
|
2018-01-05 21:17:55 +00:00
|
|
|
kvs++;
|
|
|
|
}
|
|
|
|
return (int32_t) hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate next power of 2. May overflow. If n is 0,
|
|
|
|
* will return 0. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janet_tablen(int32_t n) {
|
2018-01-05 21:17:55 +00:00
|
|
|
n |= n >> 1;
|
|
|
|
n |= n >> 2;
|
|
|
|
n |= n >> 4;
|
|
|
|
n |= n >> 8;
|
|
|
|
n |= n >> 16;
|
|
|
|
return n + 1;
|
|
|
|
}
|
|
|
|
|
2018-09-09 16:13:32 +00:00
|
|
|
/* Helper to find a value in a Janet struct or table. Returns the bucket
|
2019-01-06 08:23:03 +00:00
|
|
|
* containing the key, or the first empty bucket if there is no such key. */
|
2018-09-09 16:13:32 +00:00
|
|
|
const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key) {
|
|
|
|
int32_t index = janet_maphash(cap, janet_hash(key));
|
|
|
|
int32_t i;
|
|
|
|
const JanetKV *first_bucket = NULL;
|
|
|
|
/* Higher half */
|
|
|
|
for (i = index; i < cap; i++) {
|
|
|
|
const JanetKV *kv = buckets + i;
|
|
|
|
if (janet_checktype(kv->key, JANET_NIL)) {
|
|
|
|
if (janet_checktype(kv->value, JANET_NIL)) {
|
|
|
|
return kv;
|
|
|
|
} else if (NULL == first_bucket) {
|
|
|
|
first_bucket = kv;
|
|
|
|
}
|
|
|
|
} else if (janet_equals(kv->key, key)) {
|
|
|
|
return buckets + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Lower half */
|
|
|
|
for (i = 0; i < index; i++) {
|
|
|
|
const JanetKV *kv = buckets + i;
|
|
|
|
if (janet_checktype(kv->key, JANET_NIL)) {
|
|
|
|
if (janet_checktype(kv->value, JANET_NIL)) {
|
|
|
|
return kv;
|
|
|
|
} else if (NULL == first_bucket) {
|
|
|
|
first_bucket = kv;
|
|
|
|
}
|
|
|
|
} else if (janet_equals(kv->key, key)) {
|
|
|
|
return buckets + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return first_bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value from a janet struct or table. */
|
|
|
|
Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key) {
|
|
|
|
const JanetKV *kv = janet_dict_find(data, cap, key);
|
|
|
|
if (kv && !janet_checktype(kv->key, JANET_NIL)) {
|
|
|
|
return kv->value;
|
|
|
|
}
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:20:33 +00:00
|
|
|
/* Iterate through a struct or dictionary generically */
|
|
|
|
const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv) {
|
|
|
|
const JanetKV *end = kvs + cap;
|
|
|
|
kv = (kv == NULL) ? kvs : kv + 1;
|
|
|
|
while (kv < end) {
|
|
|
|
if (!janet_checktype(kv->key, JANET_NIL))
|
|
|
|
return kv;
|
|
|
|
kv++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Compare a janet string with a cstring. More efficient than loading
|
2018-09-06 02:18:42 +00:00
|
|
|
* c string as a janet string. */
|
|
|
|
int janet_cstrcmp(const uint8_t *str, const char *other) {
|
|
|
|
int32_t len = janet_string_length(str);
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t index;
|
|
|
|
for (index = 0; index < len; index++) {
|
|
|
|
uint8_t c = str[index];
|
|
|
|
uint8_t k = ((const uint8_t *)other)[index];
|
|
|
|
if (c < k) return -1;
|
|
|
|
if (c > k) return 1;
|
|
|
|
if (k == '\0') break;
|
|
|
|
}
|
|
|
|
return (other[index] == '\0') ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Do a binary search on a static array of structs. Each struct must
|
|
|
|
* have a string as its first element, and the struct must be sorted
|
2019-01-06 08:23:03 +00:00
|
|
|
* lexicographically by that element. */
|
2018-09-06 02:18:42 +00:00
|
|
|
const void *janet_strbinsearch(
|
2019-02-20 01:51:34 +00:00
|
|
|
const void *tab,
|
|
|
|
size_t tabcount,
|
|
|
|
size_t itemsize,
|
|
|
|
const uint8_t *key) {
|
2018-07-04 03:07:35 +00:00
|
|
|
size_t low = 0;
|
|
|
|
size_t hi = tabcount;
|
|
|
|
const char *t = (const char *)tab;
|
|
|
|
while (low < hi) {
|
|
|
|
size_t mid = low + ((hi - low) / 2);
|
|
|
|
const char **item = (const char **)(t + mid * itemsize);
|
|
|
|
const char *name = *item;
|
2018-09-06 02:18:42 +00:00
|
|
|
int comp = janet_cstrcmp(key, name);
|
2018-07-04 03:07:35 +00:00
|
|
|
if (comp < 0) {
|
|
|
|
hi = mid;
|
|
|
|
} else if (comp > 0) {
|
|
|
|
low = mid + 1;
|
|
|
|
} else {
|
|
|
|
return (const void *)item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Register a value in the global registry */
|
2018-10-21 05:35:07 +00:00
|
|
|
void janet_register(const char *name, JanetCFunction cfun) {
|
2018-11-15 20:45:41 +00:00
|
|
|
Janet key = janet_wrap_cfunction(cfun);
|
|
|
|
Janet value = janet_csymbolv(name);
|
|
|
|
janet_table_put(janet_vm_registry, key, value);
|
2018-08-23 01:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Add a def to an environment */
|
2018-11-15 20:45:41 +00:00
|
|
|
void janet_def(JanetTable *env, const char *name, Janet val, const char *doc) {
|
|
|
|
JanetTable *subt = janet_table(2);
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(subt, janet_ckeywordv("value"), val);
|
2018-11-15 20:45:41 +00:00
|
|
|
if (doc)
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(subt, janet_ckeywordv("doc"), janet_cstringv(doc));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Add a var to the environment */
|
2018-11-15 20:45:41 +00:00
|
|
|
void janet_var(JanetTable *env, const char *name, Janet val, const char *doc) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array = janet_array(1);
|
2018-11-15 20:45:41 +00:00
|
|
|
JanetTable *subt = janet_table(2);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_push(array, val);
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(subt, janet_ckeywordv("ref"), janet_wrap_array(array));
|
2018-11-15 20:45:41 +00:00
|
|
|
if (doc)
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(subt, janet_ckeywordv("doc"), janet_cstringv(doc));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Load many cfunctions at once */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) {
|
2018-01-19 21:43:19 +00:00
|
|
|
while (cfuns->name) {
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet name = janet_csymbolv(cfuns->name);
|
|
|
|
Janet longname = name;
|
2018-08-26 18:35:01 +00:00
|
|
|
if (regprefix) {
|
|
|
|
int32_t reglen = 0;
|
|
|
|
int32_t nmlen = 0;
|
|
|
|
while (regprefix[reglen]) reglen++;
|
|
|
|
while (cfuns->name[nmlen]) nmlen++;
|
|
|
|
uint8_t *longname_buffer =
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_string_begin(reglen + 1 + nmlen);
|
2018-08-26 18:35:01 +00:00
|
|
|
memcpy(longname_buffer, regprefix, reglen);
|
2019-01-03 00:41:07 +00:00
|
|
|
longname_buffer[reglen] = '/';
|
2018-08-26 18:35:01 +00:00
|
|
|
memcpy(longname_buffer + reglen + 1, cfuns->name, nmlen);
|
2018-09-06 02:18:42 +00:00
|
|
|
longname = janet_wrap_symbol(janet_string_end(longname_buffer));
|
2018-08-26 18:35:01 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2018-11-15 20:45:41 +00:00
|
|
|
janet_def(env, cfuns->name, fun, cfuns->documentation);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(janet_vm_registry, fun, longname);
|
2018-01-19 21:43:19 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 09:54:22 +00:00
|
|
|
static const JanetAbstractType type_info = {"core/type_info", NULL, NULL, NULL, NULL};
|
|
|
|
|
2019-02-22 14:57:48 +00:00
|
|
|
/*
|
2019-02-22 09:54:22 +00:00
|
|
|
void janet_register_abstract_type(const JanetAbstractType *atype,uint32_t tag) {
|
|
|
|
JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo));
|
|
|
|
abstract->type=*atype;
|
|
|
|
abstract->tag=tag;
|
|
|
|
if (!(janet_checktype(janet_table_get(janet_vm_registry,janet_wrap_number(tag)),JANET_NIL)) ||
|
|
|
|
!(janet_checktype(janet_table_get(janet_vm_registry,janet_ckeywordv(atype->name)),JANET_NIL))) {
|
|
|
|
janet_panic("Register abstract type fail, a type with same name or tag exist");
|
|
|
|
}
|
|
|
|
janet_table_put(janet_vm_registry,janet_wrap_number(tag), janet_wrap_abstract(abstract));
|
|
|
|
janet_table_put(janet_vm_registry,janet_ckeywordv(atype->name), janet_wrap_abstract(abstract));
|
|
|
|
}
|
2019-02-22 14:57:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void janet_register_abstract_type(const JanetAbstractTypeInfo * info) {
|
|
|
|
JanetAbstractTypeInfo * abstract =(JanetAbstractTypeInfo *)janet_abstract(&type_info,sizeof(JanetAbstractTypeInfo));
|
|
|
|
memcpy(abstract,info,sizeof(JanetAbstractTypeInfo));
|
|
|
|
if (!(janet_checktype(janet_table_get(janet_vm_registry,janet_wrap_number(info->tag)),JANET_NIL)) ||
|
2019-02-22 16:41:27 +00:00
|
|
|
!(janet_checktype(janet_table_get(janet_vm_registry,janet_ckeywordv(info->at->name)),JANET_NIL))) {
|
2019-02-22 14:57:48 +00:00
|
|
|
janet_panic("Register abstract type fail, a type with same name or tag exist");
|
|
|
|
}
|
|
|
|
janet_table_put(janet_vm_registry,janet_wrap_number(info->tag), janet_wrap_abstract(abstract));
|
2019-02-22 16:41:27 +00:00
|
|
|
janet_table_put(janet_vm_registry,janet_ckeywordv(info->at->name), janet_wrap_abstract(abstract));
|
2019-02-22 14:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 09:54:22 +00:00
|
|
|
|
|
|
|
JanetAbstractTypeInfo * janet_get_abstract_type_info(uint32_t tag) {
|
|
|
|
Janet info=janet_table_get(janet_vm_registry,janet_wrap_number(tag));
|
|
|
|
if (janet_checktype(info, JANET_NIL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
|
|
|
|
janet_panic("expected type_info");
|
|
|
|
}
|
|
|
|
JanetAbstractTypeInfo * type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
|
|
|
|
return type_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
JanetAbstractTypeInfo * janet_get_abstract_type_info_byname(const char * name) {
|
|
|
|
Janet info=janet_table_get(janet_vm_registry,janet_ckeywordv(name));
|
|
|
|
if (janet_checktype(info, JANET_NIL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!janet_checktype(info, JANET_ABSTRACT) || (janet_abstract_type(janet_unwrap_abstract(info)) != &type_info)) {
|
|
|
|
janet_panic("expected type_info");
|
|
|
|
}
|
|
|
|
JanetAbstractTypeInfo * type_info = (JanetAbstractTypeInfo *)janet_unwrap_abstract(info);
|
|
|
|
return type_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-08 05:44:30 +00:00
|
|
|
#ifndef JANET_BOOTSTRAP
|
|
|
|
void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p) {
|
|
|
|
(void) p;
|
|
|
|
janet_table_put(env, janet_csymbolv(name), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) {
|
|
|
|
(void) regprefix;
|
|
|
|
while (cfuns->name) {
|
|
|
|
Janet name = janet_csymbolv(cfuns->name);
|
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
|
|
|
janet_core_def(env, cfuns->name, fun, cfuns->documentation);
|
|
|
|
janet_table_put(janet_vm_registry, fun, name);
|
|
|
|
cfuns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-17 17:55:02 +00:00
|
|
|
/* Resolve a symbol in the environment */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out) {
|
|
|
|
Janet ref;
|
|
|
|
JanetTable *entry_table;
|
|
|
|
Janet entry = janet_table_get(env, janet_wrap_symbol(sym));
|
|
|
|
if (!janet_checktype(entry, JANET_TABLE))
|
|
|
|
return JANET_BINDING_NONE;
|
|
|
|
entry_table = janet_unwrap_table(entry);
|
|
|
|
if (!janet_checktype(
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_table_get(entry_table, janet_ckeywordv("macro")),
|
|
|
|
JANET_NIL)) {
|
2019-01-03 00:41:07 +00:00
|
|
|
*out = janet_table_get(entry_table, janet_ckeywordv("value"));
|
2018-09-06 02:18:42 +00:00
|
|
|
return JANET_BINDING_MACRO;
|
2018-06-17 17:55:02 +00:00
|
|
|
}
|
2019-01-03 00:41:07 +00:00
|
|
|
ref = janet_table_get(entry_table, janet_ckeywordv("ref"));
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(ref, JANET_ARRAY)) {
|
2018-06-17 17:55:02 +00:00
|
|
|
*out = ref;
|
2018-09-06 02:18:42 +00:00
|
|
|
return JANET_BINDING_VAR;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2019-01-03 00:41:07 +00:00
|
|
|
*out = janet_table_get(entry_table, janet_ckeywordv("value"));
|
2018-09-06 02:18:42 +00:00
|
|
|
return JANET_BINDING_DEF;
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
/* Read both tuples and arrays as c pointers + int32_t length. Return 1 if the
|
2017-04-24 17:12:55 +00:00
|
|
|
* view can be constructed, 0 if an invalid type. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_indexed_view(Janet seq, const Janet **data, int32_t *len) {
|
|
|
|
if (janet_checktype(seq, JANET_ARRAY)) {
|
|
|
|
*data = janet_unwrap_array(seq)->data;
|
|
|
|
*len = janet_unwrap_array(seq)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(seq, JANET_TUPLE)) {
|
|
|
|
*data = janet_unwrap_tuple(seq);
|
2019-02-08 00:03:21 +00:00
|
|
|
*len = janet_tuple_length(janet_unwrap_tuple(seq));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2017-07-02 01:51:16 +00:00
|
|
|
}
|
2017-04-24 17:12:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
/* Read both strings and buffer as unsigned character array + int32_t len.
|
2017-04-24 17:12:55 +00:00
|
|
|
* Returns 1 if the view can be constructed and 0 if the type is invalid. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len) {
|
2019-01-03 00:41:07 +00:00
|
|
|
if (janet_checktype(str, JANET_STRING) || janet_checktype(str, JANET_SYMBOL) ||
|
|
|
|
janet_checktype(str, JANET_KEYWORD)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
*data = janet_unwrap_string(str);
|
|
|
|
*len = janet_string_length(janet_unwrap_string(str));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(str, JANET_BUFFER)) {
|
|
|
|
*data = janet_unwrap_buffer(str)->data;
|
|
|
|
*len = janet_unwrap_buffer(str)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
/* Read both structs and tables as the entries of a hashtable with
|
2017-04-24 17:12:55 +00:00
|
|
|
* identical structure. Returns 1 if the view can be constructed and
|
|
|
|
* 0 if the type is invalid. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_dictionary_view(Janet tab, const JanetKV **data, int32_t *len, int32_t *cap) {
|
|
|
|
if (janet_checktype(tab, JANET_TABLE)) {
|
|
|
|
*data = janet_unwrap_table(tab)->data;
|
|
|
|
*cap = janet_unwrap_table(tab)->capacity;
|
|
|
|
*len = janet_unwrap_table(tab)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
} else if (janet_checktype(tab, JANET_STRUCT)) {
|
|
|
|
*data = janet_unwrap_struct(tab);
|
|
|
|
*cap = janet_struct_capacity(janet_unwrap_struct(tab));
|
|
|
|
*len = janet_struct_length(janet_unwrap_struct(tab));
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-28 22:10:57 +00:00
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
int janet_checkint(Janet x) {
|
|
|
|
if (!janet_checktype(x, JANET_NUMBER))
|
|
|
|
return 0;
|
|
|
|
double dval = janet_unwrap_number(x);
|
|
|
|
return janet_checkintrange(dval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int janet_checkint64(Janet x) {
|
|
|
|
if (!janet_checktype(x, JANET_NUMBER))
|
|
|
|
return 0;
|
|
|
|
double dval = janet_unwrap_number(x);
|
|
|
|
return janet_checkint64range(dval);
|
|
|
|
}
|
2018-12-30 19:23:52 +00:00
|
|
|
|
|
|
|
/* Useful for inspecting values while debugging */
|
|
|
|
void janet_inspect(Janet x) {
|
|
|
|
printf("<type=%s, ", janet_type_names[janet_type(x)]);
|
|
|
|
|
|
|
|
#ifdef JANET_BIG_ENDIAN
|
|
|
|
printf("be ");
|
|
|
|
#else
|
|
|
|
printf("le ");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_NANBOX_64
|
|
|
|
printf("nanbox64 raw=0x%.16" PRIx64 ", ", x.u64);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_NANBOX_32
|
|
|
|
printf("nanbox32 type=0x%.8" PRIx32 ", ", x.tagged.type);
|
|
|
|
printf("payload=%" PRId32 ", ", x.tagged.payload.integer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (janet_type(x)) {
|
|
|
|
case JANET_NIL:
|
|
|
|
printf("value=nil");
|
|
|
|
break;
|
|
|
|
case JANET_NUMBER:
|
|
|
|
printf("number=%.17g", janet_unwrap_number(x));
|
|
|
|
break;
|
|
|
|
case JANET_TRUE:
|
|
|
|
printf("value=true");
|
|
|
|
break;
|
|
|
|
case JANET_FALSE:
|
|
|
|
printf("value=false");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("pointer=%p", janet_unwrap_pointer(x));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(">\n");
|
|
|
|
}
|