Browse Source

- BlockClosure >> ensure
- Fix REPL with Smalltalk errors

Nicolas Petton 12 years ago
parent
commit
635120b7c8
6 changed files with 111 additions and 6 deletions
  1. 24 0
      js/Kernel.deploy.js
  2. 34 0
      js/Kernel.js
  3. 0 2
      repl/REPL.js
  4. 3 2
      repl/REPL.st
  5. 34 2
      repl/amber.js
  6. 16 0
      st/Kernel.st

+ 24 - 0
js/Kernel.deploy.js

@@ -2262,6 +2262,19 @@ return self;}
 }),
 smalltalk.BlockClosure);
 
+smalltalk.addMethod(
+'_ensure_',
+smalltalk.method({
+selector: 'ensure:',
+fn: function (aBlock){
+var self=this;
+var success=nil;
+success=false;
+smalltalk.send((function(){smalltalk.send(self, "_value", []);success=true;return smalltalk.send(aBlock, "_value", []);}), "_on_do_", [(smalltalk.Error || Error), (function(ex){((($receiver = success).klass === smalltalk.Boolean) ? (! $receiver ? (function(){return smalltalk.send(aBlock, "_value", []);})() : nil) : smalltalk.send($receiver, "_ifFalse_", [(function(){return smalltalk.send(aBlock, "_value", []);})]));return smalltalk.send(ex, "_signal", []);})]);
+return self;}
+}),
+smalltalk.BlockClosure);
+
 
 
 smalltalk.addClass('Boolean', smalltalk.Object, [], 'Kernel');
@@ -4719,6 +4732,17 @@ return self;}
 }),
 smalltalk.Error);
 
+smalltalk.addMethod(
+'_isSmalltalkError',
+smalltalk.method({
+selector: 'isSmalltalkError',
+fn: function (){
+var self=this;
+return self.smalltalkError === true;
+return self;}
+}),
+smalltalk.Error);
+
 
 smalltalk.addMethod(
 '_signal_',

+ 34 - 0
js/Kernel.js

@@ -3228,6 +3228,24 @@ referencedClasses: []
 }),
 smalltalk.BlockClosure);
 
+smalltalk.addMethod(
+unescape('_ensure_'),
+smalltalk.method({
+selector: unescape('ensure%3A'),
+category: 'evaluating',
+fn: function (aBlock){
+var self=this;
+var success=nil;
+success=false;
+smalltalk.send((function(){smalltalk.send(self, "_value", []);success=true;return smalltalk.send(aBlock, "_value", []);}), "_on_do_", [(smalltalk.Error || Error), (function(ex){((($receiver = success).klass === smalltalk.Boolean) ? (! $receiver ? (function(){return smalltalk.send(aBlock, "_value", []);})() : nil) : smalltalk.send($receiver, "_ifFalse_", [(function(){return smalltalk.send(aBlock, "_value", []);})]));return smalltalk.send(ex, "_signal", []);})]);
+return self;},
+args: ["aBlock"],
+source: unescape('ensure%3A%20aBlock%0A%09%7C%20success%20%7C%0A%09success%20%3A%3D%20false.%0A%09%5Bself%20value.%20success%20%3A%3D%20true.%20aBlock%20value%5D%0A%09%09on%3A%20Error%0A%09%09do%3A%20%5B%3Aex%20%7C%0A%09%09%09success%20ifFalse%3A%20%5BaBlock%20value%5D.%0A%09%09%09ex%20signal%5D'),
+messageSends: ["on:do:", "value", "ifFalse:", "signal"],
+referencedClasses: ["Error"]
+}),
+smalltalk.BlockClosure);
+
 
 
 smalltalk.addClass('Boolean', smalltalk.Object, [], 'Kernel');
@@ -6726,6 +6744,22 @@ referencedClasses: []
 }),
 smalltalk.Error);
 
+smalltalk.addMethod(
+unescape('_isSmalltalkError'),
+smalltalk.method({
+selector: unescape('isSmalltalkError'),
+category: 'testing',
+fn: function (){
+var self=this;
+return self.smalltalkError === true;
+return self;},
+args: [],
+source: unescape('isSmalltalkError%0A%09%3Creturn%20self.smalltalkError%20%3D%3D%3D%20true%3E'),
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Error);
+
 
 smalltalk.addMethod(
 unescape('_signal_'),

File diff suppressed because it is too large
+ 0 - 2
repl/REPL.js


+ 3 - 2
repl/REPL.st

@@ -34,8 +34,9 @@ eval: buffer
 			result := Compiler new loadExpression: buffer.
 			Transcript show: result]
 		catch: [:e |
-			process stdout write: e jsStack]].
-	self setPrompt.
+			e isSmalltalkError
+			    ifTrue: [ErrorHandler new handleError: e]
+			    ifFalse: [process stdout write: e jsStack]]].
 	interface prompt
 ! !
 

File diff suppressed because it is too large
+ 34 - 2
repl/amber.js


+ 16 - 0
st/Kernel.st

@@ -1122,6 +1122,16 @@ timeToRun
 	"Answer the number of milliseconds taken to execute this block."
 
 	^ Date millisecondsToRun: self
+!
+
+ensure: aBlock
+	| success |
+	success := false.
+	[self value. success := true. aBlock value]
+		on: Error
+		do: [:ex |
+			success ifFalse: [aBlock value].
+			ex signal]
 ! !
 
 !BlockClosure methodsFor: 'timeout/interval'!
@@ -2330,6 +2340,12 @@ signal
 	<self.context = smalltalk.getThisContext(); self.smalltalkError = true; throw(self)>
 ! !
 
+!Error methodsFor: 'testing'!
+
+isSmalltalkError
+	<return self.smalltalkError === true>
+! !
+
 !Error class methodsFor: 'instance creation'!
 
 signal: aString

Some files were not shown because too many files changed in this diff