Browse Source

Refactor: Put responsibility where it's due.

Herby Vojčík 4 years ago
parent
commit
a512c9d830

+ 1 - 1
lang/API-CHANGES.txt

@@ -21,8 +21,8 @@
 + SequenceableCollection >>
   + copyWithFirst:
 + SmalltalkImage >>
-  + do:on:do:
   + isError:
+  + try:ifTrue:catch:
 + amber/boot api >>
   + detachClass(klass)
 + amber/helpers exports >>

+ 44 - 41
lang/src/Kernel-Infrastructure.js

@@ -3243,47 +3243,6 @@ return self;
 }; }),
 $globals.SmalltalkImage);
 
-$core.addMethod(
-$core.method({
-selector: "do:on:do:",
-protocol: "error handling",
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["actionBlock", "anErrorClass", "aBlock"],
-source: "do: actionBlock on: anErrorClass do: aBlock\x0a\x09\x22All exceptions thrown in the Smalltalk stack are cought.\x0a\x09Convert all JS exceptions to JavaScriptException instances.\x22\x0a\x09\x0a\x09| smalltalkError |\x0a\x09^ actionBlock\x0a\x09\x09tryIfTrue: [ :error |\x0a\x09\x09\x09smalltalkError := self asSmalltalkException: error.\x0a\x09\x09\x09smalltalkError isKindOf: anErrorClass ]\x0a\x09\x09catch: [ aBlock value: smalltalkError ]",
-referencedClasses: [],
-//>>excludeEnd("ide");
-pragmas: [],
-messageSends: ["tryIfTrue:catch:", "asSmalltalkException:", "isKindOf:", "value:"]
-}, function ($methodClass){ return function (actionBlock,anErrorClass,aBlock){
-var self=this,$self=this;
-var smalltalkError;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-return $recv(actionBlock)._tryIfTrue_catch_((function(error){
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx2) {
-//>>excludeEnd("ctx");
-smalltalkError=$self._asSmalltalkException_(error);
-return $recv(smalltalkError)._isKindOf_(anErrorClass);
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx2) {$ctx2.fillBlock({error:error},$ctx1,1)});
-//>>excludeEnd("ctx");
-}),(function(){
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx2) {
-//>>excludeEnd("ctx");
-return $recv(aBlock)._value_(smalltalkError);
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
-//>>excludeEnd("ctx");
-}));
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"do:on:do:",{actionBlock:actionBlock,anErrorClass:anErrorClass,aBlock:aBlock,smalltalkError:smalltalkError})});
-//>>excludeEnd("ctx");
-}; }),
-$globals.SmalltalkImage);
-
 $core.addMethod(
 $core.method({
 selector: "existsJsGlobal:",
@@ -3924,6 +3883,50 @@ return self;
 }; }),
 $globals.SmalltalkImage);
 
+$core.addMethod(
+$core.method({
+selector: "try:ifTrue:catch:",
+protocol: "error handling",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["actionBlock", "aBlock", "anotherBlock"],
+source: "try: actionBlock ifTrue: aBlock catch: anotherBlock\x0a\x09\x22Similar to BlockClosure >> tryifTrue:catch:, but\x0a\x09converts all JS exceptions to JavaScriptException instances.\x22\x0a\x09\x0a\x09| smalltalkError |\x0a\x09^ actionBlock\x0a\x09\x09tryIfTrue: [ :error |\x0a\x09\x09\x09smalltalkError := self asSmalltalkException: error.\x0a\x09\x09\x09aBlock value: smalltalkError ]\x0a\x09\x09catch: [ anotherBlock value: smalltalkError ]",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["tryIfTrue:catch:", "asSmalltalkException:", "value:"]
+}, function ($methodClass){ return function (actionBlock,aBlock,anotherBlock){
+var self=this,$self=this;
+var smalltalkError;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return $recv(actionBlock)._tryIfTrue_catch_((function(error){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+smalltalkError=$self._asSmalltalkException_(error);
+return $recv(aBlock)._value_(smalltalkError);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["value:"]=1;
+//>>excludeEnd("ctx");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({error:error},$ctx1,1)});
+//>>excludeEnd("ctx");
+}),(function(){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $recv(anotherBlock)._value_(smalltalkError);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
+//>>excludeEnd("ctx");
+}));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"try:ifTrue:catch:",{actionBlock:actionBlock,aBlock:aBlock,anotherBlock:anotherBlock,smalltalkError:smalltalkError})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.SmalltalkImage);
+
 $core.addMethod(
 $core.method({
 selector: "tryAdoptPackageDescriptorsBeyond:",

+ 5 - 5
lang/src/Kernel-Infrastructure.st

@@ -916,16 +916,16 @@ asSmalltalkException: anObject
 		ifFalse: [ JavaScriptException on: anObject ]
 !
 
-do: actionBlock on: anErrorClass do: aBlock
-	"All exceptions thrown in the Smalltalk stack are cought.
-	Convert all JS exceptions to JavaScriptException instances."
+try: actionBlock ifTrue: aBlock catch: anotherBlock
+	"Similar to BlockClosure >> tryifTrue:catch:, but
+	converts all JS exceptions to JavaScriptException instances."
 	
 	| smalltalkError |
 	^ actionBlock
 		tryIfTrue: [ :error |
 			smalltalkError := self asSmalltalkException: error.
-			smalltalkError isKindOf: anErrorClass ]
-		catch: [ aBlock value: smalltalkError ]
+			aBlock value: smalltalkError ]
+		catch: [ anotherBlock value: smalltalkError ]
 ! !
 
 !SmalltalkImage methodsFor: 'globals'!

+ 11 - 3
lang/src/Kernel-Methods.js

@@ -345,17 +345,25 @@ selector: "on:do:",
 protocol: "error handling",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["anErrorClass", "aBlock"],
-source: "on: anErrorClass do: aBlock\x0a\x09^ Smalltalk do: self on: anErrorClass do: aBlock",
+source: "on: anErrorClass do: aBlock\x0a\x09^ Smalltalk try: self ifTrue: [ :err | err isKindOf: anErrorClass ] catch: aBlock",
 referencedClasses: ["Smalltalk"],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["do:on:do:"]
+messageSends: ["try:ifTrue:catch:", "isKindOf:"]
 }, function ($methodClass){ return function (anErrorClass,aBlock){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return $recv($globals.Smalltalk)._do_on_do_(self,anErrorClass,aBlock);
+return $recv($globals.Smalltalk)._try_ifTrue_catch_(self,(function(err){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $recv(err)._isKindOf_(anErrorClass);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({err:err},$ctx1,1)});
+//>>excludeEnd("ctx");
+}),aBlock);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"on:do:",{anErrorClass:anErrorClass,aBlock:aBlock})});
 //>>excludeEnd("ctx");

+ 1 - 1
lang/src/Kernel-Methods.st

@@ -103,7 +103,7 @@ provided
 !BlockClosure methodsFor: 'error handling'!
 
 on: anErrorClass do: aBlock
-	^ Smalltalk do: self on: anErrorClass do: aBlock
+	^ Smalltalk try: self ifTrue: [ :err | err isKindOf: anErrorClass ] catch: aBlock
 !
 
 tryCatch: aBlock

+ 4 - 4
lang/src/Platform-Services.js

@@ -735,17 +735,17 @@ selector: "evaluate:on:do:",
 protocol: "error handling",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aBlock", "anErrorClass", "exceptionBlock"],
-source: "evaluate: aBlock on: anErrorClass do: exceptionBlock\x0a\x09\x22Evaluate a block and catch exceptions happening on the environment stack\x22\x0a\x09\x0a\x09^ Smalltalk do: aBlock on: (self classNamed: anErrorClass name) do: exceptionBlock",
-referencedClasses: ["Smalltalk"],
+source: "evaluate: aBlock on: anErrorClass do: exceptionBlock\x0a\x09\x22Evaluate a block and catch exceptions happening on the environment stack\x22\x0a\x09\x0a\x09^ aBlock on: (self classNamed: anErrorClass name) do: exceptionBlock",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["do:on:do:", "classNamed:", "name"]
+messageSends: ["on:do:", "classNamed:", "name"]
 }, function ($methodClass){ return function (aBlock,anErrorClass,exceptionBlock){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return $recv($globals.Smalltalk)._do_on_do_(aBlock,$self._classNamed_($recv(anErrorClass)._name()),exceptionBlock);
+return $recv(aBlock)._on_do_($self._classNamed_($recv(anErrorClass)._name()),exceptionBlock);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"evaluate:on:do:",{aBlock:aBlock,anErrorClass:anErrorClass,exceptionBlock:exceptionBlock})});
 //>>excludeEnd("ctx");

+ 1 - 1
lang/src/Platform-Services.st

@@ -257,7 +257,7 @@ compileMethod: sourceCode for: class protocol: protocol
 evaluate: aBlock on: anErrorClass do: exceptionBlock
 	"Evaluate a block and catch exceptions happening on the environment stack"
 	
-	^ Smalltalk do: aBlock on: (self classNamed: anErrorClass name) do: exceptionBlock
+	^ aBlock on: (self classNamed: anErrorClass name) do: exceptionBlock
 ! !
 
 !Environment methodsFor: 'evaluating'!