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