ソースを参照

Merge pull request #609 from herby/boot-js-refactorings

Boot js refactorings
Nicolas Petton 10 年 前
コミット
8179ccf60b
1 ファイル変更31 行追加36 行削除
  1. 31 36
      support/boot.js

+ 31 - 36
support/boot.js

@@ -181,7 +181,7 @@ function DNUBrik(brikz, st) {
 		this.selectors.push(string);
 		var selector = st.selector(string);
 		checker[selector] = true;
-		var method = {jsSelector: selector, fn: this.createHandler(selector)};
+		var method = {jsSelector: selector, fn: createHandler(selector)};
 		methods.push(method);
 		return method;
 	};
@@ -192,14 +192,12 @@ function DNUBrik(brikz, st) {
 
 	/* Dnu handler method */
 
-	this.createHandler = function (selector) {
-		var handler = function() {
+	function createHandler(selector) {
+		return function() {
 			var args = Array.prototype.slice.call(arguments);
 			return brikz.messageSend.messageNotUnderstood(this, selector, args);
 		};
-
-		return handler;
-	};
+	}
 
 	this.installHandlers = function (klass) {
 		for(var i=0; i<methods.length; i++) {
@@ -235,7 +233,7 @@ function ClassInitBrik(brikz, st) {
 	};
 
 	function copySuperclass(klass, superclass) {
-		var inheritedMethods = {};
+		var inheritedMethods = Object.create(null);
 		deinstallAllMethods(klass);
 		for (superclass = superclass || klass.superclass;
 			superclass && superclass !== nil;
@@ -250,8 +248,7 @@ function ClassInitBrik(brikz, st) {
 			var selector = method.selector;
 
 			//TODO: prepare klass methods into inheritedMethods to only test once
-			//TODO: Object.create(null) to ditch hasOwnProperty call (very slow)
-			if(klass.methods.hasOwnProperty(selector) || inheritedMethods.hasOwnProperty(selector)) {
+			if(klass.methods[selector] || inheritedMethods[selector]) {
 				return;
 			}
 
@@ -288,13 +285,6 @@ function ManipulationBrik(brikz, st) {
 	}
 	this.installMethod = installMethod;
 
-	this.wireKlass = function (klass) {
-		Object.defineProperty(klass.fn.prototype, "klass", {
-			value: klass,
-			enumerable: false, configurable: true, writable: true
-		});
-	};
-
 	this.reinstallMethods = function (klass) {
 		var methods = klass.methods;
 		for(var keys = Object.keys(methods), i=0; i<keys.length; i++) {
@@ -306,21 +296,20 @@ function ManipulationBrik(brikz, st) {
 function ClassesBrik(brikz, st) {
 
 	var org = brikz.ensure("organize");
-	var manip = brikz.ensure("manipulation");
 	var nil = brikz.ensure("root").nil;
 
 	function SmalltalkPackage() {}
 	function SmalltalkBehavior() {}
 	function SmalltalkClass() {}
-	function SmalltalkMetaclass() {
-		this.meta = true;
-	}
+	function SmalltalkMetaclass() {}
 
 	inherits(SmalltalkPackage, SmalltalkObject);
 	inherits(SmalltalkBehavior, SmalltalkObject);
 	inherits(SmalltalkClass, SmalltalkBehavior);
 	inherits(SmalltalkMetaclass, SmalltalkBehavior);
 
+	SmalltalkMetaclass.prototype.meta = true;
+
 	this.__init__ = function () {
 		st.addPackage("Kernel-Classes");
 		st.wrapClassName("Behavior", "Kernel-Classes", SmalltalkBehavior, st.Object, false);
@@ -393,10 +382,13 @@ function ClassesBrik(brikz, st) {
 
 		org.setupClassOrganization(klass);
 		Object.defineProperty(klass, "methods", {
-			value: {},
+			value: Object.create(null),
+			enumerable: false, configurable: true, writable: true
+		});
+		Object.defineProperty(klass.fn.prototype, "klass", {
+			value: klass,
 			enumerable: false, configurable: true, writable: true
 		});
-		manip.wireKlass(klass);
 	}
 
 	/* Add a package to the smalltalk.packages object, creating a new one if needed.
@@ -870,20 +862,9 @@ function RuntimeBrik(brikz, st) {
 			try {
 				return inContext(worker, setup);
 			} catch(error) {
-				if(error.smalltalkError) {
-					handleError(error);
-				} else {
-					var errorWrapper = st.JavaScriptException._on_(error);
-					try {errorWrapper._signal();} catch(ex) {}
-					errorWrapper._context_(st.getThisContext());
-					handleError(errorWrapper);
-				}
-				// Reset the context stack in any case
-				st.thisContext = undefined;
-				// Throw the exception anyway, as we want to stop
-				// the execution to avoid infinite loops
-				// Update: do not throw the exception. It's really annoying.
-				// throw error;
+				handleError(error);
+			} finally {
+				st.thisContext = null;
 			}
 		}
 	};
@@ -895,11 +876,25 @@ function RuntimeBrik(brikz, st) {
 		return result;
 	}
 
+	function wrappedError(error) {
+		var errorWrapper = st.JavaScriptException._on_(error);
+		try { errorWrapper._signal(); } catch (ex) {}
+		errorWrapper._context_(st.getThisContext());
+		return errorWrapper;
+	}
+
 	/* Handles Smalltalk errors. Triggers the registered ErrorHandler
 		(See the Smalltalk class ErrorHandler and its subclasses */
 
 	function handleError(error) {
+		if (!error.smalltalkError) {
+			error = wrappedError(error);
+		}
 		st.ErrorHandler._current()._handleError_(error);
+		// Throw the exception anyway, as we want to stop
+		// the execution to avoid infinite loops
+		// Update: do not throw the exception. It's really annoying.
+		// throw error;
 	}
 
 	/* Handle thisContext pseudo variable */