Browse Source

Merge pull request #287 from herby/gh-286

Wrapping result of set{Timeout,Interval}.
Nicolas Petton 11 years ago
parent
commit
ca71181d96

+ 16 - 8
js/Kernel-Methods.deploy.js

@@ -185,10 +185,14 @@ smalltalk.addMethod(
 "_valueWithInterval_",
 smalltalk.method({
 selector: "valueWithInterval:",
-fn: function (aNumber) {
-    var self = this;
-    return setInterval(self, aNumber);
-    return self;
+fn: function (aNumber){
+var self=this;
+var $1;
+var local;
+local = setInterval(self, aNumber);
+;
+$1=smalltalk.send((smalltalk.Timeout || Timeout),"_on_",[local]);
+return $1;
 }
 }),
 smalltalk.BlockClosure);
@@ -209,10 +213,14 @@ smalltalk.addMethod(
 "_valueWithTimeout_",
 smalltalk.method({
 selector: "valueWithTimeout:",
-fn: function (aNumber) {
-    var self = this;
-    return setTimeout(self, aNumber);
-    return self;
+fn: function (aNumber){
+var self=this;
+var $1;
+var local;
+local = setTimeout(self, aNumber);
+;
+$1=smalltalk.send((smalltalk.Timeout || Timeout),"_on_",[local]);
+return $1;
 }
 }),
 smalltalk.BlockClosure);

+ 22 - 14
js/Kernel-Methods.js

@@ -262,15 +262,19 @@ smalltalk.addMethod(
 smalltalk.method({
 selector: "valueWithInterval:",
 category: 'timeout/interval',
-fn: function (aNumber) {
-    var self = this;
-    return setInterval(self, aNumber);
-    return self;
+fn: function (aNumber){
+var self=this;
+var $1;
+var local;
+local = setInterval(self, aNumber);
+;
+$1=smalltalk.send((smalltalk.Timeout || Timeout),"_on_",[local]);
+return $1;
 },
 args: ["aNumber"],
-source: "valueWithInterval: aNumber\x0a\x09<return setInterval(self, aNumber)>",
-messageSends: [],
-referencedClasses: []
+source: "valueWithInterval: aNumber\x0a\x09| local |\x0a\x09<local = setInterval(self, aNumber)>.\x0a    ^ Timeout on: local",
+messageSends: ["on:"],
+referencedClasses: ["Timeout"]
 }),
 smalltalk.BlockClosure);
 
@@ -296,15 +300,19 @@ smalltalk.addMethod(
 smalltalk.method({
 selector: "valueWithTimeout:",
 category: 'timeout/interval',
-fn: function (aNumber) {
-    var self = this;
-    return setTimeout(self, aNumber);
-    return self;
+fn: function (aNumber){
+var self=this;
+var $1;
+var local;
+local = setTimeout(self, aNumber);
+;
+$1=smalltalk.send((smalltalk.Timeout || Timeout),"_on_",[local]);
+return $1;
 },
 args: ["aNumber"],
-source: "valueWithTimeout: aNumber\x0a\x09<return setTimeout(self, aNumber)>",
-messageSends: [],
-referencedClasses: []
+source: "valueWithTimeout: aNumber\x0a\x09| local |\x0a\x09<local = setTimeout(self, aNumber)>.\x0a    ^ Timeout on: local",
+messageSends: ["on:"],
+referencedClasses: ["Timeout"]
 }),
 smalltalk.BlockClosure);
 

+ 58 - 24
js/Kernel-Objects.deploy.js

@@ -1785,30 +1785,6 @@ fn: function () {
 }),
 smalltalk.Number);
 
-smalltalk.addMethod(
-"_clearInterval",
-smalltalk.method({
-selector: "clearInterval",
-fn: function () {
-    var self = this;
-    clearInterval(Number(self));
-    return self;
-}
-}),
-smalltalk.Number);
-
-smalltalk.addMethod(
-"_clearTimeout",
-smalltalk.method({
-selector: "clearTimeout",
-fn: function () {
-    var self = this;
-    clearTimeout(Number(self));
-    return self;
-}
-}),
-smalltalk.Number);
-
 smalltalk.addMethod(
 "_copy",
 smalltalk.method({
@@ -3184,6 +3160,64 @@ fn: function () {
 smalltalk.Smalltalk.klass);
 
 
+smalltalk.addClass('Timeout', smalltalk.Object, ['rawTimeout'], 'Kernel-Objects');
+smalltalk.addMethod(
+"_clearInterval",
+smalltalk.method({
+selector: "clearInterval",
+fn: function (){
+var self=this;
+var local;
+local=self["@rawTimeout"];
+clearInterval(local);
+;
+return self}
+}),
+smalltalk.Timeout);
+
+smalltalk.addMethod(
+"_clearTimeout",
+smalltalk.method({
+selector: "clearTimeout",
+fn: function (){
+var self=this;
+var local;
+local=self["@rawTimeout"];
+clearTimeout(local);
+;
+return self}
+}),
+smalltalk.Timeout);
+
+smalltalk.addMethod(
+"_rawTimeout_",
+smalltalk.method({
+selector: "rawTimeout:",
+fn: function (anObject){
+var self=this;
+self["@rawTimeout"]=anObject;
+return self}
+}),
+smalltalk.Timeout);
+
+
+smalltalk.addMethod(
+"_on_",
+smalltalk.method({
+selector: "on:",
+fn: function (anObject){
+var self=this;
+var $2,$3,$1;
+$2=smalltalk.send(self,"_new",[]);
+smalltalk.send($2,"_rawTimeout_",[anObject]);
+$3=smalltalk.send($2,"_yourself",[]);
+$1=$3;
+return $1;
+}
+}),
+smalltalk.Timeout.klass);
+
+
 smalltalk.addClass('UndefinedObject', smalltalk.Object, [], 'Kernel-Objects');
 smalltalk.addMethod(
 "_asJSON",

+ 79 - 34
js/Kernel-Objects.js

@@ -2480,40 +2480,6 @@ referencedClasses: ["Random"]
 }),
 smalltalk.Number);
 
-smalltalk.addMethod(
-"_clearInterval",
-smalltalk.method({
-selector: "clearInterval",
-category: 'timeouts/intervals',
-fn: function () {
-    var self = this;
-    clearInterval(Number(self));
-    return self;
-},
-args: [],
-source: "clearInterval\x0a\x09<clearInterval(Number(self))>",
-messageSends: [],
-referencedClasses: []
-}),
-smalltalk.Number);
-
-smalltalk.addMethod(
-"_clearTimeout",
-smalltalk.method({
-selector: "clearTimeout",
-category: 'timeouts/intervals',
-fn: function () {
-    var self = this;
-    clearTimeout(Number(self));
-    return self;
-},
-args: [],
-source: "clearTimeout\x0a\x09<clearTimeout(Number(self))>",
-messageSends: [],
-referencedClasses: []
-}),
-smalltalk.Number);
-
 smalltalk.addMethod(
 "_copy",
 smalltalk.method({
@@ -4378,6 +4344,85 @@ referencedClasses: []
 smalltalk.Smalltalk.klass);
 
 
+smalltalk.addClass('Timeout', smalltalk.Object, ['rawTimeout'], 'Kernel-Objects');
+smalltalk.Timeout.comment="I am wrapping the returns from set{Timeout,Interval}.\x0a\x0aNumber suffices in browsers, but node.js returns an object."
+smalltalk.addMethod(
+"_clearInterval",
+smalltalk.method({
+selector: "clearInterval",
+category: 'timeout/interval',
+fn: function (){
+var self=this;
+var local;
+local=self["@rawTimeout"];
+clearInterval(local);
+;
+return self},
+args: [],
+source: "clearInterval\x0a\x09| local |\x0a    local := rawTimeout.\x0a\x09<clearInterval(local)>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Timeout);
+
+smalltalk.addMethod(
+"_clearTimeout",
+smalltalk.method({
+selector: "clearTimeout",
+category: 'timeout/interval',
+fn: function (){
+var self=this;
+var local;
+local=self["@rawTimeout"];
+clearTimeout(local);
+;
+return self},
+args: [],
+source: "clearTimeout\x0a\x09| local |\x0a    local := rawTimeout.\x0a\x09<clearTimeout(local)>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Timeout);
+
+smalltalk.addMethod(
+"_rawTimeout_",
+smalltalk.method({
+selector: "rawTimeout:",
+category: 'accessing',
+fn: function (anObject){
+var self=this;
+self["@rawTimeout"]=anObject;
+return self},
+args: ["anObject"],
+source: "rawTimeout: anObject\x0a\x09rawTimeout := anObject",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Timeout);
+
+
+smalltalk.addMethod(
+"_on_",
+smalltalk.method({
+selector: "on:",
+category: 'instance creation',
+fn: function (anObject){
+var self=this;
+var $2,$3,$1;
+$2=smalltalk.send(self,"_new",[]);
+smalltalk.send($2,"_rawTimeout_",[anObject]);
+$3=smalltalk.send($2,"_yourself",[]);
+$1=$3;
+return $1;
+},
+args: ["anObject"],
+source: "on: anObject\x0a\x09^self new rawTimeout: anObject; yourself",
+messageSends: ["rawTimeout:", "new", "yourself"],
+referencedClasses: []
+}),
+smalltalk.Timeout.klass);
+
+
 smalltalk.addClass('UndefinedObject', smalltalk.Object, [], 'Kernel-Objects');
 smalltalk.UndefinedObject.comment="UndefinedObject describes the behavior of its sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.\x0a\x0a`nil` is the Smalltalk representation of the `undefined` JavaScript object."
 smalltalk.addMethod(

+ 30 - 0
js/Kernel-Tests.deploy.js

@@ -1,5 +1,35 @@
 smalltalk.addPackage('Kernel-Tests', {});
 smalltalk.addClass('BlockClosureTest', smalltalk.TestCase, [], 'Kernel-Tests');
+smalltalk.addMethod(
+"_testCanClearInterval",
+smalltalk.method({
+selector: "testCanClearInterval",
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldnt_raise_",[(function(){
+return smalltalk.send(smalltalk.send((function(){
+return smalltalk.send(smalltalk.send((smalltalk.Error || Error),"_new",[]),"_signal",[]);
+}),"_valueWithInterval_",[(0)]),"_clearInterval",[]);
+}),(smalltalk.Error || Error)]);
+return self}
+}),
+smalltalk.BlockClosureTest);
+
+smalltalk.addMethod(
+"_testCanClearTimeout",
+smalltalk.method({
+selector: "testCanClearTimeout",
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldnt_raise_",[(function(){
+return smalltalk.send(smalltalk.send((function(){
+return smalltalk.send(smalltalk.send((smalltalk.Error || Error),"_new",[]),"_signal",[]);
+}),"_valueWithTimeout_",[(0)]),"_clearTimeout",[]);
+}),(smalltalk.Error || Error)]);
+return self}
+}),
+smalltalk.BlockClosureTest);
+
 smalltalk.addMethod(
 "_testCompiledSource",
 smalltalk.method({

+ 40 - 0
js/Kernel-Tests.js

@@ -1,5 +1,45 @@
 smalltalk.addPackage('Kernel-Tests', {});
 smalltalk.addClass('BlockClosureTest', smalltalk.TestCase, [], 'Kernel-Tests');
+smalltalk.addMethod(
+"_testCanClearInterval",
+smalltalk.method({
+selector: "testCanClearInterval",
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldnt_raise_",[(function(){
+return smalltalk.send(smalltalk.send((function(){
+return smalltalk.send(smalltalk.send((smalltalk.Error || Error),"_new",[]),"_signal",[]);
+}),"_valueWithInterval_",[(0)]),"_clearInterval",[]);
+}),(smalltalk.Error || Error)]);
+return self},
+args: [],
+source: "testCanClearInterval\x0a\x09self shouldnt: [([Error new signal] valueWithInterval: 0) clearInterval] raise: Error",
+messageSends: ["shouldnt:raise:", "clearInterval", "valueWithInterval:", "signal", "new"],
+referencedClasses: ["Error"]
+}),
+smalltalk.BlockClosureTest);
+
+smalltalk.addMethod(
+"_testCanClearTimeout",
+smalltalk.method({
+selector: "testCanClearTimeout",
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldnt_raise_",[(function(){
+return smalltalk.send(smalltalk.send((function(){
+return smalltalk.send(smalltalk.send((smalltalk.Error || Error),"_new",[]),"_signal",[]);
+}),"_valueWithTimeout_",[(0)]),"_clearTimeout",[]);
+}),(smalltalk.Error || Error)]);
+return self},
+args: [],
+source: "testCanClearTimeout\x0a\x09self shouldnt: [([Error new signal] valueWithTimeout: 0) clearTimeout] raise: Error",
+messageSends: ["shouldnt:raise:", "clearTimeout", "valueWithTimeout:", "signal", "new"],
+referencedClasses: ["Error"]
+}),
+smalltalk.BlockClosureTest);
+
 smalltalk.addMethod(
 "_testCompiledSource",
 smalltalk.method({

+ 6 - 2
st/Kernel-Methods.st

@@ -120,11 +120,15 @@ fork
 !
 
 valueWithInterval: aNumber
-	<return setInterval(self, aNumber)>
+	| local |
+	<local = setInterval(self, aNumber)>.
+    ^ Timeout on: local
 !
 
 valueWithTimeout: aNumber
-	<return setTimeout(self, aNumber)>
+	| local |
+	<local = setTimeout(self, aNumber)>.
+    ^ Timeout on: local
 ! !
 
 Object subclass: #CompiledMethod

+ 34 - 10
st/Kernel-Objects.st

@@ -973,16 +973,6 @@ positive
 	^ self >= 0
 ! !
 
-!Number methodsFor: 'timeouts/intervals'!
-
-clearInterval
-	<clearInterval(Number(self))>
-!
-
-clearTimeout
-	<clearTimeout(Number(self))>
-! !
-
 !Number class methodsFor: 'instance creation'!
 
 pi
@@ -1548,6 +1538,40 @@ current
 	<return smalltalk>
 ! !
 
+Object subclass: #Timeout
+	instanceVariableNames: 'rawTimeout'
+	package: 'Kernel-Objects'!
+!Timeout commentStamp!
+I am wrapping the returns from set{Timeout,Interval}.
+
+Number suffices in browsers, but node.js returns an object.!
+
+!Timeout methodsFor: 'accessing'!
+
+rawTimeout: anObject
+	rawTimeout := anObject
+! !
+
+!Timeout methodsFor: 'timeout/interval'!
+
+clearInterval
+	| local |
+    local := rawTimeout.
+	<clearInterval(local)>
+!
+
+clearTimeout
+	| local |
+    local := rawTimeout.
+	<clearTimeout(local)>
+! !
+
+!Timeout class methodsFor: 'instance creation'!
+
+on: anObject
+	^self new rawTimeout: anObject; yourself
+! !
+
 Object subclass: #UndefinedObject
 	instanceVariableNames: ''
 	package: 'Kernel-Objects'!

+ 8 - 0
st/Kernel-Tests.st

@@ -5,6 +5,14 @@ TestCase subclass: #BlockClosureTest
 
 !BlockClosureTest methodsFor: 'tests'!
 
+testCanClearInterval
+	self shouldnt: [([Error new signal] valueWithInterval: 0) clearInterval] raise: Error
+!
+
+testCanClearTimeout
+	self shouldnt: [([Error new signal] valueWithTimeout: 0) clearTimeout] raise: Error
+!
+
 testCompiledSource
 	self assert: ([1+1] compiledSource includesSubString: 'function')
 !