-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add framework and tests for compilation/code creation inside a sandbox
Open issues: * #testClassComment fails because accesses to the FilePlugin are not allowed - we could either create an exception for the sources file or process these requests outside of the sanfbox. * #testSubclass crashes the VM because of something we are doing wrong/differently than normal Trunk during the behavior creation. See: http://lists.squeakfoundation.org/pipermail/squeak-dev/2021-December/217860.html
- Loading branch information
Showing
18 changed files
with
146 additions
and
18 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...nStudio-Sandbox.package/ClassDescription.extension/class/dontLogCompiledSourcesDuring..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*SimulationStudio-Sandbox-compiling-pseudo override | ||
dontLogCompiledSourcesDuring: aBlock | ||
|
||
| previous | | ||
previous := self logCompiledSources. | ||
self logCompiledSources: false. | ||
^ aBlock ensure: [self logCompiledSources: previous] |
5 changes: 5 additions & 0 deletions
5
packages/SimulationStudio-Sandbox.package/ClassDescription.extension/methodProperties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"class" : { | ||
"dontLogCompiledSourcesDuring:" : "ct 12/28/2021 22:54" }, | ||
"instance" : { | ||
} } |
2 changes: 2 additions & 0 deletions
2
packages/SimulationStudio-Sandbox.package/ClassDescription.extension/properties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
"name" : "ClassDescription" } |
5 changes: 5 additions & 0 deletions
5
...tudio-Sandbox.package/Sandbox.class/class/disableLoggingAndNotifyingDuringCompilation..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
support | ||
disableLoggingAndNotifyingDuringCompilation: aBlock | ||
|
||
^ SystemChangeNotifier uniqueInstance doSilently: | ||
[ClassDescription dontLogCompiledSourcesDuring: aBlock] |
6 changes: 1 addition & 5 deletions
6
packages/SimulationStudio-Sandbox.package/Sandbox.class/instance/basicEvaluate..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
evaluating | ||
basicEvaluate: aBlock | ||
"Evaluate aBlock in a the receiver, isolating it from the rest of the image, and answer the result as it is seen from the global perspective. | ||
PRIVATE! Does not care about any exceptions that are signaled during the simulation, causing them to be handled still inside the sandbox, even if an exception handler has been defined outside of the sandbox stack. Depending on the configuration of the sandbox and your image, this can mean that even the eventual pop-up of the debugger will be simulated and thus invisible to you! Usually, it is a better idea to use the public #evaluate: protocol instead." | ||
|
||
^ (SandboxContext newFrom: thisContext) | ||
sandbox: self; | ||
runSimulated: aBlock contextAtEachStep: [] | ||
^ self basicEvaluate: aBlock contextAtEachStep: [] |
8 changes: 8 additions & 0 deletions
8
...mulationStudio-Sandbox.package/Sandbox.class/instance/basicEvaluate.contextAtEachStep..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
evaluating | ||
basicEvaluate: aBlock contextAtEachStep: anotherBlock | ||
"Evaluate aBlock in a the receiver, isolating it from the rest of the image. Evaluate anotherBlock with the current context prior to each instruction executed. Answer the result as it is seen from the global perspective. | ||
PRIVATE! Does not care about any exceptions that are signaled during the simulation, causing them to be handled still inside the sandbox, even if an exception handler has been defined outside of the sandbox stack. Depending on the configuration of the sandbox and your image, this can mean that even the eventual pop-up of the debugger will be simulated and thus invisible to you! Usually, it is a better idea to use the public #evaluate: protocol instead." | ||
|
||
^ (SandboxContext newFrom: thisContext) | ||
sandbox: self; | ||
runSimulated: aBlock contextAtEachStep: anotherBlock |
13 changes: 13 additions & 0 deletions
13
...tionStudio-Sandbox.package/Sandbox.class/instance/evaluate.contextAtEachStep.ifFailed..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
evaluating | ||
evaluate: aBlock contextAtEachStep: contextBlock ifFailed: failBlock | ||
"Evaluate aBlock in a the receiver and answer the result, isolating it from the rest of the image. Evaluate anotherBlock with the current context prior to each instruction executed. If aBlock signals a failure, evaluate failBlock with that failure. | ||
Example: | ||
Sandbox evaluate: [1 / 0] ifFailed: [:ex | Transcript showln: ex description]. | ||
" | ||
|
||
^ self | ||
evaluate: aBlock | ||
contextAtEachStep: contextBlock | ||
on: Error, Warning, Halt | ||
do: failBlock |
27 changes: 27 additions & 0 deletions
27
...ulationStudio-Sandbox.package/Sandbox.class/instance/evaluate.contextAtEachStep.on.do..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
evaluating | ||
evaluate: aBlock contextAtEachStep: contextBlock on: exceptionHandler do: exceptionBlock | ||
"Evaluate aBlock in the receiver, isolating it from the rest of the image. If aBlock signals an exception that can handled by the exceptionHandler, evaluate exceptionBlock with it. Evaluate contextBlock with the current context prior to each instruction executed. Answer the simulated value of aBlock. | ||
NOTE: Unhandled errors raised by aBlock will bubble up along the sender stack, but still, all handling is simulated in the sandbox until the exception will have been resumed. Depending on the configuration of the sandbox and your image, this can mean that even the pop-up of the eventual debugger will be simulated and thus invisible to you! Thus make sure to pass all relevant exceptions with this message. | ||
Example: | ||
Sandbox evaluate: [1 / 0] on: ZeroDivide do: [:ex | Transcript showln: ex description]. | ||
Sandbox new | ||
evaluate: [2 / 3] | ||
contextAtEachStep: [:ctx | Transcript showln: {ctx. ctx pc}] | ||
on: ZeroDivide | ||
do: [:ex | Transcript showln: ex description] . | ||
""Use with CAUTION and check your image via the ProcessBrowser afterwards"" | ||
Sandbox evaluate: [self halt] on: ZeroDivide do: [:ex | Transcript showln: ex description]. | ||
" | ||
|
||
| result exception | | ||
self | ||
basicEvaluate: [[result := aBlock value] | ||
on: exceptionHandler | ||
do: [:ex | exception := ex]] | ||
contextAtEachStep: contextBlock. | ||
|
||
(self basicEvaluate: [exception]) ifNotNil: [:ex | ^ exceptionBlock cull: ex]. | ||
|
||
^ self readableObjectFor: (self basicEvaluate: [result]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...udio-Sandbox.package/Sandbox2.class/class/disableLoggingAndNotifyingDuringCompilation..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
support | ||
disableLoggingAndNotifyingDuringCompilation: aBlock | ||
|
||
^ SystemChangeNotifier uniqueInstance doSilently: | ||
[ClassDescription dontLogCompiledSourcesDuring: aBlock] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/SimulationStudio-Tests-Sandbox.package/SandboxTest.class/instance/someToken.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
accessing | ||
someToken | ||
|
||
^ UUID nilUUID |
6 changes: 6 additions & 0 deletions
6
...ges/SimulationStudio-Tests-Sandbox.package/SandboxTest.class/instance/testClassComment.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
tests | ||
testClassComment | ||
|
||
| comment | | ||
comment := self sandboxClass evaluate: [Object comment]. | ||
self assert: (comment includesSubstring: 'root'). |
14 changes: 14 additions & 0 deletions
14
packages/SimulationStudio-Tests-Sandbox.package/SandboxTest.class/instance/testCompile.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
tests | ||
testCompile | ||
|
||
| result token | | ||
self assert: UUID nilUUID equals: self someToken. | ||
|
||
token := UUID new asString. | ||
result := self sandboxClass disableLoggingAndNotifyingDuringCompilation: | ||
[self sandboxClass evaluate: [ | ||
self class compile: 'someToken ^' , token printString. | ||
self someToken]]. | ||
|
||
self assert: token equals: result. | ||
self assert: UUID nilUUID equals: self someToken. |
30 changes: 30 additions & 0 deletions
30
packages/SimulationStudio-Tests-Sandbox.package/SandboxTest.class/instance/testSubclass.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
tests | ||
testSubclass | ||
|
||
| name | | ||
self fail: #forLater. "Currently, simulation of subclassing will crash the VM. See: http://lists.squeakfoundation.org/pipermail/squeak-dev/2021-December/217860.html" | ||
|
||
name := #SandboxTestClass , UUID new asString asLegalSelector asCamelCase. | ||
|
||
self shouldnt: (Smalltalk hasClassNamed: name). | ||
|
||
self sandboxClass disableLoggingAndNotifyingDuringCompilation: | ||
[self sandboxClass evaluate: [ | class instance | | ||
class := Object | ||
subclass: name asSymbol | ||
instanceVariableNames: 'griffle plonk' | ||
classVariableNames: '' | ||
poolDictionaries: '' | ||
category: 'SimulationStudio-Tests-UserObjects'. | ||
instance := class new. | ||
|
||
self | ||
assert: (instance isKindOf: class); | ||
assert: (instance class == class); | ||
assert: (thisContext objectClass: instance) == class. | ||
|
||
class compile: 'foo ^ ' , self selector printString. | ||
|
||
self assert: self selector printString equals: instance foo]]. | ||
|
||
self deny: (Smalltalk hasClassNamed: name). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters