소스 검색

kernel: code cleaning

Inlined internal methods .init (for MethodContext)
and .addUser / .removeUser (for Trait) and some more.
Herbert Vojčík 7 년 전
부모
커밋
f28999b085
2개의 변경된 파일20개의 추가작업 그리고 38개의 파일을 삭제
  1. 6 12
      support/kernel-language.js
  2. 14 26
      support/kernel-runtime.js

+ 6 - 12
support/kernel-language.js

@@ -59,15 +59,13 @@ define(['./compatibility' /* TODO remove */], function () {
         });
     }
 
-    TraitsBrik.deps = ["behaviors", "methods", "composition", "arraySet", "root"];
+    TraitsBrik.deps = ["behaviors", "methods", "composition", "root"];
     function TraitsBrik (brikz, st) {
         var coreFns = brikz.root.coreFns;
         var SmalltalkObject = brikz.root.Object;
         var setupMethods = brikz.methods.setupMethods;
         var traitMethodChanged = brikz.composition.traitMethodChanged;
         var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
-        var addElement = brikz.arraySet.addElement;
-        var removeElement = brikz.arraySet.removeElement;
 
         function SmalltalkTrait () {
         }
@@ -99,12 +97,6 @@ define(['./compatibility' /* TODO remove */], function () {
             });
             if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
         });
-        defineMethod(SmalltalkTrait, "addUser", function (traitOrBehavior) {
-            addElement(this.traitUsers, traitOrBehavior);
-        });
-        defineMethod(SmalltalkTrait, "removeUser", function (traitOrBehavior) {
-            removeElement(this.traitUsers, traitOrBehavior);
-        });
 
         function traitBuilder (className) {
             return {
@@ -126,9 +118,11 @@ define(['./compatibility' /* TODO remove */], function () {
         };
     }
 
-    MethodCompositionBrik.deps = ["methods"];
+    MethodCompositionBrik.deps = ["methods", "arraySet"];
     function MethodCompositionBrik (brikz, st) {
         var updateMethod = brikz.methods.updateMethod;
+        var addElement = brikz.arraySet.addElement;
+        var removeElement = brikz.arraySet.removeElement;
 
         function aliased (selector, method) {
             if (method.selector === selector) return method;
@@ -193,11 +187,11 @@ define(['./compatibility' /* TODO remove */], function () {
                 updateMethod(selector, traitOrBehavior);
             }
             (traitOrBehavior.traitComposition || []).forEach(function (each) {
-                each.trait.removeUser(traitOrBehavior);
+                removeElement(each.trait.traitUsers, traitOrBehavior);
             });
             traitOrBehavior.traitComposition = traitComposition && traitComposition.length ? traitComposition : null;
             (traitOrBehavior.traitComposition || []).forEach(function (each) {
-                each.trait.addUser(traitOrBehavior);
+                addElement(each.trait.traitUsers, traitOrBehavior);
             });
         };
 

+ 14 - 26
support/kernel-runtime.js

@@ -199,18 +199,13 @@ define(function () {
             var selector = method.selector;
             var jsSelector = method.jsSelector;
             st.traverseClassTree(klass, function (subclass, sentinel) {
-                if (subclass !== exclude) {
-                    if (initMethodInClass(subclass, selector, jsSelector)) return sentinel;
+                if (subclass === exclude) return;
+                if (subclass.methods[selector]) return sentinel;
+                if (subclass.detachedRoot) {
+                    installJSMethod(subclass.fn.prototype, jsSelector, subclass.superclass.fn.prototype[jsSelector]);
                 }
             });
         }
-
-        function initMethodInClass (klass, selector, jsSelector) {
-            if (klass.methods[selector]) return true;
-            if (klass.detachedRoot) {
-                installJSMethod(klass.fn.prototype, jsSelector, klass.superclass.fn.prototype[jsSelector]);
-            }
-        }
     }
 
     PrimitivesBrik.deps = ["smalltalkGlobals"];
@@ -288,14 +283,6 @@ define(function () {
             this.outerContext = ctx;
             if (index) this.index = index;
         });
-        defineMethod(SmalltalkMethodContext, "init", function () {
-            var frame = this;
-            while (frame) {
-                if (frame.init !== this.init) return frame.init();
-                frame.setup(frame);
-                frame = frame.homeContext;
-            }
-        });
         defineMethod(SmalltalkMethodContext, "method", function () {
             var method;
             var lookup = this.lookupClass || this.receiver.a$cls;
@@ -315,8 +302,6 @@ define(function () {
 
         var thisContext = null;
 
-        st.withContext = inContext;
-
         /*
          Runs worker function so that error handler is not set up
          if there isn't one. This is accomplished by unconditional
@@ -346,23 +331,26 @@ define(function () {
             }
         }
 
-        function inContext (worker, setup) {
+        /*
+         Standard way to run within context.
+         Sets up error handler if entering first ST context in a stack.
+         */
+        st.withContext = function (worker, setup) {
             var oldContext = thisContext;
             thisContext = new SmalltalkMethodContext(thisContext, setup);
             var result = oldContext == null ? resultWithErrorHandling(worker) : worker(thisContext);
             thisContext = oldContext;
             return result;
-        }
+        };
 
         /* Handle thisContext pseudo variable */
 
         st.getThisContext = function () {
-            if (thisContext) {
-                thisContext.init();
-                return thisContext;
-            } else {
-                return null;
+            if (!thisContext) return null;
+            for (var frame = thisContext; frame; frame = frame.homeContext) {
+                frame.setup(frame);
             }
+            return thisContext;
         };
     }