lua: pcall/xpcall/error via guard+raise; arity-dispatch lua-apply +9 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 18:05:35 +00:00
parent 43c13c4eb1
commit e224fb2db0
3 changed files with 85 additions and 3 deletions

View File

@@ -384,6 +384,24 @@
(define sx-apply-ref apply)
(define
lua-apply
(fn
(f rargs)
(let
((n (len rargs)))
(cond
((= n 0) (f))
((= n 1) (f (nth rargs 0)))
((= n 2) (f (nth rargs 0) (nth rargs 1)))
((= n 3) (f (nth rargs 0) (nth rargs 1) (nth rargs 2)))
((= n 4) (f (nth rargs 0) (nth rargs 1) (nth rargs 2) (nth rargs 3)))
((= n 5) (f (nth rargs 0) (nth rargs 1) (nth rargs 2) (nth rargs 3) (nth rargs 4)))
((= n 6) (f (nth rargs 0) (nth rargs 1) (nth rargs 2) (nth rargs 3) (nth rargs 4) (nth rargs 5)))
((= n 7) (f (nth rargs 0) (nth rargs 1) (nth rargs 2) (nth rargs 3) (nth rargs 4) (nth rargs 5) (nth rargs 6)))
((= n 8) (f (nth rargs 0) (nth rargs 1) (nth rargs 2) (nth rargs 3) (nth rargs 4) (nth rargs 5) (nth rargs 6) (nth rargs 7)))
(else (sx-apply-ref f rargs))))))
(define
lua-call
(fn
@@ -392,11 +410,41 @@
((f (first args)) (rargs (rest args)))
(cond
((or (= (type-of f) "function") (= (type-of f) "lambda"))
(sx-apply-ref f rargs))
(lua-apply f rargs))
((= (type-of f) "dict")
(let
((m (lua-get-mm f "__call")))
(cond
((= m nil) (error "lua: attempt to call non-function"))
(else (sx-apply-ref m (cons f rargs))))))
(else (lua-apply m (cons f rargs))))))
(else (error "lua: attempt to call non-function"))))))
(define lua-error (fn (&rest args) (raise (first args))))
(define error lua-error)
(define
pcall
(fn
(&rest args)
(let
((f (first args)) (rargs (rest args)))
(guard
(e (true (list (quote lua-multi) false e)))
(let
((r (lua-apply f rargs)))
(cond
((lua-multi? r) (cons (quote lua-multi) (cons true (rest r))))
(else (list (quote lua-multi) true r))))))))
(define
xpcall
(fn
(f msgh)
(guard
(e (true (list (quote lua-multi) false (lua-first (lua-apply msgh (list e))))))
(let
((r (lua-apply f (list))))
(cond
((lua-multi? r) (cons (quote lua-multi) (cons true (rest r))))
(else (list (quote lua-multi) true r)))))))