Explorar o código

- BlockClosure >> #newWithValues: improvements:
- improved comment
- better variable names
- better formatting

Nicolas Petton %!s(int64=11) %!d(string=hai) anos
pai
achega
9d0c4bb068

+ 5 - 5
js/Kernel-Methods.deploy.js

@@ -130,11 +130,11 @@ fn: function (aCollection){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 
-var blankObject = function() {};
-blankObject.prototype = self.prototype;
-var newObject = new blankObject;
-var result = self.apply(newObject, aCollection);
-return typeof result === "object" ? result : newObject;;
+	var constructor = function() {};
+	constructor.prototype = self.prototype;
+	var object = new constructor;
+	var result = self.apply(object, aCollection);
+	return typeof result === "object" ? result : object;;
 return self}, function($ctx1) {$ctx1.fill(self,"newWithValues:",{aCollection:aCollection},smalltalk.BlockClosure)})},
 messageSends: []}),
 smalltalk.BlockClosure);

+ 6 - 6
js/Kernel-Methods.js

@@ -182,14 +182,14 @@ fn: function (aCollection){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 
-var blankObject = function() {};
-blankObject.prototype = self.prototype;
-var newObject = new blankObject;
-var result = self.apply(newObject, aCollection);
-return typeof result === "object" ? result : newObject;;
+	var constructor = function() {};
+	constructor.prototype = self.prototype;
+	var object = new constructor;
+	var result = self.apply(object, aCollection);
+	return typeof result === "object" ? result : object;;
 return self}, function($ctx1) {$ctx1.fill(self,"newWithValues:",{aCollection:aCollection},smalltalk.BlockClosure)})},
 args: ["aCollection"],
-source: "newWithValues: aCollection\x0a\x22Answer an object that's been created in JS via `new` and had `self` applied to it.\x0aThis algorithm was inspired by http://stackoverflow.com/a/6069331.\x0a\x0aHere's a general breakdown of what's going on:\x0a1) Create a new, blank function object.\x0a2) Set it's prototype to `self`'s prototype. Remember, we're in a BlockClosure, and presumably this BlockClosure is wrapping a JS function, and also presumably this function is used as a constructor.\x0a3) Instantiate a new version of the function object just created. This forces the interpreter to set the internal [[prototype]] property to what was set on the function before. This has to be done, as we have no access to the [[prototype]] property externally.\x0a4) Apply `self` to the object I just instantiated.\x22\x0a<\x0avar blankObject = function() {};\x0ablankObject.prototype = self.prototype;\x0avar newObject = new blankObject;\x0avar result = self.apply(newObject, aCollection);\x0areturn typeof result === \x22object\x22 ? result : newObject;\x0a>",
+source: "newWithValues: aCollection\x0a\x22Use the receiver as a JavaScript constructor with a variable number of arguments.\x0aAnswer the object created using the operator `new`.\x0a\x0aThis algorithm was inspired by http://stackoverflow.com/a/6069331.\x0a\x0aHere's a general breakdown of what's going on:\x0a1) Create a new, empty constructor function.\x0a2) Set it's prototype to the receiver's prototype. Because the receiver is a `BlockClosure`, it is also a JavaScript function.\x0a3) Instantiate a new object using the constructor function just created. \x0a   This forces the interpreter to set the internal [[prototype]] property to what was set on the function before. \x0a   This has to be done, as we have no access to the [[prototype]] property externally.\x0a4) Apply `self` to the object I just instantiated.\x22\x0a\x0a<\x0a\x09var constructor = function() {};\x0a\x09constructor.prototype = self.prototype;\x0a\x09var object = new constructor;\x0a\x09var result = self.apply(object, aCollection);\x0a\x09return typeof result === \x22object\x22 ? result : object;\x0a>",
 messageSends: [],
 referencedClasses: []
 }),

+ 9 - 9
js/Kernel-Tests.deploy.js

@@ -146,17 +146,17 @@ fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 
-function theTestPrototype() {this.name = "theTestPrototype";}
-function theTestConstructor(arg1, arg2, arg3) {}
-theTestConstructor.prototype = new theTestPrototype;
+	function theTestPrototype() {this.name = "theTestPrototype";}
+	function theTestConstructor(arg1, arg2, arg3) {}
+	theTestConstructor.prototype = new theTestPrototype;
 
-var theWrappedConstructor = _st(theTestConstructor);
-var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
-self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
+	var theWrappedConstructor = _st(theTestConstructor);
+	var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
+	self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
 
-"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
-function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
-self._should_raise_(_st(constructionShouldFail), smalltalk.Error);;
+	"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
+	function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
+	self._should_raise_(_st(constructionShouldFail), smalltalk.Error);;
 return self}, function($ctx1) {$ctx1.fill(self,"testNewWithValues",{},smalltalk.BlockClosureTest)})},
 messageSends: []}),
 smalltalk.BlockClosureTest);

+ 10 - 10
js/Kernel-Tests.js

@@ -182,20 +182,20 @@ fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 
-function theTestPrototype() {this.name = "theTestPrototype";}
-function theTestConstructor(arg1, arg2, arg3) {}
-theTestConstructor.prototype = new theTestPrototype;
+	function theTestPrototype() {this.name = "theTestPrototype";}
+	function theTestConstructor(arg1, arg2, arg3) {}
+	theTestConstructor.prototype = new theTestPrototype;
 
-var theWrappedConstructor = _st(theTestConstructor);
-var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
-self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
+	var theWrappedConstructor = _st(theTestConstructor);
+	var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
+	self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
 
-"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
-function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
-self._should_raise_(_st(constructionShouldFail), smalltalk.Error);;
+	"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
+	function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
+	self._should_raise_(_st(constructionShouldFail), smalltalk.Error);;
 return self}, function($ctx1) {$ctx1.fill(self,"testNewWithValues",{},smalltalk.BlockClosureTest)})},
 args: [],
-source: "testNewWithValues\x0a<\x0afunction theTestPrototype() {this.name = \x22theTestPrototype\x22;}\x0afunction theTestConstructor(arg1, arg2, arg3) {}\x0atheTestConstructor.prototype = new theTestPrototype;\x0a\x0avar theWrappedConstructor = _st(theTestConstructor);\x0avar theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);\x0aself._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');\x0a\x0a\x22newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made.\x22\x0afunction constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}\x0aself._should_raise_(_st(constructionShouldFail), smalltalk.Error);\x0a>",
+source: "testNewWithValues\x0a<\x0a\x09function theTestPrototype() {this.name = \x22theTestPrototype\x22;}\x0a\x09function theTestConstructor(arg1, arg2, arg3) {}\x0a\x09theTestConstructor.prototype = new theTestPrototype;\x0a\x0a\x09var theWrappedConstructor = _st(theTestConstructor);\x0a\x09var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);\x0a\x09self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');\x0a\x0a\x09\x22newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made.\x22\x0a\x09function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}\x0a\x09self._should_raise_(_st(constructionShouldFail), smalltalk.Error);\x0a>",
 messageSends: [],
 referencedClasses: []
 }),

+ 14 - 9
st/Kernel-Methods.st

@@ -125,20 +125,25 @@ newValue: anObject value: anObject2 value: anObject3
 !
 
 newWithValues: aCollection
-"Answer an object that's been created in JS via `new` and had `self` applied to it.
+"Use the receiver as a JavaScript constructor with a variable number of arguments.
+Answer the object created using the operator `new`.
+
 This algorithm was inspired by http://stackoverflow.com/a/6069331.
 
 Here's a general breakdown of what's going on:
-1) Create a new, blank function object.
-2) Set it's prototype to `self`'s prototype. Remember, we're in a BlockClosure, and presumably this BlockClosure is wrapping a JS function, and also presumably this function is used as a constructor.
-3) Instantiate a new version of the function object just created. This forces the interpreter to set the internal [[prototype]] property to what was set on the function before. This has to be done, as we have no access to the [[prototype]] property externally.
+1) Create a new, empty constructor function.
+2) Set it's prototype to the receiver's prototype. Because the receiver is a `BlockClosure`, it is also a JavaScript function.
+3) Instantiate a new object using the constructor function just created. 
+   This forces the interpreter to set the internal [[prototype]] property to what was set on the function before. 
+   This has to be done, as we have no access to the [[prototype]] property externally.
 4) Apply `self` to the object I just instantiated."
+
 <
-var blankObject = function() {};
-blankObject.prototype = self.prototype;
-var newObject = new blankObject;
-var result = self.apply(newObject, aCollection);
-return typeof result === "object" ? result : newObject;
+	var constructor = function() {};
+	constructor.prototype = self.prototype;
+	var object = new constructor;
+	var result = self.apply(object, aCollection);
+	return typeof result === "object" ? result : object;
 >
 !
 

+ 9 - 9
st/Kernel-Tests.st

@@ -51,17 +51,17 @@ testExceptionSemantics
 
 testNewWithValues
 <
-function theTestPrototype() {this.name = "theTestPrototype";}
-function theTestConstructor(arg1, arg2, arg3) {}
-theTestConstructor.prototype = new theTestPrototype;
+	function theTestPrototype() {this.name = "theTestPrototype";}
+	function theTestConstructor(arg1, arg2, arg3) {}
+	theTestConstructor.prototype = new theTestPrototype;
 
-var theWrappedConstructor = _st(theTestConstructor);
-var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
-self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
+	var theWrappedConstructor = _st(theTestConstructor);
+	var theResult = theWrappedConstructor._newWithValues_([1, 2, 3]);
+	self._assert_equals_(Object.getPrototypeOf(theResult).name, 'theTestPrototype');
 
-"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
-function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
-self._should_raise_(_st(constructionShouldFail), smalltalk.Error);
+	"newWithValues: cannot help if the argument list is wrong, and should warn that a mistake was made."
+	function constructionShouldFail() {var anotherResult = theWrappedConstructor._newWithValues_('This is so wrong');}
+	self._should_raise_(_st(constructionShouldFail), smalltalk.Error);
 >
 !