File: ypsilon-compat.ikarus.ss

package info (click to toggle)
ikarus 0.0.3%2Bbzr.2010.01.26-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 39,868 kB
  • ctags: 9,284
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 264; awk: 186; perl: 66
file content (305 lines) | stat: -rw-r--r-- 10,056 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
;;; Copyright (C) 2008,2009  Abdulaziz Ghuloum
;;; 
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License version 3 as
;;; published by the Free Software Foundation.
;;; 
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;; 
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.



(library (ypsilon-compat)
  (export on-windows on-darwin on-linux on-freebsd on-posix
          load-shared-object c-argument c-function 
          microsecond usleep library-pointer
          (rename (ypsilon:format format)))
  (import 
    (ikarus system $foreign)
    (except (ikarus) library))

  (define (microsecond)
    (let ([t (current-time)])
      (+ (* (time-second t) 1000000)
         (div (time-nanosecond t) 1000))))

  (define (usleep . args) (error '#f "invalid args" args))

  (define (ypsilon:format what str . args)
    (cond
      [(eq? what #t) 
       (apply printf str args)]
      [(eq? what #f) 
       (apply format str args)]
      [else (error 'ypsion:format "invalid what" what)]))


  (define (architecture-feature what)
    (case what
      [(operating-system) (host-info)]
      [else (error 'architecture-feature "invalid args" what)]))

  (define (string-contains text s)
    (define (starts-at? i)
      (let f ([i i] [j 0])
        (cond
          [(= j (string-length s)) #t]
          [(= i (string-length text)) #f]
          [else
           (and (char=? (string-ref text i) (string-ref s j))
                (f (+ i 1) (+ j 1)))])))
    (let f ([i 0])
      (cond
        [(= i (string-length text)) #f]
        [(starts-at? i) #t]
        [else (f (+ i 1))])))

  (define on-windows (and (string-contains (architecture-feature 'operating-system) "windows") #t))
  (define on-darwin  (and (string-contains (architecture-feature 'operating-system) "darwin")  #t))
  (define on-linux   (and (string-contains (architecture-feature 'operating-system) "linux")   #t))
  (define on-freebsd (and (string-contains (architecture-feature 'operating-system) "freebsd") #t))
  (define on-posix   (not on-windows))

  
  (define-record-type library (fields name pointer))

  (define (load-shared-object libname) 
    (unless (string? libname)
      (error 'load-shared-object "library name must be a string"
             libname))
    (make-library libname
      (or (dlopen libname)
          (error 'load-shared-object (dlerror) libname))))
  
  (define (int? x) (or (fixnum? x) (bignum? x)))

  (define (check-int who x)
    (cond
      [(int? x) x]
      [else (die who "not an int" x)]))
  
  (define (vector-andmap f v)
    (andmap f (vector->list v)))

  (define (check-int* who x)
    (cond
      [(and (vector? x) (vector-andmap int? x))
       (let ([n (vector-length x)])
         (let ([p (malloc (* n 4))])
           (let f ([i 0])
             (cond
               [(= i n) p]
               [else
                (pointer-set-c-int! p (* i 4) (vector-ref x i))
                (f (+ i 1))]))))]
      [else (die who "not an int*" x)]))

  (define (check-char* who x)
    (cond
      [(string? x)
       (check-byte* who (string->utf8 x))]
      [else (die who "not a char*" x)]))
      
  (define pointer-size
    (cond
      [(<= (fixnum-width) 32) 4]
      [else                   8]))

  (define (check-char** who x)
    (cond
      [(and (vector? x) (vector-andmap string? x))
       (let ([n (vector-length x)])
         (let ([p (malloc (* n pointer-size))])
           (let f ([i 0])
             (cond
               [(= i n) p]
               [else
                (pointer-set-c-pointer! p (* i pointer-size)
                  (check-char* who (vector-ref x i)))
                (f (+ i 1))]))))]
      [else (die who "not a char**" x)]))

  (define (check-byte* who x)
    (cond
      [(bytevector? x)
       (let ([n (bytevector-length x)])
         (let ([p (malloc (+ n 1))])
           (pointer-set-c-char! p n 0)
           (let f ([i 0])
             (cond
               [(= i n) p]
               [else
                (pointer-set-c-char! p i (bytevector-u8-ref x i))
                (f (+ i 1))]))))]
      [else (die who "not a byte*" x)]))
  
  (define (check-float who x)
    (cond
      [(flonum? x) x]
      [else (die who "not a flonum" x)]))

  (define (check-double who x)
    (cond
      [(flonum? x) x]
      [else (die who "not a double" x)]))

  (define-syntax check-callback
    (lambda (x)
      (syntax-case x ()
        [(_ foreign-name val return-type (arg-type* ...))
         #'(let ([t val])
             (if (procedure? t)
                 ((make-c-callback
                    (convert-type return-type)
                    (list (convert-type arg-type*) ...))
                  t)
                 (error 'foreign-name "not a procedure" t)))])))

  (define-syntax todo
    (syntax-rules ()
      [(_ name* ...)
       (begin
         (define (name* . args) (error 'name* "not implemented"))
         ...)]))

  (define (check-void* who x)
    (cond
      [(pointer? x) x]
      [else (die who "not a void*" x)]))

  (define-syntax convert-arg
    (lambda (x)
      (syntax-case x (int char* byte* c-callback float double void*)
        [(_ form foreign-name val char*)
         #'(check-char* 'foreign-name val)]
        [(_ form foreign-name val byte*)
         #'(check-byte* 'foreign-name val)]
        [(_ form foreign-name val void*)
         #'(check-void* 'foreign-name val)]
        [(_ form foreign-name val int)
         #'(check-int 'foreign-name val)]
        [(_ form foreign-name val float)
         #'(check-float 'foreign-name val)]
        [(_ form foreign-name val double)
         #'(check-double 'foreign-name val)]
        [(_ form foreign-name val [int])
         #'(check-int* 'foreign-name val)]
        [(_ form foreign-name val [char*])
         #'(check-char** 'foreign-name val)]
        [(_ form foreign-name val [c-callback return-type (arg-types ...)])
         #'(check-callback foreign-name val return-type (arg-types ...))]
        [(_ form foreign-name val arg-type)
         (syntax-violation 'c-function "invalid argument type"
                           #'form #'arg-type)])))

  (define (convert-out-byte* who s-val c-val)
    (let ((n (bytevector-length s-val)))
      (let loop ([i 0])
        (unless (= i n)
          (bytevector-u8-set! s-val i (pointer-ref-c-unsigned-char c-val i))
          (loop (+ i 1))))))

  (define-syntax convert-out-arg
    (lambda (x)
      (syntax-case x (int char* byte* c-callback float double void*)
        ((_ form foreign-name s-val c-val byte*)
         #'(convert-out-byte* 'foreign-name s-val c-val))
        ((_ form foreign-name s-val c-val arg-ype)
         #'(void)))))

  (define (char*->string who x)
    (define (strlen x)
      (let f ([i 0])
        (cond
          [(= 0 (pointer-ref-c-unsigned-char x i)) i]
          [else (f (+ i 1))])))
    (let ([n (strlen x)])
      (let ([s (make-string n)])
        (let f ([i 0])
          (if (= i n) 
              s
              (begin
                (string-set! s i 
                  (integer->char (pointer-ref-c-unsigned-char x i)))
                (f (+ i 1))))))))

  (define-syntax convert-return
    (lambda (x)
      (syntax-case x (char*)
        [(_ form foreign-name val char*)
         #'(char*->string 'foreign-name val)]
        [(_ form foreign-name val other) 
         #'val])))

  (define-syntax convert-type
    (lambda (x)
      (define ls
        '([void    void]
          [char*   pointer]
          [float   float]
          [double  double]
          [void*   pointer]
          [byte*   pointer]
          [int     signed-int]))
      (define (valid x)
        (cond
          [(and (list? x) (= (length x) 3) (eq? (car x) 'c-callback))
           (and (valid (cadr x))
                (andmap valid (caddr x))
                'pointer)]
          [(list? x)
           (and (andmap valid x) 'pointer)]
          [(assq x ls) => cadr]
          [else #f]))
      (syntax-case x (void)
        [(ctxt t)
         (cond
           [(valid (syntax->datum #'t)) => 
            (lambda (t)
              (with-syntax ([t (datum->syntax #'ctxt t)])
                #'(quote t)))]
           [else (syntax-violation #f "invalid type" #'t)])])))

  (define (lookup-shared-object lib name) 
    (define who 'lookup-shared-object)
    (unless (symbol? name) (die who "not a symbol" name))
    (unless (library? lib) (die who "not a library" lib))
    (or (dlsym (library-pointer lib) (symbol->string name))
      (error who 
        (format "cannot find object ~a in library ~a" 
                name (library-name lib)))))

  (define-syntax c-function
    (lambda (x)
      (syntax-case x ()
        [(_ lib lib-name return-type conv foreign-name (arg-type* ...))
         (with-syntax ([x x]
                       [(t* ...) (generate-temporaries #'(arg-type* ...))]
                       [(u* ...) (generate-temporaries #'(arg-type* ...))])
         #'(let ([callout 
                   ((make-c-callout 
                      (convert-type return-type)
                      (list (convert-type arg-type*) ...))
                    (lookup-shared-object lib 'foreign-name))])
             (lambda (t* ...)
               (let ([u* (convert-arg x foreign-name t* arg-type*)] ...)
                 (let ([v (callout u* ...)])
                   (convert-out-arg x foreign-name t* u* arg-type*)
                   ...
                   (convert-return x foreign-name v return-type))))))])))

  (define-syntax c-argument
    (lambda (x)
      (syntax-case x ()
        [(_ function-name argnum argtype argval)
         (begin
           ;(printf "syntax ~s\n" (syntax->datum x))
           #'(void))])))

)