1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings arrays assocs
4 combinators compiler hashtables kernel libc math namespaces
5 parser sequences words cocoa.messages cocoa.runtime
6 compiler.units io.encodings.ascii ;
9 : init-method ( method alien -- )
11 [ >r execute r> set-objc-method-imp ] keep
12 [ >r ascii malloc-string r> set-objc-method-types ] keep
13 >r sel_registerName r> set-objc-method-name ;
15 : <empty-method-list> ( n -- alien )
16 "objc-method-list" heap-size
17 "objc-method" heap-size pick * + 1 calloc
18 [ set-objc-method-list-count ] keep ;
20 : <method-list> ( methods -- alien )
21 dup length dup <empty-method-list> -rot
22 [ pick method-list@ objc-method-nth init-method ] 2each ;
24 : define-objc-methods ( class methods -- )
25 <method-list> class_addMethods ;
27 : <objc-class> ( name info -- class )
28 "objc-class" malloc-object
29 [ set-objc-class-info ] keep
30 [ >r ascii malloc-string r> set-objc-class-name ] keep ;
32 : <protocol-list> ( name -- protocol-list )
33 "objc-protocol-list" malloc-object
34 1 over set-objc-protocol-list-count
35 swap objc-protocol over set-objc-protocol-list-class ;
37 ! The Objective C object model is a bit funny.
38 ! Every class has a metaclass.
40 ! The superclass of the metaclass of X is the metaclass of the
43 ! The metaclass of the metaclass of X is the metaclass of the
45 : meta-meta-class ( class -- class ) root-class objc-class-isa ;
47 : copy-instance-size ( class -- )
48 dup objc-class-super-class objc-class-instance-size
49 swap set-objc-class-instance-size ;
51 : <meta-class> ( superclass name -- class )
53 [ >r dup objc-class-isa r> set-objc-class-super-class ] keep
54 [ >r meta-meta-class r> set-objc-class-isa ] keep
55 dup copy-instance-size ;
57 : set-protocols ( protocols class -- )
59 { [ dup empty? ] [ 2drop ] }
60 { [ dup length 1 = ] [
62 swap set-objc-class-protocols
66 : <new-class> ( protocols metaclass superclass name -- class )
67 CLS_CLASS <objc-class>
68 [ set-objc-class-super-class ] keep
69 [ set-objc-class-isa ] keep
70 [ set-protocols ] keep
71 dup copy-instance-size ;
73 : (define-objc-class) ( protocols superclass name imeth -- )
76 [ <meta-class> ] 2keep <new-class> dup objc_addClass
77 r> <method-list> class_addMethods ;
79 : encode-types ( return types -- encoding )
81 alien>objc-types get at "0" append
84 : prepare-method ( ret types quot -- type imp )
85 >r [ encode-types ] 2keep r> [
86 "cdecl" swap 4array % \ alien-callback ,
87 ] [ ] make define-temp ;
89 : prepare-methods ( methods -- methods )
91 [ first4 prepare-method 3array ] map
92 ] with-compilation-unit ;
94 : redefine-objc-methods ( imeth name -- )
96 objc_getClass swap define-objc-methods
105 : define-objc-class ( imeth hash -- )
108 +name+ get "cocoa.classes" create drop
109 +name+ get 2dup redefine-objc-methods swap [
110 +protocols+ get , +superclass+ get , +name+ get , ,
111 \ (define-objc-class) ,
112 ] [ ] make import-objc-class
116 parse-definition unclip
117 >hashtable define-objc-class ; parsing