Bladeren bron

Type-checked Number bit arithmetics.

Herby Vojčík 4 jaren geleden
bovenliggende
commit
a1a162a6f6
2 gewijzigde bestanden met toevoegingen van 192 en 12 verwijderingen
  1. 159 9
      lang/src/Kernel-Objects.js
  2. 33 3
      lang/src/Kernel-Objects.st

+ 159 - 9
lang/src/Kernel-Objects.js

@@ -678,6 +678,30 @@ return $recv($globals.Association)._key_value_(self,anObject);
 }; }),
 $globals.Object);
 
+$core.addMethod(
+$core.method({
+selector: "andSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "andSelfToNumber: aNumber\x0a\x09self error: 'I am not a number.'",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["error:"]
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+$self._error_("I am not a number.");
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"andSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Object);
+
 $core.addMethod(
 $core.method({
 selector: "appendToString:",
@@ -1363,6 +1387,30 @@ return self;
 }; }),
 $globals.Object);
 
+$core.addMethod(
+$core.method({
+selector: "orSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "orSelfToNumber: aNumber\x0a\x09self error: 'I am not a number.'",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["error:"]
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+$self._error_("I am not a number.");
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"orSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Object);
+
 $core.addMethod(
 $core.method({
 selector: "plusSelfToNumber:",
@@ -1602,6 +1650,30 @@ return self;
 }; }),
 $globals.Object);
 
+$core.addMethod(
+$core.method({
+selector: "xorSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "xorSelfToNumber: aNumber\x0a\x09self error: 'I am not a number.'",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["error:"]
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+$self._error_("I am not a number.");
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"xorSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Object);
+
 
 $core.addMethod(
 $core.method({
@@ -3561,6 +3633,30 @@ return self;
 }; }),
 $globals.Number);
 
+$core.addMethod(
+$core.method({
+selector: "andSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "andSelfToNumber: aNumber\x0a\x09<inlineJS: 'return aNumber & self'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [["inlineJS:", ["return aNumber & self"]]],
+messageSends: []
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return aNumber & self;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"andSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Number);
+
 $core.addMethod(
 $core.method({
 selector: "arcCos",
@@ -3827,17 +3923,19 @@ selector: "bitAnd:",
 protocol: "converting",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aNumber"],
-source: "bitAnd: aNumber\x0a\x09<inlineJS: 'return self & aNumber'>",
+source: "bitAnd: aNumber\x0a\x09<inlineJS: 'return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self & aNumber :\x0a\x09\x09$recv(aNumber)._andSelfToNumber_(self)'>",
 referencedClasses: [],
 //>>excludeEnd("ide");
-pragmas: [["inlineJS:", ["return self & aNumber"]]],
+pragmas: [["inlineJS:", ["return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self & aNumber :\x0a\x09\x09$recv(aNumber)._andSelfToNumber_(self)"]]],
 messageSends: []
 }, function ($methodClass){ return function (aNumber){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return self & aNumber;
+return typeof aNumber === "number" ?
+		self & aNumber :
+		$recv(aNumber)._andSelfToNumber_(self);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"bitAnd:",{aNumber:aNumber})});
@@ -3875,17 +3973,19 @@ selector: "bitOr:",
 protocol: "converting",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aNumber"],
-source: "bitOr: aNumber\x0a\x09<inlineJS: 'return self | aNumber'>",
+source: "bitOr: aNumber\x0a\x09<inlineJS: 'return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self | aNumber :\x0a\x09\x09$recv(aNumber)._orSelfToNumber_(self)'>",
 referencedClasses: [],
 //>>excludeEnd("ide");
-pragmas: [["inlineJS:", ["return self | aNumber"]]],
+pragmas: [["inlineJS:", ["return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self | aNumber :\x0a\x09\x09$recv(aNumber)._orSelfToNumber_(self)"]]],
 messageSends: []
 }, function ($methodClass){ return function (aNumber){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return self | aNumber;
+return typeof aNumber === "number" ?
+		self | aNumber :
+		$recv(aNumber)._orSelfToNumber_(self);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"bitOr:",{aNumber:aNumber})});
@@ -3899,17 +3999,19 @@ selector: "bitXor:",
 protocol: "converting",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aNumber"],
-source: "bitXor: aNumber\x0a\x09<inlineJS: 'return self ^ aNumber'>",
+source: "bitXor: aNumber\x0a\x09<inlineJS: 'return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self ^ aNumber :\x0a\x09\x09$recv(aNumber)._xorSelfToNumber_(self)'>",
 referencedClasses: [],
 //>>excludeEnd("ide");
-pragmas: [["inlineJS:", ["return self ^ aNumber"]]],
+pragmas: [["inlineJS:", ["return typeof aNumber === \x22number\x22 ?\x0a\x09\x09self ^ aNumber :\x0a\x09\x09$recv(aNumber)._xorSelfToNumber_(self)"]]],
 messageSends: []
 }, function ($methodClass){ return function (aNumber){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return self ^ aNumber;
+return typeof aNumber === "number" ?
+		self ^ aNumber :
+		$recv(aNumber)._xorSelfToNumber_(self);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"bitXor:",{aNumber:aNumber})});
@@ -4611,6 +4713,30 @@ return $recv($self._even())._not();
 }; }),
 $globals.Number);
 
+$core.addMethod(
+$core.method({
+selector: "orSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "orSelfToNumber: aNumber\x0a\x09<inlineJS: 'return aNumber | self'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [["inlineJS:", ["return aNumber | self"]]],
+messageSends: []
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return aNumber | self;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"orSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Number);
+
 $core.addMethod(
 $core.method({
 selector: "plusSelfToNumber:",
@@ -5295,6 +5421,30 @@ return self;
 }; }),
 $globals.Number);
 
+$core.addMethod(
+$core.method({
+selector: "xorSelfToNumber:",
+protocol: "converting",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aNumber"],
+source: "xorSelfToNumber: aNumber\x0a\x09<inlineJS: 'return aNumber ^ self'>",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [["inlineJS:", ["return aNumber ^ self"]]],
+messageSends: []
+}, function ($methodClass){ return function (aNumber){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+return aNumber ^ self;
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"xorSelfToNumber:",{aNumber:aNumber})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.Number);
+
 $core.addMethod(
 $core.method({
 selector: "|",

+ 33 - 3
lang/src/Kernel-Objects.st

@@ -266,6 +266,10 @@ isNumberLessThanSelf: aNumber
 	^ Association key: self value: anObject
 !
 
+andSelfToNumber: aNumber
+	self error: 'I am not a number.'
+!
+
 asJSONString
 	^ JSON stringify: self asJavaScriptObject
 !
@@ -280,6 +284,14 @@ asJavaScriptObject
 
 asJavaScriptSource
 	^ self asString
+!
+
+orSelfToNumber: aNumber
+	self error: 'I am not a number.'
+!
+
+xorSelfToNumber: aNumber
+	self error: 'I am not a number.'
 ! !
 
 !Object methodsFor: 'copying'!
@@ -908,6 +920,10 @@ isNumberLessThanSelf: aNumber
 	^ Point x: self y: aNumber
 !
 
+andSelfToNumber: aNumber
+	<inlineJS: 'return aNumber & self'>
+!
+
 asJavaScriptObject
 	^ self
 !
@@ -933,7 +949,9 @@ atRandom
 !
 
 bitAnd: aNumber
-	<inlineJS: 'return self & aNumber'>
+	<inlineJS: 'return typeof aNumber === "number" ?
+		self & aNumber :
+		$recv(aNumber)._andSelfToNumber_(self)'>
 !
 
 bitNot
@@ -941,11 +959,15 @@ bitNot
 !
 
 bitOr: aNumber
-	<inlineJS: 'return self | aNumber'>
+	<inlineJS: 'return typeof aNumber === "number" ?
+		self | aNumber :
+		$recv(aNumber)._orSelfToNumber_(self)'>
 !
 
 bitXor: aNumber
-	<inlineJS: 'return self ^ aNumber'>
+	<inlineJS: 'return typeof aNumber === "number" ?
+		self ^ aNumber :
+		$recv(aNumber)._xorSelfToNumber_(self)'>
 !
 
 ceiling
@@ -960,6 +982,10 @@ floor
 	<inlineJS: 'return Math.floor(self);'>
 !
 
+orSelfToNumber: aNumber
+	<inlineJS: 'return aNumber | self'>
+!
+
 printStringBase: aBase
 	<inlineJS: 'return self.toString(aBase)'>
 !
@@ -1013,6 +1039,10 @@ truncated
 	'>
 !
 
+xorSelfToNumber: aNumber
+	<inlineJS: 'return aNumber ^ self'>
+!
+
 | aNumber
 	^ self bitOr: aNumber
 ! !