1 /*
2 * Copyright (c) 2021 Calvin Rose
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 
23 #ifndef JANET_STATE_H_defined
24 #define JANET_STATE_H_defined
25 
26 #include <stdint.h>
27 
28 typedef int64_t JanetTimestamp;
29 
30 typedef struct JanetScratch {
31     JanetScratchFinalizer finalize;
32     long long mem[]; /* for proper alignment */
33 } JanetScratch;
34 
35 typedef struct {
36     JanetGCObject *self;
37     JanetGCObject *other;
38     int32_t index;
39     int32_t index2;
40 } JanetTraversalNode;
41 
42 typedef struct {
43     int32_t capacity;
44     int32_t head;
45     int32_t tail;
46     void *data;
47 } JanetQueue;
48 
49 typedef struct {
50     JanetTimestamp when;
51     JanetFiber *fiber;
52     JanetFiber *curr_fiber;
53     uint32_t sched_id;
54     int is_error;
55 } JanetTimeout;
56 
57 /* Registry table for C functions - containts metadata that can
58  * be looked up by cfunction pointer. All strings here are pointing to
59  * static memory not managed by Janet. */
60 typedef struct {
61     JanetCFunction cfun;
62     const char *name;
63     const char *name_prefix;
64     const char *source_file;
65     int32_t source_line;
66     /* int32_t min_arity; */
67     /* int32_t max_arity; */
68 } JanetCFunRegistry;
69 
70 struct JanetVM {
71     /* Place for user data */
72     void *user;
73 
74     /* Top level dynamic bindings */
75     JanetTable *top_dyns;
76 
77     /* Cache the core environment */
78     JanetTable *core_env;
79 
80     /* How many VM stacks have been entered */
81     int stackn;
82 
83     /* If this flag is true, suspend on function calls and backwards jumps.
84      * When this occurs, this flag will be reset to 0. */
85     int auto_suspend;
86 
87     /* The current running fiber on the current thread.
88      * Set and unset by janet_run. */
89     JanetFiber *fiber;
90     JanetFiber *root_fiber;
91 
92     /* The current pointer to the inner most jmp_buf. The current
93      * return point for panics. */
94     jmp_buf *signal_buf;
95     Janet *return_reg;
96 
97     /* The global registry for c functions. Used to store meta-data
98      * along with otherwise bare c function pointers. */
99     JanetCFunRegistry *registry;
100     size_t registry_cap;
101     size_t registry_count;
102     int registry_dirty;
103 
104     /* Registry for abstract abstract types that can be marshalled.
105      * We need this to look up the constructors when unmarshalling. */
106     JanetTable *abstract_registry;
107 
108     /* Immutable value cache */
109     const uint8_t **cache;
110     uint32_t cache_capacity;
111     uint32_t cache_count;
112     uint32_t cache_deleted;
113     uint8_t gensym_counter[8];
114 
115     /* Garbage collection */
116     void *blocks;
117     size_t gc_interval;
118     size_t next_collection;
119     size_t block_count;
120     int gc_suspend;
121 
122     /* GC roots */
123     Janet *roots;
124     size_t root_count;
125     size_t root_capacity;
126 
127     /* Scratch memory */
128     JanetScratch **scratch_mem;
129     size_t scratch_cap;
130     size_t scratch_len;
131 
132     /* Random number generator */
133     JanetRNG rng;
134 
135     /* Traversal pointers */
136     JanetTraversalNode *traversal;
137     JanetTraversalNode *traversal_top;
138     JanetTraversalNode *traversal_base;
139 
140     /* Event loop and scheduler globals */
141 #ifdef JANET_EV
142     size_t tq_count;
143     size_t tq_capacity;
144     JanetQueue spawn;
145     JanetTimeout *tq;
146     JanetRNG ev_rng;
147     JanetListenerState **listeners;
148     size_t listener_count;
149     size_t listener_cap;
150     size_t extra_listeners;
151     JanetTable threaded_abstracts; /* All abstract types that can be shared between threads (used in this thread) */
152 #ifdef JANET_WINDOWS
153     void **iocp;
154 #elif defined(JANET_EV_EPOLL)
155     JanetHandle selfpipe[2];
156     int epoll;
157     int timerfd;
158     int timer_enabled;
159 #elif defined(JANET_EV_KQUEUE)
160     JanetHandle selfpipe[2];
161     int kq;
162     int timer;
163     int timer_enabled;
164 #else
165     JanetHandle selfpipe[2];
166     struct pollfd *fds;
167 #endif
168 #endif
169 
170 };
171 
172 extern JANET_THREAD_LOCAL JanetVM janet_vm;
173 
174 #ifdef JANET_NET
175 void janet_net_init(void);
176 void janet_net_deinit(void);
177 #endif
178 
179 #ifdef JANET_EV
180 void janet_ev_init(void);
181 void janet_ev_deinit(void);
182 #endif
183 
184 #endif /* JANET_STATE_H_defined */
185