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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_UTIL_H_defined
|
|
|
|
#define JANET_UTIL_H_defined
|
2017-04-15 20:05:59 +00:00
|
|
|
|
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>
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-04-15 20:05:59 +00:00
|
|
|
|
2019-12-31 00:06:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2019-12-30 20:12:17 +00:00
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
/* Handle runtime errors */
|
|
|
|
#ifndef janet_exit
|
|
|
|
#include <stdio.h>
|
|
|
|
#define janet_exit(m) do { \
|
|
|
|
printf("C runtime error at line %d in file %s: %s\n",\
|
|
|
|
__LINE__,\
|
|
|
|
__FILE__,\
|
|
|
|
(m));\
|
|
|
|
exit(1);\
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define janet_assert(c, m) do { \
|
|
|
|
if (!(c)) janet_exit((m)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* What to do when out of memory */
|
|
|
|
#ifndef JANET_OUT_OF_MEMORY
|
|
|
|
#include <stdio.h>
|
|
|
|
#define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0)
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 06:49:56 +00:00
|
|
|
/* Omit docstrings in some builds */
|
2019-02-08 05:44:30 +00:00
|
|
|
#ifndef JANET_BOOTSTRAP
|
2019-01-06 06:49:56 +00:00
|
|
|
#define JDOC(x) NULL
|
2019-02-08 05:44:30 +00:00
|
|
|
#define JANET_NO_BOOTSTRAP
|
2019-01-06 06:49:56 +00:00
|
|
|
#else
|
|
|
|
#define JDOC(x) x
|
|
|
|
#endif
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Utils */
|
2018-09-09 16:13:32 +00:00
|
|
|
#define janet_maphash(cap, hash) ((uint32_t)(hash) & (cap - 1))
|
2018-09-06 02:18:42 +00:00
|
|
|
extern const char janet_base64[65];
|
|
|
|
int32_t janet_array_calchash(const Janet *array, int32_t len);
|
|
|
|
int32_t janet_kv_calchash(const JanetKV *kvs, int32_t len);
|
|
|
|
int32_t janet_string_calchash(const uint8_t *str, int32_t len);
|
|
|
|
int32_t janet_tablen(int32_t n);
|
2020-01-29 05:38:52 +00:00
|
|
|
void safe_memcpy(void *dest, const void *src, size_t len);
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_buffer_push_types(JanetBuffer *buffer, int types);
|
2018-09-09 16:13:32 +00:00
|
|
|
const JanetKV *janet_dict_find(const JanetKV *buckets, int32_t cap, Janet key);
|
|
|
|
Janet janet_dict_get(const JanetKV *buckets, int32_t cap, Janet key);
|
2018-12-30 17:37:50 +00:00
|
|
|
void janet_memempty(JanetKV *mem, int32_t count);
|
|
|
|
void *janet_memalloc_empty(int32_t count);
|
2019-12-15 02:39:14 +00:00
|
|
|
JanetTable *janet_get_core_table(const char *name);
|
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);
|
2019-02-16 18:59:38 +00:00
|
|
|
void janet_buffer_format(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetBuffer *b,
|
|
|
|
const char *strfrmt,
|
|
|
|
int32_t argstart,
|
|
|
|
int32_t argc,
|
|
|
|
Janet *argv);
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2019-02-08 05:44:30 +00:00
|
|
|
/* Inside the janet core, defining globals is different
|
|
|
|
* at bootstrap time and normal runtime */
|
|
|
|
#ifdef JANET_BOOTSTRAP
|
|
|
|
#define janet_core_def janet_def
|
|
|
|
#define janet_core_cfuns janet_cfuns
|
|
|
|
#else
|
|
|
|
void janet_core_def(JanetTable *env, const char *name, Janet x, const void *p);
|
|
|
|
void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns);
|
|
|
|
#endif
|
|
|
|
|
2018-08-26 18:35:01 +00:00
|
|
|
/* Initialize builtin libraries */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_io(JanetTable *env);
|
|
|
|
void janet_lib_math(JanetTable *env);
|
|
|
|
void janet_lib_array(JanetTable *env);
|
|
|
|
void janet_lib_tuple(JanetTable *env);
|
|
|
|
void janet_lib_buffer(JanetTable *env);
|
|
|
|
void janet_lib_table(JanetTable *env);
|
|
|
|
void janet_lib_fiber(JanetTable *env);
|
|
|
|
void janet_lib_os(JanetTable *env);
|
|
|
|
void janet_lib_string(JanetTable *env);
|
|
|
|
void janet_lib_marsh(JanetTable *env);
|
|
|
|
void janet_lib_parse(JanetTable *env);
|
2018-09-29 14:58:57 +00:00
|
|
|
#ifdef JANET_ASSEMBLER
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_asm(JanetTable *env);
|
2018-09-29 14:58:57 +00:00
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_compile(JanetTable *env);
|
|
|
|
void janet_lib_debug(JanetTable *env);
|
2019-02-18 02:25:30 +00:00
|
|
|
#ifdef JANET_PEG
|
2019-01-12 00:22:24 +00:00
|
|
|
void janet_lib_peg(JanetTable *env);
|
2019-02-18 02:25:30 +00:00
|
|
|
#endif
|
2019-02-20 23:15:48 +00:00
|
|
|
#ifdef JANET_TYPED_ARRAY
|
|
|
|
void janet_lib_typed_array(JanetTable *env);
|
|
|
|
#endif
|
2019-03-19 01:12:38 +00:00
|
|
|
#ifdef JANET_INT_TYPES
|
|
|
|
void janet_lib_inttypes(JanetTable *env);
|
2019-03-12 22:58:54 +00:00
|
|
|
#endif
|
2019-11-27 05:13:53 +00:00
|
|
|
#ifdef JANET_THREADS
|
|
|
|
void janet_lib_thread(JanetTable *env);
|
|
|
|
#endif
|
2019-03-12 22:58:54 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
#endif
|