factor/basis/cocoa/messages/messages.factor

249 lines
6.7 KiB
Factor
Raw Normal View History

2010-01-14 10:10:13 -05:00
! Copyright (C) 2006, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.data alien.strings
arrays assocs classes.struct continuations combinators compiler
core-graphics.types stack-checker kernel math namespaces make
quotations sequences strings words cocoa.runtime cocoa.types io
macros memoize io.encodings.utf8 effects layouts libc lexer init
core-foundation fry generalizations specialized-arrays locals ;
QUALIFIED-WITH: alien.c-types c
2007-09-20 18:09:08 -04:00
IN: cocoa.messages
SPECIALIZED-ARRAY: void*
: make-sender ( signature function -- quot )
2007-09-20 18:09:08 -04:00
[ over first , f , , second , \ alien-invoke , ] [ ] make ;
: sender-stub ( name signature function -- word )
[ "( sender-stub:" ")" surround f <word> dup ] 2dip
2007-09-20 18:09:08 -04:00
over first large-struct? [ "_stret" append ] when
make-sender dup infer define-declared ;
2007-09-20 18:09:08 -04:00
SYMBOL: message-senders
SYMBOL: super-message-senders
message-senders [ H{ } clone ] initialize
super-message-senders [ H{ } clone ] initialize
2007-09-20 18:09:08 -04:00
:: cache-stub ( name signature function assoc -- )
signature assoc [ [ name ] dip function sender-stub ] cache drop ;
2007-09-20 18:09:08 -04:00
: cache-stubs ( name signature -- )
[ "objc_msgSendSuper" super-message-senders get cache-stub ]
[ "objc_msgSend" message-senders get cache-stub ]
2bi ;
2007-09-20 18:09:08 -04:00
: <super> ( receiver -- super )
[ ] [ object_getClass class_getSuperclass ] bi
objc-super <struct-boa> ;
2007-09-20 18:09:08 -04:00
TUPLE: selector name object ;
MEMO: <selector> ( name -- sel ) f \ selector boa ;
2007-09-20 18:09:08 -04:00
: selector ( selector -- alien )
2008-08-29 11:24:22 -04:00
dup object>> expired? [
dup name>> sel_registerName
[ >>object drop ] keep
2007-09-20 18:09:08 -04:00
] [
2008-08-29 11:24:22 -04:00
object>>
2007-09-20 18:09:08 -04:00
] if ;
SYMBOL: objc-methods
objc-methods [ H{ } clone ] initialize
2007-09-20 18:09:08 -04:00
: lookup-method ( selector -- method )
dup objc-methods get at
[ ] [ "No such method: " prepend throw ] ?if ;
2007-09-20 18:09:08 -04:00
2007-10-06 13:39:59 -04:00
MEMO: make-prepare-send ( selector method super? -- quot )
2007-09-20 18:09:08 -04:00
[
[ \ <super> , ] when
swap <selector> , \ selector ,
2007-09-20 18:09:08 -04:00
] [ ] make
2008-11-29 16:35:39 -05:00
swap second length 2 - '[ _ _ ndip ] ;
2007-09-20 18:09:08 -04:00
MACRO: (send) ( selector super? -- quot )
2008-11-29 14:47:45 -05:00
[ dup lookup-method ] dip
2007-10-06 13:39:59 -04:00
[ make-prepare-send ] 2keep
super-message-senders message-senders ? get at
1quotation append ;
2007-09-20 18:09:08 -04:00
2008-01-24 18:20:27 -05:00
: send ( receiver args... selector -- return... ) f (send) ; inline
2007-09-20 18:09:08 -04:00
2008-01-24 18:20:27 -05:00
: super-send ( receiver args... selector -- return... ) t (send) ; inline
2007-09-20 18:09:08 -04:00
! Runtime introspection
SYMBOL: class-init-hooks
class-init-hooks [ H{ } clone ] initialize
: (objc-class) ( name word -- class )
2dup execute dup [ 2nip ] [
drop over class-init-hooks get at [ call( -- ) ] when*
2dup execute dup [ 2nip ] [
2drop "No such class: " prepend throw
] if
] if ; inline
2007-09-20 18:09:08 -04:00
: objc-class ( string -- class )
\ objc_getClass (objc-class) ;
: objc-protocol ( string -- class )
\ objc_getProtocol (objc-class) ;
: objc-meta-class ( string -- class )
\ objc_getMetaClass (objc-class) ;
SYMBOL: objc>alien-types
H{
{ "c" c:char }
{ "i" c:int }
{ "s" c:short }
{ "C" c:uchar }
{ "I" c:uint }
{ "S" c:ushort }
{ "f" c:float }
{ "d" c:double }
{ "B" c:bool }
{ "v" c:void }
{ "*" c:void* }
{ "?" unknown_type }
{ "@" id }
{ "#" Class }
{ ":" SEL }
}
cell {
{ 4 [ H{
{ "l" c:long }
{ "q" c:longlong }
{ "L" c:ulong }
{ "Q" c:ulonglong }
} ] }
{ 8 [ H{
{ "l" long32 }
{ "q" long }
{ "L" ulong32 }
{ "Q" ulong }
} ] }
} case
assoc-union objc>alien-types set-global
2007-09-20 18:09:08 -04:00
SYMBOL: objc>struct-types
H{
{ "_NSPoint" NSPoint }
{ "NSPoint" NSPoint }
{ "CGPoint" NSPoint }
{ "_NSRect" NSRect }
{ "NSRect" NSRect }
{ "CGRect" NSRect }
{ "_NSSize" NSSize }
{ "NSSize" NSSize }
{ "CGSize" NSSize }
{ "_NSRange" NSRange }
{ "NSRange" NSRange }
} objc>struct-types set-global
2007-09-20 18:09:08 -04:00
! The transpose of the above map
SYMBOL: alien>objc-types
objc>alien-types get [ swap ] assoc-map
! A hack...
cell {
{ 4 [ H{
{ NSPoint "{_NSPoint=ff}" }
{ NSRect "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
{ NSSize "{_NSSize=ff}" }
{ NSRange "{_NSRange=II}" }
{ NSInteger "i" }
{ NSUInteger "I" }
{ CGFloat "f" }
} ] }
{ 8 [ H{
{ NSPoint "{CGPoint=dd}" }
{ NSRect "{CGRect={CGPoint=dd}{CGSize=dd}}" }
{ NSSize "{CGSize=dd}" }
{ NSRange "{_NSRange=QQ}" }
{ NSInteger "q" }
{ NSUInteger "Q" }
{ CGFloat "d" }
} ] }
} case
assoc-union alien>objc-types set-global
2007-09-20 18:09:08 -04:00
: objc-struct-type ( i string -- ctype )
[ CHAR: = ] 2keep index-from swap subseq
objc>struct-types get at* [ drop void* ] unless ;
2007-09-20 18:09:08 -04:00
ERROR: no-objc-type name ;
: decode-type ( ch -- ctype )
1string dup objc>alien-types get at
[ ] [ no-objc-type ] ?if ;
2007-09-20 18:09:08 -04:00
: (parse-objc-type) ( i string -- ctype )
[ [ 1 + ] dip ] [ nth ] 2bi {
2007-09-20 18:09:08 -04:00
{ [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
{ [ dup CHAR: ^ = ] [ 3drop void* ] }
2007-09-20 18:09:08 -04:00
{ [ dup CHAR: { = ] [ drop objc-struct-type ] }
{ [ dup CHAR: [ = ] [ 3drop void* ] }
[ 2nip decode-type ]
2007-09-20 18:09:08 -04:00
} cond ;
: parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
: method-arg-type ( method i -- type )
method_copyArgumentType
[ utf8 alien>string parse-objc-type ] keep
(free) ;
2007-09-20 18:09:08 -04:00
: method-arg-types ( method -- args )
2010-01-14 10:10:13 -05:00
dup method_getNumberOfArguments iota
[ method-arg-type ] with map ;
2007-09-20 18:09:08 -04:00
: method-return-type ( method -- ctype )
method_copyReturnType
[ utf8 alien>string parse-objc-type ] keep
(free) ;
2007-09-20 18:09:08 -04:00
: method-name ( method -- name )
method_getName sel_getName ;
2007-09-20 18:09:08 -04:00
: register-objc-method ( method -- )
[ method-name ]
[ [ method-return-type ] [ method-arg-types ] bi 2array ] bi
[ cache-stubs ] [ swap objc-methods get set-at ] 2bi ;
2007-09-20 18:09:08 -04:00
2008-09-13 12:32:47 -04:00
: each-method-in-class ( class quot -- )
[ { uint } [ class_copyMethodList ] with-out-parameters ] dip
over 0 = [ 3drop ] [
[ void* <c-direct-array> ] dip
[ each ] [ drop (free) ] 2bi
] if ; inline
2007-09-20 18:09:08 -04:00
: register-objc-methods ( class -- )
2008-09-13 12:32:47 -04:00
[ register-objc-method ] each-method-in-class ;
2007-09-20 18:09:08 -04:00
: class-exists? ( string -- class ) objc_getClass >boolean ;
: define-objc-class-word ( quot name -- )
[ class-init-hooks get set-at ]
2007-09-20 18:09:08 -04:00
[
[ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
( -- class ) define-declared
] bi ;
2007-09-20 18:09:08 -04:00
: import-objc-class ( name quot -- )
2010-02-21 19:10:20 -05:00
2dup swap define-objc-class-word
2010-02-24 02:18:48 -05:00
over class-exists? [ drop ] [ call( -- ) ] if
dup class-exists? [
2010-02-21 19:10:20 -05:00
[ objc_getClass register-objc-methods ]
[ objc_getMetaClass register-objc-methods ] bi
] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: root-class ( class -- root )
dup class_getSuperclass [ root-class ] [ ] ?if ;