Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulty code: make undeclared write resumable with the right value #13297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Kernel-Tests-Extended/CompiledMethodTest.class.st
Expand Up @@ -1003,6 +1003,38 @@ CompiledMethodTest >> testWritesSlot [
self assert: ((Point>>#setX:setY:) writesSlot: (Point slotNamed: #x))
]

{ #category : #'tests - testing' }
CompiledMethodTest >> testWritesUndeclared [
| method |

"x is an ivar"
method := self class compiler compile: 'x ^ x := 0'.
self deny: method usesUndeclareds.
self assert: (self executeMethod: method) equals: 0.

"undeclaredxyz is not declared"
method := self class compiler permitFaulty: true; compile: 'z ^ undeclaredxyz := 1'.
self assert: method usesUndeclareds.
self should: [ self executeMethod: method ] raise: UndeclaredVariableWrite.
self
assert:
([ self executeMethod: method ]
on: UndeclaredVariableWrite
do: [:e | e resume])
equals: 1.

"check same behavior in blocks"
method := self class compiler permitFaulty: true; compile: 'msg ^ self in: [ :anObject | undeclaredxyz := 2 ]'.
self assert: method usesUndeclareds.
self should: [ self executeMethod: method ] raise: UndeclaredVariableWrite.
self
assert:
([ self executeMethod: method ]
on: UndeclaredVariableWrite
do: [:e | e resume])
equals: 2
]

{ #category : #examples }
CompiledMethodTest >> writeX [

Expand Down
3 changes: 2 additions & 1 deletion src/Kernel/UndeclaredVariable.class.st
Expand Up @@ -83,9 +83,10 @@ UndeclaredVariable >> register [
UndeclaredVariable >> runtimeUndeclaredWrite: anObject [

<debuggerCompleteToSender>
UndeclaredVariableWrite new
^ UndeclaredVariableWrite new
messageText: 'Attempt to write to undeclared variable ' , self name;
variable: self;
value: anObject;
signal
]

Expand Down
28 changes: 27 additions & 1 deletion src/Kernel/UndeclaredVariableWrite.class.st
Expand Up @@ -5,11 +5,37 @@ Class {
#name : #UndeclaredVariableWrite,
#superclass : #Error,
#instVars : [
'variable'
'variable',
'value'
],
#category : #'Kernel-Exceptions'
}

{ #category : #testing }
UndeclaredVariableWrite >> defaultResumeValue [

^ value
]

{ #category : #testing }
UndeclaredVariableWrite >> isResumable [
"The undeclared variable write become a no-op"

^ true
]

{ #category : #accessing }
UndeclaredVariableWrite >> value [

^ value
]

{ #category : #accessing }
UndeclaredVariableWrite >> value: anObject [

value := anObject
]

{ #category : #accessing }
UndeclaredVariableWrite >> variable [

Expand Down