weak-refs.scm 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;;; Hoot weak references
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  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. ;;; Host weak ref bindings.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot weak-refs)
  21. (export make-weak-ref weak-ref? weak-ref-deref weak-ref-null?)
  22. (import (hoot eq)
  23. (hoot ffi)
  24. (hoot pairs)
  25. (hoot syntax))
  26. (define-foreign %make-weak-ref
  27. "rt" "make_weak_ref"
  28. (ref eq) -> (ref extern))
  29. (define-foreign %weak-ref-deref
  30. "rt" "weak_ref_deref"
  31. (ref extern) (ref eq) -> (ref eq))
  32. (define %weak-ref-null (list 'weak-ref-null))
  33. (define (weak-ref-null? obj)
  34. (eq? obj %weak-ref-null))
  35. (define-external-type <weak-ref>
  36. weak-ref?
  37. wrap-weak-ref
  38. unwrap-weak-ref)
  39. (define (make-weak-ref val)
  40. (wrap-weak-ref (%make-weak-ref val)))
  41. (define (weak-ref-deref ref)
  42. (%weak-ref-deref (unwrap-weak-ref ref) %weak-ref-null)))