test-foreign-object-scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. exec guile -q -s "$0" "$@"
  3. !#
  4. ;;; test-foreign-object-scm --- Foreign object interface. -*- Scheme -*-
  5. ;;;
  6. ;;; Copyright (C) 2014, 2017 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 ()
  28. (dynamic-pointer name
  29. (cond
  30. ((string-contains %host-type "cygwin")
  31. ;; On Cygwin, dynamic-link does not search
  32. ;; recursively into linked DLLs. Thus, one
  33. ;; needs to link to the core C library DLL
  34. ;; explicitly.
  35. (dynamic-link "cygwin1"))
  36. ((string-contains %host-type "mingw")
  37. (dynamic-link "msvcrt"))
  38. (else
  39. (dynamic-link)))))
  40. (lambda (k . args)
  41. (print-exception (current-error-port) #f k args)
  42. (write "Skipping test.\n" (current-error-port))
  43. (exit 0))))
  44. (define malloc (pointer->procedure '* (libc-ptr "malloc") (list size_t)))
  45. (define memcpy (pointer->procedure void (libc-ptr "memcpy") (list '* '* size_t)))
  46. (define free (pointer->procedure void (libc-ptr "free") '(*)))
  47. (define (finalize-cstr cstr)
  48. (free (make-pointer (addr cstr))))
  49. (define-foreign-object-type <cstr> make-cstr (addr len)
  50. #:finalizer finalize-cstr)
  51. (define (cstr->string cstr)
  52. (pointer->string (make-pointer (addr cstr)) (len cstr) "UTF-8"))
  53. (define* (string->cstr str #:optional (k make-cstr))
  54. (let* ((bv (string->utf8 str))
  55. (len (bytevector-length bv))
  56. (mem (malloc len)))
  57. (when (null-pointer? mem)
  58. (error "Out of memory."))
  59. (memcpy mem (bytevector->pointer bv) len)
  60. (k (pointer-address mem) len)))
  61. (define-method (write (cstr <cstr>) port)
  62. (format port "<<cstr> ~s>" (cstr->string cstr)))
  63. (define-method (display (cstr <cstr>) port)
  64. (display (cstr->string cstr) port))
  65. (define-method (+ (a <cstr>) (b <cstr>))
  66. (string->cstr (string-append (cstr->string a) (cstr->string b))))
  67. (define-method (equal? (a <cstr>) (b <cstr>))
  68. (equal? (cstr->string a) (cstr->string b)))
  69. (define failed? #f)
  70. (define-syntax test
  71. (syntax-rules ()
  72. ((_ exp res)
  73. (let ((expected res)
  74. (actual exp))
  75. (if (not (equal? actual expected))
  76. (begin
  77. (set! failed? #t)
  78. (format (current-error-port)
  79. "bad return from expression `~a': expected ~A; got ~A~%"
  80. 'exp expected actual)))))))
  81. (test (string->cstr "Hello, world!")
  82. (+ (string->cstr "Hello, ") (string->cstr "world!")))
  83. ;; GOOPS construction syntax instead of make-cstr.
  84. (test (string->cstr "Hello, world!")
  85. (string->cstr "Hello, world!"
  86. (lambda (addr len)
  87. (make <cstr> #:addr addr #:len len))))
  88. ;; Subclassing.
  89. (define-class <wrapped-cstr> (<cstr>)
  90. (wrapped-string #:init-keyword #:wrapped-string
  91. #:getter wrapped-string
  92. #:init-form (error "missing #:wrapped-string")))
  93. (define (string->wrapped-cstr string)
  94. (string->cstr string (lambda (addr len)
  95. (make <wrapped-cstr> #:addr addr #:len len
  96. #:wrapped-string string))))
  97. (let ((wrapped-cstr (string->wrapped-cstr "Hello, world!")))
  98. ;; Tests that <cst> methods work on <wrapped-cstr>.
  99. (test "Hello, world!" (cstr->string wrapped-cstr))
  100. ;; Test the additional #:wrapped-string slot.
  101. (test "Hello, world!" (wrapped-string wrapped-cstr)))
  102. (gc) (gc) (gc)
  103. ;; Sleep 50 milliseconds to allow the finalization thread to run.
  104. (usleep #e50e3)
  105. ;; But we don't really know if it ran. Oh well.
  106. (exit (if failed? 1 0))
  107. ;; Local Variables:
  108. ;; mode: scheme
  109. ;; End: