1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Allow yielding by transfering to nil

This commit is contained in:
Calvin Rose 2017-07-01 11:22:10 -04:00
parent 957a513fd6
commit 171c0ce49e
2 changed files with 12 additions and 0 deletions

View File

@ -359,6 +359,9 @@ int gst_continue(Gst *vm) {
v1 = stack[pc[3]]; /* The value to pass in */ v1 = stack[pc[3]]; /* The value to pass in */
if (temp.type != GST_THREAD && temp.type != GST_NIL) if (temp.type != GST_THREAD && temp.type != GST_NIL)
gst_error(vm, "expected thread"); 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.type == GST_THREAD) {
if (temp.data.thread->status == GST_THREAD_DEAD || if (temp.data.thread->status == GST_THREAD_DEAD ||
temp.data.thread->status == GST_THREAD_ERROR) temp.data.thread->status == GST_THREAD_ERROR)

View File

@ -80,6 +80,15 @@
(assert (= athread-result "hello, world!") "thread error result") (assert (= athread-result "hello, world!") "thread error result")
(assert (= (status athread) "error") "thread error status") (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" "report"
(print num-tests-passed "of" num-tests-run "tests passed") (print num-tests-passed "of" num-tests-run "tests passed")