Initial revision
authorclaus
Mon, 20 Feb 1995 23:56:52 +0100
changeset 274 0c35c797ac6c
parent 273 f97f3fcf8214
child 275 a76029ddaa98
Initial revision
ImmutableArray.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ImmutableArray.st	Mon Feb 20 23:56:52 1995 +0100
@@ -0,0 +1,148 @@
+"
+ COPYRIGHT (c) 1995 by Claus Gittinger
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+'From Smalltalk/X, Version:2.10.4 on 20-feb-1995 at 6:26:23 am'!
+
+Array subclass:#ImmutableArray
+	 instanceVariableNames:''
+	 classVariableNames:''
+	 poolDictionaries:''
+	 category:'Compiler-Support'
+!
+
+!ImmutableArray class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1995 by Claus Gittinger
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+!
+
+version
+"
+$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.1 1995-02-20 22:56:52 claus Exp $
+"
+!
+
+documentation
+"
+    By default, array literals in smalltalk are mutable objects. That
+    may lead to some subtle (and hard to find errors) if some method passes
+    a literal array constant as argument to someone else, who changes the
+    array using at:put: like messages. Since the array object is kept in 
+    the first methods literals, the array constant has now been changed without
+    having the methods sourcecode reflect this. Thus, method the methods will
+    behave differently from what its source may make you think.
+
+    To help finding this kind of 'feature/bug', the compiler class can be
+    configured to create instances of this ImmutableArray instead of Arrays
+    for array literals. Instances of ImmutableArray catch storing accesses and
+    enter the debugger. Although useful, this feature is disabled by default
+    for compatibility to other smalltalk implementations. 
+    (Also, if turned on, this makes inspecting array literals entered in
+     a workspace somewhat strange: you cannot modify it any longer).
+
+    Turn the ImmutableArray feature on by setting the Parsers class variable
+    'ArraysAreImmutable' to true.
+"
+! !
+
+!ImmutableArray methodsFor:'error handling'!
+
+creator 
+    "find the method that contains me"
+
+    Method allDerivedInstances do:[:aMethod |
+	|lits|
+
+	lits := aMethod literals.
+	(lits notNil and:[(lits identityIndexOf:self) ~~ 0]) ifTrue:[
+	    ^ aMethod
+	]
+    ].
+    ^ nil
+!
+
+notifyStoreError
+    "a store is attempted - for our convenience, find the method that
+     contains me, for a nicer error message"
+
+    |creator msg|
+
+    creator := self creator.
+    msg := 'store into literal'.
+    creator notNil ifTrue:[
+	msg := msg , ' (' , creator whoString , ')'
+    ].
+    "
+     this error is reported on an attempt to store into a literal
+     array. The literal was created in creator.
+     If you press continue in the debugger, the store will be performed.
+     If you dont want this, press abort and check your code.
+     Storing into literals is VERY VERY bad coding style.
+    "
+    self error:msg
+! !
+
+!ImmutableArray methodsFor:'accessing'!
+
+at: index put: value
+    "Trigger an error if an immutable array is stored into.
+     The store will be performed (for compatibility reasons) if you continue
+     in the debugger."
+
+    self notifyStoreError.
+    ^ super at: index put: value
+!
+
+basicAt: index put: value
+    "Trigger an error if an immutable array is stored into.
+     The store will be performed (for compatibility reasons) if you continue
+     in the debugger."
+
+    self notifyStoreError.
+    ^ super basicAt: index put: value
+! !
+
+!ImmutableArray methodsFor:'private'!
+
+species
+    "Copies should be mutable"
+
+    ^Array
+! !
+
+!ImmutableArray methodsFor:'queries'!
+
+isLiteral
+    "return true, if the receiver can be used as a literal
+     (i.e. can be used in constant arrays)"
+
+    "yes, I must be"
+    ^ true
+
+! !
+
+!ImmutableArray methodsFor:'copying'!
+
+postCopy
+    self changeClassTo:Array.
+! !
+