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_FIBER_H_defined
24 #define JANET_FIBER_H_defined
25 
26 #ifndef JANET_AMALG
27 #include <janet.h>
28 #endif
29 
30 /* Fiber signal masks. */
31 #define JANET_FIBER_MASK_ERROR 2
32 #define JANET_FIBER_MASK_DEBUG 4
33 #define JANET_FIBER_MASK_YIELD 8
34 
35 #define JANET_FIBER_MASK_USER0 (16 << 0)
36 #define JANET_FIBER_MASK_USER1 (16 << 1)
37 #define JANET_FIBER_MASK_USER2 (16 << 2)
38 #define JANET_FIBER_MASK_USER3 (16 << 3)
39 #define JANET_FIBER_MASK_USER4 (16 << 4)
40 #define JANET_FIBER_MASK_USER5 (16 << 5)
41 #define JANET_FIBER_MASK_USER6 (16 << 6)
42 #define JANET_FIBER_MASK_USER7 (16 << 7)
43 #define JANET_FIBER_MASK_USER8 (16 << 8)
44 #define JANET_FIBER_MASK_USER9 (16 << 9)
45 
46 #define JANET_FIBER_MASK_USERN(N) (16 << (N))
47 #define JANET_FIBER_MASK_USER 0x3FF0
48 
49 #define JANET_FIBER_STATUS_MASK 0x3F0000
50 #define JANET_FIBER_RESUME_SIGNAL 0x400000
51 #define JANET_FIBER_FLAG_CANCELED 0x400000
52 #define JANET_FIBER_STATUS_OFFSET 16
53 
54 #define JANET_FIBER_BREAKPOINT       0x1000000
55 #define JANET_FIBER_RESUME_NO_USEVAL 0x2000000
56 #define JANET_FIBER_RESUME_NO_SKIP   0x4000000
57 #define JANET_FIBER_DID_LONGJUMP     0x8000000
58 #define JANET_FIBER_FLAG_MASK        0xF000000
59 
60 #define janet_fiber_set_status(f, s) do {\
61     (f)->flags &= ~JANET_FIBER_STATUS_MASK;\
62     (f)->flags |= (s) << JANET_FIBER_STATUS_OFFSET;\
63 } while (0)
64 
65 #define janet_stack_frame(s) ((JanetStackFrame *)((s) - JANET_FRAME_SIZE))
66 #define janet_fiber_frame(f) janet_stack_frame((f)->data + (f)->frame)
67 void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n);
68 void janet_fiber_push(JanetFiber *fiber, Janet x);
69 void janet_fiber_push2(JanetFiber *fiber, Janet x, Janet y);
70 void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z);
71 void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n);
72 int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func);
73 int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func);
74 void janet_fiber_cframe(JanetFiber *fiber, JanetCFunction cfun);
75 void janet_fiber_popframe(JanetFiber *fiber);
76 void janet_env_maybe_detach(JanetFuncEnv *env);
77 int janet_env_valid(JanetFuncEnv *env);
78 
79 #ifdef JANET_EV
80 void janet_fiber_did_resume(JanetFiber *fiber);
81 #endif
82 
83 #endif
84