factor/library/cocoa/subclassing.factor

74 lines
2.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
IN: objc
2006-03-09 01:44:17 -05:00
USING: alien arrays compiler hashtables kernel kernel-internals
libc math namespaces sequences strings ;
2006-03-09 01:44:17 -05:00
: encode-types ( return types -- encoding )
>r 1array r> append
2006-03-10 22:57:57 -05:00
[ [ alien>objc-types get hash % CHAR: 0 , ] each ] "" make ;
2006-03-09 01:44:17 -05:00
: prepare-method ( { name return types quot } -- sel type imp )
[ first3 encode-types ] keep
2006-03-10 21:33:08 -05:00
[ 1 swap tail % \ alien-callback , ] [ ] make ;
2006-03-09 01:44:17 -05:00
: init-method ( method alien -- )
>r prepare-method r>
2006-03-10 21:33:08 -05:00
[ >r compile-1 r> set-objc-method-imp ] keep
[ >r <malloc-string> r> set-objc-method-types ] keep
>r sel_registerName r> set-objc-method-name ;
2006-03-09 01:44:17 -05:00
: <empty-method-list> ( n -- alien )
"objc-method-list" c-size
2006-03-10 21:33:08 -05:00
"objc-method" c-size pick * + 1 calloc
[ set-objc-method-list-count ] keep ;
2006-03-09 01:44:17 -05:00
: <method-list> ( methods -- alien )
dup length dup <empty-method-list> -rot
2006-03-10 21:33:08 -05:00
[ pick method-list@ objc-method-nth init-method ] 2each ;
2006-03-09 01:44:17 -05:00
: <method-lists> ( methods -- lists )
<method-list> alien-address
"void*" <malloc-object> [ 0 set-alien-unsigned-cell ] keep ;
: <objc-class> ( name info -- class )
"objc-class" <malloc-object>
[ set-objc-class-info ] keep
2006-03-09 01:44:17 -05:00
[ >r <malloc-string> r> set-objc-class-name ] keep ;
2006-03-09 01:44:17 -05:00
! The Objective C object model is a bit funny.
! Every class has a metaclass.
! The superclass of the metaclass of X is the metaclass of the
! superclass of X.
! The metaclass of the metaclass of X is the metaclass of the
! root class of X.
: meta-meta-class ( class -- class ) root-class objc-class-isa ;
: copy-instance-size ( class -- )
dup objc-class-super-class objc-class-instance-size
swap set-objc-class-instance-size ;
2006-03-09 01:44:17 -05:00
: <meta-class> ( methods superclass name -- class )
CLS_META <objc-class>
[ >r dup objc-class-isa r> set-objc-class-super-class ] keep
2006-03-09 01:44:17 -05:00
[ >r meta-meta-class r> set-objc-class-isa ] keep
[ >r <method-lists> r> set-objc-class-methodLists ] keep
dup copy-instance-size ;
2006-03-09 01:44:17 -05:00
: <new-class> ( methods metaclass superclass name -- class )
CLS_CLASS <objc-class>
[ set-objc-class-super-class ] keep
2006-03-09 01:44:17 -05:00
[ set-objc-class-isa ] keep
[ >r <method-lists> r> set-objc-class-methodLists ] keep
dup copy-instance-size ;
2006-03-09 01:44:17 -05:00
: (define-objc-class) ( imeth cmeth superclass name -- class )
>r objc-class r> [ <meta-class> ] 2keep <new-class>
dup objc_addClass ;
2006-03-09 01:44:17 -05:00
: define-objc-class ( imeth cmeth superclass name -- class )
dup class-exists?
2006-03-09 01:44:17 -05:00
[ >r 3drop r> objc-class ] [ (define-objc-class) ] if ;