errors.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; Error constructors
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Exception constructors for common errors.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot errors)
  21. (export make-size-error
  22. make-index-error
  23. make-range-error
  24. make-start-offset-error
  25. make-end-offset-error
  26. make-type-error
  27. make-unimplemented-error
  28. make-assertion-error
  29. make-not-seekable-error
  30. make-runtime-error-with-message
  31. make-runtime-error-with-message+irritants
  32. make-match-error
  33. make-arity-error
  34. make-invalid-keyword-error
  35. make-unrecognized-keyword-error
  36. make-missing-keyword-argument-error
  37. raise
  38. raise-continuable
  39. raise-exception
  40. with-exception-handler
  41. error
  42. assert
  43. check-size
  44. check-index
  45. check-range
  46. check-type)
  47. (import (hoot primitives))
  48. (define-syntax-rule (define-error-constructor (name arg ...) global)
  49. (define (name arg ...)
  50. ((%inline-wasm '(func (result (ref eq)) (global.get global))) arg ...)))
  51. (define-error-constructor (make-size-error val max who)
  52. $make-size-error)
  53. (define-error-constructor (make-index-error val size who)
  54. $make-index-error)
  55. (define-error-constructor (make-range-error val min max who)
  56. $make-range-error)
  57. (define-error-constructor (make-start-offset-error val size who)
  58. $make-start-offset-error)
  59. (define-error-constructor (make-end-offset-error val size who)
  60. $make-end-offset-error)
  61. (define-error-constructor (make-type-error val who what)
  62. $make-type-error)
  63. (define-error-constructor (make-unimplemented-error who)
  64. $make-unimplemented-error)
  65. (define-error-constructor (make-assertion-error expr who)
  66. $make-assertion-error)
  67. (define-error-constructor (make-not-seekable-error port who)
  68. $make-not-seekable-error)
  69. (define-error-constructor (make-runtime-error-with-message msg)
  70. $make-runtime-error-with-message)
  71. (define-error-constructor (make-runtime-error-with-message+irritants msg irritants)
  72. $make-runtime-error-with-message+irritants)
  73. (define-error-constructor (make-match-error v)
  74. $make-match-error)
  75. (define-error-constructor (make-arity-error v who)
  76. $make-arity-error)
  77. (define-error-constructor (make-invalid-keyword-error kw)
  78. $make-invalid-keyword-error)
  79. (define-error-constructor (make-unrecognized-keyword-error kw)
  80. $make-unrecognized-keyword-error)
  81. (define-error-constructor (make-missing-keyword-argument-error kw)
  82. $make-missing-keyword-argument-error)
  83. (define (raise exn) (%raise-exception exn))
  84. (define (raise-continuable exn)
  85. ((%inline-wasm '(func (result (ref eq))
  86. (global.get $raise-exception)))
  87. exn #:continuable? #t))
  88. (define raise-exception
  89. (case-lambda*
  90. ((exn) (%raise-exception exn))
  91. ;; FIXME: keyword
  92. ((exn #:optional continuable-keyword continuable?)
  93. (if continuable?
  94. (raise-continuable exn)
  95. (%raise-exception exn)))))
  96. (define* (with-exception-handler handler thunk #:key (unwind? #f))
  97. ((%inline-wasm
  98. '(func (result (ref eq))
  99. (global.get $with-exception-handler)))
  100. handler thunk #:unwind? unwind?))
  101. (define error
  102. (case-lambda
  103. ((msg)
  104. (raise (make-runtime-error-with-message msg)))
  105. ((msg . args)
  106. (raise (make-runtime-error-with-message+irritants msg args)))))
  107. (define-syntax-rule (assert expr who)
  108. (unless expr
  109. (raise (make-assertion-error 'expr who))))
  110. (define-syntax-rule (check-size x max who)
  111. (unless (and (%exact-integer? x) (%<= 0 x) (%<= x max))
  112. (raise (make-size-error x max who))))
  113. (define-syntax-rule (check-index x size who)
  114. (unless (and (%exact-integer? x) (%<= 0 x) (%< x size))
  115. (raise (make-index-error x size who))))
  116. (define-syntax-rule (check-range x min max who)
  117. (unless (and (%exact-integer? x) (%<= min x) (%<= x max))
  118. (raise (make-range-error x min max who))))
  119. (define-syntax-rule (check-type x predicate who)
  120. (unless (predicate x)
  121. (raise (make-type-error x who 'predicate)))))