1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs hashtables kernel sequences generic words
4 arrays classes slots slots.private classes.tuple
5 classes.tuple.private math vectors quotations accessors
9 TUPLE: mirror { object read-only } ;
13 : object-slots ( mirror -- slots ) object>> class all-slots ; inline
16 [ nip object>> ] [ object-slots slot-named ] 2bi
17 dup [ offset>> slot t ] [ 2drop f f ] if ;
19 ERROR: no-such-slot slot ;
20 ERROR: read-only-slot slot ;
22 : check-set-slot ( val slot -- val offset )
24 { [ dup not ] [ no-such-slot ] }
25 { [ dup read-only>> ] [ read-only-slot ] }
26 { [ 2dup class>> instance? not ] [ class>> bad-slot-value ] }
30 M: mirror set-at ( val key mirror -- )
31 [ object-slots slot-named check-set-slot ] [ object>> ] bi
34 M: mirror delete-at ( key mirror -- )
37 M: mirror clear-assoc ( mirror -- )
38 [ object>> ] [ object-slots ] bi [
39 [ initial>> ] [ offset>> ] bi swapd set-slot
42 M: mirror >alist ( mirror -- alist )
43 [ object-slots [ [ name>> ] map ] [ [ offset>> ] map ] bi ]
44 [ object>> [ swap slot ] curry ] bi
47 M: mirror assoc-size object>> layout-of second ;
49 INSTANCE: mirror assoc
51 GENERIC: make-mirror ( obj -- assoc )
52 M: hashtable make-mirror ;
53 M: integer make-mirror drop f ;
54 M: array make-mirror <enum> ;
55 M: vector make-mirror <enum> ;
56 M: quotation make-mirror <enum> ;
57 M: object make-mirror <mirror> ;