2018-01-20 16:04:30 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2018-01-20 16:04:30 +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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifndef JANET_STATE_H_defined
|
|
|
|
#define JANET_STATE_H_defined
|
2018-01-20 16:04:30 +00:00
|
|
|
|
2023-04-01 23:57:13 +00:00
|
|
|
#include <janet.h>
|
2018-01-20 16:04:30 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-11-11 17:48:50 +00:00
|
|
|
#ifdef JANET_EV
|
|
|
|
#ifndef JANET_WINDOWS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
typedef int64_t JanetTimestamp;
|
|
|
|
|
|
|
|
typedef struct JanetScratch {
|
|
|
|
JanetScratchFinalizer finalize;
|
|
|
|
long long mem[]; /* for proper alignment */
|
|
|
|
} JanetScratch;
|
|
|
|
|
2020-04-24 21:18:31 +00:00
|
|
|
typedef struct {
|
|
|
|
JanetGCObject *self;
|
|
|
|
JanetGCObject *other;
|
|
|
|
int32_t index;
|
|
|
|
int32_t index2;
|
|
|
|
} JanetTraversalNode;
|
2021-07-17 01:59:03 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int32_t capacity;
|
|
|
|
int32_t head;
|
|
|
|
int32_t tail;
|
|
|
|
void *data;
|
|
|
|
} JanetQueue;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
JanetTimestamp when;
|
|
|
|
JanetFiber *fiber;
|
|
|
|
JanetFiber *curr_fiber;
|
|
|
|
uint32_t sched_id;
|
|
|
|
int is_error;
|
|
|
|
} JanetTimeout;
|
|
|
|
|
2023-01-25 09:45:19 +00:00
|
|
|
/* Registry table for C functions - contains metadata that can
|
2021-07-30 02:29:08 +00:00
|
|
|
* be looked up by cfunction pointer. All strings here are pointing to
|
|
|
|
* static memory not managed by Janet. */
|
|
|
|
typedef struct {
|
|
|
|
JanetCFunction cfun;
|
|
|
|
const char *name;
|
|
|
|
const char *name_prefix;
|
|
|
|
const char *source_file;
|
|
|
|
int32_t source_line;
|
|
|
|
/* int32_t min_arity; */
|
|
|
|
/* int32_t max_arity; */
|
|
|
|
} JanetCFunRegistry;
|
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
struct JanetVM {
|
2021-08-27 16:46:42 +00:00
|
|
|
/* Place for user data */
|
|
|
|
void *user;
|
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
/* Top level dynamic bindings */
|
|
|
|
JanetTable *top_dyns;
|
|
|
|
|
|
|
|
/* Cache the core environment */
|
|
|
|
JanetTable *core_env;
|
|
|
|
|
|
|
|
/* How many VM stacks have been entered */
|
|
|
|
int stackn;
|
|
|
|
|
2021-07-24 20:14:37 +00:00
|
|
|
/* If this flag is true, suspend on function calls and backwards jumps.
|
|
|
|
* When this occurs, this flag will be reset to 0. */
|
2023-10-01 15:52:05 +00:00
|
|
|
volatile JanetAtomicInt auto_suspend;
|
2021-07-24 20:14:37 +00:00
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
/* The current running fiber on the current thread.
|
2023-01-25 09:45:19 +00:00
|
|
|
* Set and unset by functions in vm.c */
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
JanetFiber *root_fiber;
|
|
|
|
|
|
|
|
/* The current pointer to the inner most jmp_buf. The current
|
|
|
|
* return point for panics. */
|
|
|
|
jmp_buf *signal_buf;
|
|
|
|
Janet *return_reg;
|
|
|
|
|
|
|
|
/* The global registry for c functions. Used to store meta-data
|
|
|
|
* along with otherwise bare c function pointers. */
|
2021-07-30 02:29:08 +00:00
|
|
|
JanetCFunRegistry *registry;
|
|
|
|
size_t registry_cap;
|
|
|
|
size_t registry_count;
|
|
|
|
int registry_dirty;
|
2021-07-17 01:59:03 +00:00
|
|
|
|
2023-01-25 09:45:19 +00:00
|
|
|
/* Registry for abstract types that can be marshalled.
|
2021-07-17 01:59:03 +00:00
|
|
|
* We need this to look up the constructors when unmarshalling. */
|
|
|
|
JanetTable *abstract_registry;
|
|
|
|
|
|
|
|
/* Immutable value cache */
|
|
|
|
const uint8_t **cache;
|
|
|
|
uint32_t cache_capacity;
|
|
|
|
uint32_t cache_count;
|
|
|
|
uint32_t cache_deleted;
|
|
|
|
uint8_t gensym_counter[8];
|
|
|
|
|
|
|
|
/* Garbage collection */
|
|
|
|
void *blocks;
|
2023-09-28 04:27:48 +00:00
|
|
|
void *weak_blocks;
|
2021-07-17 01:59:03 +00:00
|
|
|
size_t gc_interval;
|
|
|
|
size_t next_collection;
|
|
|
|
size_t block_count;
|
|
|
|
int gc_suspend;
|
2023-09-24 17:08:40 +00:00
|
|
|
int gc_mark_phase;
|
2021-07-17 01:59:03 +00:00
|
|
|
|
|
|
|
/* GC roots */
|
|
|
|
Janet *roots;
|
|
|
|
size_t root_count;
|
|
|
|
size_t root_capacity;
|
|
|
|
|
|
|
|
/* Scratch memory */
|
|
|
|
JanetScratch **scratch_mem;
|
|
|
|
size_t scratch_cap;
|
|
|
|
size_t scratch_len;
|
|
|
|
|
2023-02-06 14:41:04 +00:00
|
|
|
/* Sandbox flags */
|
|
|
|
uint32_t sandbox_flags;
|
|
|
|
|
2021-07-17 01:59:03 +00:00
|
|
|
/* Random number generator */
|
|
|
|
JanetRNG rng;
|
|
|
|
|
|
|
|
/* Traversal pointers */
|
|
|
|
JanetTraversalNode *traversal;
|
|
|
|
JanetTraversalNode *traversal_top;
|
|
|
|
JanetTraversalNode *traversal_base;
|
|
|
|
|
|
|
|
/* Event loop and scheduler globals */
|
|
|
|
#ifdef JANET_EV
|
|
|
|
size_t tq_count;
|
|
|
|
size_t tq_capacity;
|
|
|
|
JanetQueue spawn;
|
|
|
|
JanetTimeout *tq;
|
|
|
|
JanetRNG ev_rng;
|
2023-10-01 15:27:51 +00:00
|
|
|
volatile JanetAtomicInt listener_count; /* used in signal handler, must be volatile */
|
2021-08-20 01:56:48 +00:00
|
|
|
JanetTable threaded_abstracts; /* All abstract types that can be shared between threads (used in this thread) */
|
2023-04-01 23:57:13 +00:00
|
|
|
JanetTable active_tasks; /* All possibly live task fibers - used just for tracking */
|
2023-09-27 03:57:01 +00:00
|
|
|
JanetArray *listeners; /* For GC */
|
2023-08-19 18:13:22 +00:00
|
|
|
JanetTable signal_handlers;
|
2021-07-17 01:59:03 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2021-07-17 02:10:02 +00:00
|
|
|
void **iocp;
|
2021-07-17 01:59:03 +00:00
|
|
|
#elif defined(JANET_EV_EPOLL)
|
2022-11-11 17:01:59 +00:00
|
|
|
pthread_attr_t new_thread_attr;
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetHandle selfpipe[2];
|
|
|
|
int epoll;
|
|
|
|
int timerfd;
|
|
|
|
int timer_enabled;
|
2021-09-03 19:29:13 +00:00
|
|
|
#elif defined(JANET_EV_KQUEUE)
|
2022-11-11 17:01:59 +00:00
|
|
|
pthread_attr_t new_thread_attr;
|
2021-09-03 19:29:13 +00:00
|
|
|
JanetHandle selfpipe[2];
|
|
|
|
int kq;
|
|
|
|
int timer;
|
2021-09-03 21:31:20 +00:00
|
|
|
int timer_enabled;
|
2021-07-17 01:59:03 +00:00
|
|
|
#else
|
2022-11-11 17:01:59 +00:00
|
|
|
pthread_attr_t new_thread_attr;
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetHandle selfpipe[2];
|
|
|
|
struct pollfd *fds;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
extern JANET_THREAD_LOCAL JanetVM janet_vm;
|
2020-04-24 21:18:31 +00:00
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
void janet_net_init(void);
|
|
|
|
void janet_net_deinit(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_EV
|
|
|
|
void janet_ev_init(void);
|
|
|
|
void janet_ev_deinit(void);
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#endif /* JANET_STATE_H_defined */
|