test-foreign-object-scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/sh
  2. exec guile -q -s "$0" "$@"
  3. !#
  4. ;;; test-foreign-object-scm --- Foreign object interface. -*- Scheme -*-
  5. ;;;
  6. ;;; Copyright (C) 2014 Free Software Foundation, Inc.
  7. ;;;
  8. ;;; This library is free software; you can redistribute it and/or
  9. ;;; modify it under the terms of the GNU Lesser General Public
  10. ;;; License as published by the Free Software Foundation; either
  11. ;;; version 3 of the License, or (at your option) any later version.
  12. ;;;
  13. ;;; This library is distributed in the hope that it will be useful,
  14. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ;;; Lesser General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU Lesser General Public
  19. ;;; License along with this library; if not, write to the Free Software
  20. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. (use-modules (system foreign)
  22. (system foreign-object)
  23. (rnrs bytevectors)
  24. (oop goops))
  25. (define (libc-ptr name)
  26. (catch #t
  27. (lambda () (dynamic-pointer name (dynamic-link)))
  28. (lambda (k . args)
  29. (print-exception (current-error-port) #f k args)
  30. (write "Skipping test.\n" (current-error-port))
  31. (exit 0))))
  32. (define malloc (pointer->procedure '* (libc-ptr "malloc") (list size_t)))
  33. (define memcpy (pointer->procedure void (libc-ptr "memcpy") (list '* '* size_t)))
  34. (define free (pointer->procedure void (libc-ptr "free") '(*)))
  35. (define (finalize-cstr cstr)
  36. (free (make-pointer (addr cstr))))
  37. (define-foreign-object-type <cstr> make-cstr (addr len)
  38. #:finalizer finalize-cstr)
  39. (define (cstr->string cstr)
  40. (pointer->string (make-pointer (addr cstr)) (len cstr) "UTF-8"))
  41. (define* (string->cstr str #:optional (k make-cstr))
  42. (let* ((bv (string->utf8 str))
  43. (len (bytevector-length bv))
  44. (mem (malloc len)))
  45. (when (null-pointer? mem)
  46. (error "Out of memory."))
  47. (memcpy mem (bytevector->pointer bv) len)
  48. (k (pointer-address mem) len)))
  49. (define-method (write (cstr <cstr>) port)
  50. (format port "<<cstr> ~s>" (cstr->string cstr)))
  51. (define-method (display (cstr <cstr>) port)
  52. (display (cstr->string cstr) port))
  53. (define-method (+ (a <cstr>) (b <cstr>))
  54. (string->cstr (string-append (cstr->string a) (cstr->string b))))
  55. (define-method (equal? (a <cstr>) (b <cstr>))
  56. (equal? (cstr->string a) (cstr->string b)))
  57. (define failed? #f)
  58. (define-syntax test
  59. (syntax-rules ()
  60. ((_ exp res)
  61. (let ((expected res)
  62. (actual exp))
  63. (if (not (equal? actual expected))
  64. (begin
  65. (set! failed? #t)
  66. (format (current-error-port)
  67. "bad return from expression `~a': expected ~A; got ~A~%"
  68. 'exp expected actual)))))))
  69. (test (string->cstr "Hello, world!")
  70. (+ (string->cstr "Hello, ") (string->cstr "world!")))
  71. ;; GOOPS construction syntax instead of make-cstr.
  72. (test (string->cstr "Hello, world!")
  73. (string->cstr "Hello, world!"
  74. (lambda (addr len)
  75. (make <cstr> #:addr addr #:len len))))
  76. ;; Subclassing.
  77. (define-class <wrapped-cstr> (<cstr>)
  78. (wrapped-string #:init-keyword #:wrapped-string
  79. #:getter wrapped-string
  80. #:init-form (error "missing #:wrapped-string")))
  81. (define (string->wrapped-cstr string)
  82. (string->cstr string (lambda (addr len)
  83. (make <wrapped-cstr> #:addr addr #:len len
  84. #:wrapped-string string))))
  85. (let ((wrapped-cstr (string->wrapped-cstr "Hello, world!")))
  86. ;; Tests that <cst> methods work on <wrapped-cstr>.
  87. (test "Hello, world!" (cstr->string wrapped-cstr))
  88. ;; Test the additional #:wrapped-string slot.
  89. (test "Hello, world!" (wrapped-string wrapped-cstr)))
  90. (gc) (gc) (gc)
  91. ;; Sleep 50 milliseconds to allow the finalization thread to run.
  92. (usleep #e50e3)
  93. ;; But we don't really know if it ran. Oh well.
  94. (exit (if failed? 1 0))
  95. ;; Local Variables:
  96. ;; mode: scheme
  97. ;; End: