test-exceptions.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Exception tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (hoot compile)
  20. (hoot reflect)
  21. (srfi srfi-64)
  22. (test utils))
  23. (test-begin "test-exceptions")
  24. (test-call "79" (lambda ()
  25. (with-exception-handler
  26. (lambda (exn) 42)
  27. (lambda () (+ 10 69)))))
  28. (test-call "52" (lambda ()
  29. (with-exception-handler
  30. (lambda (exn) 42)
  31. (lambda () (+ 10 (raise-continuable 69))))))
  32. (test-call "42" (lambda ()
  33. (with-exception-handler
  34. (lambda (exn) 42)
  35. (lambda () (+ 10 (raise-continuable 69)))
  36. #:unwind? #t)))
  37. (test-call "69" (lambda ()
  38. (with-exception-handler
  39. (lambda (exn) exn)
  40. (lambda () (+ 10 (raise-continuable 69)))
  41. #:unwind? #t)))
  42. (test-call "42" (lambda ()
  43. (with-exception-handler
  44. (lambda (exn) 42)
  45. (lambda () (+ 10 (raise 69)))
  46. #:unwind? #t)))
  47. (test-call "69" (lambda ()
  48. (with-exception-handler
  49. (lambda (exn) exn)
  50. (lambda () (+ 10 (raise 69)))
  51. #:unwind? #t)))
  52. (test-call "42" (lambda ()
  53. (with-exception-handler
  54. (lambda (exn) 42)
  55. (lambda () (error "what"))
  56. #:unwind? #t)))
  57. (with-additional-imports
  58. ((hoot exceptions))
  59. (test-call "#(#t \"hey\" (ho))"
  60. (lambda (message irritants)
  61. (let ((exn (make-compound-exception
  62. (list
  63. (make-exception-with-message message)
  64. (make-exception-with-irritants irritants)))))
  65. (vector (error-object? exn)
  66. (error-object-message exn)
  67. (error-object-irritants exn))))
  68. "hey"
  69. '(ho)))
  70. (test-call "42"
  71. (lambda ()
  72. (guard (condition
  73. ((assq 'a condition) => cdr)
  74. ((assq 'b condition)))
  75. (raise (list (cons 'a 42))))))
  76. (test-call "(b . 23)"
  77. (lambda ()
  78. (guard (condition
  79. ((assq 'a condition) => cdr)
  80. ((assq 'b condition)))
  81. (raise (list (cons 'b 23))))))
  82. ;; Exception thrown from stdlib
  83. (test-call "42"
  84. (lambda (x y)
  85. (with-exception-handler
  86. (lambda (exn) 42)
  87. (lambda () (+ x y))
  88. #:unwind? #t))
  89. 1 "two")
  90. (test-equal "re-entrant exception handling"
  91. 42
  92. (compile-value '(let ()
  93. (define-foreign callback
  94. "host" "callback"
  95. (ref null extern) -> none)
  96. (with-exception-handler (lambda (exn) 42)
  97. (lambda ()
  98. (callback
  99. (procedure->external
  100. (lambda () (error "uh oh")))))
  101. #:unwind? #t))
  102. #:imports `(,@%default-program-imports
  103. (hoot exceptions)
  104. (hoot ffi))
  105. #:wasm-imports
  106. `(("host" .
  107. (("callback" .
  108. ,(lambda (proc)
  109. (proc))))))))
  110. (test-end* "test-exceptions")