Skip to content

Commit

Permalink
Since Pharo9. literals are immutable.
Browse files Browse the repository at this point in the history
But we did not recurse into the Litearl Arrays.

This PR:
- add #beReadOnlyLiteral and test
- use it in the compiler
  • Loading branch information
MarcusDenker committed Aug 24, 2021
1 parent bee0f2d commit 4b7b4cb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Collections-Sequenceable-Tests/ArrayTest.class.st
Expand Up @@ -602,6 +602,13 @@ ArrayTest >> testAtWrap2 [
self assert: (tabTest atWrap: -2) equals: 5
]

{ #category : #tests }
ArrayTest >> testBeReadOnlyLiteral [
self assert: (Array new) beReadOnlyLiteral isReadOnlyObject.
self assert: (Array with: (String new)) beReadOnlyLiteral first isReadOnlyObject.
self deny: (Array with: (Object new)) first isReadOnlyObject
]

{ #category : #tests }
ArrayTest >> testCombinations [
self assert: #(1 2 3) combinations equals: #(#(1) #(2) #(3) #(1 2) #(1 3) #(2 3) #(1 2 3)).
Expand Down
7 changes: 7 additions & 0 deletions src/Collections-Sequenceable/Array.class.st
Expand Up @@ -168,6 +168,13 @@ Array >> atWrap: index put: anObject [
^ self at: index - 1 \\ self size + 1 put: anObject
]

{ #category : #'write barrier' }
Array >> beReadOnlyLiteral [
self isLiteral ifFalse: [ ^ self ].
self beReadOnlyObject.
self do: [ :each | each beReadOnlyLiteral ]
]

{ #category : #copying }
Array >> copyWithDependent: newElement [
self size = 0 ifTrue:[^DependentsArray with: newElement].
Expand Down
6 changes: 6 additions & 0 deletions src/Kernel-Tests/ObjectTest.class.st
Expand Up @@ -27,6 +27,12 @@ ObjectTest >> testAs [
self assert: coll1 identicalTo: coll2
]

{ #category : #'tests - write barrier' }
ObjectTest >> testBeReadOnlyLiteral [
self assert: String new beReadOnlyLiteral isReadOnlyObject.
self deny: Object new beReadOnlyLiteral isReadOnlyObject
]

{ #category : #'tests - write barrier' }
ObjectTest >> testBeRecursivelyReadOnlyObject [

Expand Down
7 changes: 7 additions & 0 deletions src/Kernel/Object.class.st
Expand Up @@ -459,6 +459,13 @@ Object >> basicSize [
^0
]

{ #category : #'write barrier' }
Object >> beReadOnlyLiteral [
"This is used by the compiler, it recurses into Arrays (see the override there)"
self isLiteral ifFalse: [ ^ self ].
self beReadOnlyObject
]

{ #category : #'write barrier' }
Object >> beReadOnlyObject [
"If the VM supports read-only objects it will not write to read-only objects.
Expand Down
7 changes: 1 addition & 6 deletions src/OpalCompiler-Core/IRTranslator.class.st
Expand Up @@ -248,12 +248,7 @@ IRTranslator >> visitPushInstVar: instVar [

{ #category : #visiting }
IRTranslator >> visitPushLiteral: lit [
| literal |
literal := lit literal.
"all objects that are not literals we keep writable"
literal isLiteral ifTrue: [ literal beReadOnlyObject ].
^ gen pushLiteral: literal

^ gen pushLiteral: lit literal beReadOnlyLiteral
]

{ #category : #visiting }
Expand Down

0 comments on commit 4b7b4cb

Please sign in to comment.