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