Browse Source

kernel: Proper enumerability (yes for data, no for methods).

Herbert Vojčík 7 years ago
parent
commit
95ee6c6194
3 changed files with 53 additions and 49 deletions
  1. 1 4
      support/kernel-fundamentals.js
  2. 35 34
      support/kernel-language.js
  3. 17 11
      support/kernel-runtime.js

+ 1 - 4
support/kernel-fundamentals.js

@@ -214,10 +214,7 @@ define(['./compatibility'], function () {
 
         this.setupBehavior = function (behaviorBody) {
             setupClassOrganization(behaviorBody);
-            Object.defineProperty(behaviorBody, "methods", {
-                value: Object.create(null),
-                enumerable: false, configurable: true, writable: true
-            });
+            behaviorBody.methods = Object.create(null);
         };
 
         this.buildBehaviorBody = function (pkgName, builder) {

+ 35 - 34
support/kernel-language.js

@@ -52,6 +52,14 @@ define(['./compatibility'], function () {
         return child;
     }
 
+    function defineMethod (klass, name, method) {
+        Object.defineProperty(klass.prototype, name, {
+            value: method,
+            enumerable: false, configurable: true, writable: true
+        });
+        return defineMethod;
+    }
+
     TraitsBrik.deps = ["behaviors", "root"];
     function TraitsBrik (brikz, st) {
         var coreFns = brikz.root.coreFns;
@@ -64,27 +72,24 @@ define(['./compatibility'], function () {
 
         coreFns.Trait = inherits(SmalltalkTrait, SmalltalkBehaviorBody);
 
-        SmalltalkTrait.prototype.toString = function () {
-            return 'Smalltalk Trait ' + this.className;
-        };
-
         SmalltalkTrait.prototype.trait = true;
 
-        SmalltalkTrait.prototype.added = function () {
+        defineMethod
+        (SmalltalkTrait, "toString", function () {
+            return 'Smalltalk Trait ' + this.className;
+        })
+        (SmalltalkTrait, "added", function () {
             if (st._traitAdded) st._traitAdded(this);
-        };
-
-        SmalltalkTrait.prototype.removed = function () {
+        })
+        (SmalltalkTrait, "removed", function () {
             if (st._traitRemoved) st._traitRemoved(this);
-        };
-
-        SmalltalkTrait.prototype.methodAdded = function (method) {
+        })
+        (SmalltalkTrait, "methodAdded", function (method) {
             if (st._traitMethodAdded) st._traitMethodAdded(method, this);
-        };
-
-        SmalltalkTrait.prototype.methodRemoved = function (method) {
+        })
+        (SmalltalkTrait, "methodRemoved", function (method) {
             if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
-        };
+        });
 
         function traitBuilder (className) {
             return {
@@ -140,33 +145,29 @@ define(['./compatibility'], function () {
         // Effective superclass of all classes created with `nil subclass: ...`.
         var nilAsClass = this.nilAsClass = {fn: SmalltalkRoot, klass: {fn: SmalltalkClass}};
 
-        SmalltalkClass.prototype.toString = function () {
-            return 'Smalltalk ' + this.className;
-        };
-
-        SmalltalkMetaclass.prototype.toString = function () {
-            return 'Smalltalk Metaclass ' + this.instanceClass.className;
-        };
-
         SmalltalkMetaclass.prototype.meta = true;
 
-        SmalltalkClass.prototype.added = function () {
+        defineMethod
+        (SmalltalkClass, "toString", function () {
+            return 'Smalltalk ' + this.className;
+        })
+        (SmalltalkMetaclass, "toString", function () {
+            return 'Smalltalk Metaclass ' + this.instanceClass.className;
+        })
+        (SmalltalkClass, "added", function () {
             addSubclass(this);
             if (st._classAdded) st._classAdded(this);
-        };
-
-        SmalltalkClass.prototype.removed = function () {
+        })
+        (SmalltalkClass, "removed", function () {
             if (st._classRemoved) st._classRemoved(this);
             removeSubclass(this);
-        };
-
-        SmalltalkBehavior.prototype.methodAdded = function (method) {
+        })
+        (SmalltalkBehavior, "methodAdded", function (method) {
             if (st._methodAdded) st._methodAdded(method, this);
-        };
-
-        SmalltalkBehavior.prototype.methodRemoved = function (method) {
+        })
+        (SmalltalkBehavior, "methodRemoved", function (method) {
             if (st._methodRemoved) st._methodRemoved(method, this);
-        };
+        });
 
         this.bootstrapHierarchy = function () {
             var nilSubclasses = [globals.ProtoObject];

+ 17 - 11
support/kernel-runtime.js

@@ -3,6 +3,14 @@
 define(function () {
     "use strict";
 
+    function defineMethod(klass, name, method) {
+        Object.defineProperty(klass.prototype, name, {
+            value: method,
+            enumerable: false, configurable: true, writable: true
+        });
+        return defineMethod;
+    }
+
     DNUBrik.deps = ["selectors", "messageSend", "manipulation", "classes"];
     function DNUBrik(brikz, st) {
         var selectorsBrik = brikz.selectors;
@@ -268,7 +276,8 @@ define(function () {
         SmalltalkMethodContext.prototype.selector = null;
         SmalltalkMethodContext.prototype.lookupClass = null;
 
-        SmalltalkMethodContext.prototype.fill = function (receiver, selector, locals, lookupClass) {
+        defineMethod
+        (SmalltalkMethodContext, "fill", function (receiver, selector, locals, lookupClass) {
             this.receiver = receiver;
             this.selector = selector;
             this.locals = locals || {};
@@ -276,24 +285,21 @@ define(function () {
             if (this.homeContext) {
                 this.homeContext.evaluatedSelector = selector;
             }
-        };
-
-        SmalltalkMethodContext.prototype.fillBlock = function (locals, ctx, index) {
+        })
+        (SmalltalkMethodContext, "fillBlock", function (locals, ctx, index) {
             this.locals = locals || {};
             this.outerContext = ctx;
             this.index = index || 0;
-        };
-
-        SmalltalkMethodContext.prototype.init = function () {
+        })
+        (SmalltalkMethodContext, "init", function () {
             var home = this.homeContext;
             if (home) {
                 home.init();
             }
 
             this.setup(this);
-        };
-
-        SmalltalkMethodContext.prototype.method = function () {
+        })
+        (SmalltalkMethodContext, "method", function () {
             var method;
             var lookup = this.lookupClass || this.receiver.klass;
             while (!method && lookup) {
@@ -301,7 +307,7 @@ define(function () {
                 lookup = lookup.superclass;
             }
             return method;
-        };
+        });
 
         setClassConstructor(globals.MethodContext, SmalltalkMethodContext);