2018-09-06 02:18:42 +00:00
|
|
|
/*
|
2022-03-21 23:22:59 +00:00
|
|
|
* Copyright (c) 2022 Calvin Rose
|
2018-09-06 02:18:42 +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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-11-17 15:48:31 +00:00
|
|
|
#include "janetconf.h"
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_H_defined
|
|
|
|
#define JANET_H_defined
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-04-25 21:13:00 +00:00
|
|
|
/* Variable length arrays are ok */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4200 )
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/***** START SECTION CONFIG *****/
|
|
|
|
|
2019-03-22 18:31:20 +00:00
|
|
|
#ifndef JANET_VERSION
|
|
|
|
#define JANET_VERSION "latest"
|
|
|
|
#endif
|
2018-12-08 19:17:03 +00:00
|
|
|
|
|
|
|
#ifndef JANET_BUILD
|
|
|
|
#define JANET_BUILD "local"
|
2018-10-31 20:21:21 +00:00
|
|
|
#endif
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detect OS and endianess.
|
|
|
|
* From webkit source. There is likely some extreneous
|
|
|
|
* detection for unsupported platforms
|
|
|
|
*/
|
|
|
|
|
2019-12-15 21:41:58 +00:00
|
|
|
/* Check for any flavor of BSD (except apple) */
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || \
|
|
|
|
defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#define JANET_BSD 1
|
|
|
|
#endif
|
|
|
|
|
2022-03-17 06:20:55 +00:00
|
|
|
/* Check for macOS or OS X */
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
2019-12-15 21:41:58 +00:00
|
|
|
#define JANET_APPLE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check for Linux */
|
|
|
|
#ifdef __linux__
|
|
|
|
#define JANET_LINUX 1
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Check Unix */
|
|
|
|
#if defined(_AIX) \
|
|
|
|
|| defined(__APPLE__) /* Darwin */ \
|
|
|
|
|| defined(__FreeBSD__) || defined(__DragonFly__) \
|
|
|
|
|| defined(__FreeBSD_kernel__) \
|
|
|
|
|| defined(__GNU__) /* GNU/Hurd */ \
|
2019-05-14 10:41:33 +00:00
|
|
|
|| defined(__HAIKU__) \
|
2018-09-06 02:18:42 +00:00
|
|
|
|| defined(__linux__) \
|
|
|
|
|| defined(__NetBSD__) \
|
|
|
|
|| defined(__OpenBSD__) \
|
|
|
|
|| defined(__QNXNTO__) \
|
|
|
|
|| defined(sun) || defined(__sun) /* Solaris */ \
|
|
|
|
|| defined(unix) || defined(__unix) || defined(__unix__)
|
2019-12-15 21:41:58 +00:00
|
|
|
#define JANET_POSIX 1
|
2018-09-06 02:18:42 +00:00
|
|
|
#elif defined(__EMSCRIPTEN__)
|
|
|
|
#define JANET_WEB 1
|
|
|
|
#elif defined(WIN32) || defined(_WIN32)
|
|
|
|
#define JANET_WINDOWS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check 64-bit vs 32-bit */
|
|
|
|
#if ((defined(__x86_64__) || defined(_M_X64)) \
|
2019-12-15 21:41:58 +00:00
|
|
|
&& (defined(JANET_POSIX) || defined(JANET_WINDOWS))) \
|
2019-02-19 23:41:21 +00:00
|
|
|
|| (defined(_WIN64)) /* Windows 64 bit */ \
|
2018-09-06 02:18:42 +00:00
|
|
|
|| (defined(__ia64__) && defined(__LP64__)) /* Itanium in LP64 mode */ \
|
|
|
|
|| defined(__alpha__) /* DEC Alpha */ \
|
|
|
|
|| (defined(__sparc__) && defined(__arch64__) || defined (__sparcv9)) /* BE */ \
|
|
|
|
|| defined(__s390x__) /* S390 64-bit (BE) */ \
|
|
|
|
|| (defined(__ppc64__) || defined(__PPC64__)) \
|
|
|
|
|| defined(__aarch64__) /* ARM 64-bit */
|
|
|
|
#define JANET_64 1
|
|
|
|
#else
|
|
|
|
#define JANET_32 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check big endian */
|
2020-03-30 16:30:19 +00:00
|
|
|
#if defined(__LITTLE_ENDIAN__) || \
|
|
|
|
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
|
|
|
/* If we know the target is LE, always use that - e.g. ppc64 little endian
|
|
|
|
* defines the __LITTLE_ENDIAN__ macro in the ABI spec, so we can rely
|
|
|
|
* on that and if that's not defined, fall back to big endian assumption
|
|
|
|
*/
|
|
|
|
#define JANET_LITTLE_ENDIAN 1
|
|
|
|
#elif defined(__MIPSEB__) /* MIPS 32-bit */ \
|
2018-09-06 02:18:42 +00:00
|
|
|
|| defined(__ppc__) || defined(__PPC__) /* CPU(PPC) - PowerPC 32-bit */ \
|
|
|
|
|| defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) \
|
|
|
|
|| defined(_M_PPC) || defined(__PPC) \
|
|
|
|
|| defined(__ppc64__) || defined(__PPC64__) /* PowerPC 64-bit */ \
|
|
|
|
|| defined(__sparc) /* Sparc 32bit */ \
|
|
|
|
|| defined(__sparc__) /* Sparc 64-bit */ \
|
|
|
|
|| defined(__s390x__) /* S390 64-bit */ \
|
|
|
|
|| defined(__s390__) /* S390 32-bit */ \
|
|
|
|
|| defined(__ARMEB__) /* ARM big endian */ \
|
|
|
|
|| ((defined(__CC_ARM) || defined(__ARMCC__)) /* ARM RealView compiler */ \
|
|
|
|
&& defined(__BIG_ENDIAN))
|
|
|
|
#define JANET_BIG_ENDIAN 1
|
|
|
|
#else
|
|
|
|
#define JANET_LITTLE_ENDIAN 1
|
|
|
|
#endif
|
|
|
|
|
2020-06-27 17:39:16 +00:00
|
|
|
/* Limits for converting doubles to 64 bit integers */
|
2020-07-03 19:14:59 +00:00
|
|
|
#define JANET_INTMAX_DOUBLE 9007199254740992.0
|
|
|
|
#define JANET_INTMIN_DOUBLE (-9007199254740992.0)
|
|
|
|
#define JANET_INTMAX_INT64 9007199254740992
|
|
|
|
#define JANET_INTMIN_INT64 (-9007199254740992)
|
2020-06-27 17:39:16 +00:00
|
|
|
|
2018-10-17 03:08:26 +00:00
|
|
|
/* Check emscripten */
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#define JANET_NO_DYNAMIC_MODULES
|
2020-05-09 17:04:47 +00:00
|
|
|
#define JANET_NO_PROCESSES
|
2018-10-17 03:08:26 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-09 17:00:01 +00:00
|
|
|
/* Check sun */
|
|
|
|
#ifdef __sun
|
|
|
|
#define JANET_NO_UTC_MKTIME
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Define how global janet state is declared */
|
2021-07-17 20:13:28 +00:00
|
|
|
/* Also enable the thread library only if not single-threaded */
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_SINGLE_THREADED
|
|
|
|
#define JANET_THREAD_LOCAL
|
2021-07-17 20:13:28 +00:00
|
|
|
#undef JANET_THREADS
|
2018-09-06 02:18:42 +00:00
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define JANET_THREAD_LOCAL __thread
|
|
|
|
#elif defined(_MSC_BUILD)
|
|
|
|
#define JANET_THREAD_LOCAL __declspec(thread)
|
|
|
|
#else
|
|
|
|
#define JANET_THREAD_LOCAL
|
2021-07-17 20:13:28 +00:00
|
|
|
#undef JANET_THREADS
|
2018-09-06 02:18:42 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Enable or disable dynamic module loading. Enabled by default. */
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_NO_DYNAMIC_MODULES
|
|
|
|
#define JANET_DYNAMIC_MODULES
|
|
|
|
#endif
|
|
|
|
|
2022-06-08 14:50:31 +00:00
|
|
|
/* Enable or disable the FFI library. Currently, FFI only enabled on
|
2022-08-14 18:26:13 +00:00
|
|
|
* x86-64 operating systems. */
|
2022-06-06 23:54:17 +00:00
|
|
|
#ifndef JANET_NO_FFI
|
2022-08-14 18:26:13 +00:00
|
|
|
#if !defined(__EMSCRIPTEN__) && (defined(__x86_64__) || defined(_M_X64))
|
2022-06-06 23:54:17 +00:00
|
|
|
#define JANET_FFI
|
|
|
|
#endif
|
2022-06-08 14:50:31 +00:00
|
|
|
#endif
|
2022-06-06 23:54:17 +00:00
|
|
|
|
2018-09-29 14:58:57 +00:00
|
|
|
/* Enable or disable the assembler. Enabled by default. */
|
|
|
|
#ifndef JANET_NO_ASSEMBLER
|
|
|
|
#define JANET_ASSEMBLER
|
|
|
|
#endif
|
|
|
|
|
2019-02-18 02:22:03 +00:00
|
|
|
/* Enable or disable the peg module */
|
|
|
|
#ifndef JANET_NO_PEG
|
|
|
|
#define JANET_PEG
|
|
|
|
#endif
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Enable or disable event loop */
|
|
|
|
#if !defined(JANET_NO_EV) && !defined(__EMSCRIPTEN__)
|
|
|
|
#define JANET_EV
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 02:39:54 +00:00
|
|
|
/* Enable or disable networking */
|
2020-05-28 15:39:40 +00:00
|
|
|
#if defined(JANET_EV) && !defined(JANET_NO_NET) && !defined(__EMSCRIPTEN__)
|
2020-02-02 02:39:54 +00:00
|
|
|
#define JANET_NET
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
/* Enable or disable large int types (for now 64 bit, maybe 128 / 256 bit integer types) */
|
|
|
|
#ifndef JANET_NO_INT_TYPES
|
|
|
|
#define JANET_INT_TYPES
|
2019-03-12 22:58:54 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-22 02:46:26 +00:00
|
|
|
/* Enable or disable epoll on Linux */
|
|
|
|
#if defined(JANET_LINUX) && !defined(JANET_EV_NO_EPOLL)
|
|
|
|
#define JANET_EV_EPOLL
|
|
|
|
#endif
|
2019-03-12 22:58:54 +00:00
|
|
|
|
2021-09-06 21:01:06 +00:00
|
|
|
/* Enable or disable kqueue on BSD */
|
2021-09-03 19:29:13 +00:00
|
|
|
#if defined(JANET_BSD) && !defined(JANET_EV_NO_KQUEUE)
|
|
|
|
#define JANET_EV_KQUEUE
|
|
|
|
#endif
|
|
|
|
|
2021-09-06 21:01:06 +00:00
|
|
|
/* Enable or disable kqueue on Apple */
|
|
|
|
#if defined(JANET_APPLE) && !defined(JANET_EV_NO_KQUEUE)
|
|
|
|
#define JANET_EV_KQUEUE
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* How to export symbols */
|
|
|
|
#ifndef JANET_API
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#define JANET_API __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
#define JANET_API __attribute__((visibility ("default")))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-05-31 14:10:20 +00:00
|
|
|
/* Tell complier some functions don't return */
|
|
|
|
#ifndef JANET_NO_RETURN
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#define JANET_NO_RETURN __declspec(noreturn)
|
|
|
|
#else
|
2020-09-04 22:05:28 +00:00
|
|
|
#define JANET_NO_RETURN __attribute__((noreturn))
|
2019-05-31 14:10:20 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Prevent some recursive functions from recursing too deeply
|
|
|
|
* ands crashing (the parser). Instead, error out. */
|
|
|
|
#define JANET_RECURSION_GUARD 1024
|
|
|
|
|
|
|
|
/* Maximum depth to follow table prototypes before giving up and returning nil. */
|
|
|
|
#define JANET_MAX_PROTO_DEPTH 200
|
|
|
|
|
2022-08-30 07:21:20 +00:00
|
|
|
/* Prevent macros to expand too deeply and error out. */
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_MAX_MACRO_EXPAND 200
|
|
|
|
|
2019-08-19 05:19:51 +00:00
|
|
|
/* Define default max stack size for stacks before raising a stack overflow error.
|
|
|
|
* This can also be set on a per fiber basis. */
|
2019-02-27 21:28:43 +00:00
|
|
|
#ifndef JANET_STACK_MAX
|
2019-08-19 05:19:51 +00:00
|
|
|
#define JANET_STACK_MAX 0x7fffffff
|
2019-02-27 21:28:43 +00:00
|
|
|
#endif
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Use nanboxed values - uses 8 bytes per value instead of 12 or 16.
|
|
|
|
* To turn of nanboxing, for debugging purposes or for certain
|
|
|
|
* architectures (Nanboxing only tested on x86 and x64), comment out
|
|
|
|
* the JANET_NANBOX define.*/
|
2020-07-28 15:51:19 +00:00
|
|
|
|
|
|
|
#if defined(_M_ARM64) || defined(_M_ARM) || defined(__aarch64__)
|
|
|
|
#define JANET_NO_NANBOX
|
|
|
|
#endif
|
|
|
|
|
2018-09-30 00:01:57 +00:00
|
|
|
#ifndef JANET_NO_NANBOX
|
2018-11-18 21:43:43 +00:00
|
|
|
#ifdef JANET_32
|
|
|
|
#define JANET_NANBOX_32
|
2019-04-18 16:52:28 +00:00
|
|
|
#elif defined(__x86_64__) || defined(_WIN64)
|
|
|
|
/* We will only enable nanboxing by default on 64 bit systems
|
|
|
|
* on x86. This is mainly because the approach is tied to the
|
|
|
|
* implicit 47 bit address space. */
|
2018-11-18 21:43:43 +00:00
|
|
|
#define JANET_NANBOX_64
|
|
|
|
#endif
|
2018-09-30 00:01:57 +00:00
|
|
|
#endif
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-05-28 01:43:35 +00:00
|
|
|
/* Runtime config constants */
|
|
|
|
#ifdef JANET_NO_NANBOX
|
|
|
|
#define JANET_NANBOX_BIT 0
|
|
|
|
#else
|
2019-05-30 01:57:03 +00:00
|
|
|
#define JANET_NANBOX_BIT 0x1
|
2019-05-28 01:43:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_SINGLE_THREADED
|
2019-05-30 01:57:03 +00:00
|
|
|
#define JANET_SINGLE_THREADED_BIT 0x2
|
2019-05-28 01:43:35 +00:00
|
|
|
#else
|
|
|
|
#define JANET_SINGLE_THREADED_BIT 0
|
|
|
|
#endif
|
|
|
|
|
2019-05-30 01:57:03 +00:00
|
|
|
#define JANET_CURRENT_CONFIG_BITS \
|
|
|
|
(JANET_SINGLE_THREADED_BIT | \
|
|
|
|
JANET_NANBOX_BIT)
|
|
|
|
|
|
|
|
/* Represents the settings used to compile Janet, as well as the version */
|
2019-05-28 01:43:35 +00:00
|
|
|
typedef struct {
|
2019-05-30 01:57:03 +00:00
|
|
|
unsigned major;
|
|
|
|
unsigned minor;
|
|
|
|
unsigned patch;
|
|
|
|
unsigned bits;
|
2019-05-28 01:43:35 +00:00
|
|
|
} JanetBuildConfig;
|
|
|
|
|
2019-05-30 01:57:03 +00:00
|
|
|
/* Get config of current compilation unit. */
|
2020-09-05 14:45:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
/* C++11 syntax */
|
|
|
|
#define janet_config_current() (JanetBuildConfig { \
|
|
|
|
JANET_VERSION_MAJOR, \
|
|
|
|
JANET_VERSION_MINOR, \
|
|
|
|
JANET_VERSION_PATCH, \
|
2020-09-06 02:26:21 +00:00
|
|
|
JANET_CURRENT_CONFIG_BITS })
|
2020-09-05 14:45:34 +00:00
|
|
|
#else
|
|
|
|
/* C99 syntax */
|
2019-05-30 01:57:03 +00:00
|
|
|
#define janet_config_current() ((JanetBuildConfig){ \
|
|
|
|
JANET_VERSION_MAJOR, \
|
|
|
|
JANET_VERSION_MINOR, \
|
|
|
|
JANET_VERSION_PATCH, \
|
|
|
|
JANET_CURRENT_CONFIG_BITS })
|
2020-09-05 14:45:34 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-17 22:13:58 +00:00
|
|
|
/* Some extra includes if EV is enabled */
|
|
|
|
#ifdef JANET_EV
|
|
|
|
typedef struct JanetOSMutex JanetOSMutex;
|
|
|
|
typedef struct JanetOSRWLock JanetOSRWLock;
|
2021-04-29 17:33:49 +00:00
|
|
|
#endif
|
2019-05-30 01:57:03 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/***** END SECTION CONFIG *****/
|
|
|
|
|
|
|
|
/***** START SECTION TYPES *****/
|
|
|
|
|
2019-12-03 22:29:11 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2021-05-28 20:12:05 +00:00
|
|
|
/* Must be defined before including stdlib.h */
|
2019-12-03 22:29:11 +00:00
|
|
|
#define _CRT_RAND_S
|
|
|
|
#endif
|
2021-05-28 20:12:05 +00:00
|
|
|
|
2019-12-03 22:29:11 +00:00
|
|
|
#include <stdlib.h>
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2019-01-05 04:20:34 +00:00
|
|
|
#include <setjmp.h>
|
2019-02-21 16:22:29 +00:00
|
|
|
#include <stddef.h>
|
2019-04-27 19:45:28 +00:00
|
|
|
#include <stdio.h>
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2022-06-17 22:13:58 +00:00
|
|
|
|
|
|
|
/* What to do when out of memory */
|
|
|
|
#ifndef JANET_OUT_OF_MEMORY
|
|
|
|
#define JANET_OUT_OF_MEMORY do { fprintf(stderr, "janet out of memory\n"); exit(1); } while (0)
|
2021-08-20 22:57:23 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-30 00:53:35 +00:00
|
|
|
#ifdef JANET_BSD
|
|
|
|
int _setjmp(jmp_buf);
|
|
|
|
JANET_NO_RETURN void _longjmp(jmp_buf, int);
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Names of all of the types */
|
2019-07-14 14:58:11 +00:00
|
|
|
JANET_API extern const char *const janet_type_names[16];
|
|
|
|
JANET_API extern const char *const janet_signal_names[14];
|
|
|
|
JANET_API extern const char *const janet_status_names[16];
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2020-09-13 00:48:12 +00:00
|
|
|
/* For various IO routines, we want to use an int on posix and HANDLE on windows */
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
typedef void *JanetHandle;
|
|
|
|
#define JANET_HANDLE_NONE NULL
|
|
|
|
#else
|
|
|
|
typedef int JanetHandle;
|
|
|
|
#define JANET_HANDLE_NONE (-1)
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Fiber signals */
|
|
|
|
typedef enum {
|
|
|
|
JANET_SIGNAL_OK,
|
|
|
|
JANET_SIGNAL_ERROR,
|
|
|
|
JANET_SIGNAL_DEBUG,
|
|
|
|
JANET_SIGNAL_YIELD,
|
|
|
|
JANET_SIGNAL_USER0,
|
|
|
|
JANET_SIGNAL_USER1,
|
|
|
|
JANET_SIGNAL_USER2,
|
|
|
|
JANET_SIGNAL_USER3,
|
|
|
|
JANET_SIGNAL_USER4,
|
|
|
|
JANET_SIGNAL_USER5,
|
|
|
|
JANET_SIGNAL_USER6,
|
|
|
|
JANET_SIGNAL_USER7,
|
|
|
|
JANET_SIGNAL_USER8,
|
|
|
|
JANET_SIGNAL_USER9
|
|
|
|
} JanetSignal;
|
|
|
|
|
2020-04-17 21:27:02 +00:00
|
|
|
#define JANET_SIGNAL_EVENT JANET_SIGNAL_USER9
|
2021-07-25 01:30:36 +00:00
|
|
|
#define JANET_SIGNAL_INTERRUPT JANET_SIGNAL_USER8
|
2020-04-17 21:27:02 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Fiber statuses - mostly corresponds to signals. */
|
|
|
|
typedef enum {
|
|
|
|
JANET_STATUS_DEAD,
|
|
|
|
JANET_STATUS_ERROR,
|
|
|
|
JANET_STATUS_DEBUG,
|
|
|
|
JANET_STATUS_PENDING,
|
|
|
|
JANET_STATUS_USER0,
|
|
|
|
JANET_STATUS_USER1,
|
|
|
|
JANET_STATUS_USER2,
|
|
|
|
JANET_STATUS_USER3,
|
|
|
|
JANET_STATUS_USER4,
|
|
|
|
JANET_STATUS_USER5,
|
|
|
|
JANET_STATUS_USER6,
|
|
|
|
JANET_STATUS_USER7,
|
|
|
|
JANET_STATUS_USER8,
|
|
|
|
JANET_STATUS_USER9,
|
|
|
|
JANET_STATUS_NEW,
|
|
|
|
JANET_STATUS_ALIVE
|
|
|
|
} JanetFiberStatus;
|
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
/* For encapsulating all thread-local Janet state (except natives) */
|
|
|
|
typedef struct JanetVM JanetVM;
|
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
/* Use type punning for GC objects */
|
|
|
|
typedef struct JanetGCObject JanetGCObject;
|
|
|
|
|
|
|
|
/* All of the primary Janet GCed types */
|
2018-09-06 02:18:42 +00:00
|
|
|
typedef struct JanetFunction JanetFunction;
|
|
|
|
typedef struct JanetArray JanetArray;
|
|
|
|
typedef struct JanetBuffer JanetBuffer;
|
|
|
|
typedef struct JanetTable JanetTable;
|
|
|
|
typedef struct JanetFiber JanetFiber;
|
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
/* Prefixed Janet types */
|
|
|
|
typedef struct JanetTupleHead JanetTupleHead;
|
|
|
|
typedef struct JanetStructHead JanetStructHead;
|
|
|
|
typedef struct JanetStringHead JanetStringHead;
|
|
|
|
typedef struct JanetAbstractHead JanetAbstractHead;
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Other structs */
|
|
|
|
typedef struct JanetFuncDef JanetFuncDef;
|
|
|
|
typedef struct JanetFuncEnv JanetFuncEnv;
|
|
|
|
typedef struct JanetKV JanetKV;
|
|
|
|
typedef struct JanetStackFrame JanetStackFrame;
|
|
|
|
typedef struct JanetAbstractType JanetAbstractType;
|
|
|
|
typedef struct JanetReg JanetReg;
|
2021-07-25 18:03:01 +00:00
|
|
|
typedef struct JanetRegExt JanetRegExt;
|
2019-02-06 00:43:41 +00:00
|
|
|
typedef struct JanetMethod JanetMethod;
|
2018-09-06 02:18:42 +00:00
|
|
|
typedef struct JanetSourceMapping JanetSourceMapping;
|
2019-01-06 01:09:03 +00:00
|
|
|
typedef struct JanetView JanetView;
|
|
|
|
typedef struct JanetByteView JanetByteView;
|
|
|
|
typedef struct JanetDictView JanetDictView;
|
|
|
|
typedef struct JanetRange JanetRange;
|
2019-11-08 23:35:27 +00:00
|
|
|
typedef struct JanetRNG JanetRNG;
|
2020-04-25 20:41:23 +00:00
|
|
|
|
2020-05-11 01:14:47 +00:00
|
|
|
/* Basic types for all Janet Values */
|
|
|
|
typedef enum JanetType {
|
|
|
|
JANET_NUMBER,
|
|
|
|
JANET_NIL,
|
|
|
|
JANET_BOOLEAN,
|
|
|
|
JANET_FIBER,
|
|
|
|
JANET_STRING,
|
|
|
|
JANET_SYMBOL,
|
|
|
|
JANET_KEYWORD,
|
|
|
|
JANET_ARRAY,
|
|
|
|
JANET_TUPLE,
|
|
|
|
JANET_TABLE,
|
|
|
|
JANET_STRUCT,
|
|
|
|
JANET_BUFFER,
|
|
|
|
JANET_FUNCTION,
|
|
|
|
JANET_CFUNCTION,
|
|
|
|
JANET_ABSTRACT,
|
|
|
|
JANET_POINTER
|
|
|
|
} JanetType;
|
|
|
|
|
2020-04-25 20:41:23 +00:00
|
|
|
/* Recursive type (Janet) */
|
|
|
|
#ifdef JANET_NANBOX_64
|
|
|
|
typedef union Janet Janet;
|
|
|
|
union Janet {
|
|
|
|
uint64_t u64;
|
|
|
|
int64_t i64;
|
|
|
|
double number;
|
|
|
|
void *pointer;
|
|
|
|
};
|
|
|
|
#elif defined(JANET_NANBOX_32)
|
|
|
|
typedef union Janet Janet;
|
|
|
|
union Janet {
|
|
|
|
struct {
|
|
|
|
#ifdef JANET_BIG_ENDIAN
|
|
|
|
uint32_t type;
|
|
|
|
union {
|
|
|
|
int32_t integer;
|
|
|
|
void *pointer;
|
|
|
|
} payload;
|
|
|
|
#else
|
|
|
|
union {
|
|
|
|
int32_t integer;
|
|
|
|
void *pointer;
|
|
|
|
} payload;
|
|
|
|
uint32_t type;
|
|
|
|
#endif
|
|
|
|
} tagged;
|
|
|
|
double number;
|
|
|
|
uint64_t u64;
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
typedef struct Janet Janet;
|
|
|
|
struct Janet {
|
|
|
|
union {
|
|
|
|
uint64_t u64;
|
|
|
|
double number;
|
|
|
|
int32_t integer;
|
|
|
|
void *pointer;
|
|
|
|
const void *cpointer;
|
|
|
|
} as;
|
|
|
|
JanetType type;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* C functions */
|
2019-02-20 01:51:34 +00:00
|
|
|
typedef Janet(*JanetCFunction)(int32_t argc, Janet *argv);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-12-10 02:05:53 +00:00
|
|
|
/* String and other aliased pointer types */
|
|
|
|
typedef const uint8_t *JanetString;
|
|
|
|
typedef const uint8_t *JanetSymbol;
|
|
|
|
typedef const uint8_t *JanetKeyword;
|
|
|
|
typedef const Janet *JanetTuple;
|
|
|
|
typedef const JanetKV *JanetStruct;
|
|
|
|
typedef void *JanetAbstract;
|
|
|
|
|
2019-03-13 18:50:25 +00:00
|
|
|
#define JANET_COUNT_TYPES (JANET_POINTER + 1)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Type flags */
|
|
|
|
#define JANET_TFLAG_NIL (1 << JANET_NIL)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define JANET_TFLAG_BOOLEAN (1 << JANET_BOOLEAN)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_TFLAG_FIBER (1 << JANET_FIBER)
|
2018-12-27 18:05:29 +00:00
|
|
|
#define JANET_TFLAG_NUMBER (1 << JANET_NUMBER)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_TFLAG_STRING (1 << JANET_STRING)
|
|
|
|
#define JANET_TFLAG_SYMBOL (1 << JANET_SYMBOL)
|
2019-01-03 00:41:07 +00:00
|
|
|
#define JANET_TFLAG_KEYWORD (1 << JANET_KEYWORD)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_TFLAG_ARRAY (1 << JANET_ARRAY)
|
|
|
|
#define JANET_TFLAG_TUPLE (1 << JANET_TUPLE)
|
|
|
|
#define JANET_TFLAG_TABLE (1 << JANET_TABLE)
|
|
|
|
#define JANET_TFLAG_STRUCT (1 << JANET_STRUCT)
|
|
|
|
#define JANET_TFLAG_BUFFER (1 << JANET_BUFFER)
|
|
|
|
#define JANET_TFLAG_FUNCTION (1 << JANET_FUNCTION)
|
|
|
|
#define JANET_TFLAG_CFUNCTION (1 << JANET_CFUNCTION)
|
|
|
|
#define JANET_TFLAG_ABSTRACT (1 << JANET_ABSTRACT)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define JANET_TFLAG_POINTER (1 << JANET_POINTER)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-01-03 03:08:51 +00:00
|
|
|
#define JANET_TFLAG_BYTES (JANET_TFLAG_STRING | JANET_TFLAG_SYMBOL | JANET_TFLAG_BUFFER | JANET_TFLAG_KEYWORD)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_TFLAG_INDEXED (JANET_TFLAG_ARRAY | JANET_TFLAG_TUPLE)
|
|
|
|
#define JANET_TFLAG_DICTIONARY (JANET_TFLAG_TABLE | JANET_TFLAG_STRUCT)
|
|
|
|
#define JANET_TFLAG_LENGTHABLE (JANET_TFLAG_BYTES | JANET_TFLAG_INDEXED | JANET_TFLAG_DICTIONARY)
|
2019-05-21 19:35:39 +00:00
|
|
|
#define JANET_TFLAG_CALLABLE (JANET_TFLAG_FUNCTION | JANET_TFLAG_CFUNCTION | \
|
|
|
|
JANET_TFLAG_LENGTHABLE | JANET_TFLAG_ABSTRACT)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2020-07-03 18:47:48 +00:00
|
|
|
/* Event Loop Types */
|
|
|
|
#ifdef JANET_EV
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
#define JANET_STREAM_CLOSED 0x1
|
|
|
|
#define JANET_STREAM_SOCKET 0x2
|
2020-11-14 20:55:26 +00:00
|
|
|
#define JANET_STREAM_IOCP 0x4
|
2020-11-14 20:28:10 +00:00
|
|
|
#define JANET_STREAM_READABLE 0x200
|
|
|
|
#define JANET_STREAM_WRITABLE 0x400
|
|
|
|
#define JANET_STREAM_ACCEPTABLE 0x800
|
|
|
|
#define JANET_STREAM_UDPSERVER 0x1000
|
2020-07-03 18:47:48 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JANET_ASYNC_EVENT_INIT,
|
|
|
|
JANET_ASYNC_EVENT_MARK,
|
|
|
|
JANET_ASYNC_EVENT_DEINIT,
|
|
|
|
JANET_ASYNC_EVENT_CLOSE,
|
2020-11-14 20:28:10 +00:00
|
|
|
JANET_ASYNC_EVENT_ERR,
|
|
|
|
JANET_ASYNC_EVENT_HUP,
|
2020-07-03 18:47:48 +00:00
|
|
|
JANET_ASYNC_EVENT_READ,
|
|
|
|
JANET_ASYNC_EVENT_WRITE,
|
2020-11-17 00:46:59 +00:00
|
|
|
JANET_ASYNC_EVENT_CANCEL,
|
2020-11-08 16:38:28 +00:00
|
|
|
JANET_ASYNC_EVENT_COMPLETE, /* Used on windows for IOCP */
|
|
|
|
JANET_ASYNC_EVENT_USER
|
2020-07-03 18:47:48 +00:00
|
|
|
} JanetAsyncEvent;
|
|
|
|
|
|
|
|
#define JANET_ASYNC_LISTEN_READ (1 << JANET_ASYNC_EVENT_READ)
|
|
|
|
#define JANET_ASYNC_LISTEN_WRITE (1 << JANET_ASYNC_EVENT_WRITE)
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JANET_ASYNC_STATUS_NOT_DONE,
|
|
|
|
JANET_ASYNC_STATUS_DONE
|
|
|
|
} JanetAsyncStatus;
|
|
|
|
|
|
|
|
/* Typedefs */
|
|
|
|
typedef struct JanetListenerState JanetListenerState;
|
2020-11-14 20:28:10 +00:00
|
|
|
typedef struct JanetStream JanetStream;
|
2020-07-03 18:47:48 +00:00
|
|
|
typedef JanetAsyncStatus(*JanetListener)(JanetListenerState *state, JanetAsyncEvent event);
|
|
|
|
|
|
|
|
/* Wrapper around file descriptors and HANDLEs that can be polled. */
|
2020-11-14 20:28:10 +00:00
|
|
|
struct JanetStream {
|
2020-10-04 17:46:15 +00:00
|
|
|
JanetHandle handle;
|
2020-07-03 18:47:48 +00:00
|
|
|
uint32_t flags;
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Linked list of all in-flight IO routines for this stream */
|
2020-07-03 18:47:48 +00:00
|
|
|
JanetListenerState *state;
|
2020-11-14 20:28:10 +00:00
|
|
|
const void *methods; /* Methods for this stream */
|
|
|
|
/* internal - used to disallow multiple concurrent reads / writes on the same stream.
|
2020-10-04 17:46:15 +00:00
|
|
|
* this constraint may be lifted later but allowing such would require more internal book keeping
|
2020-11-14 20:28:10 +00:00
|
|
|
* for some implementations. You can read and write at the same time on the same stream, though. */
|
2020-07-03 18:47:48 +00:00
|
|
|
int _mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Interface for state machine based event loop */
|
|
|
|
struct JanetListenerState {
|
|
|
|
JanetListener machine;
|
|
|
|
JanetFiber *fiber;
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream;
|
2020-10-04 17:46:15 +00:00
|
|
|
void *event; /* Used to pass data from asynchronous IO event. Contents depend on both
|
|
|
|
implementation of the event loop and the particular event. */
|
2020-11-08 16:38:28 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
void *tag; /* Used to associate listeners with an overlapped structure */
|
2020-11-09 00:56:13 +00:00
|
|
|
int bytes; /* Used to track how many bytes were transfered. */
|
2020-11-08 16:38:28 +00:00
|
|
|
#endif
|
2020-07-03 18:47:48 +00:00
|
|
|
/* internal */
|
2020-11-19 01:31:20 +00:00
|
|
|
size_t _index;
|
2020-07-03 18:47:48 +00:00
|
|
|
int _mask;
|
|
|
|
JanetListenerState *_next;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* We provide three possible implementations of Janets. The preferred
|
2018-11-18 21:43:43 +00:00
|
|
|
* nanboxing approach, for 32 or 64 bits, and the standard C version. Code in the rest of the
|
2018-09-06 02:18:42 +00:00
|
|
|
* application must interact through exposed interface. */
|
|
|
|
|
|
|
|
/* Required interface for Janet */
|
|
|
|
/* wrap and unwrap for all types */
|
|
|
|
/* Get type quickly */
|
|
|
|
/* Check against type quickly */
|
|
|
|
/* Small footprint */
|
|
|
|
/* 32 bit integer support */
|
|
|
|
|
|
|
|
/* janet_type(x)
|
|
|
|
* janet_checktype(x, t)
|
|
|
|
* janet_wrap_##TYPE(x)
|
|
|
|
* janet_unwrap_##TYPE(x)
|
|
|
|
* janet_truthy(x)
|
|
|
|
* janet_memclear(p, n) - clear memory for hash tables to nils
|
|
|
|
* janet_u64(x) - get 64 bits of payload for hashing
|
|
|
|
*/
|
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
/***** START SECTION NON-C API *****/
|
|
|
|
|
|
|
|
/* Some janet types use offset tricks to make operations easier in C. For
|
|
|
|
* external bindings, we should prefer using the Head structs directly, and
|
|
|
|
* use the host language to add sugar around the manipulation of the Janet types. */
|
|
|
|
|
|
|
|
JANET_API JanetStructHead *janet_struct_head(const JanetKV *st);
|
|
|
|
JANET_API JanetAbstractHead *janet_abstract_head(const void *abstract);
|
|
|
|
JANET_API JanetStringHead *janet_string_head(const uint8_t *s);
|
|
|
|
JANET_API JanetTupleHead *janet_tuple_head(const Janet *tuple);
|
|
|
|
|
|
|
|
/* Some language bindings won't have access to the macro versions. */
|
|
|
|
|
|
|
|
JANET_API JanetType janet_type(Janet x);
|
|
|
|
JANET_API int janet_checktype(Janet x, JanetType type);
|
|
|
|
JANET_API int janet_checktypes(Janet x, int typeflags);
|
|
|
|
JANET_API int janet_truthy(Janet x);
|
|
|
|
|
|
|
|
JANET_API const JanetKV *janet_unwrap_struct(Janet x);
|
|
|
|
JANET_API const Janet *janet_unwrap_tuple(Janet x);
|
|
|
|
JANET_API JanetFiber *janet_unwrap_fiber(Janet x);
|
|
|
|
JANET_API JanetArray *janet_unwrap_array(Janet x);
|
|
|
|
JANET_API JanetTable *janet_unwrap_table(Janet x);
|
|
|
|
JANET_API JanetBuffer *janet_unwrap_buffer(Janet x);
|
|
|
|
JANET_API const uint8_t *janet_unwrap_string(Janet x);
|
|
|
|
JANET_API const uint8_t *janet_unwrap_symbol(Janet x);
|
|
|
|
JANET_API const uint8_t *janet_unwrap_keyword(Janet x);
|
|
|
|
JANET_API void *janet_unwrap_abstract(Janet x);
|
|
|
|
JANET_API void *janet_unwrap_pointer(Janet x);
|
|
|
|
JANET_API JanetFunction *janet_unwrap_function(Janet x);
|
|
|
|
JANET_API JanetCFunction janet_unwrap_cfunction(Janet x);
|
|
|
|
JANET_API int janet_unwrap_boolean(Janet x);
|
|
|
|
JANET_API double janet_unwrap_number(Janet x);
|
|
|
|
JANET_API int32_t janet_unwrap_integer(Janet x);
|
|
|
|
|
|
|
|
JANET_API Janet janet_wrap_nil(void);
|
|
|
|
JANET_API Janet janet_wrap_number(double x);
|
|
|
|
JANET_API Janet janet_wrap_true(void);
|
|
|
|
JANET_API Janet janet_wrap_false(void);
|
|
|
|
JANET_API Janet janet_wrap_boolean(int x);
|
|
|
|
JANET_API Janet janet_wrap_string(const uint8_t *x);
|
|
|
|
JANET_API Janet janet_wrap_symbol(const uint8_t *x);
|
|
|
|
JANET_API Janet janet_wrap_keyword(const uint8_t *x);
|
|
|
|
JANET_API Janet janet_wrap_array(JanetArray *x);
|
|
|
|
JANET_API Janet janet_wrap_tuple(const Janet *x);
|
|
|
|
JANET_API Janet janet_wrap_struct(const JanetKV *x);
|
|
|
|
JANET_API Janet janet_wrap_fiber(JanetFiber *x);
|
|
|
|
JANET_API Janet janet_wrap_buffer(JanetBuffer *x);
|
|
|
|
JANET_API Janet janet_wrap_function(JanetFunction *x);
|
|
|
|
JANET_API Janet janet_wrap_cfunction(JanetCFunction x);
|
|
|
|
JANET_API Janet janet_wrap_table(JanetTable *x);
|
|
|
|
JANET_API Janet janet_wrap_abstract(void *x);
|
|
|
|
JANET_API Janet janet_wrap_pointer(void *x);
|
|
|
|
JANET_API Janet janet_wrap_integer(int32_t x);
|
|
|
|
|
|
|
|
/***** END SECTION NON-C API *****/
|
|
|
|
|
2018-11-18 21:43:43 +00:00
|
|
|
#ifdef JANET_NANBOX_64
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define janet_u64(x) ((x).u64)
|
|
|
|
|
|
|
|
#define JANET_NANBOX_TAGBITS 0xFFFF800000000000llu
|
|
|
|
#define JANET_NANBOX_PAYLOADBITS 0x00007FFFFFFFFFFFllu
|
2018-11-18 21:43:43 +00:00
|
|
|
#define janet_nanbox_lowtag(type) ((uint64_t)(type) | 0x1FFF0)
|
|
|
|
#define janet_nanbox_tag(type) (janet_nanbox_lowtag(type) << 47)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_type(x) \
|
2018-12-27 18:05:29 +00:00
|
|
|
(isnan((x).number) \
|
2020-09-04 22:05:28 +00:00
|
|
|
? (JanetType) (((x).u64 >> 47) & 0xF) \
|
2018-12-27 18:05:29 +00:00
|
|
|
: JANET_NUMBER)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
#define janet_nanbox_checkauxtype(x, type) \
|
|
|
|
(((x).u64 & JANET_NANBOX_TAGBITS) == janet_nanbox_tag((type)))
|
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
#define janet_nanbox_isnumber(x) \
|
2020-09-06 19:58:30 +00:00
|
|
|
(!isnan((x).number) || ((((x).u64 >> 47) & 0xF) == JANET_NUMBER))
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
#define janet_checktype(x, t) \
|
2018-12-27 18:05:29 +00:00
|
|
|
(((t) == JANET_NUMBER) \
|
|
|
|
? janet_nanbox_isnumber(x) \
|
2018-09-06 02:18:42 +00:00
|
|
|
: janet_nanbox_checkauxtype((x), (t)))
|
|
|
|
|
|
|
|
JANET_API void *janet_nanbox_to_pointer(Janet x);
|
|
|
|
JANET_API Janet janet_nanbox_from_pointer(void *p, uint64_t tagmask);
|
|
|
|
JANET_API Janet janet_nanbox_from_cpointer(const void *p, uint64_t tagmask);
|
|
|
|
JANET_API Janet janet_nanbox_from_double(double d);
|
|
|
|
JANET_API Janet janet_nanbox_from_bits(uint64_t bits);
|
|
|
|
|
|
|
|
#define janet_truthy(x) \
|
2019-03-13 18:50:25 +00:00
|
|
|
(!janet_checktype((x), JANET_NIL) && \
|
|
|
|
(!janet_checktype((x), JANET_BOOLEAN) || ((x).u64 & 0x1)))
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
#define janet_nanbox_from_payload(t, p) \
|
|
|
|
janet_nanbox_from_bits(janet_nanbox_tag(t) | (p))
|
|
|
|
|
|
|
|
#define janet_nanbox_wrap_(p, t) \
|
|
|
|
janet_nanbox_from_pointer((p), janet_nanbox_tag(t))
|
|
|
|
|
|
|
|
#define janet_nanbox_wrap_c(p, t) \
|
|
|
|
janet_nanbox_from_cpointer((p), janet_nanbox_tag(t))
|
|
|
|
|
|
|
|
/* Wrap the simple types */
|
|
|
|
#define janet_wrap_nil() janet_nanbox_from_payload(JANET_NIL, 1)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_wrap_true() janet_nanbox_from_payload(JANET_BOOLEAN, 1)
|
|
|
|
#define janet_wrap_false() janet_nanbox_from_payload(JANET_BOOLEAN, 0)
|
|
|
|
#define janet_wrap_boolean(b) janet_nanbox_from_payload(JANET_BOOLEAN, !!(b))
|
2018-12-27 18:05:29 +00:00
|
|
|
#define janet_wrap_number(r) janet_nanbox_from_double(r)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Unwrap the simple types */
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_unwrap_boolean(x) ((x).u64 & 0x1)
|
2018-12-27 18:05:29 +00:00
|
|
|
#define janet_unwrap_number(x) ((x).number)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Wrap the pointer types */
|
|
|
|
#define janet_wrap_struct(s) janet_nanbox_wrap_c((s), JANET_STRUCT)
|
|
|
|
#define janet_wrap_tuple(s) janet_nanbox_wrap_c((s), JANET_TUPLE)
|
|
|
|
#define janet_wrap_fiber(s) janet_nanbox_wrap_((s), JANET_FIBER)
|
|
|
|
#define janet_wrap_array(s) janet_nanbox_wrap_((s), JANET_ARRAY)
|
|
|
|
#define janet_wrap_table(s) janet_nanbox_wrap_((s), JANET_TABLE)
|
|
|
|
#define janet_wrap_buffer(s) janet_nanbox_wrap_((s), JANET_BUFFER)
|
|
|
|
#define janet_wrap_string(s) janet_nanbox_wrap_c((s), JANET_STRING)
|
|
|
|
#define janet_wrap_symbol(s) janet_nanbox_wrap_c((s), JANET_SYMBOL)
|
2019-01-03 00:41:07 +00:00
|
|
|
#define janet_wrap_keyword(s) janet_nanbox_wrap_c((s), JANET_KEYWORD)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_wrap_abstract(s) janet_nanbox_wrap_((s), JANET_ABSTRACT)
|
|
|
|
#define janet_wrap_function(s) janet_nanbox_wrap_((s), JANET_FUNCTION)
|
|
|
|
#define janet_wrap_cfunction(s) janet_nanbox_wrap_((s), JANET_CFUNCTION)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_wrap_pointer(s) janet_nanbox_wrap_((s), JANET_POINTER)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Unwrap the pointer types */
|
|
|
|
#define janet_unwrap_struct(x) ((const JanetKV *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_tuple(x) ((const Janet *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_fiber(x) ((JanetFiber *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_array(x) ((JanetArray *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_table(x) ((JanetTable *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_buffer(x) ((JanetBuffer *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_string(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_symbol(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
|
2019-01-03 00:41:07 +00:00
|
|
|
#define janet_unwrap_keyword(x) ((const uint8_t *)janet_nanbox_to_pointer(x))
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_unwrap_abstract(x) (janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_pointer(x) (janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_function(x) ((JanetFunction *)janet_nanbox_to_pointer(x))
|
|
|
|
#define janet_unwrap_cfunction(x) ((JanetCFunction)janet_nanbox_to_pointer(x))
|
|
|
|
|
2018-11-18 21:43:43 +00:00
|
|
|
#elif defined(JANET_NANBOX_32)
|
|
|
|
|
|
|
|
#define JANET_DOUBLE_OFFSET 0xFFFF
|
|
|
|
|
|
|
|
#define janet_u64(x) ((x).u64)
|
2020-09-04 22:05:28 +00:00
|
|
|
#define janet_type(x) (((x).tagged.type < JANET_DOUBLE_OFFSET) ? (JanetType)((x).tagged.type) : JANET_NUMBER)
|
2018-12-30 19:23:52 +00:00
|
|
|
#define janet_checktype(x, t) ((t) == JANET_NUMBER \
|
|
|
|
? (x).tagged.type >= JANET_DOUBLE_OFFSET \
|
|
|
|
: (x).tagged.type == (t))
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_truthy(x) \
|
|
|
|
((x).tagged.type != JANET_NIL && ((x).tagged.type != JANET_BOOLEAN || ((x).tagged.payload.integer & 0x1)))
|
2018-11-18 21:43:43 +00:00
|
|
|
|
|
|
|
JANET_API Janet janet_nanbox32_from_tagi(uint32_t tag, int32_t integer);
|
|
|
|
JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer);
|
|
|
|
|
|
|
|
#define janet_wrap_nil() janet_nanbox32_from_tagi(JANET_NIL, 0)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_wrap_true() janet_nanbox32_from_tagi(JANET_BOOLEAN, 1)
|
|
|
|
#define janet_wrap_false() janet_nanbox32_from_tagi(JANET_BOOLEAN, 0)
|
|
|
|
#define janet_wrap_boolean(b) janet_nanbox32_from_tagi(JANET_BOOLEAN, !!(b))
|
2018-11-18 21:43:43 +00:00
|
|
|
|
|
|
|
/* Wrap the pointer types */
|
|
|
|
#define janet_wrap_struct(s) janet_nanbox32_from_tagp(JANET_STRUCT, (void *)(s))
|
|
|
|
#define janet_wrap_tuple(s) janet_nanbox32_from_tagp(JANET_TUPLE, (void *)(s))
|
|
|
|
#define janet_wrap_fiber(s) janet_nanbox32_from_tagp(JANET_FIBER, (void *)(s))
|
|
|
|
#define janet_wrap_array(s) janet_nanbox32_from_tagp(JANET_ARRAY, (void *)(s))
|
|
|
|
#define janet_wrap_table(s) janet_nanbox32_from_tagp(JANET_TABLE, (void *)(s))
|
|
|
|
#define janet_wrap_buffer(s) janet_nanbox32_from_tagp(JANET_BUFFER, (void *)(s))
|
|
|
|
#define janet_wrap_string(s) janet_nanbox32_from_tagp(JANET_STRING, (void *)(s))
|
|
|
|
#define janet_wrap_symbol(s) janet_nanbox32_from_tagp(JANET_SYMBOL, (void *)(s))
|
2019-01-03 03:08:51 +00:00
|
|
|
#define janet_wrap_keyword(s) janet_nanbox32_from_tagp(JANET_KEYWORD, (void *)(s))
|
2018-11-18 21:43:43 +00:00
|
|
|
#define janet_wrap_abstract(s) janet_nanbox32_from_tagp(JANET_ABSTRACT, (void *)(s))
|
|
|
|
#define janet_wrap_function(s) janet_nanbox32_from_tagp(JANET_FUNCTION, (void *)(s))
|
|
|
|
#define janet_wrap_cfunction(s) janet_nanbox32_from_tagp(JANET_CFUNCTION, (void *)(s))
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_wrap_pointer(s) janet_nanbox32_from_tagp(JANET_POINTER, (void *)(s))
|
2018-11-18 21:43:43 +00:00
|
|
|
|
|
|
|
#define janet_unwrap_struct(x) ((const JanetKV *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_tuple(x) ((const Janet *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_fiber(x) ((JanetFiber *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_array(x) ((JanetArray *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_table(x) ((JanetTable *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_buffer(x) ((JanetBuffer *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_string(x) ((const uint8_t *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_symbol(x) ((const uint8_t *)(x).tagged.payload.pointer)
|
2019-01-03 00:41:07 +00:00
|
|
|
#define janet_unwrap_keyword(x) ((const uint8_t *)(x).tagged.payload.pointer)
|
2018-11-18 21:43:43 +00:00
|
|
|
#define janet_unwrap_abstract(x) ((x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_pointer(x) ((x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_function(x) ((JanetFunction *)(x).tagged.payload.pointer)
|
|
|
|
#define janet_unwrap_cfunction(x) ((JanetCFunction)(x).tagged.payload.pointer)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_unwrap_boolean(x) ((x).tagged.payload.integer)
|
2018-11-18 21:43:43 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define janet_u64(x) ((x).as.u64)
|
|
|
|
#define janet_type(x) ((x).type)
|
|
|
|
#define janet_checktype(x, t) ((x).type == (t))
|
|
|
|
#define janet_truthy(x) \
|
2020-03-30 17:19:51 +00:00
|
|
|
((x).type != JANET_NIL && ((x).type != JANET_BOOLEAN || ((x).as.u64 & 0x1)))
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
#define janet_unwrap_struct(x) ((const JanetKV *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_tuple(x) ((const Janet *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_fiber(x) ((JanetFiber *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_array(x) ((JanetArray *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_table(x) ((JanetTable *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_buffer(x) ((JanetBuffer *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_string(x) ((const uint8_t *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_symbol(x) ((const uint8_t *)(x).as.pointer)
|
2019-01-03 00:41:07 +00:00
|
|
|
#define janet_unwrap_keyword(x) ((const uint8_t *)(x).as.pointer)
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_unwrap_abstract(x) ((x).as.pointer)
|
|
|
|
#define janet_unwrap_pointer(x) ((x).as.pointer)
|
|
|
|
#define janet_unwrap_function(x) ((JanetFunction *)(x).as.pointer)
|
|
|
|
#define janet_unwrap_cfunction(x) ((JanetCFunction)(x).as.pointer)
|
2019-03-13 18:50:25 +00:00
|
|
|
#define janet_unwrap_boolean(x) ((x).as.u64 & 0x1)
|
2018-12-27 18:05:29 +00:00
|
|
|
#define janet_unwrap_number(x) ((x).as.number)
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* End of tagged union implementation */
|
|
|
|
#endif
|
|
|
|
|
2018-12-27 18:05:29 +00:00
|
|
|
JANET_API int janet_checkint(Janet x);
|
|
|
|
JANET_API int janet_checkint64(Janet x);
|
2022-06-06 23:54:17 +00:00
|
|
|
JANET_API int janet_checkuint64(Janet x);
|
2019-03-08 05:41:26 +00:00
|
|
|
JANET_API int janet_checksize(Janet x);
|
2019-12-14 17:31:46 +00:00
|
|
|
JANET_API JanetAbstract janet_checkabstract(Janet x, const JanetAbstractType *at);
|
2020-02-27 06:16:54 +00:00
|
|
|
#define janet_checkintrange(x) ((x) >= INT32_MIN && (x) <= INT32_MAX && (x) == (int32_t)(x))
|
2020-06-27 16:23:47 +00:00
|
|
|
#define janet_checkint64range(x) ((x) >= JANET_INTMIN_DOUBLE && (x) <= JANET_INTMAX_DOUBLE && (x) == (int64_t)(x))
|
2018-12-27 18:05:29 +00:00
|
|
|
#define janet_unwrap_integer(x) ((int32_t) janet_unwrap_number(x))
|
|
|
|
#define janet_wrap_integer(x) janet_wrap_number((int32_t)(x))
|
|
|
|
|
2019-01-04 03:46:25 +00:00
|
|
|
#define janet_checktypes(x, tps) ((1 << janet_type(x)) & (tps))
|
2019-01-03 01:50:31 +00:00
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
/* GC Object type pun. The lower 16 bits of flags are reserved for the garbage collector,
|
|
|
|
* but the upper 16 can be used per type for custom flags. The current collector is a linked
|
|
|
|
* list of blocks, which is naive but works. */
|
|
|
|
struct JanetGCObject {
|
|
|
|
int32_t flags;
|
2021-08-20 01:56:48 +00:00
|
|
|
union {
|
|
|
|
JanetGCObject *next;
|
|
|
|
int32_t refcount; /* For threaded abstract types */
|
2021-10-22 00:48:37 +00:00
|
|
|
} data;
|
2019-02-21 16:22:29 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* A lightweight green thread in janet. Does not correspond to
|
|
|
|
* operating system threads. */
|
|
|
|
struct JanetFiber {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc; /* GC Object stuff */
|
|
|
|
int32_t flags; /* More flags */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t frame; /* Index of the stack frame */
|
|
|
|
int32_t stackstart; /* Beginning of next args */
|
|
|
|
int32_t stacktop; /* Top of stack. Where values are pushed and popped from. */
|
2020-07-03 18:47:48 +00:00
|
|
|
int32_t capacity; /* How big is the stack memory */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t maxstack; /* Arbitrary defined limit for stack overflow */
|
2019-04-16 19:41:45 +00:00
|
|
|
JanetTable *env; /* Dynamic bindings table (usually current environment). */
|
2020-07-03 18:47:48 +00:00
|
|
|
Janet *data; /* Dynamically resized stack memory */
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetFiber *child; /* Keep linked list of fibers for restarting pending fibers */
|
2021-01-03 22:17:36 +00:00
|
|
|
Janet last_value; /* Last returned value from a fiber */
|
2020-07-03 18:47:48 +00:00
|
|
|
#ifdef JANET_EV
|
2021-01-10 02:23:06 +00:00
|
|
|
/* These fields are only relevant for fibers that are used as "root fibers" -
|
|
|
|
* that is, fibers that are scheduled on the event loop and behave much like threads
|
|
|
|
* in a multi-tasking system. It would be possible to move these fields to a new
|
|
|
|
* type, say "JanetTask", that as separate from fibers to save a bit of space. */
|
2020-08-08 12:51:46 +00:00
|
|
|
JanetListenerState *waiting;
|
2020-08-09 05:20:27 +00:00
|
|
|
uint32_t sched_id; /* Increment everytime fiber is scheduled by event loop */
|
2021-01-13 03:35:28 +00:00
|
|
|
void *supervisor_channel; /* Channel to push self to when complete */
|
2020-07-03 18:47:48 +00:00
|
|
|
#endif
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Mark if a stack frame is a tail call for debugging */
|
|
|
|
#define JANET_STACKFRAME_TAILCALL 1
|
|
|
|
|
2019-02-01 16:56:25 +00:00
|
|
|
/* Mark if a stack frame is an entrance frame */
|
|
|
|
#define JANET_STACKFRAME_ENTRANCE 2
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* A stack frame on the fiber. Is stored along with the stack values. */
|
|
|
|
struct JanetStackFrame {
|
|
|
|
JanetFunction *func;
|
|
|
|
uint32_t *pc;
|
|
|
|
JanetFuncEnv *env;
|
|
|
|
int32_t prevframe;
|
|
|
|
int32_t flags;
|
|
|
|
};
|
|
|
|
|
2020-04-06 17:41:56 +00:00
|
|
|
/* Number of Janets a frame takes up in the stack
|
|
|
|
* Should be constant across architectures */
|
|
|
|
#define JANET_FRAME_SIZE 4
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* A dynamic array type. */
|
|
|
|
struct JanetArray {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t count;
|
|
|
|
int32_t capacity;
|
2019-02-21 16:22:29 +00:00
|
|
|
Janet *data;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* A byte buffer type. Used as a mutable string or string builder. */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetBuffer {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t count;
|
|
|
|
int32_t capacity;
|
2019-02-21 16:22:29 +00:00
|
|
|
uint8_t *data;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A mutable associative data type. Backed by a hashtable. */
|
|
|
|
struct JanetTable {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t count;
|
|
|
|
int32_t capacity;
|
|
|
|
int32_t deleted;
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetKV *data;
|
|
|
|
JanetTable *proto;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A key value pair in a struct or table */
|
|
|
|
struct JanetKV {
|
|
|
|
Janet key;
|
|
|
|
Janet value;
|
|
|
|
};
|
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
/* Prefix for a tuple */
|
|
|
|
struct JanetTupleHead {
|
|
|
|
JanetGCObject gc;
|
|
|
|
int32_t length;
|
|
|
|
int32_t hash;
|
2019-09-22 22:18:28 +00:00
|
|
|
int32_t sm_line;
|
|
|
|
int32_t sm_column;
|
2019-02-21 16:22:29 +00:00
|
|
|
const Janet data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Prefix for a struct */
|
|
|
|
struct JanetStructHead {
|
|
|
|
JanetGCObject gc;
|
|
|
|
int32_t length;
|
|
|
|
int32_t hash;
|
|
|
|
int32_t capacity;
|
2021-05-29 02:37:13 +00:00
|
|
|
const JanetKV *proto;
|
2019-02-21 16:22:29 +00:00
|
|
|
const JanetKV data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Prefix for a string */
|
|
|
|
struct JanetStringHead {
|
|
|
|
JanetGCObject gc;
|
|
|
|
int32_t length;
|
|
|
|
int32_t hash;
|
|
|
|
const uint8_t data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Prefix for an abstract value */
|
|
|
|
struct JanetAbstractHead {
|
|
|
|
JanetGCObject gc;
|
|
|
|
const JanetAbstractType *type;
|
|
|
|
size_t size;
|
2019-02-24 18:43:38 +00:00
|
|
|
long long data[]; /* Use long long to ensure most general alignment */
|
2019-02-21 16:22:29 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Some function definition flags */
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FUNCDEF_FLAG_VARARG 0x10000
|
|
|
|
#define JANET_FUNCDEF_FLAG_NEEDSENV 0x20000
|
|
|
|
#define JANET_FUNCDEF_FLAG_HASNAME 0x80000
|
|
|
|
#define JANET_FUNCDEF_FLAG_HASSOURCE 0x100000
|
|
|
|
#define JANET_FUNCDEF_FLAG_HASDEFS 0x200000
|
|
|
|
#define JANET_FUNCDEF_FLAG_HASENVS 0x400000
|
|
|
|
#define JANET_FUNCDEF_FLAG_HASSOURCEMAP 0x800000
|
2019-05-24 21:03:22 +00:00
|
|
|
#define JANET_FUNCDEF_FLAG_STRUCTARG 0x1000000
|
2020-03-18 14:30:10 +00:00
|
|
|
#define JANET_FUNCDEF_FLAG_HASCLOBITSET 0x2000000
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_FUNCDEF_FLAG_TAG 0xFFFF
|
|
|
|
|
|
|
|
/* Source mapping structure for a bytecode instruction */
|
|
|
|
struct JanetSourceMapping {
|
2019-09-22 22:18:28 +00:00
|
|
|
int32_t line;
|
|
|
|
int32_t column;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A function definition. Contains information needed to instantiate closures. */
|
|
|
|
struct JanetFuncDef {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t *environments; /* Which environments to capture from parent. */
|
|
|
|
Janet *constants;
|
|
|
|
JanetFuncDef **defs;
|
|
|
|
uint32_t *bytecode;
|
2020-03-18 14:30:10 +00:00
|
|
|
uint32_t *closure_bitset; /* Bit set indicating which slots can be referenced by closures. */
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Various debug information */
|
|
|
|
JanetSourceMapping *sourcemap;
|
2019-12-10 02:05:53 +00:00
|
|
|
JanetString source;
|
|
|
|
JanetString name;
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
int32_t flags;
|
|
|
|
int32_t slotcount; /* The amount of stack space required for the function */
|
|
|
|
int32_t arity; /* Not including varargs */
|
2019-03-12 04:23:14 +00:00
|
|
|
int32_t min_arity; /* Including varargs */
|
|
|
|
int32_t max_arity; /* Including varargs */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t constants_length;
|
|
|
|
int32_t bytecode_length;
|
2018-10-17 03:08:26 +00:00
|
|
|
int32_t environments_length;
|
|
|
|
int32_t defs_length;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* A function environment */
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetFuncEnv {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
union {
|
|
|
|
JanetFiber *fiber;
|
|
|
|
Janet *values;
|
|
|
|
} as;
|
|
|
|
int32_t length; /* Size of environment */
|
|
|
|
int32_t offset; /* Stack offset when values still on stack. If offset is <= 0, then
|
|
|
|
environment is no longer on the stack. */
|
|
|
|
};
|
|
|
|
|
2019-05-04 19:03:41 +00:00
|
|
|
#define JANET_FUNCFLAG_TRACE (1 << 16)
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* A function */
|
|
|
|
struct JanetFunction {
|
2019-02-21 16:22:29 +00:00
|
|
|
JanetGCObject gc;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFuncDef *def;
|
|
|
|
JanetFuncEnv *envs[];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct JanetParseState JanetParseState;
|
|
|
|
typedef struct JanetParser JanetParser;
|
|
|
|
|
|
|
|
enum JanetParserStatus {
|
|
|
|
JANET_PARSE_ROOT,
|
|
|
|
JANET_PARSE_ERROR,
|
2019-02-27 18:09:19 +00:00
|
|
|
JANET_PARSE_PENDING,
|
|
|
|
JANET_PARSE_DEAD
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A janet parser */
|
|
|
|
struct JanetParser {
|
2019-02-20 01:51:34 +00:00
|
|
|
Janet *args;
|
2018-09-06 02:18:42 +00:00
|
|
|
const char *error;
|
|
|
|
JanetParseState *states;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t argcount;
|
|
|
|
size_t argcap;
|
|
|
|
size_t statecount;
|
|
|
|
size_t statecap;
|
|
|
|
size_t bufcount;
|
|
|
|
size_t bufcap;
|
2019-09-22 22:18:28 +00:00
|
|
|
size_t line;
|
|
|
|
size_t column;
|
2019-01-04 01:44:58 +00:00
|
|
|
size_t pending;
|
2018-09-06 02:18:42 +00:00
|
|
|
int lookback;
|
2019-02-27 18:09:19 +00:00
|
|
|
int flag;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
2020-03-14 15:12:47 +00:00
|
|
|
/* A context for marshaling and unmarshaling abstract types */
|
2019-02-24 01:02:54 +00:00
|
|
|
typedef struct {
|
2020-03-14 15:12:47 +00:00
|
|
|
void *m_state;
|
2019-02-24 01:02:54 +00:00
|
|
|
void *u_state;
|
|
|
|
int flags;
|
|
|
|
const uint8_t *data;
|
2019-12-07 04:12:18 +00:00
|
|
|
const JanetAbstractType *at;
|
2019-02-24 01:02:54 +00:00
|
|
|
} JanetMarshalContext;
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Defines an abstract type */
|
|
|
|
struct JanetAbstractType {
|
|
|
|
const char *name;
|
|
|
|
int (*gc)(void *data, size_t len);
|
|
|
|
int (*gcmark)(void *data, size_t len);
|
2019-12-09 03:50:33 +00:00
|
|
|
int (*get)(void *data, Janet key, Janet *out);
|
2019-02-05 16:14:13 +00:00
|
|
|
void (*put)(void *data, Janet key, Janet value);
|
2019-02-24 01:02:54 +00:00
|
|
|
void (*marshal)(void *p, JanetMarshalContext *ctx);
|
2019-12-07 04:12:18 +00:00
|
|
|
void *(*unmarshal)(JanetMarshalContext *ctx);
|
2019-03-19 17:36:26 +00:00
|
|
|
void (*tostring)(void *p, JanetBuffer *buffer);
|
2019-12-28 20:57:36 +00:00
|
|
|
int (*compare)(void *lhs, void *rhs);
|
|
|
|
int32_t (*hash)(void *p, size_t len);
|
2020-01-19 00:09:20 +00:00
|
|
|
Janet(*next)(void *p, Janet key);
|
2020-01-22 00:22:24 +00:00
|
|
|
Janet(*call)(void *p, int32_t argc, Janet *argv);
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
2020-01-20 19:06:50 +00:00
|
|
|
/* Some macros to let us add extra types to JanetAbstract types without
|
|
|
|
* needing to changing native modules that declare them as static const
|
|
|
|
* structures. If more fields are added, these macros are modified to include
|
|
|
|
* default values (usually NULL). This silences missing field warnings. */
|
|
|
|
#define JANET_ATEND_NAME NULL,JANET_ATEND_GC
|
|
|
|
#define JANET_ATEND_GC NULL,JANET_ATEND_GCMARK
|
|
|
|
#define JANET_ATEND_GCMARK NULL,JANET_ATEND_GET
|
|
|
|
#define JANET_ATEND_GET NULL,JANET_ATEND_PUT
|
|
|
|
#define JANET_ATEND_PUT NULL,JANET_ATEND_MARSHAL
|
|
|
|
#define JANET_ATEND_MARSHAL NULL,JANET_ATEND_UNMARSHAL
|
|
|
|
#define JANET_ATEND_UNMARSHAL NULL,JANET_ATEND_TOSTRING
|
|
|
|
#define JANET_ATEND_TOSTRING NULL,JANET_ATEND_COMPARE
|
|
|
|
#define JANET_ATEND_COMPARE NULL,JANET_ATEND_HASH
|
|
|
|
#define JANET_ATEND_HASH NULL,JANET_ATEND_NEXT
|
2020-01-22 00:22:24 +00:00
|
|
|
#define JANET_ATEND_NEXT NULL,JANET_ATEND_CALL
|
|
|
|
#define JANET_ATEND_CALL
|
2020-01-20 19:06:50 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
struct JanetReg {
|
|
|
|
const char *name;
|
|
|
|
JanetCFunction cfun;
|
2018-11-15 20:45:41 +00:00
|
|
|
const char *documentation;
|
2018-09-06 02:18:42 +00:00
|
|
|
};
|
|
|
|
|
2021-07-25 18:03:01 +00:00
|
|
|
struct JanetRegExt {
|
|
|
|
const char *name;
|
|
|
|
JanetCFunction cfun;
|
|
|
|
const char *documentation;
|
|
|
|
const char *source_file;
|
|
|
|
int32_t source_line;
|
|
|
|
};
|
|
|
|
|
2019-02-06 00:43:41 +00:00
|
|
|
struct JanetMethod {
|
|
|
|
const char *name;
|
|
|
|
JanetCFunction cfun;
|
|
|
|
};
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
struct JanetView {
|
|
|
|
const Janet *items;
|
|
|
|
int32_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JanetByteView {
|
|
|
|
const uint8_t *bytes;
|
|
|
|
int32_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JanetDictView {
|
|
|
|
const JanetKV *kvs;
|
|
|
|
int32_t len;
|
|
|
|
int32_t cap;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JanetRange {
|
|
|
|
int32_t start;
|
|
|
|
int32_t end;
|
|
|
|
};
|
|
|
|
|
2019-11-08 23:35:27 +00:00
|
|
|
struct JanetRNG {
|
|
|
|
uint32_t a, b, c, d;
|
|
|
|
uint32_t counter;
|
|
|
|
};
|
|
|
|
|
2020-03-14 15:12:47 +00:00
|
|
|
typedef struct JanetFile JanetFile;
|
|
|
|
struct JanetFile {
|
|
|
|
FILE *file;
|
2020-07-03 21:19:05 +00:00
|
|
|
int32_t flags;
|
2020-03-14 15:12:47 +00:00
|
|
|
};
|
|
|
|
|
2020-09-19 23:47:47 +00:00
|
|
|
/* For janet_try and janet_restore */
|
|
|
|
typedef struct {
|
|
|
|
/* old state */
|
|
|
|
int32_t stackn;
|
|
|
|
int gc_handle;
|
|
|
|
JanetFiber *vm_fiber;
|
|
|
|
jmp_buf *vm_jmp_buf;
|
|
|
|
Janet *vm_return_reg;
|
|
|
|
/* new state */
|
|
|
|
jmp_buf buf;
|
|
|
|
Janet payload;
|
|
|
|
} JanetTryState;
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/***** END SECTION TYPES *****/
|
|
|
|
|
|
|
|
/***** START SECTION OPCODES *****/
|
|
|
|
|
|
|
|
/* Bytecode op argument types */
|
|
|
|
enum JanetOpArgType {
|
|
|
|
JANET_OAT_SLOT,
|
|
|
|
JANET_OAT_ENVIRONMENT,
|
|
|
|
JANET_OAT_CONSTANT,
|
|
|
|
JANET_OAT_INTEGER,
|
|
|
|
JANET_OAT_TYPE,
|
|
|
|
JANET_OAT_SIMPLETYPE,
|
|
|
|
JANET_OAT_LABEL,
|
|
|
|
JANET_OAT_FUNCDEF
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Various types of instructions */
|
|
|
|
enum JanetInstructionType {
|
|
|
|
JINT_0, /* No args */
|
|
|
|
JINT_S, /* Slot(3) */
|
|
|
|
JINT_L, /* Label(3) */
|
|
|
|
JINT_SS, /* Slot(1), Slot(2) */
|
|
|
|
JINT_SL, /* Slot(1), Label(2) */
|
|
|
|
JINT_ST, /* Slot(1), Slot(2) */
|
|
|
|
JINT_SI, /* Slot(1), Immediate(2) */
|
|
|
|
JINT_SD, /* Slot(1), Closure(2) */
|
|
|
|
JINT_SU, /* Slot(1), Unsigned Immediate(2) */
|
|
|
|
JINT_SSS, /* Slot(1), Slot(1), Slot(1) */
|
|
|
|
JINT_SSI, /* Slot(1), Slot(1), Immediate(1) */
|
|
|
|
JINT_SSU, /* Slot(1), Slot(1), Unsigned Immediate(1) */
|
|
|
|
JINT_SES, /* Slot(1), Environment(1), Far Slot(1) */
|
|
|
|
JINT_SC /* Slot(1), Constant(2) */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* All opcodes for the bytecode interpreter. */
|
|
|
|
enum JanetOpCode {
|
|
|
|
JOP_NOOP,
|
|
|
|
JOP_ERROR,
|
|
|
|
JOP_TYPECHECK,
|
|
|
|
JOP_RETURN,
|
|
|
|
JOP_RETURN_NIL,
|
|
|
|
JOP_ADD_IMMEDIATE,
|
|
|
|
JOP_ADD,
|
|
|
|
JOP_SUBTRACT,
|
|
|
|
JOP_MULTIPLY_IMMEDIATE,
|
|
|
|
JOP_MULTIPLY,
|
|
|
|
JOP_DIVIDE_IMMEDIATE,
|
|
|
|
JOP_DIVIDE,
|
2020-01-24 00:54:30 +00:00
|
|
|
JOP_MODULO,
|
|
|
|
JOP_REMAINDER,
|
2018-09-06 02:18:42 +00:00
|
|
|
JOP_BAND,
|
|
|
|
JOP_BOR,
|
|
|
|
JOP_BXOR,
|
|
|
|
JOP_BNOT,
|
|
|
|
JOP_SHIFT_LEFT,
|
|
|
|
JOP_SHIFT_LEFT_IMMEDIATE,
|
|
|
|
JOP_SHIFT_RIGHT,
|
|
|
|
JOP_SHIFT_RIGHT_IMMEDIATE,
|
|
|
|
JOP_SHIFT_RIGHT_UNSIGNED,
|
|
|
|
JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE,
|
|
|
|
JOP_MOVE_FAR,
|
|
|
|
JOP_MOVE_NEAR,
|
|
|
|
JOP_JUMP,
|
|
|
|
JOP_JUMP_IF,
|
|
|
|
JOP_JUMP_IF_NOT,
|
2020-01-18 23:55:07 +00:00
|
|
|
JOP_JUMP_IF_NIL,
|
|
|
|
JOP_JUMP_IF_NOT_NIL,
|
2018-09-06 02:18:42 +00:00
|
|
|
JOP_GREATER_THAN,
|
|
|
|
JOP_GREATER_THAN_IMMEDIATE,
|
|
|
|
JOP_LESS_THAN,
|
|
|
|
JOP_LESS_THAN_IMMEDIATE,
|
|
|
|
JOP_EQUALS,
|
|
|
|
JOP_EQUALS_IMMEDIATE,
|
|
|
|
JOP_COMPARE,
|
|
|
|
JOP_LOAD_NIL,
|
|
|
|
JOP_LOAD_TRUE,
|
|
|
|
JOP_LOAD_FALSE,
|
|
|
|
JOP_LOAD_INTEGER,
|
|
|
|
JOP_LOAD_CONSTANT,
|
|
|
|
JOP_LOAD_UPVALUE,
|
|
|
|
JOP_LOAD_SELF,
|
|
|
|
JOP_SET_UPVALUE,
|
|
|
|
JOP_CLOSURE,
|
|
|
|
JOP_PUSH,
|
|
|
|
JOP_PUSH_2,
|
|
|
|
JOP_PUSH_3,
|
|
|
|
JOP_PUSH_ARRAY,
|
|
|
|
JOP_CALL,
|
|
|
|
JOP_TAILCALL,
|
|
|
|
JOP_RESUME,
|
|
|
|
JOP_SIGNAL,
|
2019-06-24 16:44:13 +00:00
|
|
|
JOP_PROPAGATE,
|
2019-12-03 03:15:08 +00:00
|
|
|
JOP_IN,
|
2018-09-06 02:18:42 +00:00
|
|
|
JOP_GET,
|
|
|
|
JOP_PUT,
|
|
|
|
JOP_GET_INDEX,
|
|
|
|
JOP_PUT_INDEX,
|
|
|
|
JOP_LENGTH,
|
|
|
|
JOP_MAKE_ARRAY,
|
|
|
|
JOP_MAKE_BUFFER,
|
|
|
|
JOP_MAKE_STRING,
|
|
|
|
JOP_MAKE_STRUCT,
|
|
|
|
JOP_MAKE_TABLE,
|
|
|
|
JOP_MAKE_TUPLE,
|
2019-03-31 18:14:25 +00:00
|
|
|
JOP_MAKE_BRACKET_TUPLE,
|
2019-12-28 20:57:36 +00:00
|
|
|
JOP_GREATER_THAN_EQUAL,
|
|
|
|
JOP_LESS_THAN_EQUAL,
|
2020-01-18 23:55:07 +00:00
|
|
|
JOP_NEXT,
|
2020-06-28 23:16:57 +00:00
|
|
|
JOP_NOT_EQUALS,
|
|
|
|
JOP_NOT_EQUALS_IMMEDIATE,
|
2020-08-22 20:35:37 +00:00
|
|
|
JOP_CANCEL,
|
2018-09-06 02:18:42 +00:00
|
|
|
JOP_INSTRUCTION_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Info about all instructions */
|
|
|
|
extern enum JanetInstructionType janet_instructions[JOP_INSTRUCTION_COUNT];
|
|
|
|
|
|
|
|
/***** END SECTION OPCODES *****/
|
|
|
|
|
|
|
|
/***** START SECTION MAIN *****/
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_stream_type;
|
2021-08-15 18:14:33 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_channel_type;
|
2020-11-14 20:28:10 +00:00
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Run the event loop */
|
2020-02-10 01:04:34 +00:00
|
|
|
JANET_API void janet_loop(void);
|
2020-05-28 15:39:40 +00:00
|
|
|
|
2021-07-25 01:30:36 +00:00
|
|
|
/* Run the event loop, but allow for user scheduled interrupts triggered
|
|
|
|
* by janet_loop1_interrupt being called in library code, a signal handler, or
|
|
|
|
* another thread.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* while (!janet_loop_done()) {
|
|
|
|
* // One turn of the event loop
|
|
|
|
* JanetFiber *interrupted_fiber = janet_loop1();
|
|
|
|
* // interrupted_fiber may be NULL
|
|
|
|
* // do some work here periodically...
|
|
|
|
* if (NULL != interrupted_fiber) {
|
|
|
|
* if (cancel_interrupted_fiber) {
|
|
|
|
* janet_cancel(interrupted_fiber, janet_cstringv("fiber was interrupted for [reason]"));
|
|
|
|
* } else {
|
|
|
|
* janet_schedule(interrupted_fiber, janet_wrap_nil());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
JANET_API int janet_loop_done(void);
|
|
|
|
JANET_API JanetFiber *janet_loop1(void);
|
|
|
|
JANET_API void janet_loop1_interrupt(JanetVM *vm);
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Wrapper around streams */
|
|
|
|
JANET_API JanetStream *janet_stream(JanetHandle handle, uint32_t flags, const JanetMethod *methods);
|
|
|
|
JANET_API void janet_stream_close(JanetStream *stream);
|
|
|
|
JANET_API Janet janet_cfun_stream_close(int32_t argc, Janet *argv);
|
|
|
|
JANET_API Janet janet_cfun_stream_read(int32_t argc, Janet *argv);
|
|
|
|
JANET_API Janet janet_cfun_stream_chunk(int32_t argc, Janet *argv);
|
|
|
|
JANET_API Janet janet_cfun_stream_write(int32_t argc, Janet *argv);
|
|
|
|
JANET_API void janet_stream_flags(JanetStream *stream, uint32_t flags);
|
2020-05-28 15:39:40 +00:00
|
|
|
|
|
|
|
/* Queue a fiber to run on the event loop */
|
|
|
|
JANET_API void janet_schedule(JanetFiber *fiber, Janet value);
|
2020-08-16 22:29:57 +00:00
|
|
|
JANET_API void janet_cancel(JanetFiber *fiber, Janet value);
|
|
|
|
JANET_API void janet_schedule_signal(JanetFiber *fiber, Janet value, JanetSignal sig);
|
2020-05-28 15:39:40 +00:00
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Start a state machine listening for events from a stream */
|
|
|
|
JANET_API JanetListenerState *janet_listen(JanetStream *stream, JanetListener behavior, int mask, size_t size, void *user);
|
2020-05-28 15:39:40 +00:00
|
|
|
|
|
|
|
/* Shorthand for yielding to event loop in C */
|
|
|
|
JANET_NO_RETURN JANET_API void janet_await(void);
|
2020-11-29 21:36:21 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_sleep_await(double sec);
|
2020-05-28 15:39:40 +00:00
|
|
|
|
2020-07-05 22:26:17 +00:00
|
|
|
/* For use inside listeners - adds a timeout to the current fiber, such that
|
|
|
|
* it will be resumed after sec seconds if no other event schedules the current fiber. */
|
2020-11-14 17:48:23 +00:00
|
|
|
JANET_API void janet_addtimeout(double sec);
|
2020-11-29 21:36:21 +00:00
|
|
|
JANET_API void janet_ev_inc_refcount(void);
|
|
|
|
JANET_API void janet_ev_dec_refcount(void);
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2021-08-20 01:56:48 +00:00
|
|
|
/* Thread aware abstract types and helpers */
|
|
|
|
JANET_API void *janet_abstract_begin_threaded(const JanetAbstractType *atype, size_t size);
|
|
|
|
JANET_API void *janet_abstract_end_threaded(void *x);
|
|
|
|
JANET_API void *janet_abstract_threaded(const JanetAbstractType *atype, size_t size);
|
|
|
|
JANET_API int32_t janet_abstract_incref(void *abst);
|
|
|
|
JANET_API int32_t janet_abstract_decref(void *abst);
|
|
|
|
|
2022-06-05 21:42:18 +00:00
|
|
|
/* Expose some OS sync primitives */
|
2022-06-17 22:13:58 +00:00
|
|
|
JANET_API size_t janet_os_mutex_size(void);
|
|
|
|
JANET_API size_t janet_os_rwlock_size(void);
|
2021-08-20 22:57:23 +00:00
|
|
|
JANET_API void janet_os_mutex_init(JanetOSMutex *mutex);
|
|
|
|
JANET_API void janet_os_mutex_deinit(JanetOSMutex *mutex);
|
|
|
|
JANET_API void janet_os_mutex_lock(JanetOSMutex *mutex);
|
|
|
|
JANET_API void janet_os_mutex_unlock(JanetOSMutex *mutex);
|
2022-06-05 21:42:18 +00:00
|
|
|
JANET_API void janet_os_rwlock_init(JanetOSRWLock *rwlock);
|
|
|
|
JANET_API void janet_os_rwlock_deinit(JanetOSRWLock *rwlock);
|
|
|
|
JANET_API void janet_os_rwlock_rlock(JanetOSRWLock *rwlock);
|
|
|
|
JANET_API void janet_os_rwlock_wlock(JanetOSRWLock *rwlock);
|
|
|
|
JANET_API void janet_os_rwlock_runlock(JanetOSRWLock *rwlock);
|
|
|
|
JANET_API void janet_os_rwlock_wunlock(JanetOSRWLock *rwlock);
|
2021-08-20 22:57:23 +00:00
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
/* Get last error from an IO operation */
|
2020-11-14 17:48:23 +00:00
|
|
|
JANET_API Janet janet_ev_lasterr(void);
|
|
|
|
|
2020-12-31 22:12:42 +00:00
|
|
|
/* Async service for calling a function or syscall in a background thread. This is not
|
|
|
|
* as efficient in the slightest as using Streams but can be used for arbitrary blocking
|
|
|
|
* functions and syscalls. */
|
|
|
|
|
|
|
|
/* Used to pass data between the main thread and worker threads for simple tasks.
|
|
|
|
* We could just use a pointer but this prevents malloc/free in the common case
|
|
|
|
* of only a handful of arguments. */
|
|
|
|
typedef struct {
|
|
|
|
int tag;
|
|
|
|
int argi;
|
|
|
|
void *argp;
|
2021-08-15 16:48:13 +00:00
|
|
|
Janet argj;
|
2020-12-31 22:12:42 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
} JanetEVGenericMessage;
|
|
|
|
|
|
|
|
/* How to resume or cancel after a threaded call. Not exhaustive of the possible
|
|
|
|
* ways one might want to resume after returning from a threaded call, but should
|
|
|
|
* cover most of the common cases. For something more complicated, such as resuming
|
|
|
|
* with an abstract type or a struct, one should use janet_ev_threaded_call instead
|
|
|
|
* of janet_ev_threaded_await with a custom callback. */
|
|
|
|
|
|
|
|
#define JANET_EV_TCTAG_NIL 0 /* resume with nil */
|
|
|
|
#define JANET_EV_TCTAG_INTEGER 1 /* resume with janet_wrap_integer(argi) */
|
|
|
|
#define JANET_EV_TCTAG_STRING 2 /* resume with janet_cstringv((const char *) argp) */
|
|
|
|
#define JANET_EV_TCTAG_STRINGF 3 /* resume with janet_cstringv((const char *) argp), then call free on argp. */
|
|
|
|
#define JANET_EV_TCTAG_KEYWORD 4 /* resume with janet_ckeywordv((const char *) argp) */
|
|
|
|
#define JANET_EV_TCTAG_ERR_STRING 5 /* cancel with janet_cstringv((const char *) argp) */
|
|
|
|
#define JANET_EV_TCTAG_ERR_STRINGF 6 /* cancel with janet_cstringv((const char *) argp), then call free on argp. */
|
|
|
|
#define JANET_EV_TCTAG_ERR_KEYWORD 7 /* cancel with janet_ckeywordv((const char *) argp) */
|
2021-01-18 22:44:22 +00:00
|
|
|
#define JANET_EV_TCTAG_BOOLEAN 8 /* resume with janet_wrap_boolean(argi) */
|
2020-12-31 22:12:42 +00:00
|
|
|
|
|
|
|
/* Function pointer that is run in the thread pool */
|
|
|
|
typedef JanetEVGenericMessage(*JanetThreadedSubroutine)(JanetEVGenericMessage arguments);
|
|
|
|
|
2021-07-25 01:30:36 +00:00
|
|
|
/* Handler for events posted to the event loop */
|
|
|
|
typedef void (*JanetCallback)(JanetEVGenericMessage return_value);
|
|
|
|
|
|
|
|
/* Handler that is run in the main thread with the result of the JanetAsyncSubroutine (same as JanetCallback) */
|
2020-12-31 22:12:42 +00:00
|
|
|
typedef void (*JanetThreadedCallback)(JanetEVGenericMessage return_value);
|
|
|
|
|
|
|
|
/* API calls for quickly offloading some work in C to a new thread or thread pool. */
|
|
|
|
JANET_API void janet_ev_threaded_call(JanetThreadedSubroutine fp, JanetEVGenericMessage arguments, JanetThreadedCallback cb);
|
2021-01-18 22:44:22 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_ev_threaded_await(JanetThreadedSubroutine fp, int tag, int argi, void *argp);
|
2020-12-31 22:12:42 +00:00
|
|
|
|
2021-07-25 01:30:36 +00:00
|
|
|
/* Post callback + userdata to an event loop. Takes the vm parameter to allow posting from other
|
|
|
|
* threads or signal handlers. Use NULL to post to the current thread. */
|
|
|
|
JANET_API void janet_ev_post_event(JanetVM *vm, JanetCallback cb, JanetEVGenericMessage msg);
|
|
|
|
|
2020-12-31 22:12:42 +00:00
|
|
|
/* Callback used by janet_ev_threaded_await */
|
|
|
|
JANET_API void janet_ev_default_threaded_callback(JanetEVGenericMessage return_value);
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Read async from a stream */
|
|
|
|
JANET_API void janet_ev_read(JanetStream *stream, JanetBuffer *buf, int32_t nbytes);
|
|
|
|
JANET_API void janet_ev_readchunk(JanetStream *stream, JanetBuffer *buf, int32_t nbytes);
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
2020-11-14 20:28:10 +00:00
|
|
|
JANET_API void janet_ev_recv(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
|
|
|
JANET_API void janet_ev_recvchunk(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
|
|
|
JANET_API void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags);
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Write async to a stream */
|
|
|
|
JANET_API void janet_ev_write_buffer(JanetStream *stream, JanetBuffer *buf);
|
|
|
|
JANET_API void janet_ev_write_string(JanetStream *stream, JanetString str);
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
2020-11-14 20:28:10 +00:00
|
|
|
JANET_API void janet_ev_send_buffer(JanetStream *stream, JanetBuffer *buf, int flags);
|
|
|
|
JANET_API void janet_ev_send_string(JanetStream *stream, JanetString str, int flags);
|
|
|
|
JANET_API void janet_ev_sendto_buffer(JanetStream *stream, JanetBuffer *buf, void *dest, int flags);
|
|
|
|
JANET_API void janet_ev_sendto_string(JanetStream *stream, JanetString str, void *dest, int flags);
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-07-05 22:26:17 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Parsing */
|
2020-03-14 15:12:47 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_parser_type;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_parser_init(JanetParser *parser);
|
|
|
|
JANET_API void janet_parser_deinit(JanetParser *parser);
|
2019-02-27 18:09:19 +00:00
|
|
|
JANET_API void janet_parser_consume(JanetParser *parser, uint8_t c);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API enum JanetParserStatus janet_parser_status(JanetParser *parser);
|
|
|
|
JANET_API Janet janet_parser_produce(JanetParser *parser);
|
2021-01-30 17:33:15 +00:00
|
|
|
JANET_API Janet janet_parser_produce_wrapped(JanetParser *parser);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API const char *janet_parser_error(JanetParser *parser);
|
|
|
|
JANET_API void janet_parser_flush(JanetParser *parser);
|
2019-02-27 18:09:19 +00:00
|
|
|
JANET_API void janet_parser_eof(JanetParser *parser);
|
2019-04-27 19:45:28 +00:00
|
|
|
JANET_API int janet_parser_has_more(JanetParser *parser);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Assembly */
|
2018-09-29 14:58:57 +00:00
|
|
|
#ifdef JANET_ASSEMBLER
|
2018-09-06 02:18:42 +00:00
|
|
|
typedef struct JanetAssembleResult JanetAssembleResult;
|
|
|
|
enum JanetAssembleStatus {
|
|
|
|
JANET_ASSEMBLE_OK,
|
|
|
|
JANET_ASSEMBLE_ERROR
|
|
|
|
};
|
|
|
|
struct JanetAssembleResult {
|
|
|
|
JanetFuncDef *funcdef;
|
2019-12-10 02:05:53 +00:00
|
|
|
JanetString error;
|
2018-09-06 02:18:42 +00:00
|
|
|
enum JanetAssembleStatus status;
|
|
|
|
};
|
|
|
|
JANET_API JanetAssembleResult janet_asm(Janet source, int flags);
|
|
|
|
JANET_API Janet janet_disasm(JanetFuncDef *def);
|
|
|
|
JANET_API Janet janet_asm_decode_instruction(uint32_t instr);
|
2018-09-29 14:58:57 +00:00
|
|
|
#endif
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Compilation */
|
|
|
|
typedef struct JanetCompileResult JanetCompileResult;
|
|
|
|
enum JanetCompileStatus {
|
|
|
|
JANET_COMPILE_OK,
|
|
|
|
JANET_COMPILE_ERROR
|
|
|
|
};
|
|
|
|
struct JanetCompileResult {
|
|
|
|
JanetFuncDef *funcdef;
|
2019-12-10 02:05:53 +00:00
|
|
|
JanetString error;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFiber *macrofiber;
|
|
|
|
JanetSourceMapping error_mapping;
|
|
|
|
enum JanetCompileStatus status;
|
|
|
|
};
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetCompileResult janet_compile(Janet source, JanetTable *env, JanetString where);
|
2021-05-28 20:12:05 +00:00
|
|
|
JANET_API JanetCompileResult janet_compile_lint(
|
|
|
|
Janet source,
|
|
|
|
JanetTable *env,
|
|
|
|
JanetString where,
|
|
|
|
JanetArray *lints);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Get the default environment for janet */
|
2019-03-08 16:39:18 +00:00
|
|
|
JANET_API JanetTable *janet_core_env(JanetTable *replacements);
|
2021-01-23 22:52:26 +00:00
|
|
|
JANET_API JanetTable *janet_core_lookup_table(JanetTable *replacements);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2021-07-18 14:39:37 +00:00
|
|
|
/* Execute strings */
|
2018-10-17 03:08:26 +00:00
|
|
|
JANET_API int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out);
|
|
|
|
JANET_API int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2021-07-18 14:39:37 +00:00
|
|
|
/* Run the entrypoint of a wrapped program */
|
|
|
|
JANET_API int janet_loop_fiber(JanetFiber *fiber);
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Number scanning */
|
2019-01-03 00:41:07 +00:00
|
|
|
JANET_API int janet_scan_number(const uint8_t *str, int32_t len, double *out);
|
2021-09-21 23:02:42 +00:00
|
|
|
JANET_API int janet_scan_number_base(const uint8_t *str, int32_t len, int32_t base, double *out);
|
2019-03-19 01:12:38 +00:00
|
|
|
JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out);
|
|
|
|
JANET_API int janet_scan_uint64(const uint8_t *str, int32_t len, uint64_t *out);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2018-12-15 18:52:07 +00:00
|
|
|
/* Debugging */
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API void janet_debug_break(JanetFuncDef *def, int32_t pc);
|
|
|
|
JANET_API void janet_debug_unbreak(JanetFuncDef *def, int32_t pc);
|
|
|
|
JANET_API void janet_debug_find(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetFuncDef **def_out, int32_t *pc_out,
|
2019-12-10 02:05:53 +00:00
|
|
|
JanetString source, int32_t line, int32_t column);
|
2018-12-15 18:52:07 +00:00
|
|
|
|
2019-11-08 23:35:27 +00:00
|
|
|
/* RNG */
|
2020-03-14 15:12:47 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_rng_type;
|
2019-11-08 23:35:27 +00:00
|
|
|
JANET_API JanetRNG *janet_default_rng(void);
|
|
|
|
JANET_API void janet_rng_seed(JanetRNG *rng, uint32_t seed);
|
2019-11-22 05:30:31 +00:00
|
|
|
JANET_API void janet_rng_longseed(JanetRNG *rng, const uint8_t *bytes, int32_t len);
|
2019-11-08 23:35:27 +00:00
|
|
|
JANET_API uint32_t janet_rng_u32(JanetRNG *rng);
|
2021-10-03 03:52:34 +00:00
|
|
|
JANET_API double janet_rng_double(JanetRNG *rng);
|
2019-11-08 23:35:27 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Array functions */
|
|
|
|
JANET_API JanetArray *janet_array(int32_t capacity);
|
|
|
|
JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n);
|
2018-11-08 03:27:06 +00:00
|
|
|
JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_array_setcount(JanetArray *array, int32_t count);
|
|
|
|
JANET_API void janet_array_push(JanetArray *array, Janet x);
|
|
|
|
JANET_API Janet janet_array_pop(JanetArray *array);
|
|
|
|
JANET_API Janet janet_array_peek(JanetArray *array);
|
|
|
|
|
|
|
|
/* Buffer functions */
|
|
|
|
JANET_API JanetBuffer *janet_buffer(int32_t capacity);
|
|
|
|
JANET_API JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity);
|
|
|
|
JANET_API void janet_buffer_deinit(JanetBuffer *buffer);
|
2018-11-08 03:27:06 +00:00
|
|
|
JANET_API void janet_buffer_ensure(JanetBuffer *buffer, int32_t capacity, int32_t growth);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_buffer_setcount(JanetBuffer *buffer, int32_t count);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API void janet_buffer_extra(JanetBuffer *buffer, int32_t n);
|
|
|
|
JANET_API void janet_buffer_push_bytes(JanetBuffer *buffer, const uint8_t *string, int32_t len);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API void janet_buffer_push_string(JanetBuffer *buffer, JanetString string);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API void janet_buffer_push_cstring(JanetBuffer *buffer, const char *cstring);
|
|
|
|
JANET_API void janet_buffer_push_u8(JanetBuffer *buffer, uint8_t x);
|
|
|
|
JANET_API void janet_buffer_push_u16(JanetBuffer *buffer, uint16_t x);
|
|
|
|
JANET_API void janet_buffer_push_u32(JanetBuffer *buffer, uint32_t x);
|
|
|
|
JANET_API void janet_buffer_push_u64(JanetBuffer *buffer, uint64_t x);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Tuple */
|
2019-02-09 16:00:35 +00:00
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
#define JANET_TUPLE_FLAG_BRACKETCTOR 0x10000
|
2019-02-09 16:00:35 +00:00
|
|
|
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_tuple_head(t) ((JanetTupleHead *)((char *)t - offsetof(JanetTupleHead, data)))
|
2020-04-24 21:18:31 +00:00
|
|
|
#define janet_tuple_from_head(gcobject) ((const Janet *)((char *)gcobject + offsetof(JanetTupleHead, data)))
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_tuple_length(t) (janet_tuple_head(t)->length)
|
|
|
|
#define janet_tuple_hash(t) (janet_tuple_head(t)->hash)
|
2019-09-22 22:18:28 +00:00
|
|
|
#define janet_tuple_sm_line(t) (janet_tuple_head(t)->sm_line)
|
|
|
|
#define janet_tuple_sm_column(t) (janet_tuple_head(t)->sm_column)
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_tuple_flag(t) (janet_tuple_head(t)->gc.flags)
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API Janet *janet_tuple_begin(int32_t length);
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetTuple janet_tuple_end(Janet *tuple);
|
|
|
|
JANET_API JanetTuple janet_tuple_n(const Janet *values, int32_t n);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* String/Symbol functions */
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_string_head(s) ((JanetStringHead *)((char *)s - offsetof(JanetStringHead, data)))
|
|
|
|
#define janet_string_length(s) (janet_string_head(s)->length)
|
|
|
|
#define janet_string_hash(s) (janet_string_head(s)->hash)
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API uint8_t *janet_string_begin(int32_t length);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetString janet_string_end(uint8_t *str);
|
|
|
|
JANET_API JanetString janet_string(const uint8_t *buf, int32_t len);
|
|
|
|
JANET_API JanetString janet_cstring(const char *cstring);
|
|
|
|
JANET_API int janet_string_compare(JanetString lhs, JanetString rhs);
|
|
|
|
JANET_API int janet_string_equal(JanetString lhs, JanetString rhs);
|
|
|
|
JANET_API int janet_string_equalconst(JanetString lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash);
|
|
|
|
JANET_API JanetString janet_description(Janet x);
|
|
|
|
JANET_API JanetString janet_to_string(Janet x);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_to_string_b(JanetBuffer *buffer, Janet x);
|
2019-01-06 02:23:44 +00:00
|
|
|
JANET_API void janet_description_b(JanetBuffer *buffer, Janet x);
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_cstringv(cstr) janet_wrap_string(janet_cstring(cstr))
|
|
|
|
#define janet_stringv(str, len) janet_wrap_string(janet_string((str), (len)))
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetString janet_formatc(const char *format, ...);
|
2020-04-14 12:33:33 +00:00
|
|
|
JANET_API JanetBuffer *janet_formatb(JanetBuffer *bufp, const char *format, ...);
|
|
|
|
JANET_API void janet_formatbv(JanetBuffer *bufp, const char *format, va_list args);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Symbol functions */
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetSymbol janet_symbol(const uint8_t *str, int32_t len);
|
|
|
|
JANET_API JanetSymbol janet_csymbol(const char *str);
|
|
|
|
JANET_API JanetSymbol janet_symbol_gen(void);
|
2018-09-06 02:18:42 +00:00
|
|
|
#define janet_symbolv(str, len) janet_wrap_symbol(janet_symbol((str), (len)))
|
|
|
|
#define janet_csymbolv(cstr) janet_wrap_symbol(janet_csymbol(cstr))
|
|
|
|
|
2019-01-03 00:41:07 +00:00
|
|
|
/* Keyword functions */
|
|
|
|
#define janet_keyword janet_symbol
|
|
|
|
#define janet_ckeyword janet_csymbol
|
|
|
|
#define janet_keywordv(str, len) janet_wrap_keyword(janet_keyword((str), (len)))
|
|
|
|
#define janet_ckeywordv(cstr) janet_wrap_keyword(janet_ckeyword(cstr))
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Structs */
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_struct_head(t) ((JanetStructHead *)((char *)t - offsetof(JanetStructHead, data)))
|
2020-04-24 21:18:31 +00:00
|
|
|
#define janet_struct_from_head(t) ((const JanetKV *)((char *)gcobject + offsetof(JanetStructHead, data)))
|
2019-02-21 16:22:29 +00:00
|
|
|
#define janet_struct_length(t) (janet_struct_head(t)->length)
|
|
|
|
#define janet_struct_capacity(t) (janet_struct_head(t)->capacity)
|
|
|
|
#define janet_struct_hash(t) (janet_struct_head(t)->hash)
|
2021-05-29 02:37:13 +00:00
|
|
|
#define janet_struct_proto(t) (janet_struct_head(t)->proto)
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API JanetKV *janet_struct_begin(int32_t count);
|
|
|
|
JANET_API void janet_struct_put(JanetKV *st, Janet key, Janet value);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetStruct janet_struct_end(JanetKV *st);
|
|
|
|
JANET_API Janet janet_struct_get(JanetStruct st, Janet key);
|
2021-05-29 02:37:13 +00:00
|
|
|
JANET_API Janet janet_struct_rawget(JanetStruct st, Janet key);
|
|
|
|
JANET_API Janet janet_struct_get_ex(JanetStruct st, Janet key, JanetStruct *which);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetTable *janet_struct_to_table(JanetStruct st);
|
|
|
|
JANET_API const JanetKV *janet_struct_find(JanetStruct st, Janet key);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Table functions */
|
|
|
|
JANET_API JanetTable *janet_table(int32_t capacity);
|
|
|
|
JANET_API JanetTable *janet_table_init(JanetTable *table, int32_t capacity);
|
2021-08-15 16:48:13 +00:00
|
|
|
JANET_API JanetTable *janet_table_init_raw(JanetTable *table, int32_t capacity);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_table_deinit(JanetTable *table);
|
|
|
|
JANET_API Janet janet_table_get(JanetTable *t, Janet key);
|
2019-08-30 00:56:04 +00:00
|
|
|
JANET_API Janet janet_table_get_ex(JanetTable *t, Janet key, JanetTable **which);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API Janet janet_table_rawget(JanetTable *t, Janet key);
|
|
|
|
JANET_API Janet janet_table_remove(JanetTable *t, Janet key);
|
|
|
|
JANET_API void janet_table_put(JanetTable *t, Janet key, Janet value);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetStruct janet_table_to_struct(JanetTable *t);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_table_merge_table(JanetTable *table, JanetTable *other);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API void janet_table_merge_struct(JanetTable *table, JanetStruct other);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API JanetKV *janet_table_find(JanetTable *t, Janet key);
|
2019-07-08 04:18:39 +00:00
|
|
|
JANET_API JanetTable *janet_table_clone(JanetTable *table);
|
2020-06-15 20:33:41 +00:00
|
|
|
JANET_API void janet_table_clear(JanetTable *table);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Fiber */
|
2019-01-12 22:31:15 +00:00
|
|
|
JANET_API JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv);
|
|
|
|
JANET_API JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv);
|
2019-04-27 19:45:28 +00:00
|
|
|
JANET_API JanetFiberStatus janet_fiber_status(JanetFiber *fiber);
|
2019-05-04 23:07:04 +00:00
|
|
|
JANET_API JanetFiber *janet_current_fiber(void);
|
2020-02-10 01:04:34 +00:00
|
|
|
JANET_API JanetFiber *janet_root_fiber(void);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Treat similar types through uniform interfaces for iteration */
|
|
|
|
JANET_API int janet_indexed_view(Janet seq, const Janet **data, int32_t *len);
|
|
|
|
JANET_API int janet_bytes_view(Janet str, const uint8_t **data, int32_t *len);
|
|
|
|
JANET_API int janet_dictionary_view(Janet tab, const JanetKV **data, int32_t *len, int32_t *cap);
|
2018-09-09 16:13:32 +00:00
|
|
|
JANET_API Janet janet_dictionary_get(const JanetKV *data, int32_t cap, Janet key);
|
2018-09-10 01:20:33 +00:00
|
|
|
JANET_API const JanetKV *janet_dictionary_next(const JanetKV *kvs, int32_t cap, const JanetKV *kv);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Abstract */
|
2019-04-27 19:45:28 +00:00
|
|
|
#define janet_abstract_head(u) ((JanetAbstractHead *)((char *)u - offsetof(JanetAbstractHead, data)))
|
2020-04-24 21:18:31 +00:00
|
|
|
#define janet_abstract_from_head(gcobject) ((JanetAbstract)((char *)gcobject + offsetof(JanetAbstractHead, data)))
|
2019-04-27 19:45:28 +00:00
|
|
|
#define janet_abstract_type(u) (janet_abstract_head(u)->type)
|
|
|
|
#define janet_abstract_size(u) (janet_abstract_head(u)->size)
|
2019-06-20 16:37:57 +00:00
|
|
|
JANET_API void *janet_abstract_begin(const JanetAbstractType *type, size_t size);
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetAbstract janet_abstract_end(void *abstractTemplate);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetAbstract janet_abstract(const JanetAbstractType *type, size_t size); /* begin and end in one call */
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Native */
|
2019-01-06 01:09:03 +00:00
|
|
|
typedef void (*JanetModule)(JanetTable *);
|
2019-05-30 02:11:56 +00:00
|
|
|
typedef JanetBuildConfig(*JanetModconf)(void);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetModule janet_native(const char *name, JanetString *error);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2018-09-10 01:20:33 +00:00
|
|
|
/* Marshaling */
|
2020-04-19 15:55:00 +00:00
|
|
|
#define JANET_MARSHAL_UNSAFE 0x20000
|
2022-08-26 16:20:02 +00:00
|
|
|
#define JANET_MARSHAL_NO_CYCLES 0x40000
|
2020-04-19 15:55:00 +00:00
|
|
|
|
2019-02-22 15:10:41 +00:00
|
|
|
JANET_API void janet_marshal(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetBuffer *buf,
|
|
|
|
Janet x,
|
|
|
|
JanetTable *rreg,
|
|
|
|
int flags);
|
2019-02-22 15:10:41 +00:00
|
|
|
JANET_API Janet janet_unmarshal(
|
2019-02-20 01:51:34 +00:00
|
|
|
const uint8_t *bytes,
|
|
|
|
size_t len,
|
|
|
|
int flags,
|
|
|
|
JanetTable *reg,
|
|
|
|
const uint8_t **next);
|
2018-10-21 15:46:36 +00:00
|
|
|
JANET_API JanetTable *janet_env_lookup(JanetTable *env);
|
2019-07-28 18:25:14 +00:00
|
|
|
JANET_API void janet_env_lookup_into(JanetTable *renv, JanetTable *env, const char *prefix, int recurse);
|
2018-09-10 01:20:33 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* GC */
|
|
|
|
JANET_API void janet_mark(Janet x);
|
|
|
|
JANET_API void janet_sweep(void);
|
|
|
|
JANET_API void janet_collect(void);
|
|
|
|
JANET_API void janet_clear_memory(void);
|
|
|
|
JANET_API void janet_gcroot(Janet root);
|
|
|
|
JANET_API int janet_gcunroot(Janet root);
|
|
|
|
JANET_API int janet_gcunrootall(Janet root);
|
|
|
|
JANET_API int janet_gclock(void);
|
|
|
|
JANET_API void janet_gcunlock(int handle);
|
2020-01-23 02:52:35 +00:00
|
|
|
JANET_API void janet_gcpressure(size_t s);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
JANET_API JanetFuncDef *janet_funcdef_alloc(void);
|
|
|
|
JANET_API JanetFunction *janet_thunk(JanetFuncDef *def);
|
|
|
|
JANET_API int janet_verify(JanetFuncDef *def);
|
|
|
|
|
2019-03-24 19:00:22 +00:00
|
|
|
/* Pretty printing */
|
|
|
|
#define JANET_PRETTY_COLOR 1
|
2019-08-25 02:53:45 +00:00
|
|
|
#define JANET_PRETTY_ONELINE 2
|
2020-04-26 18:17:28 +00:00
|
|
|
#define JANET_PRETTY_NOTRUNC 4
|
2019-03-24 19:00:22 +00:00
|
|
|
JANET_API JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, int flags, Janet x);
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* Misc */
|
2020-07-25 18:34:40 +00:00
|
|
|
#ifdef JANET_PRF
|
2020-01-17 04:24:32 +00:00
|
|
|
#define JANET_HASH_KEY_SIZE 16
|
|
|
|
JANET_API void janet_init_hash_key(uint8_t key[JANET_HASH_KEY_SIZE]);
|
2020-01-17 03:06:03 +00:00
|
|
|
#endif
|
2020-09-19 23:47:47 +00:00
|
|
|
JANET_API void janet_try_init(JanetTryState *state);
|
|
|
|
#if defined(JANET_BSD) || defined(JANET_APPLE)
|
|
|
|
#define janet_try(state) (janet_try_init(state), (JanetSignal) _setjmp((state)->buf))
|
|
|
|
#else
|
|
|
|
#define janet_try(state) (janet_try_init(state), (JanetSignal) setjmp((state)->buf))
|
|
|
|
#endif
|
|
|
|
JANET_API void janet_restore(JanetTryState *state);
|
2020-01-17 03:06:03 +00:00
|
|
|
JANET_API int janet_equals(Janet x, Janet y);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API int32_t janet_hash(Janet x);
|
|
|
|
JANET_API int janet_compare(Janet x, Janet y);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API int janet_cstrcmp(JanetString str, const char *other);
|
2019-12-03 03:15:08 +00:00
|
|
|
JANET_API Janet janet_in(Janet ds, Janet key);
|
2019-01-05 05:33:20 +00:00
|
|
|
JANET_API Janet janet_get(Janet ds, Janet key);
|
2020-01-18 23:55:07 +00:00
|
|
|
JANET_API Janet janet_next(Janet ds, Janet key);
|
2019-01-05 05:33:20 +00:00
|
|
|
JANET_API Janet janet_getindex(Janet ds, int32_t index);
|
|
|
|
JANET_API int32_t janet_length(Janet x);
|
2019-09-08 20:36:21 +00:00
|
|
|
JANET_API Janet janet_lengthv(Janet x);
|
2019-01-05 05:33:20 +00:00
|
|
|
JANET_API void janet_put(Janet ds, Janet key, Janet value);
|
|
|
|
JANET_API void janet_putindex(Janet ds, int32_t index, Janet value);
|
2019-06-12 16:05:01 +00:00
|
|
|
#define janet_flag_at(F, I) ((F) & ((1ULL) << (I)))
|
2019-07-12 12:19:29 +00:00
|
|
|
JANET_API Janet janet_wrap_number_safe(double x);
|
2019-12-14 17:31:46 +00:00
|
|
|
JANET_API int janet_keyeq(Janet x, const char *cstring);
|
|
|
|
JANET_API int janet_streq(Janet x, const char *cstring);
|
|
|
|
JANET_API int janet_symeq(Janet x, const char *cstring);
|
2021-03-14 01:17:07 +00:00
|
|
|
JANET_API int32_t janet_sorted_keys(const JanetKV *dict, int32_t cap, int32_t *index_buffer);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
|
|
|
/* VM functions */
|
|
|
|
JANET_API int janet_init(void);
|
|
|
|
JANET_API void janet_deinit(void);
|
2021-07-17 01:59:03 +00:00
|
|
|
JANET_API JanetVM *janet_vm_alloc(void);
|
2021-07-24 20:14:37 +00:00
|
|
|
JANET_API JanetVM *janet_local_vm(void);
|
2021-07-17 01:59:03 +00:00
|
|
|
JANET_API void janet_vm_free(JanetVM *vm);
|
|
|
|
JANET_API void janet_vm_save(JanetVM *into);
|
|
|
|
JANET_API void janet_vm_load(JanetVM *from);
|
2021-07-24 20:14:37 +00:00
|
|
|
JANET_API void janet_interpreter_interrupt(JanetVM *vm);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API JanetSignal janet_continue(JanetFiber *fiber, Janet in, Janet *out);
|
2020-08-11 12:19:34 +00:00
|
|
|
JANET_API JanetSignal janet_continue_signal(JanetFiber *fiber, Janet in, Janet *out, JanetSignal sig);
|
2019-01-12 22:31:15 +00:00
|
|
|
JANET_API JanetSignal janet_pcall(JanetFunction *fun, int32_t argn, const Janet *argv, Janet *out, JanetFiber **f);
|
2019-11-26 00:14:34 +00:00
|
|
|
JANET_API JanetSignal janet_step(JanetFiber *fiber, Janet in, Janet *out);
|
2019-01-12 22:31:15 +00:00
|
|
|
JANET_API Janet janet_call(JanetFunction *fun, int32_t argc, const Janet *argv);
|
2019-09-08 20:36:21 +00:00
|
|
|
JANET_API Janet janet_mcall(const char *name, int32_t argc, Janet *argv);
|
2019-01-31 04:07:30 +00:00
|
|
|
JANET_API void janet_stacktrace(JanetFiber *fiber, Janet err);
|
2021-12-04 19:03:05 +00:00
|
|
|
JANET_API void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
/* Scratch Memory API */
|
2020-01-29 05:38:52 +00:00
|
|
|
typedef void (*JanetScratchFinalizer)(void *);
|
|
|
|
|
2019-06-02 03:31:39 +00:00
|
|
|
JANET_API void *janet_smalloc(size_t size);
|
|
|
|
JANET_API void *janet_srealloc(void *mem, size_t size);
|
2020-01-02 23:10:17 +00:00
|
|
|
JANET_API void *janet_scalloc(size_t nmemb, size_t size);
|
2020-01-29 05:38:52 +00:00
|
|
|
JANET_API void janet_sfinalizer(void *mem, JanetScratchFinalizer finalizer);
|
2019-06-02 03:31:39 +00:00
|
|
|
JANET_API void janet_sfree(void *mem);
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/* C Library helpers */
|
|
|
|
typedef enum {
|
|
|
|
JANET_BINDING_NONE,
|
|
|
|
JANET_BINDING_DEF,
|
|
|
|
JANET_BINDING_VAR,
|
2022-01-07 01:44:03 +00:00
|
|
|
JANET_BINDING_MACRO,
|
|
|
|
JANET_BINDING_DYNAMIC_DEF,
|
|
|
|
JANET_BINDING_DYNAMIC_MACRO
|
2018-09-06 02:18:42 +00:00
|
|
|
} JanetBindingType;
|
2021-05-28 20:12:05 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
JanetBindingType type;
|
|
|
|
Janet value;
|
|
|
|
enum {
|
|
|
|
JANET_BINDING_DEP_NONE,
|
|
|
|
JANET_BINDING_DEP_RELAXED,
|
|
|
|
JANET_BINDING_DEP_NORMAL,
|
|
|
|
JANET_BINDING_DEP_STRICT,
|
|
|
|
} deprecation;
|
|
|
|
} JanetBinding;
|
|
|
|
|
2018-11-15 20:45:41 +00:00
|
|
|
JANET_API void janet_def(JanetTable *env, const char *name, Janet val, const char *documentation);
|
|
|
|
JANET_API void janet_var(JanetTable *env, const char *name, Janet val, const char *documentation);
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_API void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns);
|
2020-09-04 13:09:05 +00:00
|
|
|
JANET_API void janet_cfuns_prefix(JanetTable *env, const char *regprefix, const JanetReg *cfuns);
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetBindingType janet_resolve(JanetTable *env, JanetSymbol sym, Janet *out);
|
2021-05-28 20:12:05 +00:00
|
|
|
JANET_API JanetBinding janet_resolve_ext(JanetTable *env, JanetSymbol sym);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-12-02 10:15:22 +00:00
|
|
|
/* Get values from the core environment. */
|
|
|
|
JANET_API Janet janet_resolve_core(const char *name);
|
|
|
|
|
2019-01-05 04:20:34 +00:00
|
|
|
/* New C API */
|
2019-01-06 01:09:03 +00:00
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Shorthand for janet C function declarations */
|
|
|
|
#define JANET_CFUN(name) Janet name (int32_t argc, Janet *argv)
|
|
|
|
|
2021-07-25 18:03:01 +00:00
|
|
|
/* Declare a C function with documentation and source mapping */
|
|
|
|
#define JANET_REG_END {NULL, NULL, NULL, NULL, 0}
|
|
|
|
|
|
|
|
/* no docstrings or sourcemaps */
|
|
|
|
#define JANET_REG_(JNAME, CNAME) {JNAME, CNAME, NULL, NULL, 0}
|
|
|
|
#define JANET_FN_(CNAME, USAGE, DOCSTRING) \
|
2021-07-28 00:43:32 +00:00
|
|
|
Janet CNAME (int32_t argc, Janet *argv)
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF_(ENV, JNAME, VAL, DOC) \
|
|
|
|
janet_def(ENV, JNAME, VAL, NULL)
|
2021-07-25 18:03:01 +00:00
|
|
|
|
|
|
|
/* sourcemaps only */
|
|
|
|
#define JANET_REG_S(JNAME, CNAME) {JNAME, CNAME, NULL, __FILE__, CNAME##_sourceline_}
|
|
|
|
#define JANET_FN_S(CNAME, USAGE, DOCSTRING) \
|
|
|
|
static int32_t CNAME##_sourceline_ = __LINE__; \
|
2021-07-28 00:43:32 +00:00
|
|
|
Janet CNAME (int32_t argc, Janet *argv)
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF_S(ENV, JNAME, VAL, DOC) \
|
|
|
|
janet_def_sm(ENV, JNAME, VAL, NULL, __FILE__, __LINE__)
|
2021-07-25 18:03:01 +00:00
|
|
|
|
|
|
|
/* docstring only */
|
|
|
|
#define JANET_REG_D(JNAME, CNAME) {JNAME, CNAME, CNAME##_docstring_, NULL, 0}
|
|
|
|
#define JANET_FN_D(CNAME, USAGE, DOCSTRING) \
|
|
|
|
static const char CNAME##_docstring_[] = USAGE "\n\n" DOCSTRING; \
|
2021-07-28 00:43:32 +00:00
|
|
|
Janet CNAME (int32_t argc, Janet *argv)
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF_D(ENV, JNAME, VAL, DOC) \
|
|
|
|
janet_def(ENV, JNAME, VAL, DOC)
|
2021-07-25 18:03:01 +00:00
|
|
|
|
|
|
|
/* sourcemaps and docstrings */
|
|
|
|
#define JANET_REG_SD(JNAME, CNAME) {JNAME, CNAME, CNAME##_docstring_, __FILE__, CNAME##_sourceline_}
|
|
|
|
#define JANET_FN_SD(CNAME, USAGE, DOCSTRING) \
|
|
|
|
static int32_t CNAME##_sourceline_ = __LINE__; \
|
|
|
|
static const char CNAME##_docstring_[] = USAGE "\n\n" DOCSTRING; \
|
2021-07-28 00:43:32 +00:00
|
|
|
Janet CNAME (int32_t argc, Janet *argv)
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF_SD(ENV, JNAME, VAL, DOC) \
|
|
|
|
janet_def_sm(ENV, JNAME, VAL, DOC, __FILE__, __LINE__)
|
2021-07-25 18:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Choose defaults for source mapping and docstring based on config defs */
|
|
|
|
#if defined(JANET_NO_SOURCEMAPS) && defined(JANET_NO_DOCSTRINGS)
|
|
|
|
#define JANET_REG JANET_REG_
|
|
|
|
#define JANET_FN JANET_FN_
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF JANET_DEF_
|
2021-07-25 18:03:01 +00:00
|
|
|
#elif defined(JANET_NO_SOURCEMAPS) && !defined(JANET_NO_DOCSTRINGS)
|
|
|
|
#define JANET_REG JANET_REG_D
|
|
|
|
#define JANET_FN JANET_FN_D
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF JANET_DEF_D
|
2021-07-25 18:03:01 +00:00
|
|
|
#elif !defined(JANET_NO_SOURCEMAPS) && defined(JANET_NO_DOCSTRINGS)
|
|
|
|
#define JANET_REG JANET_REG_S
|
|
|
|
#define JANET_FN JANET_FN_S
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF JANET_DEF_S
|
2021-07-25 18:03:01 +00:00
|
|
|
#elif !defined(JANET_NO_SOURCEMAPS) && !defined(JANET_NO_DOCSTRINGS)
|
|
|
|
#define JANET_REG JANET_REG_SD
|
|
|
|
#define JANET_FN JANET_FN_SD
|
2021-07-28 01:13:09 +00:00
|
|
|
#define JANET_DEF JANET_DEF_SD
|
2021-07-25 18:03:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Define things with source mapping information */
|
|
|
|
JANET_API void janet_cfuns_ext(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns);
|
|
|
|
JANET_API void janet_cfuns_ext_prefix(JanetTable *env, const char *regprefix, const JanetRegExt *cfuns);
|
|
|
|
JANET_API void janet_def_sm(JanetTable *env, const char *name, Janet val, const char *documentation, const char *source_file, int32_t source_line);
|
|
|
|
JANET_API void janet_var_sm(JanetTable *env, const char *name, Janet val, const char *documentation, const char *source_file, int32_t source_line);
|
2021-08-22 15:23:18 +00:00
|
|
|
|
|
|
|
/* Legacy definition of C functions */
|
|
|
|
JANET_API void janet_register(const char *name, JanetCFunction cfun);
|
2021-07-25 18:03:01 +00:00
|
|
|
|
2019-07-27 03:43:54 +00:00
|
|
|
/* Allow setting entry name for static libraries */
|
2020-09-04 22:05:28 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#define JANET_MODULE_PREFIX extern "C"
|
|
|
|
#else
|
|
|
|
#define JANET_MODULE_PREFIX
|
|
|
|
#endif
|
2019-07-27 03:43:54 +00:00
|
|
|
#ifndef JANET_ENTRY_NAME
|
2019-05-30 01:57:03 +00:00
|
|
|
#define JANET_MODULE_ENTRY \
|
2020-09-04 22:05:28 +00:00
|
|
|
JANET_MODULE_PREFIX JANET_API JanetBuildConfig _janet_mod_config(void) { \
|
2019-05-30 01:57:03 +00:00
|
|
|
return janet_config_current(); \
|
|
|
|
} \
|
2020-09-04 22:05:28 +00:00
|
|
|
JANET_MODULE_PREFIX JANET_API void _janet_init
|
2020-01-12 16:01:20 +00:00
|
|
|
#else
|
2020-09-04 22:05:28 +00:00
|
|
|
#define JANET_MODULE_ENTRY JANET_MODULE_PREFIX JANET_API void JANET_ENTRY_NAME
|
2020-01-12 16:01:20 +00:00
|
|
|
#endif
|
2019-05-28 01:43:35 +00:00
|
|
|
|
2020-01-10 23:25:10 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_signalv(JanetSignal signal, Janet message);
|
2019-05-31 14:10:20 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_panicv(Janet message);
|
|
|
|
JANET_NO_RETURN JANET_API void janet_panic(const char *message);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_panics(JanetString message);
|
2019-05-31 14:10:20 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_panicf(const char *format, ...);
|
2019-10-19 14:44:27 +00:00
|
|
|
JANET_API void janet_dynprintf(const char *name, FILE *dflt_file, const char *format, ...);
|
|
|
|
#define janet_printf(...) janet_dynprintf("out", stdout, __VA_ARGS__)
|
|
|
|
#define janet_eprintf(...) janet_dynprintf("err", stderr, __VA_ARGS__)
|
2019-05-31 14:10:20 +00:00
|
|
|
JANET_NO_RETURN JANET_API void janet_panic_type(Janet x, int32_t n, int expected);
|
|
|
|
JANET_NO_RETURN JANET_API void janet_panic_abstract(Janet x, int32_t n, const JanetAbstractType *at);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API void janet_arity(int32_t arity, int32_t min, int32_t max);
|
2019-01-06 01:45:24 +00:00
|
|
|
JANET_API void janet_fixarity(int32_t arity, int32_t fix);
|
2019-01-06 01:09:03 +00:00
|
|
|
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API int janet_getmethod(JanetKeyword method, const JanetMethod *methods, Janet *out);
|
2021-01-12 05:14:07 +00:00
|
|
|
JANET_API Janet janet_nextmethod(const JanetMethod *methods, Janet key);
|
2019-09-25 00:40:49 +00:00
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API double janet_getnumber(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetArray *janet_getarray(const Janet *argv, int32_t n);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetTuple janet_gettuple(const Janet *argv, int32_t n);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API JanetTable *janet_gettable(const Janet *argv, int32_t n);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetStruct janet_getstruct(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetString janet_getstring(const Janet *argv, int32_t n);
|
2019-03-29 03:22:58 +00:00
|
|
|
JANET_API const char *janet_getcstring(const Janet *argv, int32_t n);
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetSymbol janet_getsymbol(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetKeyword janet_getkeyword(const Janet *argv, int32_t n);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API JanetBuffer *janet_getbuffer(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetFiber *janet_getfiber(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetFunction *janet_getfunction(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetCFunction janet_getcfunction(const Janet *argv, int32_t n);
|
|
|
|
JANET_API int janet_getboolean(const Janet *argv, int32_t n);
|
2019-03-14 17:17:44 +00:00
|
|
|
JANET_API void *janet_getpointer(const Janet *argv, int32_t n);
|
2019-01-06 01:09:03 +00:00
|
|
|
|
2019-11-09 15:39:14 +00:00
|
|
|
JANET_API int32_t janet_getnat(const Janet *argv, int32_t n);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API int32_t janet_getinteger(const Janet *argv, int32_t n);
|
|
|
|
JANET_API int64_t janet_getinteger64(const Janet *argv, int32_t n);
|
2022-06-06 23:54:17 +00:00
|
|
|
JANET_API uint64_t janet_getuinteger64(const Janet *argv, int32_t n);
|
2019-03-08 05:41:26 +00:00
|
|
|
JANET_API size_t janet_getsize(const Janet *argv, int32_t n);
|
2019-01-06 01:09:03 +00:00
|
|
|
JANET_API JanetView janet_getindexed(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetByteView janet_getbytes(const Janet *argv, int32_t n);
|
|
|
|
JANET_API JanetDictView janet_getdictionary(const Janet *argv, int32_t n);
|
|
|
|
JANET_API void *janet_getabstract(const Janet *argv, int32_t n, const JanetAbstractType *at);
|
|
|
|
JANET_API JanetRange janet_getslice(int32_t argc, const Janet *argv);
|
2019-01-09 18:25:51 +00:00
|
|
|
JANET_API int32_t janet_gethalfrange(const Janet *argv, int32_t n, int32_t length, const char *which);
|
2019-01-12 22:31:15 +00:00
|
|
|
JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length, const char *which);
|
2019-11-09 15:39:14 +00:00
|
|
|
JANET_API uint64_t janet_getflags(const Janet *argv, int32_t n, const char *flags);
|
2018-09-06 02:18:42 +00:00
|
|
|
|
2019-09-25 00:40:49 +00:00
|
|
|
/* Optionals */
|
|
|
|
JANET_API double janet_optnumber(const Janet *argv, int32_t argc, int32_t n, double dflt);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetTuple janet_opttuple(const Janet *argv, int32_t argc, int32_t n, JanetTuple dflt);
|
|
|
|
JANET_API JanetStruct janet_optstruct(const Janet *argv, int32_t argc, int32_t n, JanetStruct dflt);
|
|
|
|
JANET_API JanetString janet_optstring(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
2019-09-25 00:40:49 +00:00
|
|
|
JANET_API const char *janet_optcstring(const Janet *argv, int32_t argc, int32_t n, const char *dflt);
|
2019-12-10 02:12:10 +00:00
|
|
|
JANET_API JanetSymbol janet_optsymbol(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
|
|
|
JANET_API JanetKeyword janet_optkeyword(const Janet *argv, int32_t argc, int32_t n, JanetString dflt);
|
2019-09-25 00:40:49 +00:00
|
|
|
JANET_API JanetFiber *janet_optfiber(const Janet *argv, int32_t argc, int32_t n, JanetFiber *dflt);
|
|
|
|
JANET_API JanetFunction *janet_optfunction(const Janet *argv, int32_t argc, int32_t n, JanetFunction *dflt);
|
|
|
|
JANET_API JanetCFunction janet_optcfunction(const Janet *argv, int32_t argc, int32_t n, JanetCFunction dflt);
|
|
|
|
JANET_API int janet_optboolean(const Janet *argv, int32_t argc, int32_t n, int dflt);
|
|
|
|
JANET_API void *janet_optpointer(const Janet *argv, int32_t argc, int32_t n, void *dflt);
|
2019-11-09 15:39:14 +00:00
|
|
|
JANET_API int32_t janet_optnat(const Janet *argv, int32_t argc, int32_t n, int32_t dflt);
|
2019-09-25 00:40:49 +00:00
|
|
|
JANET_API int32_t janet_optinteger(const Janet *argv, int32_t argc, int32_t n, int32_t dflt);
|
|
|
|
JANET_API int64_t janet_optinteger64(const Janet *argv, int32_t argc, int32_t n, int64_t dflt);
|
|
|
|
JANET_API size_t janet_optsize(const Janet *argv, int32_t argc, int32_t n, size_t dflt);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetAbstract janet_optabstract(const Janet *argv, int32_t argc, int32_t n, const JanetAbstractType *at, JanetAbstract dflt);
|
2019-09-25 00:40:49 +00:00
|
|
|
|
2019-11-09 15:39:14 +00:00
|
|
|
/* Mutable optional types specify a size default, and construct a new value if none is provided */
|
|
|
|
JANET_API JanetBuffer *janet_optbuffer(const Janet *argv, int32_t argc, int32_t n, int32_t dflt_len);
|
|
|
|
JANET_API JanetTable *janet_opttable(const Janet *argv, int32_t argc, int32_t n, int32_t dflt_len);
|
|
|
|
JANET_API JanetArray *janet_optarray(const Janet *argv, int32_t argc, int32_t n, int32_t dflt_len);
|
|
|
|
|
2019-04-16 23:10:01 +00:00
|
|
|
JANET_API Janet janet_dyn(const char *name);
|
|
|
|
JANET_API void janet_setdyn(const char *name, Janet value);
|
|
|
|
|
2020-03-14 15:12:47 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_file_type;
|
|
|
|
|
2019-12-14 15:03:56 +00:00
|
|
|
#define JANET_FILE_WRITE 1
|
|
|
|
#define JANET_FILE_READ 2
|
|
|
|
#define JANET_FILE_APPEND 4
|
|
|
|
#define JANET_FILE_UPDATE 8
|
|
|
|
#define JANET_FILE_NOT_CLOSEABLE 16
|
|
|
|
#define JANET_FILE_CLOSED 32
|
|
|
|
#define JANET_FILE_BINARY 64
|
|
|
|
#define JANET_FILE_SERIALIZABLE 128
|
2020-08-27 12:46:00 +00:00
|
|
|
#define JANET_FILE_NONIL 512
|
2019-12-14 15:03:56 +00:00
|
|
|
|
2020-07-03 21:19:05 +00:00
|
|
|
JANET_API Janet janet_makefile(FILE *f, int32_t flags);
|
2020-09-13 00:48:12 +00:00
|
|
|
JANET_API JanetFile *janet_makejfile(FILE *f, int32_t flags);
|
2020-07-03 21:19:05 +00:00
|
|
|
JANET_API FILE *janet_getfile(const Janet *argv, int32_t n, int32_t *flags);
|
2019-04-17 01:44:19 +00:00
|
|
|
JANET_API FILE *janet_dynfile(const char *name, FILE *def);
|
2020-08-29 22:36:14 +00:00
|
|
|
JANET_API JanetFile *janet_getjfile(const Janet *argv, int32_t n);
|
2019-12-29 14:49:05 +00:00
|
|
|
JANET_API JanetAbstract janet_checkfile(Janet j);
|
2020-07-03 21:19:05 +00:00
|
|
|
JANET_API FILE *janet_unwrapfile(Janet j, int32_t *flags);
|
2021-01-16 21:11:07 +00:00
|
|
|
JANET_API int janet_file_close(JanetFile *file);
|
2019-04-17 01:44:19 +00:00
|
|
|
|
2020-08-07 02:02:26 +00:00
|
|
|
JANET_API int janet_cryptorand(uint8_t *out, size_t n);
|
2020-08-04 00:11:05 +00:00
|
|
|
|
2019-02-24 21:24:18 +00:00
|
|
|
/* Marshal API */
|
2019-04-27 19:45:28 +00:00
|
|
|
JANET_API void janet_marshal_size(JanetMarshalContext *ctx, size_t value);
|
2019-02-23 16:13:43 +00:00
|
|
|
JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value);
|
2019-03-19 01:12:38 +00:00
|
|
|
JANET_API void janet_marshal_int64(JanetMarshalContext *ctx, int64_t value);
|
2019-02-23 16:13:43 +00:00
|
|
|
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value);
|
2019-03-08 03:19:34 +00:00
|
|
|
JANET_API void janet_marshal_bytes(JanetMarshalContext *ctx, const uint8_t *bytes, size_t len);
|
2019-02-23 16:13:43 +00:00
|
|
|
JANET_API void janet_marshal_janet(JanetMarshalContext *ctx, Janet x);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API void janet_marshal_abstract(JanetMarshalContext *ctx, JanetAbstract abstract);
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-12-07 04:12:18 +00:00
|
|
|
JANET_API void janet_unmarshal_ensure(JanetMarshalContext *ctx, size_t size);
|
2019-04-27 19:45:28 +00:00
|
|
|
JANET_API size_t janet_unmarshal_size(JanetMarshalContext *ctx);
|
2019-03-19 01:12:38 +00:00
|
|
|
JANET_API int32_t janet_unmarshal_int(JanetMarshalContext *ctx);
|
|
|
|
JANET_API int64_t janet_unmarshal_int64(JanetMarshalContext *ctx);
|
|
|
|
JANET_API uint8_t janet_unmarshal_byte(JanetMarshalContext *ctx);
|
2019-03-08 03:19:34 +00:00
|
|
|
JANET_API void janet_unmarshal_bytes(JanetMarshalContext *ctx, uint8_t *dest, size_t len);
|
2019-03-19 01:12:38 +00:00
|
|
|
JANET_API Janet janet_unmarshal_janet(JanetMarshalContext *ctx);
|
2019-12-10 02:05:53 +00:00
|
|
|
JANET_API JanetAbstract janet_unmarshal_abstract(JanetMarshalContext *ctx, size_t size);
|
2021-08-17 02:14:06 +00:00
|
|
|
JANET_API void janet_unmarshal_abstract_reuse(JanetMarshalContext *ctx, void *p);
|
2019-02-22 14:57:48 +00:00
|
|
|
|
2019-02-24 01:02:54 +00:00
|
|
|
JANET_API void janet_register_abstract_type(const JanetAbstractType *at);
|
2019-02-24 01:51:34 +00:00
|
|
|
JANET_API const JanetAbstractType *janet_get_abstract_type(Janet key);
|
2019-02-23 16:13:43 +00:00
|
|
|
|
2020-03-14 15:12:47 +00:00
|
|
|
#ifdef JANET_PEG
|
|
|
|
|
|
|
|
extern JANET_API const JanetAbstractType janet_peg_type;
|
|
|
|
|
|
|
|
/* opcodes for peg vm */
|
|
|
|
typedef enum {
|
|
|
|
RULE_LITERAL, /* [len, bytes...] */
|
|
|
|
RULE_NCHAR, /* [n] */
|
|
|
|
RULE_NOTNCHAR, /* [n] */
|
|
|
|
RULE_RANGE, /* [lo | hi << 16 (1 word)] */
|
|
|
|
RULE_SET, /* [bitmap (8 words)] */
|
|
|
|
RULE_LOOK, /* [offset, rule] */
|
|
|
|
RULE_CHOICE, /* [len, rules...] */
|
|
|
|
RULE_SEQUENCE, /* [len, rules...] */
|
|
|
|
RULE_IF, /* [rule_a, rule_b (b if a)] */
|
|
|
|
RULE_IFNOT, /* [rule_a, rule_b (b if not a)] */
|
|
|
|
RULE_NOT, /* [rule] */
|
|
|
|
RULE_BETWEEN, /* [lo, hi, rule] */
|
|
|
|
RULE_GETTAG, /* [searchtag, tag] */
|
|
|
|
RULE_CAPTURE, /* [rule, tag] */
|
|
|
|
RULE_POSITION, /* [tag] */
|
|
|
|
RULE_ARGUMENT, /* [argument-index, tag] */
|
|
|
|
RULE_CONSTANT, /* [constant, tag] */
|
|
|
|
RULE_ACCUMULATE, /* [rule, tag] */
|
|
|
|
RULE_GROUP, /* [rule, tag] */
|
|
|
|
RULE_REPLACE, /* [rule, constant, tag] */
|
|
|
|
RULE_MATCHTIME, /* [rule, constant, tag] */
|
|
|
|
RULE_ERROR, /* [rule] */
|
|
|
|
RULE_DROP, /* [rule] */
|
|
|
|
RULE_BACKMATCH, /* [tag] */
|
2020-06-11 02:18:50 +00:00
|
|
|
RULE_TO, /* [rule] */
|
|
|
|
RULE_THRU, /* [rule] */
|
2020-05-02 15:37:39 +00:00
|
|
|
RULE_LENPREFIX, /* [rule_a, rule_b (repeat rule_b rule_a times)] */
|
2020-09-27 17:18:12 +00:00
|
|
|
RULE_READINT, /* [(signedness << 4) | (endianess << 5) | bytewidth, tag] */
|
2020-11-27 00:32:56 +00:00
|
|
|
RULE_LINE, /* [tag] */
|
2021-02-26 23:25:09 +00:00
|
|
|
RULE_COLUMN, /* [tag] */
|
2021-09-19 18:02:16 +00:00
|
|
|
RULE_UNREF, /* [rule, tag] */
|
|
|
|
RULE_CAPTURE_NUM /* [rule, tag] */
|
2020-11-27 00:32:56 +00:00
|
|
|
} JanetPegOpcod;
|
2020-03-14 15:12:47 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t *bytecode;
|
|
|
|
Janet *constants;
|
|
|
|
size_t bytecode_len;
|
|
|
|
uint32_t num_constants;
|
2021-01-06 01:51:00 +00:00
|
|
|
int has_backref;
|
2020-03-14 15:12:47 +00:00
|
|
|
} JanetPeg;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
#ifdef JANET_INT_TYPES
|
2019-03-13 00:28:26 +00:00
|
|
|
|
2020-03-14 15:12:47 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_s64_type;
|
2020-03-16 22:02:49 +00:00
|
|
|
extern JANET_API const JanetAbstractType janet_u64_type;
|
2020-03-14 15:12:47 +00:00
|
|
|
|
2019-03-13 00:28:26 +00:00
|
|
|
typedef enum {
|
2019-03-19 01:12:38 +00:00
|
|
|
JANET_INT_NONE,
|
|
|
|
JANET_INT_S64,
|
|
|
|
JANET_INT_U64
|
|
|
|
} JanetIntType;
|
|
|
|
|
|
|
|
JANET_API JanetIntType janet_is_int(Janet x);
|
|
|
|
JANET_API Janet janet_wrap_s64(int64_t x);
|
|
|
|
JANET_API Janet janet_wrap_u64(uint64_t x);
|
|
|
|
JANET_API int64_t janet_unwrap_s64(Janet x);
|
|
|
|
JANET_API uint64_t janet_unwrap_u64(Janet x);
|
2019-03-16 15:46:57 +00:00
|
|
|
JANET_API int janet_scan_int64(const uint8_t *str, int32_t len, int64_t *out);
|
|
|
|
JANET_API int janet_scan_uint64(const uint8_t *str, int32_t len, uint64_t *out);
|
2019-03-13 00:28:26 +00:00
|
|
|
|
2019-03-19 01:12:38 +00:00
|
|
|
#endif
|
2019-03-13 00:28:26 +00:00
|
|
|
|
2021-03-23 10:00:48 +00:00
|
|
|
/* Custom allocator support */
|
|
|
|
JANET_API void *(janet_malloc)(size_t);
|
|
|
|
JANET_API void *(janet_realloc)(void *, size_t);
|
|
|
|
JANET_API void *(janet_calloc)(size_t, size_t);
|
|
|
|
JANET_API void (janet_free)(void *);
|
|
|
|
#ifndef janet_malloc
|
|
|
|
#define janet_malloc(X) malloc((X))
|
|
|
|
#endif
|
|
|
|
#ifndef janet_realloc
|
|
|
|
#define janet_realloc(X, Y) realloc((X), (Y))
|
|
|
|
#endif
|
|
|
|
#ifndef janet_calloc
|
|
|
|
#define janet_calloc(X, Y) calloc((X), (Y))
|
|
|
|
#endif
|
|
|
|
#ifndef janet_free
|
|
|
|
#define janet_free(X) free((X))
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
/***** END SECTION MAIN *****/
|
|
|
|
|
2020-04-25 21:13:00 +00:00
|
|
|
/* Re-enable popped variable length array warnings */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* JANET_H_defined */
|