File: objc-ffi.scm

package info (click to toggle)
gauche-c-wrapper 0.5.4-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,892 kB
  • ctags: 2,869
  • sloc: ansic: 14,863; sh: 14,017; lisp: 6,822; asm: 6,456; makefile: 541; exp: 194; cpp: 157; objc: 144; perl: 2
file content (298 lines) | stat: -rw-r--r-- 11,902 bytes parent folder | download
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
;; -*- coding: utf-8; mode: scheme -*-
;;
;; objc-ffi.scm 
;;
;;   Copyright (c) 2006 KOGURO, Naoki (naoki@koguro.net)
;;   All rights reserved.
;;
;;   Redistribution and use in source and binary forms, with or without 
;;   modification, are permitted provided that the following conditions 
;;   are met:
;;
;;   1. Redistributions of source code must retain the above copyright 
;;      notice, this list of conditions and the following disclaimer.
;;   2. Redistributions in binary form must reproduce the above copyright 
;;      notice, this list of conditions and the following disclaimer in the 
;;      documentation and/or other materials provided with the distribution.
;;   3. Neither the name of the authors nor the names of its contributors 
;;      may be used to endorse or promote products derived from this 
;;      software without specific prior written permission.
;;
;;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
;;   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
;;   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
;;   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
;;   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
;;   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
;;   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
;;   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
;;   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
;;   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
;;   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;;   $Id: $
;;

(define-module c-wrapper.objc-ffi
  (use c-wrapper)
  (use file.util)
  (use srfi-1)
  (extend c-wrapper.c-ffi)
  
  (c-load '("stdlib.h" "objc/objc-runtime.h" "objc/objc-class.h")
          :libs "-lobjc"
          :compiled-lib "objc-ffilib")

  (export @ 
          @selector
          c-load-framework
          objc-lookup-class
          objc-register-method
          objc-add-method
          make-super
          <id>
          <SEL>)
  )

(select-module c-wrapper.objc-ffi)

(with-module c-wrapper.c-ffi
  (set! %c-load-framework
        (lambda (name)
          (or (and-let* ((filename (find-file-in-paths
                                    name :paths (map (cut string-append 
                                                          <>
                                                          name
                                                          ".framework/")
                                                     (list (build-path 
                                                            (sys-getenv "HOME")
                                                            "Library/Frameworks/")
                                                           "/Library/Frameworks/"
                                                           "/System/Library/Frameworks/"))
                                    :pred file-is-readable?)))
                (c-load-library filename))
              (errorf "framework ~a is not found" name)))))

(define c-load-framework (with-module c-wrapper.objc-ffi %c-load-framework))

(define method-pool (make-hash-table 'string=?))
(define ivar-pool (make-hash-table 'equal?))

(define (@ str)
  (objc-string <id> str)  )

(define (@selector name)
  (sel_registerName (x->string name)))

(define (encode-objc-type c-type)
  (format "~a~a"
          (cond
           ((equal? c-type <c-char>)
            "c")
           ((equal? c-type <c-int>)
            "i")
           ((equal? c-type <c-short>)
            "s")
           ((equal? c-type <c-long>)
            "l")
           ((equal? c-type <c-longlong>)
            "q")
           ((equal? c-type <c-uchar>)
            "C")
           ((equal? c-type <c-uint>)
            "I")
           ((equal? c-type <c-ushort>)
            "S")
           ((equal? c-type <c-ulong>)
            "L")
           ((equal? c-type <c-ulonglong>)
            "Q")
           ((equal? c-type <c-float>)
            "f")
           ((equal? c-type <c-double>)
            "d")
           ((equal? c-type <c-void>)
            "v")
           ((equal? c-type <id>)
            "@")
           ((equal? c-type <Class>)
            "#")
           ((equal? c-type <SEL>)
            ":")
           ((is-a? c-type <c-array-meta>)
            (format "[~a]" (encode-objc-type (element-type-of c-type))))
           ((is-a? c-type <c-struct-meta>)
            (format "{~a}"
                    (string-append (map (lambda (alist)
                                          (format "~a=~a"
                                                  (car alist)
                                                  (encode-objc-type (cdr alist))))
                                        (decl-alist-of c-type)))))
           ((is-a? c-type <c-union-meta>)
            (format "(~a)"
                    (string-append (map (lambda (alist)
                                          (format "~a"
                                                  (encode-objc-type (cdr alist))))
                                        (decl-alist-of c-type)))))
           ((bit-field? c-type)
                   "b")
                  ((is-a? c-type <c-ptr-meta>)
                   (format "^~a" (encode-objc-type (orig-c-type-of c-type))))
                  (else
                   "?"))
          (cond
           ((bit-field? c-type)
            (bits-of c-type))
           (else
            (c-sizeof c-type)))))
            
                           
(define (objc-lookup-class name)
  (objc_lookUpClass (symbol->string name)))

(define (objc-register-method name type-list)
  (hash-table-put! method-pool name type-list))

(define (nil? x)
  (eq? (ref x) nil))

(define (objc-make-class name super-class-ptr)
  (unless (nil? (objc-lookup-class name))
    (errorf "Objective-C class '~a' is already exists." name))
  (let ((root-class-ptr (do ((class-ptr super-class-ptr
                                        (ref (deref class-ptr) 'super_class)))
                            ((nil? (ref (deref class-ptr) 'super_class))
                             class-ptr)))
        (new-class (deref (cast <Class>
                                (malloc (c-sizeof (c-struct 'objc_class))))))
        (meta-class (deref (cast <Class>
                                 (malloc (c-sizeof (c-struct 'objc_class)))))))
    (set! (ref new-class 'isa) (ptr meta-class))
    (set! (ref new-class 'info) CLS_CLASS)
    (set! (ref meta-class 'info) CLS_META)
    
    (set! (ref new-class 'name) (symbol->string name))
    (set! (ref meta-class 'name) (symbol->string name))
    
    (set! (ref new-class 'methodLists) (make-null-ptr))
    (set! (ref meta-class 'methodLists) (make-null-ptr))
    
    (set! (ref new-class 'super_class) super-class-ptr)
    (set! (ref meta-class 'super_class) (ref (deref super-class-ptr) 'isa))
    (set! (ref meta-class 'isa) (ref (deref root-class-ptr) 'isa))
    
    (set! (ref new-class 'instance_size) (ref (deref super-class-ptr)
                                              'instance_size))
    (set! (ref meta-class 'instance_size)
          (ref (deref (ref meta-class 'super_class)) 'instance_size))
    
    (objc_addClass (ptr new-class))
    new-class))

(define (objc-add-method class method-name ret-type arg-types proc)
  (let ((selector (@selector method-name))
        (method (make (c-struct 'objc_method)))
        (method-list (make (c-struct 'objc_method_list))))
    (set! (ref method 'method_name) selector)
    (set! (ref method 'method_types)
          (apply string-append (map encode-objc-type
                                    (list* ret-type <id> <SEL> arg-types))))
    (set! (ref method 'method_imp)
          (cast (c-func-ptr ret-type (list* <id> <SEL> arg-types)) proc))
    
    (set! (ref method-list 'method_count) 1)
    (set! (ref (ref method-list 'method_list) 0) method)
    
    (class_addMethods class (ptr method-list))))

(define (needs-objc_msgSend_stret? ret-type)
  (define (composite-type? ret-type)
    (or (is-a? ret-type <c-struct-meta>)
        (is-a? ret-type <c-union-meta>)))
  (if (big-endian?)
      ;; Mac on PPC
      (composite-type? ret-type)
      ;; Mac on Intel
      (cond
       ((not (composite-type? ret-type))
        #f)
       ((< 8 (c-sizeof ret-type))
        #t)
       (else
        (let loop ((composite-type ret-type))
          (find (lambda (type)
                  (cond
                   ((composite-type? type)
                    (loop type))
                   ((and (is-a? type <c-array-meta>)
                         (< 1 (size-of type)))
                    #t)
                   (else
                    #f)))
                (map cdr (decl-alist-of composite-type))))))))
        
(define (%objc-msg-send obj super? selector-name . args)
  (or (and-let* ((lst (hash-table-get method-pool selector-name)))
        (let* ((ret-type (car lst))
               (arg-types (cdr lst))
               (send (if (needs-objc_msgSend_stret? ret-type)
                         (lambda (obj selector . args)
                           (let ((retval (make ret-type)))
                             (apply (make-c-func-vaargs
                                     (if super?
                                         'objc_msgSendSuper_stret
                                         'objc_msgSend_stret)
                                     <c-void>
                                     (append (list (ptr <c-void>)
                                                   (if super?
                                                       (ptr (c-struct 'objc_super))
                                                       <id>)
                                                   <SEL>)
                                             arg-types))
                                    (ptr retval) obj selector args)
                             retval))
                         (make-c-func-vaargs (if super?
                                                 'objc_msgSendSuper
                                                 'objc_msgSend)
                                             ret-type
                                             (append (list (if super?
                                                               (ptr (c-struct 'objc_super))
                                                               <id>)
                                                           <SEL>)
                                                     arg-types))))
               (sel (@selector selector-name)))
          (if (null-ptr? sel)
              (errorf "selector ~a is not found." selector-name)
              (apply send obj sel args))))
      (errorf "selector ~a is not found." selector-name)))
                                       
(define (args->selector-name args)
  (string-append
   (string-join (map x->string
                     (filter (lambda (elem)
                               (or (symbol? elem) (keyword? elem)))
                             args))
                ":")
   (if (= (length args) 1)
       ""
       ":")))

(define (filter-args args)
  (remove (lambda (elem) (or (symbol? elem) (keyword? elem))) args))

(define-method object-apply ((obj <id>) . rest)
  (apply %objc-msg-send obj #f (args->selector-name rest) (filter-args rest)))

(define-method object-apply ((obj (ptr (c-struct 'objc_super))) . rest)
  (apply %objc-msg-send obj #t (args->selector-name rest) (filter-args rest)))

(define (make-super class obj)
  (let ((data (make (c-struct 'objc_super))))
    (set! (ref data 'receiver) obj)
    (set! (ref data 'class)
          (ref (deref (cast (ptr (c-struct 'objc_class)) class)) 'super_class))
    (ptr data)))

(provide "c-wrapper/objc-ffi")

;; end of file