Rename fiber.resume and fiber.yield to

resume and yield.
This commit is contained in:
Calvin Rose 2018-06-09 13:08:30 -04:00
parent eba6242978
commit 2a87dada47
9 changed files with 48 additions and 56 deletions

View File

@ -956,18 +956,18 @@ onvalue."
(chunks buf p)
(:= len (length buf))
(loop [i :range [0 len]]
(fiber.yield (get buf i))))
(yield (get buf i))))
0))
# Fiber stream of values
(def vals (coro
(while going
(switch (parser.status p)
:full (fiber.yield (parser.produce p))
:full (yield (parser.produce p))
:error (onerr "parse" (parser.error p))
(switch (fiber.status chars)
:new (parser.byte p (fiber.resume chars))
:pending (parser.byte p (fiber.resume chars))
:new (parser.byte p (resume chars))
:pending (parser.byte p (resume chars))
(:= going false))))
(when (not= :root (parser.status p))
(onerr "parse" "unexpected end of source"))))
@ -982,7 +982,7 @@ onvalue."
(do
(:= good false)
(onerr "compile" (get res :error))))) :a))
(def res (fiber.resume f))
(def res (resume f))
(when good
(def sig (fiber.status f))
(if going
@ -993,7 +993,7 @@ onvalue."
# Run loop
(def oldenv *env*)
(:= *env* env)
(while going (eval1 (fiber.resume vals)))
(while going (eval1 (resume vals)))
(:= *env* oldenv)
env)

View File

@ -58,6 +58,26 @@ static const DstReg cfuns[] = {
{NULL, NULL}
};
/* Utility for inline assembly */
static DstFunction *dst_quick_asm(
int32_t arity,
int varargs,
int32_t slots,
const uint32_t *bytecode,
size_t bytecode_size) {
DstFuncDef *def = dst_funcdef_alloc();
def->arity = arity;
def->flags = varargs ? DST_FUNCDEF_FLAG_VARARG : 0;
def->slotcount = slots;
def->bytecode = malloc(bytecode_size);
def->bytecode_length = bytecode_size / sizeof(uint32_t);
if (!def->bytecode) {
DST_OUT_OF_MEMORY;
}
memcpy(def->bytecode, bytecode, bytecode_size);
return dst_thunk(def);
}
DstTable *dst_stl_env(int flags) {
static uint32_t error_asm[] = {
DOP_ERROR
@ -70,6 +90,15 @@ DstTable *dst_stl_env(int flags) {
DOP_SIGNAL | (2 << 24),
DOP_RETURN_NIL
};
static uint32_t yield_asm[] = {
DOP_SIGNAL | (3 << 24),
DOP_RETURN
};
static uint32_t resume_asm[] = {
DOP_RESUME | (1 << 24),
DOP_RETURN
};
DstTable *env = dst_table(0);
Dst ret = dst_wrap_table(env);
@ -79,6 +108,8 @@ DstTable *dst_stl_env(int flags) {
dst_env_def(env, "debug", dst_wrap_function(dst_quick_asm(0, 0, 1, debug_asm, sizeof(debug_asm))));
dst_env_def(env, "error", dst_wrap_function(dst_quick_asm(1, 0, 1, error_asm, sizeof(error_asm))));
dst_env_def(env, "apply1", dst_wrap_function(dst_quick_asm(2, 0, 2, apply_asm, sizeof(apply_asm))));
dst_env_def(env, "yield", dst_wrap_function(dst_quick_asm(1, 0, 2, yield_asm, sizeof(yield_asm))));
dst_env_def(env, "resume", dst_wrap_function(dst_quick_asm(2, 0, 2, resume_asm, sizeof(resume_asm))));
dst_env_def(env, "VERSION", dst_cstringv(DST_VERSION));

View File

@ -241,18 +241,3 @@ DstFunction *dst_thunk(DstFuncDef *def) {
dst_assert(def->environments_length == 0, "tried to create thunk that needs upvalues");
return func;
}
/* Utility for inline assembly */
DstFunction *dst_quick_asm(int32_t arity, int varargs, int32_t slots, const uint32_t *bytecode, size_t bytecode_size) {
DstFuncDef *def = dst_funcdef_alloc();
def->arity = arity;
def->flags = varargs ? DST_FUNCDEF_FLAG_VARARG : 0;
def->slotcount = slots;
def->bytecode = malloc(bytecode_size);
def->bytecode_length = bytecode_size / sizeof(uint32_t);
if (!def->bytecode) {
DST_OUT_OF_MEMORY;
}
memcpy(def->bytecode, bytecode, bytecode_size);
return dst_thunk(def);
}

View File

@ -438,21 +438,7 @@ static const DstReg cfuns[] = {
/* Module entry point */
int dst_lib_fiber(DstArgs args) {
static uint32_t yield_asm[] = {
DOP_SIGNAL | (3 << 24),
DOP_RETURN
};
static uint32_t resume_asm[] = {
DOP_RESUME | (1 << 24),
DOP_RETURN
};
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
dst_env_def(env, "fiber.yield",
dst_wrap_function(dst_quick_asm(1, 0, 2,
yield_asm, sizeof(yield_asm))));
dst_env_def(env, "fiber.resume",
dst_wrap_function(dst_quick_asm(2, 0, 2,
resume_asm, sizeof(resume_asm))));
return 0;
}

View File

@ -84,10 +84,6 @@ static int os_exit(DstArgs args) {
DST_MAXARITY(args, 1);
if (args.n == 0) {
exit(EXIT_SUCCESS);
} else if (dst_checktype(args.v[0], DST_TRUE)
|| dst_checktype(args.v[0], DST_FALSE)) {
exit(dst_unwrap_boolean(args.v[0]) ? EXIT_SUCCESS : EXIT_FAILURE);
return 0;
} else if (dst_checktype(args.v[0], DST_INTEGER)) {
exit(dst_unwrap_integer(args.v[0]));
} else {

View File

@ -922,8 +922,8 @@ int dst_init() {
dst_vm_next_collection = 0;
/* Setting memoryInterval to zero forces
* a collection pretty much every cycle, which is
* horrible for performance, but helps ensure
* there are no memory bugs during dev */
* incredibly horrible for performance, but can help ensure
* there are no memory bugs during development */
dst_vm_gc_interval = 0x10000;
dst_symcache_init();
/* Initialize gc roots */

View File

@ -168,12 +168,6 @@ void dst_gcunlock(int handle);
DstFuncDef *dst_funcdef_alloc(void);
DstFunction *dst_thunk(DstFuncDef *def);
int dst_verify(DstFuncDef *def);
DstFunction *dst_quick_asm(
int32_t arity,
int varargs,
int32_t slots,
const uint32_t *bytecode,
size_t bytecode_size);
/* Misc */
int dst_equals(Dst x, Dst y);

View File

@ -149,18 +149,18 @@
(def afiber (fiber.new (fn [x]
(error (string "hello, " x))) :e))
(def afiber-result (fiber.resume afiber "world!"))
(def afiber-result (resume afiber "world!"))
(assert (= afiber-result "hello, world!") "fiber error result")
(assert (= (fiber.status afiber) :error) "fiber error status")
# yield tests
(def t (fiber.new (fn [] (fiber.yield 1) (fiber.yield 2) 3)))
(def t (fiber.new (fn [] (yield 1) (yield 2) 3)))
(assert (= 1 (fiber.resume t)) "initial transfer to new fiber")
(assert (= 2 (fiber.resume t)) "second transfer to fiber")
(assert (= 3 (fiber.resume t)) "return from fiber")
(assert (= 1 (resume t)) "initial transfer to new fiber")
(assert (= 2 (resume t)) "second transfer to fiber")
(assert (= 3 (resume t)) "return from fiber")
(assert (= (fiber.status t) :dead) "finished fiber is dead")
# Var arg tests
@ -192,7 +192,7 @@
(def 🦊 :fox)
(def 🐮 :cow)
(assert (= (string "🐼" 🦊 🐮) "🐼:fox:cow") "emojis 🙉 :)")
(assert (not= 🦊 :🦊) "utf8 strings are not symbols and vice versa")
(assert (not= 🦊 "🦊") "utf8 strings are not symbols and vice versa")
# Symbols with @ character

View File

@ -80,16 +80,16 @@
(defn fiberstuff []
(++ myvar)
(def f (fiber.new (fn [] (++ myvar) (debug) (++ myvar))))
(fiber.resume f)
(resume f)
(++ myvar))
(def myfiber (fiber.new fiberstuff :dey))
(assert (= myvar 0) "fiber creation does not call fiber function")
(fiber.resume myfiber)
(resume myfiber)
(assert (= myvar 2) "fiber debug statement breaks at proper point")
(assert (= (fiber.status myfiber) :debug) "fiber enters debug state")
(fiber.resume myfiber)
(resume myfiber)
(assert (= myvar 4) "fiber resumes properly from debug state")
(assert (= (fiber.status myfiber) :dead) "fiber properly dies from debug state")