errors.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. make-syntax-violation
  38. raise
  39. raise-continuable
  40. raise-exception
  41. with-exception-handler
  42. error
  43. assert
  44. check-size
  45. check-index
  46. check-range
  47. check-type)
  48. (import (only (hoot primitives) %raise-exception %exact-integer? %< %<=)
  49. (hoot inline-wasm)
  50. (hoot syntax))
  51. (define-syntax-rule (define-error-constructor (name arg ...) global)
  52. (define (name arg ...)
  53. ((%inline-wasm '(func (result (ref eq)) (global.get global))) arg ...)))
  54. (define-error-constructor (make-size-error val max who)
  55. $make-size-error)
  56. (define-error-constructor (make-index-error val size who)
  57. $make-index-error)
  58. (define-error-constructor (make-range-error val min max who)
  59. $make-range-error)
  60. (define-error-constructor (make-start-offset-error val size who)
  61. $make-start-offset-error)
  62. (define-error-constructor (make-end-offset-error val size who)
  63. $make-end-offset-error)
  64. (define-error-constructor (make-type-error val who what)
  65. $make-type-error)
  66. (define-error-constructor (make-unimplemented-error who)
  67. $make-unimplemented-error)
  68. (define-error-constructor (make-assertion-error expr who)
  69. $make-assertion-error)
  70. (define-error-constructor (make-not-seekable-error port who)
  71. $make-not-seekable-error)
  72. (define-error-constructor (make-runtime-error-with-message msg)
  73. $make-runtime-error-with-message)
  74. (define-error-constructor (make-runtime-error-with-message+irritants msg irritants)
  75. $make-runtime-error-with-message+irritants)
  76. (define-error-constructor (make-match-error v)
  77. $make-match-error)
  78. (define-error-constructor (make-arity-error v who)
  79. $make-arity-error)
  80. (define-error-constructor (make-invalid-keyword-error kw)
  81. $make-invalid-keyword-error)
  82. (define-error-constructor (make-unrecognized-keyword-error kw)
  83. $make-unrecognized-keyword-error)
  84. (define-error-constructor (make-missing-keyword-argument-error kw)
  85. $make-missing-keyword-argument-error)
  86. (define-error-constructor (make-syntax-violation who message form subform)
  87. $make-syntax-violation)
  88. (define (raise exn) (%raise-exception exn))
  89. (define (raise-continuable exn)
  90. ((%inline-wasm '(func (result (ref eq))
  91. (global.get $raise-exception)))
  92. exn #:continuable? #t))
  93. (define raise-exception
  94. (case-lambda*
  95. ((exn) (%raise-exception exn))
  96. ((exn #:key continuable?)
  97. (if continuable?
  98. (raise-continuable exn)
  99. (%raise-exception exn)))))
  100. (define* (with-exception-handler handler thunk #:key (unwind? #f) (unwind-for-type #t))
  101. ((%inline-wasm
  102. '(func (result (ref eq))
  103. (global.get $with-exception-handler)))
  104. handler thunk #:unwind? unwind? #:unwind-for-type unwind-for-type))
  105. (define error
  106. (case-lambda
  107. ((msg)
  108. (raise (make-runtime-error-with-message msg)))
  109. ((msg . args)
  110. (raise (make-runtime-error-with-message+irritants msg args)))))
  111. (define-syntax-rule (assert expr who)
  112. (unless expr
  113. (raise (make-assertion-error 'expr who))))
  114. (define-syntax-rule (check-size x max who)
  115. (unless (and (%exact-integer? x) (%<= 0 x) (%<= x max))
  116. (raise (make-size-error x max who))))
  117. (define-syntax-rule (check-index x size who)
  118. (unless (and (%exact-integer? x) (%<= 0 x) (%< x size))
  119. (raise (make-index-error x size who))))
  120. (define-syntax-rule (check-range x min max who)
  121. (unless (and (%exact-integer? x) (%<= min x) (%<= x max))
  122. (raise (make-range-error x min max who))))
  123. (define-syntax-rule (check-type x predicate who)
  124. (unless (predicate x)
  125. (raise (make-type-error x who 'predicate)))))