erlang: spawn/1 + self/0 + is_pid (+13 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 19:50:09 +00:00
parent bc1a69925e
commit 266693a2f6
4 changed files with 133 additions and 4 deletions

View File

@@ -208,3 +208,68 @@
(define
er-proc-mailbox-size
(fn (pid) (er-q-len (er-proc-field pid :mailbox))))
;; ── process BIFs ────────────────────────────────────────────────
(define
er-bif-is-pid
(fn (vs) (er-bool (er-pid? (er-bif-arg1 vs "is_pid")))))
(define
er-bif-self
(fn
(vs)
(if
(not (= (len vs) 0))
(error "Erlang: self/0: arity")
(let
((pid (er-sched-current-pid)))
(if
(= pid nil)
(error "Erlang: self/0: no current process")
pid)))))
(define
er-bif-spawn
(fn
(vs)
(cond
(= (len vs) 1) (er-spawn-fun (nth vs 0))
(= (len vs) 3) (error
"Erlang: spawn/3: module-based spawn deferred to Phase 5 (modules)")
:else (error "Erlang: spawn: wrong arity"))))
(define
er-spawn-fun
(fn
(fv)
(if
(not (er-fun? fv))
(error "Erlang: spawn/1: not a fun")
(let
((proc (er-proc-new! (er-env-new))))
(dict-set! proc :initial-fun fv)
(get proc :pid)))))
;; ── scheduler loop ──────────────────────────────────────────────
;; Drain all runnable processes to completion. Synchronous — each
;; spawned process runs its :initial-fun front-to-back with no yielding.
;; receive-driven suspension arrives in the next roadmap step.
(define
er-sched-drain!
(fn
()
(let
((pid (er-sched-next-runnable!)))
(when
(not (= pid nil))
(er-sched-set-current! pid)
(er-proc-set! pid :state "running")
(let
((fv (er-proc-field pid :initial-fun)))
(when
(not (= fv nil))
(er-apply-fun fv (list))))
(er-proc-set! pid :state "dead")
(er-proc-set! pid :exit-reason (er-mk-atom "normal"))
(er-sched-set-current! nil)
(er-sched-drain!)))))