ImmArray.st
author Stefan Vogel <sv@exept.de>
Fri, 06 Mar 1998 16:38:37 +0100
changeset 657 0ecf1ff6f6bf
parent 539 69a1cd05c7d6
child 754 1363fa7dfcd1
permissions -rw-r--r--
Fix #makeMethod:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65
441a440ab59d Initial revision
claus
parents:
diff changeset
     1
"
441a440ab59d Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1995 by Claus Gittinger
441a440ab59d Initial revision
claus
parents:
diff changeset
     3
	      All Rights Reserved
441a440ab59d Initial revision
claus
parents:
diff changeset
     4
441a440ab59d Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
441a440ab59d Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
441a440ab59d Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
441a440ab59d Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
441a440ab59d Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
441a440ab59d Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
441a440ab59d Initial revision
claus
parents:
diff changeset
    11
"
441a440ab59d Initial revision
claus
parents:
diff changeset
    12
441a440ab59d Initial revision
claus
parents:
diff changeset
    13
Array subclass:#ImmutableArray
261
0372e948ca2d commentary
Claus Gittinger <cg@exept.de>
parents: 148
diff changeset
    14
	instanceVariableNames:''
0372e948ca2d commentary
Claus Gittinger <cg@exept.de>
parents: 148
diff changeset
    15
	classVariableNames:''
0372e948ca2d commentary
Claus Gittinger <cg@exept.de>
parents: 148
diff changeset
    16
	poolDictionaries:''
0372e948ca2d commentary
Claus Gittinger <cg@exept.de>
parents: 148
diff changeset
    17
	category:'System-Compiler-Support'
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    18
!
441a440ab59d Initial revision
claus
parents:
diff changeset
    19
539
69a1cd05c7d6 added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 292
diff changeset
    20
!ImmutableArray class methodsFor:'documentation'!
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    21
441a440ab59d Initial revision
claus
parents:
diff changeset
    22
copyright
441a440ab59d Initial revision
claus
parents:
diff changeset
    23
"
441a440ab59d Initial revision
claus
parents:
diff changeset
    24
 COPYRIGHT (c) 1995 by Claus Gittinger
441a440ab59d Initial revision
claus
parents:
diff changeset
    25
	      All Rights Reserved
441a440ab59d Initial revision
claus
parents:
diff changeset
    26
441a440ab59d Initial revision
claus
parents:
diff changeset
    27
 This software is furnished under a license and may be used
441a440ab59d Initial revision
claus
parents:
diff changeset
    28
 only in accordance with the terms of that license and with the
441a440ab59d Initial revision
claus
parents:
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
441a440ab59d Initial revision
claus
parents:
diff changeset
    30
 be provided or otherwise made available to, or used by, any
441a440ab59d Initial revision
claus
parents:
diff changeset
    31
 other person.  No title to or ownership of the software is
441a440ab59d Initial revision
claus
parents:
diff changeset
    32
 hereby transferred.
441a440ab59d Initial revision
claus
parents:
diff changeset
    33
"
441a440ab59d Initial revision
claus
parents:
diff changeset
    34
!
441a440ab59d Initial revision
claus
parents:
diff changeset
    35
441a440ab59d Initial revision
claus
parents:
diff changeset
    36
documentation
441a440ab59d Initial revision
claus
parents:
diff changeset
    37
"
441a440ab59d Initial revision
claus
parents:
diff changeset
    38
    By default, array literals in smalltalk are mutable objects. That
98
claus
parents: 97
diff changeset
    39
    may lead to some subtle (and hard to find errors), if some method passes
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    40
    a literal array constant as argument to someone else, who changes the
441a440ab59d Initial revision
claus
parents:
diff changeset
    41
    array using at:put: like messages. Since the array object is kept in 
441a440ab59d Initial revision
claus
parents:
diff changeset
    42
    the first methods literals, the array constant has now been changed without
98
claus
parents: 97
diff changeset
    43
    having the methods sourcecode reflect this. Thus, the method will
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    44
    behave differently from what its source may make you think.
441a440ab59d Initial revision
claus
parents:
diff changeset
    45
98
claus
parents: 97
diff changeset
    46
    To help finding this kind of 'feature/bug', the compiler can be
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    47
    configured to create instances of this ImmutableArray instead of Arrays
441a440ab59d Initial revision
claus
parents:
diff changeset
    48
    for array literals. Instances of ImmutableArray catch storing accesses and
441a440ab59d Initial revision
claus
parents:
diff changeset
    49
    enter the debugger. Although useful, this feature is disabled by default
441a440ab59d Initial revision
claus
parents:
diff changeset
    50
    for compatibility to other smalltalk implementations. 
441a440ab59d Initial revision
claus
parents:
diff changeset
    51
    (Also, if turned on, this makes inspecting array literals entered in
441a440ab59d Initial revision
claus
parents:
diff changeset
    52
     a workspace somewhat strange: you cannot modify it any longer).
441a440ab59d Initial revision
claus
parents:
diff changeset
    53
441a440ab59d Initial revision
claus
parents:
diff changeset
    54
    Turn the ImmutableArray feature on by setting the Parsers class variable
98
claus
parents: 97
diff changeset
    55
    'ArraysAreImmutable' to true or use the new launchers settings menu.
263
3b21d0991eff documentation
Claus Gittinger <cg@exept.de>
parents: 261
diff changeset
    56
3b21d0991eff documentation
Claus Gittinger <cg@exept.de>
parents: 261
diff changeset
    57
    [author:]
3b21d0991eff documentation
Claus Gittinger <cg@exept.de>
parents: 261
diff changeset
    58
        Claus Gittinger
65
441a440ab59d Initial revision
claus
parents:
diff changeset
    59
"
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    60
! !
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    61
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    62
!ImmutableArray methodsFor:'accessing'!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    63
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    64
at: index put: value
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    65
    "Trigger an error if an immutable array is stored into.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    66
     The store will be performed (for compatibility reasons) if you continue
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    67
     in the debugger."
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    68
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    69
    self notifyStoreError.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    70
    ^ super at: index put: value
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    71
!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    72
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    73
basicAt: index put: value
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    74
    "Trigger an error if an immutable array is stored into.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    75
     The store will be performed (for compatibility reasons) if you continue
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    76
     in the debugger."
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    77
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    78
    self notifyStoreError.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    79
    ^ super basicAt: index put: value
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    80
! !
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    81
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    82
!ImmutableArray methodsFor:'copying'!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    83
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    84
copyEmpty
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    85
    "when copying, return a real (mutable) Array"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    86
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    87
    ^ Array new:self size
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    88
!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    89
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    90
copyEmptyAndGrow:size
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    91
    "when copying, return a real (mutable) Array"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    92
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    93
    ^ Array new:size
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    94
!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    95
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    96
postCopy
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    97
    "when copied, make it me a real (mutable) Array"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    98
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    99
    self changeClassTo:Array.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   100
!
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   101
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   102
shallowCopy
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   103
    "when copying, return a real (mutable) Array"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   104
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   105
    |sz|
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   106
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   107
    sz := self size.
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   108
    ^ (Array new:sz)
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   109
	replaceFrom:1 to:sz with:self startingAt:1
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   110
! !
441a440ab59d Initial revision
claus
parents:
diff changeset
   111
441a440ab59d Initial revision
claus
parents:
diff changeset
   112
!ImmutableArray methodsFor:'error handling'!
441a440ab59d Initial revision
claus
parents:
diff changeset
   113
441a440ab59d Initial revision
claus
parents:
diff changeset
   114
creator 
441a440ab59d Initial revision
claus
parents:
diff changeset
   115
    "find the method that contains me"
441a440ab59d Initial revision
claus
parents:
diff changeset
   116
84
claus
parents: 69
diff changeset
   117
    Method allSubInstances do:[:aMethod |
292
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   118
        (aMethod referencesGlobal:self) ifTrue:[
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   119
            ^ aMethod.
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   120
        ].
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   121
    ].
441a440ab59d Initial revision
claus
parents:
diff changeset
   122
    ^ nil
292
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   123
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   124
    " 
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   125
      #(1 2 3) creator
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   126
    "
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   127
163651658aee Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents: 263
diff changeset
   128
    "Modified: 24.6.1996 / 15:36:28 / stefan"
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   129
!
441a440ab59d Initial revision
claus
parents:
diff changeset
   130
441a440ab59d Initial revision
claus
parents:
diff changeset
   131
notifyStoreError
441a440ab59d Initial revision
claus
parents:
diff changeset
   132
    "a store is attempted - for our convenience, find the method that
441a440ab59d Initial revision
claus
parents:
diff changeset
   133
     contains me, for a nicer error message"
441a440ab59d Initial revision
claus
parents:
diff changeset
   134
441a440ab59d Initial revision
claus
parents:
diff changeset
   135
    |creator msg|
441a440ab59d Initial revision
claus
parents:
diff changeset
   136
441a440ab59d Initial revision
claus
parents:
diff changeset
   137
    creator := self creator.
97
claus
parents: 84
diff changeset
   138
    msg := 'store into/change of literal'.
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   139
    creator notNil ifTrue:[
441a440ab59d Initial revision
claus
parents:
diff changeset
   140
	msg := msg , ' (' , creator whoString , ')'
441a440ab59d Initial revision
claus
parents:
diff changeset
   141
    ].
441a440ab59d Initial revision
claus
parents:
diff changeset
   142
    "
441a440ab59d Initial revision
claus
parents:
diff changeset
   143
     this error is reported on an attempt to store into a literal
441a440ab59d Initial revision
claus
parents:
diff changeset
   144
     array. The literal was created in creator.
441a440ab59d Initial revision
claus
parents:
diff changeset
   145
     If you press continue in the debugger, the store will be performed.
441a440ab59d Initial revision
claus
parents:
diff changeset
   146
     If you dont want this, press abort and check your code.
441a440ab59d Initial revision
claus
parents:
diff changeset
   147
     Storing into literals is VERY VERY bad coding style.
441a440ab59d Initial revision
claus
parents:
diff changeset
   148
    "
441a440ab59d Initial revision
claus
parents:
diff changeset
   149
    self error:msg
441a440ab59d Initial revision
claus
parents:
diff changeset
   150
! !
441a440ab59d Initial revision
claus
parents:
diff changeset
   151
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   152
!ImmutableArray methodsFor:'private'!
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   153
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   154
species
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   155
    "Copies should be mutable"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   156
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   157
    ^Array
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   158
! !
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   159
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   160
!ImmutableArray methodsFor:'queries'!
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   161
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   162
isLiteral
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   163
    "return true, if the receiver can be used as a literal
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   164
     (i.e. can be used in constant arrays)"
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   165
140
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   166
    "yes, I must be"
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   167
    ^ true
1ef1d1395146 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   168
65
441a440ab59d Initial revision
claus
parents:
diff changeset
   169
! !
441a440ab59d Initial revision
claus
parents:
diff changeset
   170
97
claus
parents: 84
diff changeset
   171
!ImmutableArray methodsFor:'specials'!
claus
parents: 84
diff changeset
   172
claus
parents: 84
diff changeset
   173
become:anotherObject
100
claus
parents: 98
diff changeset
   174
    "trigger an error if I should become something else
claus
parents: 98
diff changeset
   175
     (this would be an even more tricky manipulation)"
claus
parents: 98
diff changeset
   176
97
claus
parents: 84
diff changeset
   177
    self notifyStoreError.
claus
parents: 84
diff changeset
   178
    ^ super become:anotherObject
claus
parents: 84
diff changeset
   179
!
claus
parents: 84
diff changeset
   180
claus
parents: 84
diff changeset
   181
becomeNil
100
claus
parents: 98
diff changeset
   182
    "trigger an error if I should become nil
claus
parents: 98
diff changeset
   183
     (this would be an even more tricky manipulation)"
claus
parents: 98
diff changeset
   184
97
claus
parents: 84
diff changeset
   185
    self notifyStoreError.
claus
parents: 84
diff changeset
   186
    ^ super becomeNil
claus
parents: 84
diff changeset
   187
! !
claus
parents: 84
diff changeset
   188
539
69a1cd05c7d6 added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 292
diff changeset
   189
!ImmutableArray class methodsFor:'documentation'!
148
ef0e604209ec version method at the end
Claus Gittinger <cg@exept.de>
parents: 140
diff changeset
   190
ef0e604209ec version method at the end
Claus Gittinger <cg@exept.de>
parents: 140
diff changeset
   191
version
539
69a1cd05c7d6 added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 292
diff changeset
   192
    ^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmArray.st,v 1.16 1997-06-19 14:57:04 cg Exp $'
148
ef0e604209ec version method at the end
Claus Gittinger <cg@exept.de>
parents: 140
diff changeset
   193
! !