2017-04-18 02:40:39 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 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"
|
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"
|
2020-07-03 15:13:55 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#include <windows.h>
|
2020-08-04 00:11:05 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2020-07-03 15:13:55 +00:00
|
|
|
#endif
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-04-26 14:21:03 +00:00
|
|
|
|
2022-08-14 18:26:13 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#ifdef JANET_DYNAMIC_MODULES
|
|
|
|
#include <psapi.h>
|
2023-01-21 16:37:34 +00:00
|
|
|
#ifdef JANET_MSVC
|
2022-08-14 18:26:13 +00:00
|
|
|
#pragma comment (lib, "Psapi.lib")
|
|
|
|
#endif
|
|
|
|
#endif
|
2023-01-21 16:37:34 +00:00
|
|
|
#endif
|
2022-08-14 18:26:13 +00:00
|
|
|
|
2022-03-22 02:23:09 +00:00
|
|
|
#ifdef JANET_APPLE
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
#endif
|
|
|
|
|
2019-12-31 00:06:15 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
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",
|
|
|
|
"fiber",
|
|
|
|
"string",
|
|
|
|
"symbol",
|
|
|
|
"keyword",
|
|
|
|
"array",
|
|
|
|
"tuple",
|
|
|
|
"table",
|
|
|
|
"struct",
|
|
|
|
"buffer",
|
|
|
|
"function",
|
|
|
|
"cfunction",
|
2019-03-13 18:50:25 +00:00
|
|
|
"abstract",
|
|
|
|
"pointer"
|
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",
|
2023-04-01 23:57:13 +00:00
|
|
|
"interrupt",
|
|
|
|
"await"
|
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",
|
2023-04-01 23:57:13 +00:00
|
|
|
"interrupted",
|
|
|
|
"suspended",
|
2019-01-03 00:41:07 +00:00
|
|
|
"new",
|
|
|
|
"alive"
|
2018-12-13 23:46:53 +00:00
|
|
|
};
|
|
|
|
|
2020-07-25 18:34:40 +00:00
|
|
|
#ifndef JANET_PRF
|
2020-01-17 03:06:03 +00:00
|
|
|
|
|
|
|
int32_t janet_string_calchash(const uint8_t *str, int32_t len) {
|
2023-03-24 23:49:21 +00:00
|
|
|
if (NULL == str) return 5381;
|
2020-01-17 03:06:03 +00:00
|
|
|
const uint8_t *end = str + len;
|
|
|
|
uint32_t hash = 5381;
|
|
|
|
while (str < end)
|
|
|
|
hash = (hash << 5) + hash + *str++;
|
|
|
|
return (int32_t) hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-01-16 22:52:29 +00:00
|
|
|
/*
|
|
|
|
Public domain siphash implementation sourced from:
|
|
|
|
|
|
|
|
https://raw.githubusercontent.com/veorq/SipHash/master/halfsiphash.c
|
|
|
|
|
|
|
|
We have made a few alterations, such as hardcoding the output size
|
|
|
|
and then removing dead code.
|
|
|
|
*/
|
|
|
|
#define cROUNDS 2
|
|
|
|
#define dROUNDS 4
|
|
|
|
|
|
|
|
#define ROTL(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
|
|
|
|
|
|
|
|
#define U8TO32_LE(p) \
|
|
|
|
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
|
|
|
|
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
|
|
|
|
|
|
|
|
#define SIPROUND \
|
|
|
|
do { \
|
|
|
|
v0 += v1; \
|
|
|
|
v1 = ROTL(v1, 5); \
|
|
|
|
v1 ^= v0; \
|
|
|
|
v0 = ROTL(v0, 16); \
|
|
|
|
v2 += v3; \
|
|
|
|
v3 = ROTL(v3, 8); \
|
|
|
|
v3 ^= v2; \
|
|
|
|
v0 += v3; \
|
|
|
|
v3 = ROTL(v3, 7); \
|
|
|
|
v3 ^= v0; \
|
|
|
|
v2 += v1; \
|
|
|
|
v1 = ROTL(v1, 13); \
|
|
|
|
v1 ^= v2; \
|
|
|
|
v2 = ROTL(v2, 16); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static uint32_t halfsiphash(const uint8_t *in, const size_t inlen, const uint8_t *k) {
|
|
|
|
|
|
|
|
uint32_t v0 = 0;
|
|
|
|
uint32_t v1 = 0;
|
|
|
|
uint32_t v2 = UINT32_C(0x6c796765);
|
|
|
|
uint32_t v3 = UINT32_C(0x74656462);
|
|
|
|
uint32_t k0 = U8TO32_LE(k);
|
|
|
|
uint32_t k1 = U8TO32_LE(k + 4);
|
|
|
|
uint32_t m;
|
|
|
|
int i;
|
|
|
|
const uint8_t *end = in + inlen - (inlen % sizeof(uint32_t));
|
|
|
|
const int left = inlen & 3;
|
|
|
|
uint32_t b = ((uint32_t)inlen) << 24;
|
|
|
|
v3 ^= k1;
|
|
|
|
v2 ^= k0;
|
|
|
|
v1 ^= k1;
|
|
|
|
v0 ^= k0;
|
|
|
|
|
|
|
|
for (; in != end; in += 4) {
|
|
|
|
m = U8TO32_LE(in);
|
|
|
|
v3 ^= m;
|
|
|
|
|
|
|
|
for (i = 0; i < cROUNDS; ++i)
|
|
|
|
SIPROUND;
|
|
|
|
|
|
|
|
v0 ^= m;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (left) {
|
|
|
|
case 3:
|
|
|
|
b |= ((uint32_t)in[2]) << 16;
|
2020-01-18 06:17:08 +00:00
|
|
|
/* fallthrough */
|
2020-01-16 22:52:29 +00:00
|
|
|
case 2:
|
|
|
|
b |= ((uint32_t)in[1]) << 8;
|
2020-01-18 06:17:08 +00:00
|
|
|
/* fallthrough */
|
2020-01-16 22:52:29 +00:00
|
|
|
case 1:
|
|
|
|
b |= ((uint32_t)in[0]);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
v3 ^= b;
|
|
|
|
|
|
|
|
for (i = 0; i < cROUNDS; ++i)
|
|
|
|
SIPROUND;
|
|
|
|
|
|
|
|
v0 ^= b;
|
|
|
|
|
|
|
|
v2 ^= 0xff;
|
|
|
|
|
|
|
|
for (i = 0; i < dROUNDS; ++i)
|
|
|
|
SIPROUND;
|
|
|
|
|
|
|
|
b = v1 ^ v3;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
/* end of siphash */
|
|
|
|
|
2020-01-17 04:24:32 +00:00
|
|
|
static uint8_t hash_key[JANET_HASH_KEY_SIZE] = {0};
|
2020-01-16 22:52:29 +00:00
|
|
|
|
2020-01-17 04:24:32 +00:00
|
|
|
void janet_init_hash_key(uint8_t new_key[JANET_HASH_KEY_SIZE]) {
|
2020-01-16 22:52:29 +00:00
|
|
|
memcpy(hash_key, new_key, sizeof(hash_key));
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-01-16 22:52:29 +00:00
|
|
|
uint32_t hash;
|
|
|
|
hash = halfsiphash(str, len, hash_key);
|
|
|
|
return (int32_t)hash;
|
2018-01-19 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 03:06:03 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-05 22:31:18 +00:00
|
|
|
uint32_t janet_hash_mix(uint32_t input, uint32_t more) {
|
2021-12-06 23:23:00 +00:00
|
|
|
uint32_t mix1 = (more + 0x9e3779b9 + (input << 6) + (input >> 2));
|
|
|
|
return input ^ (0x9e3779b9 + (mix1 << 6) + (mix1 >> 2));
|
2021-12-05 22:31:18 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2021-12-05 22:31:18 +00:00
|
|
|
uint32_t hash = 33;
|
2020-12-22 21:51:16 +00:00
|
|
|
while (array < end) {
|
2021-12-05 22:31:18 +00:00
|
|
|
hash = janet_hash_mix(hash, janet_hash(*array++));
|
2020-12-22 21:51:16 +00:00
|
|
|
}
|
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;
|
2021-12-05 22:31:18 +00:00
|
|
|
uint32_t hash = 33;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (kvs < end) {
|
2021-12-05 22:31:18 +00:00
|
|
|
hash = janet_hash_mix(hash, janet_hash(kvs->key));
|
|
|
|
hash = janet_hash_mix(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) {
|
2022-05-05 23:24:53 +00:00
|
|
|
if (n < 0) return 0;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
/* Avoid some undefined behavior that was common in the code base. */
|
|
|
|
void safe_memcpy(void *dest, const void *src, size_t len) {
|
|
|
|
if (!len) return;
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-25 18:03:01 +00:00
|
|
|
/* Add sourcemapping and documentation to a binding table */
|
|
|
|
static void janet_add_meta(JanetTable *table, const char *doc, const char *source_file, int32_t source_line) {
|
|
|
|
if (doc) {
|
|
|
|
janet_table_put(table, janet_ckeywordv("doc"), janet_cstringv(doc));
|
|
|
|
}
|
|
|
|
if (source_file && source_line) {
|
|
|
|
Janet triple[3];
|
|
|
|
triple[0] = janet_cstringv(source_file);
|
|
|
|
triple[1] = janet_wrap_integer(source_line);
|
|
|
|
triple[2] = janet_wrap_integer(1);
|
|
|
|
Janet value = janet_wrap_tuple(janet_tuple_n(triple, 3));
|
|
|
|
janet_table_put(table, janet_ckeywordv("source-map"), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Add a def to an environment */
|
2021-07-25 18:03:01 +00:00
|
|
|
void janet_def_sm(JanetTable *env, const char *name, Janet val, const char *doc, const char *source_file, int32_t source_line) {
|
2018-11-15 20:45:41 +00:00
|
|
|
JanetTable *subt = janet_table(2);
|
2019-01-03 00:41:07 +00:00
|
|
|
janet_table_put(subt, janet_ckeywordv("value"), val);
|
2021-07-25 18:03:01 +00:00
|
|
|
janet_add_meta(subt, doc, source_file, source_line);
|
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
|
|
|
}
|
2021-07-25 18:03:01 +00:00
|
|
|
void janet_def(JanetTable *env, const char *name, Janet value, const char *doc) {
|
|
|
|
janet_def_sm(env, name, value, doc, NULL, 0);
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Add a var to the environment */
|
2021-07-25 18:03:01 +00:00
|
|
|
void janet_var_sm(JanetTable *env, const char *name, Janet val, const char *doc, const char *source_file, int32_t source_line) {
|
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));
|
2021-07-25 18:03:01 +00:00
|
|
|
janet_add_meta(subt, doc, source_file, source_line);
|
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
|
|
|
}
|
2021-07-25 18:03:01 +00:00
|
|
|
void janet_var(JanetTable *env, const char *name, Janet val, const char *doc) {
|
|
|
|
janet_var_sm(env, name, val, doc, NULL, 0);
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
/* Registry functions */
|
|
|
|
|
|
|
|
/* Put the registry in sorted order. */
|
|
|
|
static void janet_registry_sort(void) {
|
|
|
|
for (size_t i = 1; i < janet_vm.registry_count; i++) {
|
|
|
|
JanetCFunRegistry reg = janet_vm.registry[i];
|
|
|
|
size_t j;
|
|
|
|
for (j = i; j > 0; j--) {
|
2021-08-21 17:10:19 +00:00
|
|
|
if ((void *)(janet_vm.registry[j - 1].cfun) < (void *)(reg.cfun)) break;
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_vm.registry[j] = janet_vm.registry[j - 1];
|
|
|
|
}
|
|
|
|
janet_vm.registry[j] = reg;
|
|
|
|
}
|
|
|
|
janet_vm.registry_dirty = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_registry_put(
|
|
|
|
JanetCFunction key,
|
|
|
|
const char *name,
|
|
|
|
const char *name_prefix,
|
|
|
|
const char *source_file,
|
|
|
|
int32_t source_line) {
|
|
|
|
if (janet_vm.registry_count == janet_vm.registry_cap) {
|
|
|
|
size_t newcap = (janet_vm.registry_count + 1) * 2;
|
|
|
|
/* Size it nicely with core by default */
|
|
|
|
if (newcap < 512) {
|
|
|
|
newcap = 512;
|
|
|
|
}
|
2021-07-31 15:00:43 +00:00
|
|
|
void *newmem = janet_realloc(janet_vm.registry, newcap * sizeof(JanetCFunRegistry));
|
|
|
|
if (NULL == newmem) {
|
2020-01-03 04:02:57 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-31 15:00:43 +00:00
|
|
|
janet_vm.registry = newmem;
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_vm.registry_cap = newcap;
|
2020-01-03 04:02:57 +00:00
|
|
|
}
|
2021-07-30 02:29:08 +00:00
|
|
|
JanetCFunRegistry value = {
|
|
|
|
key,
|
|
|
|
name,
|
|
|
|
name_prefix,
|
|
|
|
source_file,
|
|
|
|
source_line
|
|
|
|
};
|
|
|
|
janet_vm.registry[janet_vm.registry_count++] = value;
|
|
|
|
janet_vm.registry_dirty = 1;
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JanetCFunRegistry *janet_registry_get(JanetCFunction key) {
|
|
|
|
if (janet_vm.registry_dirty) {
|
|
|
|
janet_registry_sort();
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < janet_vm.registry_count; i++) {
|
|
|
|
if (janet_vm.registry[i].cfun == key) {
|
|
|
|
return janet_vm.registry + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JanetCFunRegistry *lo = janet_vm.registry;
|
|
|
|
JanetCFunRegistry *hi = lo + janet_vm.registry_count;
|
|
|
|
while (lo < hi) {
|
|
|
|
JanetCFunRegistry *mid = lo + (hi - lo) / 2;
|
|
|
|
if (mid->cfun == key) {
|
|
|
|
return mid;
|
|
|
|
}
|
2021-08-21 17:10:19 +00:00
|
|
|
if ((void *)(mid->cfun) > (void *)(key)) {
|
2021-07-30 02:29:08 +00:00
|
|
|
hi = mid;
|
|
|
|
} else {
|
|
|
|
lo = mid + 1;
|
2018-08-26 18:35:01 +00:00
|
|
|
}
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
2021-07-30 02:29:08 +00:00
|
|
|
return NULL;
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
typedef struct {
|
|
|
|
char *buf;
|
|
|
|
size_t plen;
|
|
|
|
} NameBuf;
|
|
|
|
|
|
|
|
static void namebuf_init(NameBuf *namebuf, const char *prefix) {
|
|
|
|
size_t plen = strlen(prefix);
|
|
|
|
namebuf->plen = plen;
|
|
|
|
namebuf->buf = janet_malloc(namebuf->plen + 256);
|
|
|
|
if (NULL == namebuf->buf) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(namebuf->buf, prefix, plen);
|
|
|
|
namebuf->buf[plen] = '/';
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
static void namebuf_deinit(NameBuf *namebuf) {
|
|
|
|
janet_free(namebuf->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *namebuf_name(NameBuf *namebuf, const char *suffix) {
|
|
|
|
size_t slen = strlen(suffix);
|
|
|
|
namebuf->buf = janet_realloc(namebuf->buf, namebuf->plen + 2 + slen);
|
|
|
|
if (NULL == namebuf->buf) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(namebuf->buf + namebuf->plen + 1, suffix, slen);
|
|
|
|
namebuf->buf[namebuf->plen + 1 + slen] = '\0';
|
|
|
|
return (char *)(namebuf->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns) {
|
2021-07-25 18:03:01 +00:00
|
|
|
while (cfuns->name) {
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) janet_def(env, cfuns->name, fun, cfuns->documentation);
|
|
|
|
janet_registry_put(cfuns->cfun, cfuns->name, regprefix, NULL, 0);
|
2018-01-19 21:43:19 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
void janet_cfuns_ext(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns) {
|
2021-07-25 18:03:01 +00:00
|
|
|
while (cfuns->name) {
|
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) janet_def_sm(env, cfuns->name, fun, cfuns->documentation, cfuns->source_file, cfuns->source_line);
|
|
|
|
janet_registry_put(cfuns->cfun, cfuns->name, regprefix, cfuns->source_file, cfuns->source_line);
|
2021-07-25 18:03:01 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
2020-04-17 18:37:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
void janet_cfuns_prefix(JanetTable *env, const char *regprefix, const JanetReg *cfuns) {
|
|
|
|
NameBuf nb;
|
|
|
|
if (env) namebuf_init(&nb, regprefix);
|
2021-07-25 18:03:01 +00:00
|
|
|
while (cfuns->name) {
|
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) janet_def(env, namebuf_name(&nb, cfuns->name), fun, cfuns->documentation);
|
|
|
|
janet_registry_put(cfuns->cfun, cfuns->name, regprefix, NULL, 0);
|
2021-07-25 18:03:01 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) namebuf_deinit(&nb);
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_cfuns_ext_prefix(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns) {
|
2021-07-30 02:29:08 +00:00
|
|
|
NameBuf nb;
|
|
|
|
if (env) namebuf_init(&nb, regprefix);
|
2021-07-25 18:03:01 +00:00
|
|
|
while (cfuns->name) {
|
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) janet_def_sm(env, namebuf_name(&nb, cfuns->name), fun, cfuns->documentation, cfuns->source_file, cfuns->source_line);
|
|
|
|
janet_registry_put(cfuns->cfun, cfuns->name, regprefix, cfuns->source_file, cfuns->source_line);
|
2021-07-25 18:03:01 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
2021-07-30 02:29:08 +00:00
|
|
|
if (env) namebuf_deinit(&nb);
|
2020-04-17 18:37:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 15:23:18 +00:00
|
|
|
/* Register a value in the global registry */
|
|
|
|
void janet_register(const char *name, JanetCFunction cfun) {
|
|
|
|
janet_registry_put(cfun, name, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2020-03-14 15:25:39 +00:00
|
|
|
/* Abstract type introspection */
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-02-24 01:02:54 +00:00
|
|
|
void janet_register_abstract_type(const JanetAbstractType *at) {
|
2019-03-08 05:41:26 +00:00
|
|
|
Janet sym = janet_csymbolv(at->name);
|
2021-07-17 01:59:03 +00:00
|
|
|
Janet check = janet_table_get(janet_vm.abstract_registry, sym);
|
2020-10-07 00:11:29 +00:00
|
|
|
if (!janet_checktype(check, JANET_NIL) && at != janet_unwrap_pointer(check)) {
|
2019-03-08 05:41:26 +00:00
|
|
|
janet_panicf("cannot register abstract type %s, "
|
|
|
|
"a type with the same name exists", at->name);
|
2019-02-23 16:13:43 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_table_put(janet_vm.abstract_registry, sym, janet_wrap_pointer((void *) at));
|
2019-02-22 14:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 01:51:34 +00:00
|
|
|
const JanetAbstractType *janet_get_abstract_type(Janet key) {
|
2021-07-17 01:59:03 +00:00
|
|
|
Janet wrapped = janet_table_get(janet_vm.abstract_registry, key);
|
2020-03-14 15:25:39 +00:00
|
|
|
if (janet_checktype(wrapped, JANET_NIL)) {
|
2019-02-23 16:13:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-14 15:25:39 +00:00
|
|
|
return (JanetAbstractType *)(janet_unwrap_pointer(wrapped));
|
2019-02-22 09:54:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 05:44:30 +00:00
|
|
|
#ifndef JANET_BOOTSTRAP
|
2021-07-30 02:29:08 +00:00
|
|
|
void janet_core_def_sm(JanetTable *env, const char *name, Janet x, const void *p, const void *sf, int32_t sl) {
|
|
|
|
(void) sf;
|
|
|
|
(void) sl;
|
2019-02-08 05:44:30 +00:00
|
|
|
(void) p;
|
2019-03-08 16:39:18 +00:00
|
|
|
Janet key = janet_csymbolv(name);
|
2019-12-02 02:28:12 +00:00
|
|
|
janet_table_put(env, key, x);
|
|
|
|
if (janet_checktype(x, JANET_CFUNCTION)) {
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_registry_put(janet_unwrap_cfunction(x), name, NULL, NULL, 0);
|
2021-07-25 18:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_core_cfuns_ext(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns) {
|
|
|
|
(void) regprefix;
|
|
|
|
while (cfuns->name) {
|
|
|
|
Janet fun = janet_wrap_cfunction(cfuns->cfun);
|
2021-07-30 02:29:08 +00:00
|
|
|
janet_table_put(env, janet_csymbolv(cfuns->name), fun);
|
|
|
|
janet_registry_put(cfuns->cfun, cfuns->name, regprefix, cfuns->source_file, cfuns->source_line);
|
2019-02-08 05:44:30 +00:00
|
|
|
cfuns++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-24 02:08:33 +00:00
|
|
|
JanetBinding janet_binding_from_entry(Janet entry) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *entry_table;
|
2021-05-30 01:40:26 +00:00
|
|
|
JanetBinding binding = {
|
|
|
|
JANET_BINDING_NONE,
|
|
|
|
janet_wrap_nil(),
|
2022-01-07 01:44:03 +00:00
|
|
|
JANET_BINDING_DEP_NONE
|
2021-05-30 01:40:26 +00:00
|
|
|
};
|
2021-05-28 20:12:05 +00:00
|
|
|
|
|
|
|
/* Check environment for entry */
|
2022-01-24 02:08:33 +00:00
|
|
|
if (!janet_checktype(entry, JANET_TABLE))
|
|
|
|
return binding;
|
2018-09-06 02:18:42 +00:00
|
|
|
entry_table = janet_unwrap_table(entry);
|
2021-05-28 20:12:05 +00:00
|
|
|
|
|
|
|
/* deprecation check */
|
|
|
|
Janet deprecate = janet_table_get(entry_table, janet_ckeywordv("deprecated"));
|
|
|
|
if (janet_checktype(deprecate, JANET_KEYWORD)) {
|
|
|
|
JanetKeyword depkw = janet_unwrap_keyword(deprecate);
|
|
|
|
if (!janet_cstrcmp(depkw, "relaxed")) {
|
|
|
|
binding.deprecation = JANET_BINDING_DEP_RELAXED;
|
|
|
|
} else if (!janet_cstrcmp(depkw, "normal")) {
|
|
|
|
binding.deprecation = JANET_BINDING_DEP_NORMAL;
|
|
|
|
} else if (!janet_cstrcmp(depkw, "strict")) {
|
|
|
|
binding.deprecation = JANET_BINDING_DEP_STRICT;
|
|
|
|
}
|
|
|
|
} else if (!janet_checktype(deprecate, JANET_NIL)) {
|
|
|
|
binding.deprecation = JANET_BINDING_DEP_NORMAL;
|
|
|
|
}
|
|
|
|
|
2022-01-07 01:44:03 +00:00
|
|
|
int macro = janet_truthy(janet_table_get(entry_table, janet_ckeywordv("macro")));
|
|
|
|
Janet value = janet_table_get(entry_table, janet_ckeywordv("value"));
|
|
|
|
Janet ref = janet_table_get(entry_table, janet_ckeywordv("ref"));
|
2022-01-08 16:57:14 +00:00
|
|
|
int ref_is_valid = janet_checktype(ref, JANET_ARRAY);
|
|
|
|
int redef = ref_is_valid && janet_truthy(janet_table_get(entry_table, janet_ckeywordv("redef")));
|
2021-12-18 04:05:16 +00:00
|
|
|
|
2022-01-07 01:44:03 +00:00
|
|
|
if (macro) {
|
|
|
|
binding.value = redef ? ref : value;
|
|
|
|
binding.type = redef ? JANET_BINDING_DYNAMIC_MACRO : JANET_BINDING_MACRO;
|
2021-12-29 07:39:00 +00:00
|
|
|
return binding;
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:57:14 +00:00
|
|
|
if (ref_is_valid) {
|
2021-05-28 20:12:05 +00:00
|
|
|
binding.value = ref;
|
2022-01-08 16:57:14 +00:00
|
|
|
binding.type = redef ? JANET_BINDING_DYNAMIC_DEF : JANET_BINDING_VAR;
|
|
|
|
} else {
|
|
|
|
binding.value = value;
|
|
|
|
binding.type = JANET_BINDING_DEF;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2021-05-28 20:12:05 +00:00
|
|
|
|
|
|
|
return binding;
|
|
|
|
}
|
|
|
|
|
2023-04-23 06:40:32 +00:00
|
|
|
/* If the value at the given address can be coerced to a byte view,
|
|
|
|
return that byte view. If it can't, replace the value at the address
|
|
|
|
with the result of janet_to_string, and return a byte view over that
|
|
|
|
string. */
|
|
|
|
static JanetByteView memoize_byte_view(Janet *value) {
|
|
|
|
JanetByteView result;
|
|
|
|
if (!janet_bytes_view(*value, &result.bytes, &result.len)) {
|
|
|
|
JanetString str = janet_to_string(*value);
|
|
|
|
*value = janet_wrap_string(str);
|
|
|
|
result.bytes = str;
|
|
|
|
result.len = janet_string_length(str);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JanetByteView to_byte_view(Janet value) {
|
|
|
|
JanetByteView result;
|
|
|
|
if (!janet_bytes_view(value, &result.bytes, &result.len)) {
|
|
|
|
JanetString str = janet_to_string(value);
|
|
|
|
result.bytes = str;
|
|
|
|
result.len = janet_string_length(str);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:09:14 +00:00
|
|
|
JanetByteView janet_text_substitution(
|
|
|
|
Janet *subst,
|
|
|
|
const uint8_t *bytes,
|
|
|
|
uint32_t len,
|
|
|
|
JanetArray *extra_argv) {
|
|
|
|
int32_t extra_argc = extra_argv == NULL ? 0 : extra_argv->count;
|
|
|
|
JanetType type = janet_type(*subst);
|
|
|
|
switch (type) {
|
|
|
|
case JANET_FUNCTION:
|
|
|
|
case JANET_CFUNCTION: {
|
|
|
|
int32_t argc = 1 + extra_argc;
|
|
|
|
Janet *argv = janet_tuple_begin(argc);
|
|
|
|
argv[0] = janet_stringv(bytes, len);
|
|
|
|
for (int32_t i = 0; i < extra_argc; i++) {
|
|
|
|
argv[i + 1] = extra_argv->data[i];
|
|
|
|
}
|
|
|
|
janet_tuple_end(argv);
|
|
|
|
if (type == JANET_FUNCTION) {
|
|
|
|
return to_byte_view(janet_call(janet_unwrap_function(*subst), argc, argv));
|
|
|
|
} else {
|
|
|
|
return to_byte_view(janet_unwrap_cfunction(*subst)(argc, argv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return memoize_byte_view(subst);
|
|
|
|
}
|
2023-04-23 06:40:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 02:08:33 +00:00
|
|
|
JanetBinding janet_resolve_ext(JanetTable *env, const uint8_t *sym) {
|
|
|
|
Janet entry = janet_table_get(env, janet_wrap_symbol(sym));
|
|
|
|
return janet_binding_from_entry(entry);
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:12:05 +00:00
|
|
|
JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out) {
|
|
|
|
JanetBinding binding = janet_resolve_ext(env, sym);
|
2022-01-07 01:44:03 +00:00
|
|
|
if (binding.type == JANET_BINDING_DYNAMIC_DEF || binding.type == JANET_BINDING_DYNAMIC_MACRO) {
|
|
|
|
*out = janet_array_peek(janet_unwrap_array(binding.value));
|
2021-12-29 07:39:00 +00:00
|
|
|
} else {
|
|
|
|
*out = binding.value;
|
|
|
|
}
|
2021-05-28 20:12:05 +00:00
|
|
|
return binding.type;
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 10:15:22 +00:00
|
|
|
/* Resolve a symbol in the core environment. */
|
|
|
|
Janet janet_resolve_core(const char *name) {
|
|
|
|
JanetTable *env = janet_core_env(NULL);
|
|
|
|
Janet out = janet_wrap_nil();
|
|
|
|
janet_resolve(env, janet_csymbol(name), &out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-05 21:38:52 +00:00
|
|
|
JanetType t = janet_type(str);
|
|
|
|
if (t == JANET_STRING || t == JANET_SYMBOL || t == 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;
|
2022-11-05 21:38:52 +00:00
|
|
|
} else if (t == JANET_BUFFER) {
|
2018-09-06 02:18:42 +00:00
|
|
|
*data = janet_unwrap_buffer(str)->data;
|
|
|
|
*len = janet_unwrap_buffer(str)->count;
|
2017-04-24 17:12:55 +00:00
|
|
|
return 1;
|
2022-11-05 21:38:52 +00:00
|
|
|
} else if (t == JANET_ABSTRACT) {
|
|
|
|
void *abst = janet_unwrap_abstract(str);
|
|
|
|
const JanetAbstractType *atype = janet_abstract_type(abst);
|
|
|
|
if (NULL == atype->bytes) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
JanetByteView view = atype->bytes(abst, janet_abstract_size(abst));
|
|
|
|
*data = view.bytes;
|
|
|
|
*len = view.len;
|
|
|
|
return 1;
|
2017-04-24 17:12:55 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-06-06 23:54:17 +00:00
|
|
|
int janet_checkuint64(Janet x) {
|
|
|
|
if (!janet_checktype(x, JANET_NUMBER))
|
|
|
|
return 0;
|
|
|
|
double dval = janet_unwrap_number(x);
|
|
|
|
return dval >= 0 && dval <= JANET_INTMAX_DOUBLE && dval == (uint64_t) dval;
|
|
|
|
}
|
|
|
|
|
2019-03-08 05:41:26 +00:00
|
|
|
int janet_checksize(Janet x) {
|
|
|
|
if (!janet_checktype(x, JANET_NUMBER))
|
|
|
|
return 0;
|
|
|
|
double dval = janet_unwrap_number(x);
|
2020-06-27 16:23:47 +00:00
|
|
|
if (dval != (double)((size_t) dval)) return 0;
|
|
|
|
if (SIZE_MAX > JANET_INTMAX_INT64) {
|
|
|
|
return dval <= JANET_INTMAX_INT64;
|
|
|
|
} else {
|
|
|
|
return dval <= SIZE_MAX;
|
|
|
|
}
|
2018-12-30 19:23:52 +00:00
|
|
|
}
|
2019-12-15 02:39:14 +00:00
|
|
|
|
|
|
|
JanetTable *janet_get_core_table(const char *name) {
|
|
|
|
JanetTable *env = janet_core_env(NULL);
|
|
|
|
Janet out = janet_wrap_nil();
|
|
|
|
JanetBindingType bt = janet_resolve(env, janet_csymbol(name), &out);
|
|
|
|
if (bt == JANET_BINDING_NONE) return NULL;
|
|
|
|
if (!janet_checktype(out, JANET_TABLE)) return NULL;
|
|
|
|
return janet_unwrap_table(out);
|
|
|
|
}
|
2020-07-03 14:54:58 +00:00
|
|
|
|
2021-03-14 01:17:07 +00:00
|
|
|
/* Sort keys of a dictionary type */
|
|
|
|
int32_t janet_sorted_keys(const JanetKV *dict, int32_t cap, int32_t *index_buffer) {
|
|
|
|
|
|
|
|
/* First, put populated indices into index_buffer */
|
|
|
|
int32_t next_index = 0;
|
|
|
|
for (int32_t i = 0; i < cap; i++) {
|
|
|
|
if (!janet_checktype(dict[i].key, JANET_NIL)) {
|
|
|
|
index_buffer[next_index++] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, sort those (simple insertion sort here for now) */
|
|
|
|
for (int32_t i = 1; i < next_index; i++) {
|
|
|
|
int32_t index_to_insert = index_buffer[i];
|
|
|
|
Janet lhs = dict[index_to_insert].key;
|
|
|
|
for (int32_t j = i - 1; j >= 0; j--) {
|
|
|
|
index_buffer[j + 1] = index_buffer[j];
|
|
|
|
Janet rhs = dict[index_buffer[j]].key;
|
|
|
|
if (janet_compare(lhs, rhs) >= 0) {
|
|
|
|
index_buffer[j + 1] = index_to_insert;
|
|
|
|
break;
|
|
|
|
} else if (j == 0) {
|
|
|
|
index_buffer[0] = index_to_insert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return number of indices found */
|
|
|
|
return next_index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:54:58 +00:00
|
|
|
/* Clock shims for various platforms */
|
|
|
|
#ifdef JANET_GETTIME
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
int janet_gettime(struct timespec *spec) {
|
|
|
|
FILETIME ftime;
|
|
|
|
GetSystemTimeAsFileTime(&ftime);
|
|
|
|
int64_t wintime = (int64_t)(ftime.dwLowDateTime) | ((int64_t)(ftime.dwHighDateTime) << 32);
|
|
|
|
/* Windows epoch is January 1, 1601 apparently */
|
|
|
|
wintime -= 116444736000000000LL;
|
|
|
|
spec->tv_sec = wintime / 10000000LL;
|
|
|
|
/* Resolution is 100 nanoseconds. */
|
|
|
|
spec->tv_nsec = wintime % 10000000LL * 100;
|
|
|
|
return 0;
|
|
|
|
}
|
2022-03-22 01:20:20 +00:00
|
|
|
/* clock_gettime() wasn't available on Mac until 10.12. */
|
|
|
|
#elif defined(JANET_APPLE) && !defined(MAC_OS_X_VERSION_10_12)
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
2020-07-03 14:54:58 +00:00
|
|
|
int janet_gettime(struct timespec *spec) {
|
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t mts;
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &mts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
spec->tv_sec = mts.tv_sec;
|
|
|
|
spec->tv_nsec = mts.tv_nsec;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 09:41:25 +00:00
|
|
|
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
|
|
|
clockid_t cid = JANET_TIME_REALTIME;
|
|
|
|
switch (source) {
|
|
|
|
case JANET_TIME_REALTIME:
|
|
|
|
cid = CLOCK_REALTIME;
|
|
|
|
break;
|
|
|
|
case JANET_TIME_MONOTONIC:
|
|
|
|
cid = CLOCK_MONOTONIC;
|
|
|
|
break;
|
|
|
|
case JANET_TIME_CPUTIME:
|
|
|
|
cid = CLOCK_PROCESS_CPUTIME_ID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return clock_gettime(cid, spec);
|
2020-07-03 14:54:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-08-04 00:11:05 +00:00
|
|
|
|
|
|
|
/* Setting C99 standard makes this not available, but it should
|
|
|
|
* work/link properly if we detect a BSD */
|
|
|
|
#if defined(JANET_BSD) || defined(MAC_OS_X_VERSION_10_7)
|
|
|
|
void arc4random_buf(void *buf, size_t nbytes);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int janet_cryptorand(uint8_t *out, size_t n) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
for (size_t i = 0; i < n; i += sizeof(unsigned int)) {
|
|
|
|
unsigned int v;
|
|
|
|
if (rand_s(&v))
|
|
|
|
return -1;
|
2023-01-21 16:37:34 +00:00
|
|
|
for (int32_t j = 0; (j < (int32_t) sizeof(unsigned int)) && (i + j < n); j++) {
|
2020-08-04 00:11:05 +00:00
|
|
|
out[i + j] = v & 0xff;
|
|
|
|
v = v >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2023-01-03 14:48:54 +00:00
|
|
|
#elif defined(JANET_LINUX) || defined(JANET_CYGWIN) || ( defined(JANET_APPLE) && !defined(MAC_OS_X_VERSION_10_7) )
|
2020-08-04 00:11:05 +00:00
|
|
|
/* We should be able to call getrandom on linux, but it doesn't seem
|
|
|
|
to be uniformly supported on linux distros.
|
|
|
|
On Mac, arc4random_buf wasn't available on until 10.7.
|
|
|
|
In these cases, use this fallback path for now... */
|
|
|
|
int rc;
|
|
|
|
int randfd;
|
|
|
|
RETRY_EINTR(randfd, open("/dev/urandom", O_RDONLY | O_CLOEXEC));
|
|
|
|
if (randfd < 0)
|
|
|
|
return -1;
|
|
|
|
while (n > 0) {
|
|
|
|
ssize_t nread;
|
|
|
|
RETRY_EINTR(nread, read(randfd, out, n));
|
|
|
|
if (nread <= 0) {
|
|
|
|
RETRY_EINTR(rc, close(randfd));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
out += nread;
|
|
|
|
n -= nread;
|
|
|
|
}
|
|
|
|
RETRY_EINTR(rc, close(randfd));
|
|
|
|
return 0;
|
|
|
|
#elif defined(JANET_BSD) || defined(MAC_OS_X_VERSION_10_7)
|
2020-08-04 02:49:49 +00:00
|
|
|
arc4random_buf(out, n);
|
2020-08-04 00:11:05 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
2020-09-07 21:08:43 +00:00
|
|
|
(void) n;
|
|
|
|
(void) out;
|
2020-08-04 00:11:05 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
2020-08-04 02:49:49 +00:00
|
|
|
}
|
2021-03-23 10:00:48 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
/* Dynamic library loading */
|
|
|
|
|
|
|
|
char *get_processed_name(const char *name) {
|
|
|
|
if (name[0] == '.') return (char *) name;
|
|
|
|
const char *c;
|
|
|
|
for (c = name; *c; c++) {
|
|
|
|
if (*c == '/') return (char *) name;
|
|
|
|
}
|
|
|
|
size_t l = (size_t)(c - name);
|
|
|
|
char *ret = janet_malloc(l + 3);
|
|
|
|
if (NULL == ret) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
ret[0] = '.';
|
|
|
|
ret[1] = '/';
|
|
|
|
memcpy(ret + 2, name, l + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-21 15:36:03 +00:00
|
|
|
#if defined(JANET_NO_DYNAMIC_MODULES)
|
|
|
|
|
|
|
|
const char *error_clib(void) {
|
|
|
|
return "dynamic modules not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2022-06-12 15:02:02 +00:00
|
|
|
#if defined(JANET_WINDOWS)
|
2022-08-14 18:26:13 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
static char error_clib_buf[256];
|
|
|
|
char *error_clib(void) {
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
error_clib_buf, sizeof(error_clib_buf), NULL);
|
|
|
|
error_clib_buf[strlen(error_clib_buf) - 1] = '\0';
|
|
|
|
return error_clib_buf;
|
|
|
|
}
|
2022-06-18 15:31:00 +00:00
|
|
|
|
|
|
|
Clib load_clib(const char *name) {
|
|
|
|
if (name == NULL) {
|
|
|
|
return GetModuleHandle(NULL);
|
|
|
|
} else {
|
|
|
|
return LoadLibrary(name);
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 18:26:13 +00:00
|
|
|
|
|
|
|
void free_clib(HINSTANCE clib) {
|
|
|
|
if (clib != GetModuleHandle(NULL)) {
|
|
|
|
FreeLibrary(clib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *symbol_clib(HINSTANCE clib, const char *sym) {
|
|
|
|
if (clib != GetModuleHandle(NULL)) {
|
|
|
|
return GetProcAddress(clib, sym);
|
|
|
|
} else {
|
|
|
|
/* Look up symbols from all loaded modules */
|
|
|
|
HMODULE hMods[1024];
|
|
|
|
DWORD needed = 0;
|
|
|
|
if (EnumProcessModules(GetCurrentProcess(), hMods, sizeof(hMods), &needed)) {
|
|
|
|
needed /= sizeof(HMODULE);
|
|
|
|
for (DWORD i = 0; i < needed; i++) {
|
|
|
|
void *address = GetProcAddress(hMods[i], sym);
|
|
|
|
if (NULL != address) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
janet_panicf("ffi: %s", error_clib());
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 15:36:03 +00:00
|
|
|
#endif
|
2022-06-12 15:02:02 +00:00
|
|
|
#endif
|
2021-03-23 10:00:48 +00:00
|
|
|
|
|
|
|
/* Alloc function macro fills */
|
|
|
|
void *(janet_malloc)(size_t size) {
|
|
|
|
return janet_malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void (janet_free)(void *ptr) {
|
2021-05-30 21:42:58 +00:00
|
|
|
janet_free(ptr);
|
2021-03-23 10:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *(janet_calloc)(size_t nmemb, size_t size) {
|
|
|
|
return janet_calloc(nmemb, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *(janet_realloc)(void *ptr, size_t size) {
|
|
|
|
return janet_realloc(ptr, size);
|
|
|
|
}
|