lua: proper early-return via guard+raise sentinel; fixes if-then-return-end-rest +3 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -1503,9 +1503,15 @@
|
||||
(guard
|
||||
(e (true (list (quote lua-multi) nil (str e))))
|
||||
(let ((compiled (lua-transpile src)))
|
||||
(let ((wrapped (list (make-symbol "fn") (list (make-symbol "&rest") (make-symbol "__args"))
|
||||
(list (make-symbol "let") (list) compiled))))
|
||||
(eval-expr wrapped))))))
|
||||
(let ((guarded (list
|
||||
(make-symbol "guard")
|
||||
(list (make-symbol "e")
|
||||
(list
|
||||
(list (make-symbol "lua-return-sentinel?") (make-symbol "e"))
|
||||
(list (make-symbol "lua-return-value") (make-symbol "e"))))
|
||||
(list (make-symbol "let") (list) compiled))))
|
||||
(let ((wrapped (list (make-symbol "fn") (list (make-symbol "&rest") (make-symbol "__args")) guarded)))
|
||||
(eval-expr wrapped)))))))
|
||||
|
||||
(define loadstring lua-loadstring)
|
||||
(define load lua-loadstring)
|
||||
@@ -1630,3 +1636,13 @@
|
||||
(va-build skip)
|
||||
(dict-set! t "n" n)
|
||||
t))))
|
||||
|
||||
;; Return-sentinel: wrap function bodies so mid-block `return` can escape.
|
||||
(define
|
||||
lua-return-sentinel?
|
||||
(fn (e)
|
||||
(and (= (type-of e) "list") (> (len e) 0) (= (first e) (quote lua-ret)))))
|
||||
|
||||
(define
|
||||
lua-return-value
|
||||
(fn (e) (if (> (len e) 1) (nth e 1) nil)))
|
||||
|
||||
Reference in New Issue
Block a user