gnu.scm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ;;; Guile extensions to SRFI-9
  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. ;;; Guile extensions to record types.
  18. ;;;
  19. ;;; Code:
  20. (define-module (srfi srfi-9 gnu)
  21. #:use-module ((hoot errors) #:select (check-type))
  22. #:use-module ((hoot primitives)
  23. #:select (%struct?
  24. %struct-set!
  25. guile:struct-vtable?
  26. guile:vtable-index-printer))
  27. #:use-module (hoot inline-wasm)
  28. #:export (set-record-type-printer!))
  29. (define (vtable? x)
  30. (cond-expand
  31. (guile-vm
  32. (guile:struct-vtable? x))
  33. (hoot
  34. (%inline-wasm
  35. '(func (param $x (ref eq)) (result (ref eq))
  36. (if (ref eq)
  37. (ref.test $vtable (local.get $x))
  38. (then
  39. (ref.i31 (i32.const 17)))
  40. (else
  41. (ref.i31 (i32.const 1)))))
  42. x))))
  43. (define (set-record-type-printer! vtable proc)
  44. (check-type vtable vtable? 'set-record-type-printer!)
  45. (check-type proc procedure? 'set-record-type-printer!)
  46. (cond-expand
  47. (guile-vm
  48. (%struct-set! vtable guile:vtable-index-printer proc))
  49. (hoot
  50. (%inline-wasm
  51. '(func (param $vtable (ref $vtable)) (param $proc (ref eq))
  52. (struct.set $vtable $printer
  53. (local.get $vtable)
  54. (local.get $proc)))
  55. vtable (lambda (x port write-field) (proc x port))))))