]> gitweb.factorcode.org Git - factor.git/blob - basis/values/values.factor
0dd1058370a75ab334c984b88bcffd45d74a410d
[factor.git] / basis / values / values.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel parser words sequences quotations ;
4 IN: values
5
6 ! Mutating literals in word definitions is not really allowed,
7 ! and the deploy tool takes advantage of this fact to perform
8 ! some aggressive stripping and compression. However, this
9 ! breaks a naive implementation of values. We need to do two
10 ! things:
11 ! 1) Store the value in a subclass of identity-tuple, so that
12 ! two quotations from different value words are never equal.
13 ! This avoids bogus merging of values.
14 ! 2) Set the "no-def-strip" word-prop, so that the shaker leaves
15 ! the def>> slot alone, allowing us to introspect it. Otherwise,
16 ! it will get set to [ ] and we would lose access to the
17 ! value-holder.
18
19 <PRIVATE
20
21 TUPLE: value-holder < identity-tuple obj ;
22
23 PRIVATE>
24
25 : VALUE:
26     CREATE-WORD
27     dup t "no-def-strip" set-word-prop
28     T{ value-holder } clone [ obj>> ] curry
29     (( -- value )) define-declared ; parsing
30
31 : set-value ( value word -- )
32     def>> first (>>obj) ;
33
34 : to:
35     scan-word literalize parsed
36     \ set-value parsed ; parsing
37
38 : get-value ( word -- value )
39     def>> first obj>> ;
40
41 : change-value ( word quot -- )
42     [ [ get-value ] dip call ] [ drop ] 2bi set-value ; inline