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.
|
|
|
|
*/
|
|
|
|
|
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>
|
2021-07-30 02:29:08 +00:00
|
|
|
#include "state.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>
|
2022-06-08 14:41:09 +00:00
|
|
|
#include <stddef.h>
|
2022-06-10 01:27:56 +00:00
|
|
|
#include <stdbool.h>
|
2019-12-30 20:12:17 +00:00
|
|
|
|
2022-06-17 22:13:58 +00:00
|
|
|
#ifdef JANET_EV
|
2022-06-17 22:35:58 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2022-06-17 22:13:58 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2022-06-17 22:35:58 +00:00
|
|
|
#endif
|
2022-06-17 22:13:58 +00:00
|
|
|
|
2020-07-03 19:14:59 +00:00
|
|
|
#if !defined(JANET_REDUCED_OS) || !defined(JANET_SINGLE_THREADED)
|
|
|
|
#include <time.h>
|
|
|
|
#define JANET_GETTIME
|
|
|
|
#endif
|
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
/* Handle runtime errors */
|
2020-05-09 17:00:01 +00:00
|
|
|
#ifndef JANET_EXIT
|
2019-04-27 19:45:28 +00:00
|
|
|
#include <stdio.h>
|
2020-05-09 17:00:01 +00:00
|
|
|
#define JANET_EXIT(m) do { \
|
2020-04-18 23:14:38 +00:00
|
|
|
fprintf(stderr, "C runtime error at line %d in file %s: %s\n",\
|
2019-04-27 19:45:28 +00:00
|
|
|
__LINE__,\
|
|
|
|
__FILE__,\
|
|
|
|
(m));\
|
|
|
|
exit(1);\
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2021-08-20 21:41:19 +00:00
|
|
|
#define JANET_MARSHAL_DECREF 0x40000
|
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
#define janet_assert(c, m) do { \
|
2020-05-09 17:00:01 +00:00
|
|
|
if (!(c)) JANET_EXIT((m)); \
|
2019-04-27 19:45:28 +00:00
|
|
|
} while (0)
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
/* Utils */
|
2021-12-05 22:31:18 +00:00
|
|
|
uint32_t janet_hash_mix(uint32_t input, uint32_t more);
|
2018-09-09 16:13:32 +00:00
|
|
|
#define janet_maphash(cap, hash) ((uint32_t)(hash) & (cap - 1))
|
2021-10-29 16:08:53 +00:00
|
|
|
int janet_valid_utf8(const uint8_t *str, int32_t len);
|
|
|
|
int janet_is_symbol_char(uint8_t c);
|
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);
|
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);
|
2020-07-04 00:54:58 +00:00
|
|
|
void janet_def_addflags(JanetFuncDef *def);
|
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);
|
2021-01-03 22:17:36 +00:00
|
|
|
Janet janet_next_impl(Janet ds, Janet key, int is_interpreter);
|
2022-01-24 02:08:33 +00:00
|
|
|
JanetBinding janet_binding_from_entry(Janet entry);
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
/* Registry functions */
|
|
|
|
void janet_registry_put(
|
|
|
|
JanetCFunction key,
|
|
|
|
const char *name,
|
|
|
|
const char *name_prefix,
|
|
|
|
const char *source_file,
|
|
|
|
int32_t source_line);
|
|
|
|
JanetCFunRegistry *janet_registry_get(JanetCFunction key);
|
|
|
|
|
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
|
2021-07-25 18:03:01 +00:00
|
|
|
#define JANET_CORE_REG JANET_REG
|
|
|
|
#define JANET_CORE_FN JANET_FN
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_CORE_DEF JANET_DEF
|
2021-07-25 18:03:01 +00:00
|
|
|
#define janet_core_def_sm janet_def_sm
|
|
|
|
#define janet_core_cfuns_ext janet_cfuns_ext
|
2019-02-08 05:44:30 +00:00
|
|
|
#else
|
2021-07-30 02:29:08 +00:00
|
|
|
#define JANET_CORE_REG JANET_REG_S
|
|
|
|
#define JANET_CORE_FN JANET_FN_S
|
|
|
|
#define JANET_CORE_DEF(ENV, NAME, X, DOC) janet_core_def_sm(ENV, NAME, X, DOC, NULL, 0)
|
2021-07-25 18:03:01 +00:00
|
|
|
void janet_core_def_sm(JanetTable *env, const char *name, Janet x, const void *p, const void *sf, int32_t sl);
|
|
|
|
void janet_core_cfuns_ext(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns);
|
2019-02-08 05:44:30 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-03 14:54:58 +00:00
|
|
|
/* Clock gettime */
|
2020-07-03 19:14:59 +00:00
|
|
|
#ifdef JANET_GETTIME
|
2020-07-03 14:54:58 +00:00
|
|
|
int janet_gettime(struct timespec *spec);
|
2019-02-08 05:44:30 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-24 22:48:46 +00:00
|
|
|
/* strdup */
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#define strdup(x) _strdup(x)
|
|
|
|
#endif
|
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
|
|
|
* with native code. */
|
|
|
|
#if defined(JANET_NO_DYNAMIC_MODULES)
|
|
|
|
typedef int Clib;
|
|
|
|
#define load_clib(name) ((void) name, 0)
|
|
|
|
#define symbol_clib(lib, sym) ((void) lib, (void) sym, NULL)
|
|
|
|
#define error_clib() "dynamic libraries not supported"
|
|
|
|
#define free_clib(c) ((void) (c), 0)
|
|
|
|
#elif defined(JANET_WINDOWS)
|
|
|
|
#include <windows.h>
|
|
|
|
typedef HINSTANCE Clib;
|
2022-08-14 18:26:13 +00:00
|
|
|
void *symbol_clib(Clib clib, const char *sym);
|
|
|
|
void free_clib(Clib clib);
|
2022-06-18 15:31:00 +00:00
|
|
|
Clib load_clib(const char *name);
|
2022-06-12 15:02:02 +00:00
|
|
|
char *error_clib(void);
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
typedef void *Clib;
|
|
|
|
#define load_clib(name) dlopen((name), RTLD_NOW)
|
|
|
|
#define free_clib(lib) dlclose((lib))
|
|
|
|
#define symbol_clib(lib, sym) dlsym((lib), (sym))
|
|
|
|
#define error_clib() dlerror()
|
|
|
|
#endif
|
|
|
|
char *get_processed_name(const char *name);
|
|
|
|
|
2020-08-04 00:11:05 +00:00
|
|
|
#define RETRY_EINTR(RC, CALL) do { (RC) = CALL; } while((RC) < 0 && errno == EINTR)
|
|
|
|
|
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);
|
2021-05-29 02:37:13 +00:00
|
|
|
void janet_lib_struct(JanetTable *env);
|
2019-01-06 01:09:03 +00:00
|
|
|
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
|
2020-02-02 02:39:54 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
void janet_lib_net(JanetTable *env);
|
2020-11-14 17:48:23 +00:00
|
|
|
extern const JanetAbstractType janet_address_type;
|
2020-05-28 15:39:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef JANET_EV
|
|
|
|
void janet_lib_ev(JanetTable *env);
|
|
|
|
void janet_ev_mark(void);
|
2021-01-16 21:11:07 +00:00
|
|
|
int janet_make_pipe(JanetHandle handles[2], int mode);
|
2020-02-02 02:39:54 +00:00
|
|
|
#endif
|
2022-06-06 23:54:17 +00:00
|
|
|
#ifdef JANET_FFI
|
|
|
|
void janet_lib_ffi(JanetTable *env);
|
|
|
|
#endif
|
2019-03-12 22:58:54 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
#endif
|