소스 검색

Merge pull request #255 from herby/message_send

Some style fixes, ES5 inheritance.
Nicolas Petton 12 년 전
부모
커밋
70794e8d20
1개의 변경된 파일108개의 추가작업 그리고 120개의 파일을 삭제
  1. 108 120
      js/boot.js

+ 108 - 120
js/boot.js

@@ -35,7 +35,7 @@
 
 
 /* Make sure that console is defined */
 /* Make sure that console is defined */
 
 
-if (typeof console === "undefined") {
+if(typeof console === "undefined") {
 	this.console = {
 	this.console = {
 		log: function() {},
 		log: function() {},
 		warn: function() {},
 		warn: function() {},
@@ -48,8 +48,8 @@ if (typeof console === "undefined") {
 /* Array extensions */
 /* Array extensions */
 
 
 Array.prototype.addElement = function(el) {
 Array.prototype.addElement = function(el) {
-    if(typeof el === 'undefined') { return false; };
-    if(this.indexOf(el) == -1) {
+	if(typeof el === 'undefined') { return false; }
+	if(this.indexOf(el) == -1) {
         this.push(el);
         this.push(el);
     }
     }
 };
 };
@@ -66,45 +66,39 @@ Array.prototype.removeElement = function(el) {
 
 
 /* Smalltalk constructors definition */
 /* Smalltalk constructors definition */
 
 
-function SmalltalkObject(){};
-function SmalltalkBehavior(){};
-function SmalltalkClass(){};
-function SmalltalkMetaclass(){
+function SmalltalkObject() {}
+function SmalltalkBehavior() {}
+function SmalltalkClass() {}
+function SmalltalkMetaclass() {
 	this.meta = true;
 	this.meta = true;
-};
-
-function SmalltalkPackage(){};
-function SmalltalkMethod(){};
-function SmalltalkNil(){};
-
-function SmalltalkSymbol(string){
+}
+function SmalltalkPackage() {}
+function SmalltalkMethod() {}
+function SmalltalkNil() {}
+function SmalltalkSymbol(string) {
 	this.value = string;
 	this.value = string;
-};
-
+}
 function SmalltalkOrganizer() {
 function SmalltalkOrganizer() {
     this.elements = [];
     this.elements = [];
-};
-
-SmalltalkBehavior.prototype  = new SmalltalkObject();
-SmalltalkClass.prototype     = new SmalltalkBehavior();
-SmalltalkMetaclass.prototype = new SmalltalkBehavior();
-
-SmalltalkNil.prototype       = new SmalltalkObject();
-SmalltalkMethod.prototype    = new SmalltalkObject();
-SmalltalkPackage.prototype   = new SmalltalkObject();
-SmalltalkOrganizer.prototype = new SmalltalkObject();
+}
 
 
-SmalltalkBehavior.prototype.constructor  = SmalltalkBehavior;
-SmalltalkClass.prototype.constructor     = SmalltalkClass;
-SmalltalkMetaclass.prototype.constructor = SmalltalkMetaclass;
+function inherits(child, parent) {
+	child.prototype = Object.create(parent.prototype, {
+		constructor: { value: child,
+			enumerable: false, configurable: true, writable: true }
+	});
+}
 
 
-SmalltalkNil.prototype.constructor       = SmalltalkNil;
-SmalltalkMethod.prototype.constructor    = SmalltalkMethod;
-SmalltalkPackage.prototype.constructor   = SmalltalkPackage;
-SmalltalkOrganizer.prototype.constructor = SmalltalkOrganizer;
+inherits(SmalltalkBehavior, SmalltalkObject);
+inherits(SmalltalkClass, SmalltalkBehavior);
+inherits(SmalltalkMetaclass, SmalltalkBehavior);
+inherits(SmalltalkNil, SmalltalkObject);
+inherits(SmalltalkMethod, SmalltalkObject);
+inherits(SmalltalkPackage, SmalltalkObject);
+inherits(SmalltalkOrganizer, SmalltalkObject);
 
 
 
 
-function Smalltalk(){
+function Smalltalk() {
 
 
 	var st = this;
 	var st = this;
 
 
@@ -134,35 +128,35 @@ function Smalltalk(){
 
 
     var dnuHandlers = [];
     var dnuHandlers = [];
 
 
-    var addDnuHandler = function(string) {
+    function addDnuHandler(string) {
         if(dnuHandlers.indexOf(string) == -1) {
         if(dnuHandlers.indexOf(string) == -1) {
             dnuHandlers.push(string);
             dnuHandlers.push(string);
         }
         }
-    };
-    
-    /* Dnu handler method */
+	}
 
 
-    var dnu = function(selector) {
+	/* Dnu handler method */
+
+    function dnu(selector) {
         return function() {
         return function() {
             var args = Array.prototype.slice.call(arguments);
             var args = Array.prototype.slice.call(arguments);
             return messageNotUnderstood(this, selector, args);
             return messageNotUnderstood(this, selector, args);
         };
         };
-    };
+	}
 
 
 	/* The symbol table ensures symbol unicity */
 	/* The symbol table ensures symbol unicity */
 
 
-	symbolTable = {};
+	var symbolTable = {};
 	st.symbolFor = function(string) {
 	st.symbolFor = function(string) {
 		if(symbolTable[string] === undefined) {
 		if(symbolTable[string] === undefined) {
 			symbolTable[string] = new SmalltalkSymbol(string);
 			symbolTable[string] = new SmalltalkSymbol(string);
-		};
+		}
 
 
 		return symbolTable[string];
 		return symbolTable[string];
 	};
 	};
 
 
 	/* Unique ID number generator */
 	/* Unique ID number generator */
 
 
-	oid = 0;
+	var oid = 0;
 	st.nextId = function() {
 	st.nextId = function() {
 		oid += 1;
 		oid += 1;
 		return oid;
 		return oid;
@@ -180,9 +174,9 @@ function Smalltalk(){
         that.organization = new SmalltalkOrganizer();
         that.organization = new SmalltalkOrganizer();
 		that.properties = spec.properties || {};
 		that.properties = spec.properties || {};
 		return that;
 		return that;
-	};
+	}
 
 
-	/* Smalltalk class creation. A class is an instance of an automatically 
+	/* Smalltalk class creation. A class is an instance of an automatically
 	   created metaclass object. Newly created classes (not their metaclass) 
 	   created metaclass object. Newly created classes (not their metaclass) 
 	   should be added to the smalltalk object, see smalltalk.addClass().
 	   should be added to the smalltalk object, see smalltalk.addClass().
 	   Superclass linking is *not* handled here, see smalltalk.init()  */
 	   Superclass linking is *not* handled here, see smalltalk.init()  */
@@ -217,8 +211,7 @@ function Smalltalk(){
 		var that = new SmalltalkMetaclass();
 		var that = new SmalltalkMetaclass();
         that.fn            = function() {};
         that.fn            = function() {};
         that.organization  = new SmalltalkOrganizer();
         that.organization  = new SmalltalkOrganizer();
-        that.fn.prototype  = new superConstructor();
-        that.fn.prototype.constructor = that.fn;
+        inherits(that.fn, superConstructor);
 
 
         setupClass(that);
         setupClass(that);
 
 
@@ -229,7 +222,7 @@ function Smalltalk(){
 	function setupClass(klass, spec) {
 	function setupClass(klass, spec) {
         spec = spec || {};
         spec = spec || {};
         if(!klass.fn) {
         if(!klass.fn) {
-		    klass.fn = spec.fn || function(){};
+		    klass.fn = spec.fn || function() {};
         }
         }
 		klass.iVarNames = spec.iVarNames || [];
 		klass.iVarNames = spec.iVarNames || [];
 		klass.pkg = spec.pkg;
 		klass.pkg = spec.pkg;
@@ -246,7 +239,7 @@ function Smalltalk(){
         Object.defineProperties(klass.fn.prototype, {
         Object.defineProperties(klass.fn.prototype, {
 			klass: { value: klass, enumerable: false, configurable: true, writable: true }
 			klass: { value: klass, enumerable: false, configurable: true, writable: true }
 		});
 		});
-	};
+	}
 
 
 	/* Smalltalk method object. To add a method to a class,
 	/* Smalltalk method object. To add a method to a class,
 	   use smalltalk.addMethod() */
 	   use smalltalk.addMethod() */
@@ -288,21 +281,20 @@ function Smalltalk(){
         }
         }
     };
     };
 
 
-    var installSuperclass = function(klass) {
+    function installSuperclass(klass) {
         // only if the klass has not been initialized yet.
         // only if the klass has not been initialized yet.
-        if(klass.fn.prototype._yourself) { return false; };
+		if(klass.fn.prototype._yourself) { return false; }
 
 
-        if(klass.superclass && klass.superclass !== nil) {
-            klass.fn.prototype = new klass.superclass.fn();
-            klass.fn.prototype.constructor = klass.fn;
+		if(klass.superclass && klass.superclass !== nil) {
+            inherits(klass.fn, klass.superclass.fn);
             Object.defineProperties(klass.fn.prototype, {
             Object.defineProperties(klass.fn.prototype, {
 			    klass: { value: klass, enumerable: false, configurable: true, writable: true }
 			    klass: { value: klass, enumerable: false, configurable: true, writable: true }
 		    });
 		    });
             reinstallMethods(klass);
             reinstallMethods(klass);
         }
         }
-    };
+	}
 
 
-    var copySuperclass = function(klass, superclass) {
+	function copySuperclass(klass, superclass) {
         superclass = superclass || klass.superclass;
         superclass = superclass || klass.superclass;
         if(superclass && superclass !== nil) {
         if(superclass && superclass !== nil) {
 			for(var keys = Object.keys(superclass.methods), i=0; i<keys.length; i++) {
 			for(var keys = Object.keys(superclass.methods), i=0; i<keys.length; i++) {
@@ -315,59 +307,59 @@ function Smalltalk(){
                 copySuperclass(klass, superclass.superclass);
                 copySuperclass(klass, superclass.superclass);
             }
             }
         }
         }
-    };
+	}
 
 
-    var installMethod = function(method, klass) {
+	function installMethod(method, klass) {
         Object.defineProperty(klass.fn.prototype, method.jsSelector, {
         Object.defineProperty(klass.fn.prototype, method.jsSelector, {
 			value: method.fn, configurable: true, writable: true
 			value: method.fn, configurable: true, writable: true
 		});
 		});
-    };
+	}
 
 
-    var reinstallMethods = function(klass) {
+	function reinstallMethods(klass) {
         for(var keys = Object.keys(klass.methods), i=0; i<keys.length; i++) {
         for(var keys = Object.keys(klass.methods), i=0; i<keys.length; i++) {
             installMethod(klass.methods[keys[i]], klass);
             installMethod(klass.methods[keys[i]], klass);
 		}
 		}
-    };
+	}
 
 
-    var installDnuHandlers = function(klass) {
+	function installDnuHandlers(klass) {
         for(var i=0; i<dnuHandlers.length; i++) {
         for(var i=0; i<dnuHandlers.length; i++) {
             installDnuHandler(dnuHandlers[i], klass);
             installDnuHandler(dnuHandlers[i], klass);
         }
         }
-    };
+	}
 
 
-    var installDnuHandler = function(string, klass) {
+	function installDnuHandler(string, klass) {
         var selector = st.selector(string);
         var selector = st.selector(string);
         if(!klass.fn.prototype[selector]) {
         if(!klass.fn.prototype[selector]) {
             Object.defineProperty(klass.fn.prototype, selector, {
             Object.defineProperty(klass.fn.prototype, selector, {
                 value: dnu(selector), configurable: true, writable: true
                 value: dnu(selector), configurable: true, writable: true
             });
             });
         }
         }
-    };
+	}
 
 
-    var installNewDnuHandler = function(string) {
+	function installNewDnuHandler(string) {
         if(dnuHandlers.indexOf(string) === -1) {
         if(dnuHandlers.indexOf(string) === -1) {
             addDnuHandler(string);
             addDnuHandler(string);
             installDnuHandler(string, smalltalk.Object);
             installDnuHandler(string, smalltalk.Object);
             for(var i=0; i<wrappedClasses.length; i++) {
             for(var i=0; i<wrappedClasses.length; i++) {
                 installDnuHandler(string, wrappedClasses[i]);
                 installDnuHandler(string, wrappedClasses[i]);
-            };
-        }
-    };
+			}
+		}
+	}
 
 
-    /* Super sends handling */
+	/* Super sends handling */
 
 
-    var installSuperSendHandler = function(string, klass) {
+    function installSuperSendHandler(string, klass) {
         var selector = st.selector(string);
         var selector = st.selector(string);
         var superSelector = st.superSelector(string);
         var superSelector = st.superSelector(string);
         var fn = klass.superclass[selector];
         var fn = klass.superclass[selector];
         if(!fn) {
         if(!fn) {
             fn = dnu(selector);
             fn = dnu(selector);
-        };
+		}
 
 
-        Object.defineProperty(klass.fn.prototype, superSelector, {
+		Object.defineProperty(klass.fn.prototype, superSelector, {
             value: fn, configurable: true, writable: true
             value: fn, configurable: true, writable: true
         });
         });
-    };
+	}
 
 
 	/* Answer all registered Packages as Array */
 	/* Answer all registered Packages as Array */
     // TODO: Remove this hack
     // TODO: Remove this hack
@@ -375,7 +367,7 @@ function Smalltalk(){
 	st.packages.all = function() {
 	st.packages.all = function() {
 		var packages = [];
 		var packages = [];
 		for(var i in st.packages) {
 		for(var i in st.packages) {
-			if (!st.packages.hasOwnProperty(i) || typeof(st.packages[i]) === "function") continue;
+			if(!st.packages.hasOwnProperty(i) || typeof(st.packages[i]) === "function") continue;
 			packages.push(st.packages[i]);
 			packages.push(st.packages[i]);
 		}
 		}
 		return packages
 		return packages
@@ -431,15 +423,15 @@ function Smalltalk(){
 		});
 		});
 
 
         classes.addElement(st[className]);
         classes.addElement(st[className]);
-        if(wrapped) {wrappedClasses.addElement(st[className])};
-        pkg.organization.elements.addElement(st[className]);
+		if(wrapped) {wrappedClasses.addElement(st[className])}
+		pkg.organization.elements.addElement(st[className]);
 	};
 	};
 
 
 	/* Create an alias for an existing class */
 	/* Create an alias for an existing class */
 
 
 	st.alias = function(klass, alias) {
 	st.alias = function(klass, alias) {
 		st[alias] = klass;
 		st[alias] = klass;
-	}
+	};
 
 
 	/* Add a package to the smalltalk.packages object, creating a new one if needed.
 	/* Add a package to the smalltalk.packages object, creating a new one if needed.
 	   If pkgName is null or empty we return nil, which is an allowed package for a class.
 	   If pkgName is null or empty we return nil, which is an allowed package for a class.
@@ -472,7 +464,7 @@ function Smalltalk(){
 		} else {
 		} else {
             if(st[className]) {
             if(st[className]) {
                 st.removeClass(st[className]);
                 st.removeClass(st[className]);
-            };
+			}
 			st[className] = klass({
 			st[className] = klass({
 				className: className, 
 				className: className, 
 				superclass: superclass,
 				superclass: superclass,
@@ -505,13 +497,13 @@ function Smalltalk(){
 
 
         for(var i=0; i<method.superSends.length; i++) {
         for(var i=0; i<method.superSends.length; i++) {
             installSuperSendHandler(method.superSends[i], klass);
             installSuperSendHandler(method.superSends[i], klass);
-        };
+		}
 
 
-        if(initialized) {
+		if(initialized) {
             for(var i=0; i<method.messageSends.length; i++) {
             for(var i=0; i<method.messageSends.length; i++) {
                 installNewDnuHandler(method.messageSends[i]);
                 installNewDnuHandler(method.messageSends[i]);
-            };
-        };
+			}
+		}
 	};
 	};
 
 
     st.removeMethod = function(method) {
     st.removeMethod = function(method) {
@@ -525,12 +517,12 @@ function Smalltalk(){
         for(var i=0; i<klass.methods; i++) {
         for(var i=0; i<klass.methods; i++) {
             if(klass.methods[i].category == protocol) {
             if(klass.methods[i].category == protocol) {
                 shouldDeleteProtocol = true;
                 shouldDeleteProtocol = true;
-            };
-        };
-        if(shouldDeleteProtocol) {
+			}
+		}
+		if(shouldDeleteProtocol) {
             klass.organization.elements.removeElement(protocol);
             klass.organization.elements.removeElement(protocol);
-        };
-    };
+		}
+	};
 
 
 	/* Handles unhandled errors during message sends */
 	/* Handles unhandled errors during message sends */
 
 
@@ -565,7 +557,7 @@ function Smalltalk(){
 		} else {
 		} else {
 			return messageNotUnderstood(receiver, selector, args);
 			return messageNotUnderstood(receiver, selector, args);
 		}
 		}
-	};
+	}
 
 
 	st.withContext = function(fn, receiver, selector, method, args) {
 	st.withContext = function(fn, receiver, selector, method, args) {
 		if(st.thisContext) {
 		if(st.thisContext) {
@@ -589,16 +581,15 @@ function Smalltalk(){
 		var result = fn();
 		var result = fn();
 		popContext(context);
 		popContext(context);
 		return result;
 		return result;
-	};
-
+	}
 
 
-	/* Handles Smalltalk errors. Triggers the registered ErrorHandler 
+	/* Handles Smalltalk errors. Triggers the registered ErrorHandler
 	   (See the Smalltalk class ErrorHandler and its subclasses */
 	   (See the Smalltalk class ErrorHandler and its subclasses */
 
 
 	function handleError(error) {
 	function handleError(error) {
         smalltalk.ErrorHandler._current()._handleError_(error);
         smalltalk.ErrorHandler._current()._handleError_(error);
-	};
-    
+	}
+
 	/* Handles #dnu: *and* JavaScript method calls.
 	/* Handles #dnu: *and* JavaScript method calls.
 	   if the receiver has no klass, we consider it a JS object (outside of the
 	   if the receiver has no klass, we consider it a JS object (outside of the
 	   Amber system). Else assume that the receiver understands #doesNotUnderstand: */
 	   Amber system). Else assume that the receiver understands #doesNotUnderstand: */
@@ -617,7 +608,7 @@ function Smalltalk(){
 				._selector_(st.convertSelector(selector))
 				._selector_(st.convertSelector(selector))
 				._arguments_(args)
 				._arguments_(args)
 				);
 				);
-	};
+	}
 
 
 	/* Call a method of a JS object, or answer a property if it exists.
 	/* Call a method of a JS object, or answer a property if it exists.
 	   Else try wrapping a JSObjectProxy around the receiver.
 	   Else try wrapping a JSObjectProxy around the receiver.
@@ -647,8 +638,7 @@ function Smalltalk(){
 		}
 		}
 
 
 		return st.send(st.JSObjectProxy._on_(receiver), selector, args);
 		return st.send(st.JSObjectProxy._on_(receiver), selector, args);
-	};
-
+	}
 
 
 	/* Reuse one old context stored in oldContext */
 	/* Reuse one old context stored in oldContext */
 
 
@@ -665,7 +655,7 @@ function Smalltalk(){
 
 
 	function pushContext(receiver, selector, method, temps) {
 	function pushContext(receiver, selector, method, temps) {
 		var c = st.oldContext, tc = st.thisContext;
 		var c = st.oldContext, tc = st.thisContext;
-		if (!c) {
+		if(!c) {
 			return st.thisContext = new SmalltalkMethodContext(receiver, selector, method, temps, tc);
 			return st.thisContext = new SmalltalkMethodContext(receiver, selector, method, temps, tc);
 		}
 		}
 		st.oldContext = null;
 		st.oldContext = null;
@@ -676,15 +666,15 @@ function Smalltalk(){
 		c.method      = method;
 		c.method      = method;
 		c.temps       = temps || {};
 		c.temps       = temps || {};
 		return st.thisContext = c;
 		return st.thisContext = c;
-	};
+	}
 
 
 	function popContext(context) {
 	function popContext(context) {
 		st.thisContext = context.homeContext;
 		st.thisContext = context.homeContext;
 		context.homeContext = undefined;
 		context.homeContext = undefined;
 		st.oldContext = context;
 		st.oldContext = context;
-	};
+	}
 
 
-    /* Convert a Smalltalk selector into a JS selector */
+	/* Convert a Smalltalk selector into a JS selector */
 
 
     st.selector = function(string) {
     st.selector = function(string) {
         var selector = '_' + string;
         var selector = '_' + string;
@@ -719,7 +709,7 @@ function Smalltalk(){
 
 
 	function convertKeywordSelector(selector) {
 	function convertKeywordSelector(selector) {
 		return selector.replace(/^_/, '').replace(/_/g, ':');
 		return selector.replace(/^_/, '').replace(/_/g, ':');
-	};
+	}
 
 
 	function convertBinarySelector(selector) {
 	function convertBinarySelector(selector) {
 		return selector
 		return selector
@@ -733,7 +723,7 @@ function Smalltalk(){
 			.replace(/_eq/, '=')
 			.replace(/_eq/, '=')
 			.replace(/_comma/, ',')
 			.replace(/_comma/, ',')
 			.replace(/_at/, '@')
 			.replace(/_at/, '@')
-	};
+	}
 
 
 	/* Converts a JavaScript object to valid Smalltalk Object */
 	/* Converts a JavaScript object to valid Smalltalk Object */
 	st.readJSObject = function(js) {
 	st.readJSObject = function(js) {
@@ -758,19 +748,19 @@ function Smalltalk(){
     /* Boolean assertion */
     /* Boolean assertion */
 
 
     st.assert = function(boolean) {
     st.assert = function(boolean) {
-        if(boolean.klass === smalltalk.Boolean) {
-            return boolean;
-        } else {
-            smalltalk.NonBooleanReceiver._new()._object_(boolean)._signal();
-        }
-    }
+		if(boolean.klass === smalltalk.Boolean) {
+			return boolean;
+		} else {
+			smalltalk.NonBooleanReceiver._new()._object_(boolean)._signal();
+		}
+	};
 
 
     /* Smalltalk initilization. Called on page load */
     /* Smalltalk initilization. Called on page load */
 
 
     st.initialize = function() {
     st.initialize = function() {
-        if(initialized) {return false};
+		if(initialized) {return false}
 
 
-        classes.forEach(function(klass) {
+		classes.forEach(function(klass) {
             st.init(klass);
             st.init(klass);
         });
         });
         classes.forEach(function(klass) {
         classes.forEach(function(klass) {
@@ -779,10 +769,9 @@ function Smalltalk(){
 
 
         initialized = true;
         initialized = true;
     };
     };
-};
+}
 
 
-Smalltalk.prototype = new SmalltalkObject();
-Smalltalk.prototype.constructor = Smalltalk;
+inherits(Smalltalk, SmalltalkObject);
 
 
 function SmalltalkMethodContext(receiver, selector, method, temps, home) {
 function SmalltalkMethodContext(receiver, selector, method, temps, home) {
 	this.receiver    = receiver;
 	this.receiver    = receiver;
@@ -796,10 +785,9 @@ function SmalltalkMethodContext(receiver, selector, method, temps, home) {
         smalltalk.thisContext = this;
         smalltalk.thisContext = this;
         return this.method.apply(receiver, temps);
         return this.method.apply(receiver, temps);
     };
     };
-};
+}
 
 
-SmalltalkMethodContext.prototype = new SmalltalkObject();
-SmalltalkMethodContext.prototype.constructor = SmalltalkMethodContext;
+inherits(SmalltalkMethodContext, SmalltalkObject);
 
 
 SmalltalkMethodContext.prototype.copy = function() {
 SmalltalkMethodContext.prototype.copy = function() {
 	var home = this.homeContext;
 	var home = this.homeContext;
@@ -828,9 +816,9 @@ var smalltalk = new Smalltalk();
  */
  */
 
 
 var _st = function(o) {
 var _st = function(o) {
-    if(typeof o === 'undefined') {return nil};
-    if(o.klass) {return o};
-    return smalltalk.JSObjectProxy._on_(o);
+	if(typeof o === 'undefined') {return nil}
+	if(o.klass) {return o}
+	return smalltalk.JSObjectProxy._on_(o);
 }; 
 };