1
0
Jelajahi Sumber

Fixes issue 428:

Mutable literals should be aliased too in cascades
Nicolas Petton 11 tahun lalu
induk
melakukan
5c62d4313a

+ 4 - 2
js/Compiler-AST.deploy.js

@@ -1162,9 +1162,11 @@ smalltalk.method({
 selector: "isImmutable",
 fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
return true;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(_st(self)._value())._isImmutable();
+return $1;
 }, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.ValueNode)})},
-messageSends: []}),
+messageSends: ["isImmutable", "value"]}),
 smalltalk.ValueNode);
 
 smalltalk.addMethod(

+ 5 - 3
js/Compiler-AST.js

@@ -1589,11 +1589,13 @@ selector: "isImmutable",
 category: 'testing',
 fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
return true;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(_st(self)._value())._isImmutable();
+return $1;
 }, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.ValueNode)})},
 args: [],
-source: "isImmutable\x0a\x09^true",
-messageSends: [],
+source: "isImmutable\x0a\x09^ self value isImmutable",
+messageSends: ["isImmutable", "value"],
 referencedClasses: []
 }),
 smalltalk.ValueNode);

+ 11 - 0
js/Compiler-Tests.deploy.js

@@ -521,6 +521,17 @@ return self}, function($ctx1) {$ctx1.fill(self,"testMessageSends",{},smalltalk.C
 messageSends: ["should:return:"]}),
 smalltalk.CodeGeneratorTest);
 
+smalltalk.addMethod(
+"_testMutableLiterals",
+smalltalk.method({
+selector: "testMutableLiterals",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._should_return_("foo ^ #( 1 2 ) at: 1 put: 3; yourself",[(3), (2)]);
+return self}, function($ctx1) {$ctx1.fill(self,"testMutableLiterals",{},smalltalk.CodeGeneratorTest)})},
+messageSends: ["should:return:"]}),
+smalltalk.CodeGeneratorTest);
+
 smalltalk.addMethod(
 "_testNestedIfTrue",
 smalltalk.method({

+ 16 - 0
js/Compiler-Tests.js

@@ -711,6 +711,22 @@ referencedClasses: []
 }),
 smalltalk.CodeGeneratorTest);
 
+smalltalk.addMethod(
+"_testMutableLiterals",
+smalltalk.method({
+selector: "testMutableLiterals",
+category: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._should_return_("foo ^ #( 1 2 ) at: 1 put: 3; yourself",[(3), (2)]);
+return self}, function($ctx1) {$ctx1.fill(self,"testMutableLiterals",{},smalltalk.CodeGeneratorTest)})},
+args: [],
+source: "testMutableLiterals\x0a\x09\x22Mutable literals must be aliased in cascades.\x0a\x09See https://github.com/amber-smalltalk/amber/issues/428\x22\x0a\x09\x0a\x09self \x0a\x09\x09should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself' \x0a\x09\x09return: #(3 2)",
+messageSends: ["should:return:"],
+referencedClasses: []
+}),
+smalltalk.CodeGeneratorTest);
+
 smalltalk.addMethod(
 "_testNestedIfTrue",
 smalltalk.method({

+ 11 - 0
js/Kernel-Collections.deploy.js

@@ -2623,6 +2623,17 @@ return self}, function($ctx1) {$ctx1.fill(self,"includesSubString:",{subString:s
 messageSends: []}),
 smalltalk.String);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.String)})},
+messageSends: []}),
+smalltalk.String);
+
 smalltalk.addMethod(
 "_isString",
 smalltalk.method({

+ 16 - 0
js/Kernel-Collections.js

@@ -3557,6 +3557,22 @@ referencedClasses: []
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.String)})},
+args: [],
+source: "isImmutable\x0a\x09^ true",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.String);
+
 smalltalk.addMethod(
 "_isString",
 smalltalk.method({

+ 44 - 0
js/Kernel-Objects.deploy.js

@@ -367,6 +367,17 @@ return smalltalk.withContext(function($ctx1) { 
return false;
 messageSends: []}),
 smalltalk.Object);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return false;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.Object)})},
+messageSends: []}),
+smalltalk.Object);
+
 smalltalk.addMethod(
 "_isKindOf_",
 smalltalk.method({
@@ -891,6 +902,17 @@ return smalltalk.withContext(function($ctx1) { 
return true;
 messageSends: []}),
 smalltalk.Boolean);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.Boolean)})},
+messageSends: []}),
+smalltalk.Boolean);
+
 smalltalk.addMethod(
 "_not",
 smalltalk.method({
@@ -1789,6 +1811,17 @@ return $1;
 messageSends: ["x:y:"]}),
 smalltalk.Number);
 
+smalltalk.addMethod(
+"_IsImmutable",
+smalltalk.method({
+selector: "IsImmutable",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"IsImmutable",{},smalltalk.Number)})},
+messageSends: []}),
+smalltalk.Number);
+
 smalltalk.addMethod(
 "__backslash",
 smalltalk.method({
@@ -3473,6 +3506,17 @@ return $1;
 messageSends: ["value"]}),
 smalltalk.UndefinedObject);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.UndefinedObject)})},
+messageSends: []}),
+smalltalk.UndefinedObject);
+
 smalltalk.addMethod(
 "_isNil",
 smalltalk.method({

+ 64 - 0
js/Kernel-Objects.js

@@ -513,6 +513,22 @@ referencedClasses: []
 }),
 smalltalk.Object);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return false;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.Object)})},
+args: [],
+source: "isImmutable\x0a\x09^ false",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Object);
+
 smalltalk.addMethod(
 "_isKindOf_",
 smalltalk.method({
@@ -1238,6 +1254,22 @@ referencedClasses: []
 }),
 smalltalk.Boolean);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.Boolean)})},
+args: [],
+source: "isImmutable\x0a\x09^ true",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Boolean);
+
 smalltalk.addMethod(
 "_not",
 smalltalk.method({
@@ -2499,6 +2531,22 @@ referencedClasses: ["Point"]
 }),
 smalltalk.Number);
 
+smalltalk.addMethod(
+"_IsImmutable",
+smalltalk.method({
+selector: "IsImmutable",
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"IsImmutable",{},smalltalk.Number)})},
+args: [],
+source: "IsImmutable\x0a\x09^ true",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Number);
+
 smalltalk.addMethod(
 "__backslash",
 smalltalk.method({
@@ -4779,6 +4827,22 @@ referencedClasses: []
 }),
 smalltalk.UndefinedObject);
 
+smalltalk.addMethod(
+"_isImmutable",
+smalltalk.method({
+selector: "isImmutable",
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
return true;
+}, function($ctx1) {$ctx1.fill(self,"isImmutable",{},smalltalk.UndefinedObject)})},
+args: [],
+source: "isImmutable\x0a\x09^ true",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.UndefinedObject);
+
 smalltalk.addMethod(
 "_isNil",
 smalltalk.method({

+ 1 - 1
st/Compiler-AST.st

@@ -482,7 +482,7 @@ value: anObject
 !ValueNode methodsFor: 'testing'!
 
 isImmutable
-	^true
+	^ self value isImmutable
 !
 
 isValueNode

+ 9 - 0
st/Compiler-Tests.st

@@ -332,6 +332,15 @@ testMessageSends
 	self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
 !
 
+testMutableLiterals
+	"Mutable literals must be aliased in cascades.
+	See https://github.com/amber-smalltalk/amber/issues/428"
+	
+	self 
+		should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself' 
+		return: #(3 2)
+!
+
 testNestedIfTrue
 	self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
 	self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.

+ 4 - 0
st/Kernel-Collections.st

@@ -1366,6 +1366,10 @@ includesSubString: subString
 	< return self.indexOf(subString) !!= -1 >
 !
 
+isImmutable
+	^ true
+!
+
 isString
 	^true
 !

+ 16 - 0
st/Kernel-Objects.st

@@ -286,6 +286,10 @@ isClass
 	^false
 !
 
+isImmutable
+	^ false
+!
+
 isKindOf: aClass
 	^(self isMemberOf: aClass)
 		ifTrue: [true]
@@ -452,6 +456,10 @@ printOn: aStream
 
 isBoolean
 	^ true
+!
+
+isImmutable
+	^ true
 ! !
 
 Object subclass: #Date
@@ -1030,6 +1038,10 @@ printShowingDecimalPlaces: placesDesired
 
 !Number methodsFor: 'testing'!
 
+IsImmutable
+	^ true
+!
+
 even
 	^ 0 = (self \\ 2)
 !
@@ -1750,6 +1762,10 @@ ifNotNil: aBlock ifNil: anotherBlock
 	^anotherBlock value
 !
 
+isImmutable
+	^ true
+!
+
 isNil
 	^true
 !