2017-11-06 03:05:47 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2017-11-06 03:05:47 +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.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-03-11 19:35:23 +00:00
|
|
|
#include "fiber.h"
|
2018-06-12 18:24:45 +00:00
|
|
|
#include "state.h"
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
static void fiber_reset(JanetFiber *fiber) {
|
|
|
|
fiber->maxstack = JANET_STACK_MAX;
|
|
|
|
fiber->frame = 0;
|
|
|
|
fiber->stackstart = JANET_FRAME_SIZE;
|
|
|
|
fiber->stacktop = JANET_FRAME_SIZE;
|
|
|
|
fiber->child = NULL;
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags = JANET_FIBER_MASK_YIELD | JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP;
|
2019-04-16 19:41:45 +00:00
|
|
|
fiber->env = NULL;
|
2021-01-03 22:17:36 +00:00
|
|
|
fiber->last_value = janet_wrap_nil();
|
2020-07-05 22:26:17 +00:00
|
|
|
#ifdef JANET_EV
|
2020-07-03 18:47:48 +00:00
|
|
|
fiber->waiting = NULL;
|
2020-08-09 05:20:27 +00:00
|
|
|
fiber->sched_id = 0;
|
2021-01-13 03:35:28 +00:00
|
|
|
fiber->supervisor_channel = NULL;
|
2020-07-05 22:26:17 +00:00
|
|
|
#endif
|
2019-01-12 22:31:15 +00:00
|
|
|
janet_fiber_set_status(fiber, JANET_STATUS_NEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JanetFiber *fiber_alloc(int32_t capacity) {
|
2018-11-26 14:02:07 +00:00
|
|
|
Janet *data;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFiber *fiber = janet_gcalloc(JANET_MEMORY_FIBER, sizeof(JanetFiber));
|
2018-12-24 04:38:49 +00:00
|
|
|
if (capacity < 32) {
|
|
|
|
capacity = 32;
|
2018-03-11 19:35:23 +00:00
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
fiber->capacity = capacity;
|
2021-03-23 10:00:48 +00:00
|
|
|
data = janet_malloc(sizeof(Janet) * (size_t) capacity);
|
2018-11-26 14:02:07 +00:00
|
|
|
if (NULL == data) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += sizeof(Janet) * capacity;
|
2018-11-26 14:02:07 +00:00
|
|
|
fiber->data = data;
|
2017-11-06 03:05:47 +00:00
|
|
|
return fiber;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
/* Create a new fiber with argn values on the stack by reusing a fiber. */
|
|
|
|
JanetFiber *janet_fiber_reset(JanetFiber *fiber, JanetFunction *callee, int32_t argc, const Janet *argv) {
|
2018-11-26 14:02:07 +00:00
|
|
|
int32_t newstacktop;
|
2019-01-12 22:31:15 +00:00
|
|
|
fiber_reset(fiber);
|
|
|
|
if (argc) {
|
|
|
|
newstacktop = fiber->stacktop + argc;
|
|
|
|
if (newstacktop >= fiber->capacity) {
|
|
|
|
janet_fiber_setcapacity(fiber, 2 * newstacktop);
|
|
|
|
}
|
2020-02-23 20:47:29 +00:00
|
|
|
if (argv) {
|
|
|
|
memcpy(fiber->data + fiber->stacktop, argv, argc * sizeof(Janet));
|
|
|
|
} else {
|
|
|
|
/* If argv not given, fill with nil */
|
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
|
|
|
fiber->data[fiber->stacktop + i] = janet_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 22:31:15 +00:00
|
|
|
fiber->stacktop = newstacktop;
|
2018-11-26 14:02:07 +00:00
|
|
|
}
|
2023-06-01 15:52:34 +00:00
|
|
|
/* Don't panic on failure since we use this to implement janet_pcall */
|
2018-12-30 23:41:44 +00:00
|
|
|
if (janet_fiber_funcframe(fiber, callee)) return NULL;
|
2019-02-01 16:56:25 +00:00
|
|
|
janet_fiber_frame(fiber)->flags |= JANET_STACKFRAME_ENTRANCE;
|
2020-11-15 18:13:30 +00:00
|
|
|
#ifdef JANET_EV
|
2020-07-20 00:41:12 +00:00
|
|
|
fiber->waiting = NULL;
|
2021-01-13 03:35:28 +00:00
|
|
|
fiber->supervisor_channel = NULL;
|
2020-11-15 18:13:30 +00:00
|
|
|
#endif
|
2018-11-26 14:02:07 +00:00
|
|
|
return fiber;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:31:15 +00:00
|
|
|
/* Create a new fiber with argn values on the stack. */
|
|
|
|
JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity, int32_t argc, const Janet *argv) {
|
2023-06-04 16:21:52 +00:00
|
|
|
return janet_fiber_reset(fiber_alloc(capacity), callee, argc, argv);
|
2019-01-12 22:31:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 12:01:58 +00:00
|
|
|
#ifdef JANET_DEBUG
|
|
|
|
/* Test for memory issues by reallocating fiber every time we push a stack frame */
|
|
|
|
static void janet_fiber_refresh_memory(JanetFiber *fiber) {
|
|
|
|
int32_t n = fiber->capacity;
|
|
|
|
if (n) {
|
2021-03-23 10:00:48 +00:00
|
|
|
Janet *newData = janet_malloc(sizeof(Janet) * n);
|
2020-08-17 12:01:58 +00:00
|
|
|
if (NULL == newData) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(newData, fiber->data, fiber->capacity * sizeof(Janet));
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(fiber->data);
|
2020-08-17 12:01:58 +00:00
|
|
|
fiber->data = newData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Ensure that the fiber has enough extra capacity */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_setcapacity(JanetFiber *fiber, int32_t n) {
|
2020-10-18 23:41:18 +00:00
|
|
|
int32_t old_size = fiber->capacity;
|
|
|
|
int32_t diff = n - old_size;
|
2021-03-23 10:00:48 +00:00
|
|
|
Janet *newData = janet_realloc(fiber->data, sizeof(Janet) * n);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (NULL == newData) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
fiber->data = newData;
|
|
|
|
fiber->capacity = n;
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += sizeof(Janet) * diff;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 05:19:51 +00:00
|
|
|
/* Grow fiber if needed */
|
|
|
|
static void janet_fiber_grow(JanetFiber *fiber, int32_t needed) {
|
|
|
|
int32_t cap = needed > (INT32_MAX / 2) ? INT32_MAX : 2 * needed;
|
|
|
|
janet_fiber_setcapacity(fiber, cap);
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Push a value on the next stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_push(JanetFiber *fiber, Janet x) {
|
2019-08-19 05:19:51 +00:00
|
|
|
if (fiber->stacktop == INT32_MAX) janet_panic("stack overflow");
|
2017-11-06 03:05:47 +00:00
|
|
|
if (fiber->stacktop >= fiber->capacity) {
|
2019-08-19 05:19:51 +00:00
|
|
|
janet_fiber_grow(fiber, fiber->stacktop);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
fiber->data[fiber->stacktop++] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push 2 values on the next stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_push2(JanetFiber *fiber, Janet x, Janet y) {
|
2019-08-19 05:19:51 +00:00
|
|
|
if (fiber->stacktop >= INT32_MAX - 1) janet_panic("stack overflow");
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newtop = fiber->stacktop + 2;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (newtop > fiber->capacity) {
|
2019-08-19 05:19:51 +00:00
|
|
|
janet_fiber_grow(fiber, newtop);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
fiber->data[fiber->stacktop] = x;
|
|
|
|
fiber->data[fiber->stacktop + 1] = y;
|
|
|
|
fiber->stacktop = newtop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push 3 values on the next stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_push3(JanetFiber *fiber, Janet x, Janet y, Janet z) {
|
2019-08-19 05:19:51 +00:00
|
|
|
if (fiber->stacktop >= INT32_MAX - 2) janet_panic("stack overflow");
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newtop = fiber->stacktop + 3;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (newtop > fiber->capacity) {
|
2019-08-19 05:19:51 +00:00
|
|
|
janet_fiber_grow(fiber, newtop);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
fiber->data[fiber->stacktop] = x;
|
|
|
|
fiber->data[fiber->stacktop + 1] = y;
|
|
|
|
fiber->data[fiber->stacktop + 2] = z;
|
|
|
|
fiber->stacktop = newtop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push an array on the next stack frame */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_pushn(JanetFiber *fiber, const Janet *arr, int32_t n) {
|
2019-08-19 05:19:51 +00:00
|
|
|
if (fiber->stacktop > INT32_MAX - n) janet_panic("stack overflow");
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newtop = fiber->stacktop + n;
|
2017-11-06 03:05:47 +00:00
|
|
|
if (newtop > fiber->capacity) {
|
2019-08-19 05:19:51 +00:00
|
|
|
janet_fiber_grow(fiber, newtop);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(fiber->data + fiber->stacktop, arr, n * sizeof(Janet));
|
2017-11-06 03:05:47 +00:00
|
|
|
fiber->stacktop = newtop;
|
|
|
|
}
|
|
|
|
|
2019-05-24 21:03:22 +00:00
|
|
|
/* Create a struct with n values. If n is odd, the last value is ignored. */
|
|
|
|
static Janet make_struct_n(const Janet *args, int32_t n) {
|
|
|
|
int32_t i = 0;
|
|
|
|
JanetKV *st = janet_struct_begin(n & (~1));
|
|
|
|
for (; i < n; i += 2) {
|
|
|
|
janet_struct_put(st, args[i], args[i + 1]);
|
|
|
|
}
|
|
|
|
return janet_wrap_struct(janet_struct_end(st));
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Push a stack frame to a fiber */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func) {
|
|
|
|
JanetStackFrame *newframe;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2018-01-12 18:54:37 +00:00
|
|
|
int32_t oldtop = fiber->stacktop;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t oldframe = fiber->frame;
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t nextframe = fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t nextstacktop = nextframe + func->def->slotcount + JANET_FRAME_SIZE;
|
2018-12-02 20:29:21 +00:00
|
|
|
int32_t next_arity = fiber->stacktop - fiber->stackstart;
|
2018-08-03 17:41:44 +00:00
|
|
|
|
2018-12-30 23:41:44 +00:00
|
|
|
/* Check strict arity before messing with state */
|
2019-03-12 04:23:14 +00:00
|
|
|
if (next_arity < func->def->min_arity) return 1;
|
|
|
|
if (next_arity > func->def->max_arity) return 1;
|
2018-12-30 23:41:44 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
if (fiber->capacity < nextstacktop) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_setcapacity(fiber, 2 * nextstacktop);
|
2020-08-17 12:01:58 +00:00
|
|
|
#ifdef JANET_DEBUG
|
|
|
|
} else {
|
|
|
|
janet_fiber_refresh_memory(fiber);
|
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Nil unset stack arguments (Needed for gc correctness) */
|
|
|
|
for (i = fiber->stacktop; i < nextstacktop; ++i) {
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->data[i] = janet_wrap_nil();
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the next frame */
|
2017-11-06 03:05:47 +00:00
|
|
|
fiber->frame = nextframe;
|
2018-01-04 02:36:10 +00:00
|
|
|
fiber->stacktop = fiber->stackstart = nextstacktop;
|
2018-09-06 02:18:42 +00:00
|
|
|
newframe = janet_fiber_frame(fiber);
|
2017-11-06 03:05:47 +00:00
|
|
|
newframe->prevframe = oldframe;
|
|
|
|
newframe->pc = func->def->bytecode;
|
|
|
|
newframe->func = func;
|
2018-02-12 21:43:59 +00:00
|
|
|
newframe->env = NULL;
|
2018-03-22 00:53:39 +00:00
|
|
|
newframe->flags = 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Check varargs */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (func->def->flags & JANET_FUNCDEF_FLAG_VARARG) {
|
2018-01-12 18:54:37 +00:00
|
|
|
int32_t tuplehead = fiber->frame + func->def->arity;
|
2019-05-24 21:03:22 +00:00
|
|
|
int st = func->def->flags & JANET_FUNCDEF_FLAG_STRUCTARG;
|
2018-01-12 18:54:37 +00:00
|
|
|
if (tuplehead >= oldtop) {
|
2019-05-24 21:03:22 +00:00
|
|
|
fiber->data[tuplehead] = st
|
|
|
|
? make_struct_n(NULL, 0)
|
|
|
|
: janet_wrap_tuple(janet_tuple_n(NULL, 0));
|
2018-01-12 18:54:37 +00:00
|
|
|
} else {
|
2019-05-24 21:03:22 +00:00
|
|
|
fiber->data[tuplehead] = st
|
|
|
|
? make_struct_n(
|
2019-02-20 01:51:34 +00:00
|
|
|
fiber->data + tuplehead,
|
2019-05-24 21:03:22 +00:00
|
|
|
oldtop - tuplehead)
|
|
|
|
: janet_wrap_tuple(janet_tuple_n(
|
|
|
|
fiber->data + tuplehead,
|
|
|
|
oldtop - tuplehead));
|
2018-01-12 18:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 17:41:44 +00:00
|
|
|
|
|
|
|
/* Good return */
|
|
|
|
return 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* If a frame has a closure environment, detach it from
|
|
|
|
* the stack and have it keep its own values */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_env_detach(JanetFuncEnv *env) {
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Check for closure environment */
|
2018-02-12 21:43:59 +00:00
|
|
|
if (env) {
|
2020-04-06 15:58:47 +00:00
|
|
|
janet_env_valid(env);
|
2020-03-18 14:30:10 +00:00
|
|
|
int32_t len = env->length;
|
|
|
|
size_t s = sizeof(Janet) * (size_t) len;
|
2021-03-23 10:00:48 +00:00
|
|
|
Janet *vmem = janet_malloc(s);
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += (uint32_t) s;
|
2018-01-05 21:17:55 +00:00
|
|
|
if (NULL == vmem) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
2020-03-18 14:30:10 +00:00
|
|
|
Janet *values = env->as.fiber->data + env->offset;
|
|
|
|
safe_memcpy(vmem, values, s);
|
|
|
|
uint32_t *bitset = janet_stack_frame(values)->func->def->closure_bitset;
|
|
|
|
if (bitset) {
|
|
|
|
/* Clear unneeded references in closure environment */
|
|
|
|
for (int32_t i = 0; i < len; i += 32) {
|
|
|
|
uint32_t mask = ~(bitset[i >> 5]);
|
|
|
|
int32_t maxj = i + 32 > len ? len : i + 32;
|
|
|
|
for (int32_t j = i; j < maxj; j++) {
|
|
|
|
if (mask & 1) vmem[j] = janet_wrap_nil();
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
env->offset = 0;
|
|
|
|
env->as.values = vmem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:58:47 +00:00
|
|
|
/* Validate potentially untrusted func env (unmarshalled envs are difficult to verify) */
|
|
|
|
int janet_env_valid(JanetFuncEnv *env) {
|
|
|
|
if (env->offset < 0) {
|
|
|
|
int32_t real_offset = -(env->offset);
|
|
|
|
JanetFiber *fiber = env->as.fiber;
|
|
|
|
int32_t i = fiber->frame;
|
|
|
|
while (i > 0) {
|
|
|
|
JanetStackFrame *frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
|
|
|
if (real_offset == i &&
|
|
|
|
frame->env == env &&
|
|
|
|
frame->func &&
|
|
|
|
frame->func->def->slotcount == env->length) {
|
|
|
|
env->offset = real_offset;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
i = frame->prevframe;
|
|
|
|
}
|
|
|
|
/* Invalid, set to empty off-stack variant. */
|
|
|
|
env->offset = 0;
|
|
|
|
env->length = 0;
|
|
|
|
env->as.values = NULL;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:55:32 +00:00
|
|
|
/* Detach a fiber from the env if the target fiber has stopped mutating */
|
|
|
|
void janet_env_maybe_detach(JanetFuncEnv *env) {
|
|
|
|
/* Check for detachable closure envs */
|
2020-04-06 15:58:47 +00:00
|
|
|
janet_env_valid(env);
|
|
|
|
if (env->offset > 0) {
|
2020-03-17 23:55:32 +00:00
|
|
|
JanetFiberStatus s = janet_fiber_status(env->as.fiber);
|
|
|
|
int isFinished = s == JANET_STATUS_DEAD ||
|
|
|
|
s == JANET_STATUS_ERROR ||
|
|
|
|
s == JANET_STATUS_USER0 ||
|
|
|
|
s == JANET_STATUS_USER1 ||
|
|
|
|
s == JANET_STATUS_USER2 ||
|
|
|
|
s == JANET_STATUS_USER3 ||
|
|
|
|
s == JANET_STATUS_USER4;
|
|
|
|
if (isFinished) {
|
|
|
|
janet_env_detach(env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Create a tail frame for a function */
|
2018-09-06 02:18:42 +00:00
|
|
|
int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t nextframetop = fiber->frame + func->def->slotcount;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t nextstacktop = nextframetop + JANET_FRAME_SIZE;
|
2018-12-02 20:29:21 +00:00
|
|
|
int32_t next_arity = fiber->stacktop - fiber->stackstart;
|
2018-01-12 18:54:37 +00:00
|
|
|
int32_t stacksize;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-12-30 23:41:44 +00:00
|
|
|
/* Check strict arity before messing with state */
|
2019-03-12 04:23:14 +00:00
|
|
|
if (next_arity < func->def->min_arity) return 1;
|
|
|
|
if (next_arity > func->def->max_arity) return 1;
|
2018-12-30 23:41:44 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
if (fiber->capacity < nextstacktop) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_setcapacity(fiber, 2 * nextstacktop);
|
2020-08-17 12:01:58 +00:00
|
|
|
#ifdef JANET_DEBUG
|
|
|
|
} else {
|
|
|
|
janet_fiber_refresh_memory(fiber);
|
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *stack = fiber->data + fiber->frame;
|
|
|
|
Janet *args = fiber->data + fiber->stackstart;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Detach old function */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (NULL != janet_fiber_frame(fiber)->func)
|
|
|
|
janet_env_detach(janet_fiber_frame(fiber)->env);
|
|
|
|
janet_fiber_frame(fiber)->env = NULL;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-01-12 18:54:37 +00:00
|
|
|
/* Check varargs */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (func->def->flags & JANET_FUNCDEF_FLAG_VARARG) {
|
2018-01-12 18:54:37 +00:00
|
|
|
int32_t tuplehead = fiber->stackstart + func->def->arity;
|
2019-05-24 21:03:22 +00:00
|
|
|
int st = func->def->flags & JANET_FUNCDEF_FLAG_STRUCTARG;
|
2018-01-12 18:54:37 +00:00
|
|
|
if (tuplehead >= fiber->stacktop) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (tuplehead >= fiber->capacity) janet_fiber_setcapacity(fiber, 2 * (tuplehead + 1));
|
|
|
|
for (i = fiber->stacktop; i < tuplehead; ++i) fiber->data[i] = janet_wrap_nil();
|
2019-05-24 21:03:22 +00:00
|
|
|
fiber->data[tuplehead] = st
|
|
|
|
? make_struct_n(NULL, 0)
|
|
|
|
: janet_wrap_tuple(janet_tuple_n(NULL, 0));
|
2018-01-12 18:54:37 +00:00
|
|
|
} else {
|
2019-05-24 21:03:22 +00:00
|
|
|
fiber->data[tuplehead] = st
|
|
|
|
? make_struct_n(
|
2019-02-20 01:51:34 +00:00
|
|
|
fiber->data + tuplehead,
|
2019-05-24 21:03:22 +00:00
|
|
|
fiber->stacktop - tuplehead)
|
|
|
|
: janet_wrap_tuple(janet_tuple_n(
|
|
|
|
fiber->data + tuplehead,
|
|
|
|
fiber->stacktop - tuplehead));
|
2018-01-12 18:54:37 +00:00
|
|
|
}
|
|
|
|
stacksize = tuplehead - fiber->stackstart + 1;
|
|
|
|
} else {
|
|
|
|
stacksize = fiber->stacktop - fiber->stackstart;
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
if (stacksize) memmove(stack, args, stacksize * sizeof(Janet));
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Nil unset locals (Needed for functional correctness) */
|
2018-01-12 18:54:37 +00:00
|
|
|
for (i = fiber->frame + stacksize; i < nextframetop; ++i)
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->data[i] = janet_wrap_nil();
|
2018-01-12 18:54:37 +00:00
|
|
|
|
|
|
|
/* Set stack stuff */
|
|
|
|
fiber->stacktop = fiber->stackstart = nextstacktop;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Set frame stuff */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_frame(fiber)->func = func;
|
|
|
|
janet_fiber_frame(fiber)->pc = func->def->bytecode;
|
|
|
|
janet_fiber_frame(fiber)->flags |= JANET_STACKFRAME_TAILCALL;
|
2018-08-03 17:41:44 +00:00
|
|
|
|
|
|
|
/* Good return */
|
|
|
|
return 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a stack frame to a fiber for a c function */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_cframe(JanetFiber *fiber, JanetCFunction cfun) {
|
|
|
|
JanetStackFrame *newframe;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t oldframe = fiber->frame;
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t nextframe = fiber->stackstart;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t nextstacktop = fiber->stacktop + JANET_FRAME_SIZE;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
if (fiber->capacity < nextstacktop) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_setcapacity(fiber, 2 * nextstacktop);
|
2020-08-17 12:01:58 +00:00
|
|
|
#ifdef JANET_DEBUG
|
|
|
|
} else {
|
|
|
|
janet_fiber_refresh_memory(fiber);
|
|
|
|
#endif
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the next frame */
|
|
|
|
fiber->frame = nextframe;
|
2018-01-04 02:36:10 +00:00
|
|
|
fiber->stacktop = fiber->stackstart = nextstacktop;
|
2018-09-06 02:18:42 +00:00
|
|
|
newframe = janet_fiber_frame(fiber);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Set up the new frame */
|
|
|
|
newframe->prevframe = oldframe;
|
2018-06-12 18:24:45 +00:00
|
|
|
newframe->pc = (uint32_t *) cfun;
|
2017-11-06 03:05:47 +00:00
|
|
|
newframe->func = NULL;
|
2018-02-13 21:14:55 +00:00
|
|
|
newframe->env = NULL;
|
2018-03-22 00:53:39 +00:00
|
|
|
newframe->flags = 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 03:23:27 +00:00
|
|
|
/* Pop a stack frame from the fiber. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_fiber_popframe(JanetFiber *fiber) {
|
|
|
|
JanetStackFrame *frame = janet_fiber_frame(fiber);
|
2018-02-07 05:44:51 +00:00
|
|
|
if (fiber->frame == 0) return;
|
2018-11-16 21:24:10 +00:00
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Clean up the frame (detach environments) */
|
|
|
|
if (NULL != frame->func)
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_env_detach(frame->env);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Shrink stack */
|
2018-01-04 02:36:10 +00:00
|
|
|
fiber->stacktop = fiber->stackstart = fiber->frame;
|
2017-11-06 03:05:47 +00:00
|
|
|
fiber->frame = frame->prevframe;
|
|
|
|
}
|
2018-03-22 00:53:39 +00:00
|
|
|
|
2019-04-27 19:45:28 +00:00
|
|
|
JanetFiberStatus janet_fiber_status(JanetFiber *f) {
|
|
|
|
return ((f)->flags & JANET_FIBER_STATUS_MASK) >> JANET_FIBER_STATUS_OFFSET;
|
|
|
|
}
|
|
|
|
|
2019-05-04 23:07:04 +00:00
|
|
|
JanetFiber *janet_current_fiber(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
return janet_vm.fiber;
|
2019-05-04 23:07:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
JanetFiber *janet_root_fiber(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
return janet_vm.root_fiber;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 00:53:39 +00:00
|
|
|
/* CFuns */
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_getenv,
|
|
|
|
"(fiber/getenv fiber)",
|
|
|
|
"Gets the environment for a fiber. Returns nil if no such table is "
|
|
|
|
"set yet.") {
|
2019-04-16 19:41:45 +00:00
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
return fiber->env ?
|
|
|
|
janet_wrap_table(fiber->env) :
|
|
|
|
janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_setenv,
|
|
|
|
"(fiber/setenv fiber table)",
|
|
|
|
"Sets the environment table for a fiber. Set to nil to remove the current "
|
|
|
|
"environment.") {
|
2019-04-16 19:41:45 +00:00
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
if (janet_checktype(argv[1], JANET_NIL)) {
|
|
|
|
fiber->env = NULL;
|
|
|
|
} else {
|
|
|
|
fiber->env = janet_gettable(argv, 1);
|
|
|
|
}
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_new,
|
2023-06-04 19:17:18 +00:00
|
|
|
"(fiber/new func &opt sigmask env)",
|
2021-07-26 22:00:59 +00:00
|
|
|
"Create a new fiber with function body func. Can optionally "
|
2023-06-04 19:17:18 +00:00
|
|
|
"take a set of signals `sigmask` to capture from child fibers, "
|
|
|
|
"and an environment table `env`. The mask is specified as a keyword where each character "
|
2021-07-26 22:00:59 +00:00
|
|
|
"is used to indicate a signal to block. If the ev module is enabled, and "
|
|
|
|
"this fiber is used as an argument to `ev/go`, these \"blocked\" signals "
|
|
|
|
"will result in messages being sent to the supervisor channel. "
|
|
|
|
"The default sigmask is :y. "
|
|
|
|
"For example,\n\n"
|
|
|
|
" (fiber/new myfun :e123)\n\n"
|
|
|
|
"blocks error signals and user signals 1, 2 and 3. The signals are "
|
|
|
|
"as follows:\n\n"
|
|
|
|
"* :a - block all signals\n"
|
|
|
|
"* :d - block debug signals\n"
|
|
|
|
"* :e - block error signals\n"
|
|
|
|
"* :t - block termination signals: error + user[0-4]\n"
|
|
|
|
"* :u - block user signals\n"
|
|
|
|
"* :y - block yield signals\n"
|
2023-04-24 14:19:15 +00:00
|
|
|
"* :w - block await signals (user9)\n"
|
|
|
|
"* :r - block interrupt signals (user8)\n"
|
2021-07-26 22:00:59 +00:00
|
|
|
"* :0-9 - block a specific user signal\n\n"
|
|
|
|
"The sigmask argument also can take environment flags. If any mutually "
|
|
|
|
"exclusive flags are present, the last flag takes precedence.\n\n"
|
|
|
|
"* :i - inherit the environment from the current fiber\n"
|
|
|
|
"* :p - the environment table's prototype is the current environment table") {
|
2023-06-04 19:17:18 +00:00
|
|
|
janet_arity(argc, 1, 3);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFunction *func = janet_getfunction(argv, 0);
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetFiber *fiber;
|
2020-02-23 20:47:29 +00:00
|
|
|
if (func->def->min_arity > 1) {
|
|
|
|
janet_panicf("fiber function must accept 0 or 1 arguments");
|
2018-08-03 17:41:44 +00:00
|
|
|
}
|
2020-02-23 20:47:29 +00:00
|
|
|
fiber = janet_fiber(func, 64, func->def->min_arity, NULL);
|
2023-06-04 16:21:52 +00:00
|
|
|
janet_assert(fiber != NULL, "bad fiber arity check");
|
2023-06-04 19:17:18 +00:00
|
|
|
if (argc == 3 && !janet_checktype(argv[2], JANET_NIL)) {
|
|
|
|
fiber->env = janet_gettable(argv, 2);
|
|
|
|
}
|
|
|
|
if (argc >= 2) {
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t i;
|
|
|
|
JanetByteView view = janet_getbytes(argv, 1);
|
2020-01-10 23:25:10 +00:00
|
|
|
fiber->flags = JANET_FIBER_RESUME_NO_USEVAL | JANET_FIBER_RESUME_NO_SKIP;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_fiber_set_status(fiber, JANET_STATUS_NEW);
|
2019-01-06 01:09:03 +00:00
|
|
|
for (i = 0; i < view.len; i++) {
|
|
|
|
if (view.bytes[i] >= '0' && view.bytes[i] <= '9') {
|
|
|
|
fiber->flags |= JANET_FIBER_MASK_USERN(view.bytes[i] - '0');
|
2018-05-17 02:09:36 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
switch (view.bytes[i]) {
|
2018-05-17 02:09:36 +00:00
|
|
|
default:
|
2023-04-24 14:19:15 +00:00
|
|
|
janet_panicf("invalid flag %c, expected a, t, d, e, u, y, w, r, i, or p", view.bytes[i]);
|
2019-01-06 01:09:03 +00:00
|
|
|
break;
|
2018-05-17 02:09:36 +00:00
|
|
|
case 'a':
|
2018-11-16 21:24:10 +00:00
|
|
|
fiber->flags |=
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_FIBER_MASK_DEBUG |
|
|
|
|
JANET_FIBER_MASK_ERROR |
|
|
|
|
JANET_FIBER_MASK_USER |
|
|
|
|
JANET_FIBER_MASK_YIELD;
|
2018-05-17 02:09:36 +00:00
|
|
|
break;
|
2020-02-23 19:31:27 +00:00
|
|
|
case 't':
|
|
|
|
fiber->flags |=
|
|
|
|
JANET_FIBER_MASK_ERROR |
|
|
|
|
JANET_FIBER_MASK_USER0 |
|
|
|
|
JANET_FIBER_MASK_USER1 |
|
|
|
|
JANET_FIBER_MASK_USER2 |
|
|
|
|
JANET_FIBER_MASK_USER3 |
|
|
|
|
JANET_FIBER_MASK_USER4;
|
|
|
|
break;
|
2018-05-17 02:09:36 +00:00
|
|
|
case 'd':
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->flags |= JANET_FIBER_MASK_DEBUG;
|
2018-05-17 02:09:36 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->flags |= JANET_FIBER_MASK_ERROR;
|
2018-05-17 02:09:36 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->flags |= JANET_FIBER_MASK_USER;
|
2018-05-17 02:09:36 +00:00
|
|
|
break;
|
|
|
|
case 'y':
|
2018-09-06 02:18:42 +00:00
|
|
|
fiber->flags |= JANET_FIBER_MASK_YIELD;
|
2018-05-17 02:09:36 +00:00
|
|
|
break;
|
2023-04-24 14:19:15 +00:00
|
|
|
case 'w':
|
|
|
|
fiber->flags |= JANET_FIBER_MASK_USER9;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
fiber->flags |= JANET_FIBER_MASK_USER8;
|
|
|
|
break;
|
2019-04-16 19:41:45 +00:00
|
|
|
case 'i':
|
2021-07-17 01:59:03 +00:00
|
|
|
if (!janet_vm.fiber->env) {
|
|
|
|
janet_vm.fiber->env = janet_table(0);
|
2019-04-16 19:41:45 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
fiber->env = janet_vm.fiber->env;
|
2019-04-16 19:41:45 +00:00
|
|
|
break;
|
2019-06-08 14:30:30 +00:00
|
|
|
case 'p':
|
2021-07-17 01:59:03 +00:00
|
|
|
if (!janet_vm.fiber->env) {
|
|
|
|
janet_vm.fiber->env = janet_table(0);
|
2019-06-08 14:30:30 +00:00
|
|
|
}
|
|
|
|
fiber->env = janet_table(0);
|
2021-07-17 01:59:03 +00:00
|
|
|
fiber->env->proto = janet_vm.fiber->env;
|
2019-06-08 14:30:30 +00:00
|
|
|
break;
|
2018-05-17 02:09:36 +00:00
|
|
|
}
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_fiber(fiber);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_status,
|
|
|
|
"(fiber/status fib)",
|
|
|
|
"Get the status of a fiber. The status will be one of:\n\n"
|
|
|
|
"* :dead - the fiber has finished\n"
|
|
|
|
"* :error - the fiber has errored out\n"
|
|
|
|
"* :debug - the fiber is suspended in debug mode\n"
|
|
|
|
"* :pending - the fiber has been yielded\n"
|
2023-04-24 14:19:15 +00:00
|
|
|
"* :user(0-7) - the fiber is suspended by a user signal\n"
|
|
|
|
"* :interrupted - the fiber was interrupted\n"
|
|
|
|
"* :suspended - the fiber is waiting to be resumed by the scheduler\n"
|
2021-07-26 22:00:59 +00:00
|
|
|
"* :alive - the fiber is currently running and cannot be resumed\n"
|
|
|
|
"* :new - the fiber has just been created and not yet run") {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
2019-04-27 19:45:28 +00:00
|
|
|
uint32_t s = janet_fiber_status(fiber);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_ckeywordv(janet_status_names[s]);
|
2018-03-22 00:53:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_current,
|
|
|
|
"(fiber/current)",
|
|
|
|
"Returns the currently running fiber.") {
|
2019-01-06 01:09:03 +00:00
|
|
|
(void) argv;
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2021-07-17 01:59:03 +00:00
|
|
|
return janet_wrap_fiber(janet_vm.fiber);
|
2018-05-11 00:25:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_root,
|
|
|
|
"(fiber/root)",
|
|
|
|
"Returns the current root fiber. The root fiber is the oldest ancestor "
|
|
|
|
"that does not have a parent.") {
|
2020-05-01 04:21:26 +00:00
|
|
|
(void) argv;
|
|
|
|
janet_fixarity(argc, 0);
|
2021-07-17 01:59:03 +00:00
|
|
|
return janet_wrap_fiber(janet_vm.root_fiber);
|
2020-05-01 04:21:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_maxstack,
|
|
|
|
"(fiber/maxstack fib)",
|
|
|
|
"Gets the maximum stack size in janet values allowed for a fiber. While memory for "
|
|
|
|
"the fiber's stack is not allocated up front, the fiber will not allocated more "
|
|
|
|
"than this amount and will throw a stack-overflow error if more memory is needed. ") {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
return janet_wrap_integer(fiber->maxstack);
|
2018-06-03 18:00:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_setmaxstack,
|
|
|
|
"(fiber/setmaxstack fib maxstack)",
|
|
|
|
"Sets the maximum stack size in janet values for a fiber. By default, the "
|
|
|
|
"maximum stack size is usually 8192.") {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
int32_t maxs = janet_getinteger(argv, 1);
|
2018-06-03 18:00:05 +00:00
|
|
|
if (maxs < 0) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("expected positive integer");
|
2018-06-03 18:00:05 +00:00
|
|
|
}
|
|
|
|
fiber->maxstack = maxs;
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-06-03 18:00:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 23:57:13 +00:00
|
|
|
int janet_fiber_can_resume(JanetFiber *fiber) {
|
2020-02-23 19:31:27 +00:00
|
|
|
JanetFiberStatus s = janet_fiber_status(fiber);
|
|
|
|
int isFinished = s == JANET_STATUS_DEAD ||
|
|
|
|
s == JANET_STATUS_ERROR ||
|
|
|
|
s == JANET_STATUS_USER0 ||
|
|
|
|
s == JANET_STATUS_USER1 ||
|
|
|
|
s == JANET_STATUS_USER2 ||
|
|
|
|
s == JANET_STATUS_USER3 ||
|
|
|
|
s == JANET_STATUS_USER4;
|
2023-04-01 23:57:13 +00:00
|
|
|
return !isFinished;
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(cfun_fiber_can_resume,
|
|
|
|
"(fiber/can-resume? fiber)",
|
|
|
|
"Check if a fiber is finished and cannot be resumed.") {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
return janet_wrap_boolean(janet_fiber_can_resume(fiber));
|
2020-02-23 19:31:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:00:59 +00:00
|
|
|
JANET_CORE_FN(cfun_fiber_last_value,
|
2021-08-27 08:29:20 +00:00
|
|
|
"(fiber/last-value)",
|
2021-07-26 22:00:59 +00:00
|
|
|
"Get the last value returned or signaled from the fiber.") {
|
2021-01-03 22:17:36 +00:00
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
return fiber->last_value;
|
|
|
|
}
|
|
|
|
|
2018-03-22 00:53:39 +00:00
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_fiber(JanetTable *env) {
|
2021-07-26 22:00:59 +00:00
|
|
|
JanetRegExt fiber_cfuns[] = {
|
|
|
|
JANET_CORE_REG("fiber/new", cfun_fiber_new),
|
|
|
|
JANET_CORE_REG("fiber/status", cfun_fiber_status),
|
|
|
|
JANET_CORE_REG("fiber/root", cfun_fiber_root),
|
|
|
|
JANET_CORE_REG("fiber/current", cfun_fiber_current),
|
|
|
|
JANET_CORE_REG("fiber/maxstack", cfun_fiber_maxstack),
|
|
|
|
JANET_CORE_REG("fiber/setmaxstack", cfun_fiber_setmaxstack),
|
|
|
|
JANET_CORE_REG("fiber/getenv", cfun_fiber_getenv),
|
|
|
|
JANET_CORE_REG("fiber/setenv", cfun_fiber_setenv),
|
|
|
|
JANET_CORE_REG("fiber/can-resume?", cfun_fiber_can_resume),
|
|
|
|
JANET_CORE_REG("fiber/last-value", cfun_fiber_last_value),
|
|
|
|
JANET_REG_END
|
|
|
|
};
|
|
|
|
janet_core_cfuns_ext(env, NULL, fiber_cfuns);
|
2018-03-22 00:53:39 +00:00
|
|
|
}
|