From 171c0ce49e427e915b40539cb7a730cfc45a8e61 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 1 Jul 2017 11:22:10 -0400 Subject: [PATCH] Allow yielding by transfering to nil --- core/vm.c | 3 +++ gsttests/basic.gst | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/core/vm.c b/core/vm.c index ce806bf4..1d172a7f 100644 --- a/core/vm.c +++ b/core/vm.c @@ -359,6 +359,9 @@ int gst_continue(Gst *vm) { v1 = stack[pc[3]]; /* The value to pass in */ if (temp.type != GST_THREAD && temp.type != GST_NIL) gst_error(vm, "expected thread"); + if (temp.type == GST_NIL && vm->thread->parent) { + temp = gst_wrap_thread(vm->thread->parent); + } if (temp.type == GST_THREAD) { if (temp.data.thread->status == GST_THREAD_DEAD || temp.data.thread->status == GST_THREAD_ERROR) diff --git a/gsttests/basic.gst b/gsttests/basic.gst index 5a067765..d873b9c0 100644 --- a/gsttests/basic.gst +++ b/gsttests/basic.gst @@ -80,6 +80,15 @@ (assert (= athread-result "hello, world!") "thread error result") (assert (= (status athread) "error") "thread error status") +"yield tests" + +(def t (thread (fn [] (tran nil 1) (tran nil 2) 3))) + +(assert (= 1 (tran t)) "initial transfer to new thread") +(assert (= 2 (tran t)) "second transfer to thread") +(assert (= 3 (tran t)) "return from thread") +(assert (= (status t) "dead") "finished thread is dead") + "report" (print num-tests-passed "of" num-tests-run "tests passed")