Browse Source

kernel: declareJsMethod.

Implement installMethodOfJsClass
as well as declareEvent using it.
Herby Vojčík 4 years ago
parent
commit
01ced237f3

+ 4 - 8
lang/base/kernel-fundamentals.js

@@ -4,6 +4,7 @@ define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
     var inherits = $goodies.inherits;
+    var declareJsMethod = $goodies.declareJsMethod;
     var addElement = $goodies.addElement;
     var removeElement = $goodies.removeElement;
 
@@ -35,11 +36,8 @@ define(['./kernel-goodies'], function ($goodies) {
     function FundamentalsFactory (globals, emit) {
         var specialConstructors = Object.create(null);
 
-        function EventBrik (brikz, st) {
-            this.declareEvent = function (event) {
-                if (!emit[event]) emit[event] = function () {
-                };
-            }
+        function declareEvent (name) {
+            declareJsMethod(emit, name);
         }
 
         function RootBrik (brikz, st) {
@@ -102,12 +100,11 @@ define(['./kernel-goodies'], function ($goodies) {
             st.traitsOrClasses = this.traitsOrClasses = traitsOrClasses;
         }
 
-        MethodsBrik.deps = ["event", "selectors", "root"];
+        MethodsBrik.deps = ["selectors", "root"];
 
         function MethodsBrik (brikz, st) {
             var registerSelector = brikz.selectors.registerSelector;
             var SmalltalkObject = brikz.root.Object;
-            var declareEvent = brikz.event.declareEvent;
 
             function SmalltalkMethod () {
             }
@@ -230,7 +227,6 @@ define(['./kernel-goodies'], function ($goodies) {
         function configure (brikz) {
             brikz.root = RootBrik;
             brikz.nil = NilBrik;
-            brikz.event = EventBrik;
             brikz.selectors = SelectorsBrik;
             brikz.packages = PackagesBrik;
             brikz.behaviors = BehaviorsBrik;

+ 7 - 5
lang/base/kernel-goodies.js

@@ -13,16 +13,18 @@ define(function () {
         return child;
     }
 
-    function installMethodOfJsClass (klass, name, fn) {
-        Object.defineProperty(klass.prototype, name, {
+    function installMethodOfJsObject (obj, name, fn) {
+        Object.defineProperty(obj, name, {
             value: fn,
             enumerable: false, configurable: true, writable: true
         });
     }
 
-    function installMethodOfJsObject (obj, name, fn) {
+    function declareJsMethod (obj, name) {
+        if (obj[name] != null) return;
         Object.defineProperty(obj, name, {
-            value: fn,
+            value: function () {
+            },
             enumerable: false, configurable: true, writable: true
         });
     }
@@ -99,8 +101,8 @@ define(function () {
         addElement: addElement,
         js2st: js2st,
         st2js: st2js,
+        declareJsMethod: declareJsMethod,
         installMethodOfJsObject: installMethodOfJsObject,
-        installMethodOfJsClass: installMethodOfJsClass,
         inherits: inherits
     }
 });

+ 50 - 28
lang/base/kernel-language.js

@@ -4,7 +4,7 @@ define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
     var inherits = $goodies.inherits;
-    var installMethodOfJsClass = $goodies.installMethodOfJsClass;
+    var declareJsMethod = $goodies.declareJsMethod;
     var addElement = $goodies.addElement;
     var removeElement = $goodies.removeElement;
 
@@ -128,14 +128,17 @@ define(['./kernel-goodies'], function ($goodies) {
     }
 
     function LanguageFactory (specialConstructors, emit) {
-        TraitsBrik.deps = ["event", "behaviors", "methods", "composition", "root"];
+        function declareEvent (name) {
+            declareJsMethod(emit, name);
+        }
+
+        TraitsBrik.deps = ["behaviors", "methods", "composition", "root"];
 
         function TraitsBrik (brikz, st) {
             var SmalltalkObject = brikz.root.Object;
             var setupMethods = brikz.methods.setupMethods;
             var traitMethodChanged = brikz.composition.traitMethodChanged;
             var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
-            var declareEvent = brikz.event.declareEvent;
 
             function SmalltalkTrait () {
             }
@@ -143,34 +146,43 @@ define(['./kernel-goodies'], function ($goodies) {
             specialConstructors.Trait = inherits(SmalltalkTrait, SmalltalkObject);
 
             SmalltalkTrait.prototype.trait = true;
+            declareJsMethod(SmalltalkTrait.prototype, "toString");
+            declareJsMethod(SmalltalkTrait.prototype, "added");
+            declareJsMethod(SmalltalkTrait.prototype, "removed");
+            declareJsMethod(SmalltalkTrait.prototype, "methodAdded");
+            declareJsMethod(SmalltalkTrait.prototype, "methodRemoved");
 
-            installMethodOfJsClass(SmalltalkTrait, "toString", function () {
+            SmalltalkTrait.prototype.toString = function () {
                 return 'Smalltalk Trait ' + this.name;
-            });
+            };
+
             declareEvent("traitAdded");
-            installMethodOfJsClass(SmalltalkTrait, "added", function () {
+            SmalltalkTrait.prototype.added = function () {
                 emit.traitAdded(this);
-            });
+            };
+
             declareEvent("traitRemoved");
-            installMethodOfJsClass(SmalltalkTrait, "removed", function () {
+            SmalltalkTrait.prototype.removed = function () {
                 emit.traitRemoved(this);
-            });
+            };
+
             declareEvent("traitMethodAdded");
-            installMethodOfJsClass(SmalltalkTrait, "methodAdded", function (method) {
+            SmalltalkTrait.prototype.methodAdded = function (method) {
                 var self = this;
                 this.traitUsers.forEach(function (each) {
                     traitMethodChanged(method.selector, method, self, each);
                 });
                 emit.traitMethodAdded(method, this);
-            });
+            };
+
             declareEvent("traitMethodRemoved");
-            installMethodOfJsClass(SmalltalkTrait, "methodRemoved", function (method) {
+            SmalltalkTrait.prototype.methodRemoved = function (method) {
                 var self = this;
                 this.traitUsers.forEach(function (each) {
                     traitMethodChanged(method.selector, null, self, each);
                 });
                 emit.traitMethodRemoved(method, this);
-            });
+            };
 
             function traitBuilder (traitName) {
                 return {
@@ -192,7 +204,7 @@ define(['./kernel-goodies'], function ($goodies) {
             };
         }
 
-        ClassesBrik.deps = ["root", "event", "behaviors", "methods", "nil"];
+        ClassesBrik.deps = ["root", "behaviors", "methods", "nil"];
 
         function ClassesBrik (brikz, st) {
             var SmalltalkRoot = brikz.root.Root;
@@ -200,7 +212,6 @@ define(['./kernel-goodies'], function ($goodies) {
             var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
             var setupMethods = brikz.methods.setupMethods;
             var removeTraitOrClass = brikz.behaviors.removeTraitOrClass;
-            var declareEvent = brikz.event.declareEvent;
             var nilAsReceiver = brikz.nil.nilAsReceiver;
 
             function SmalltalkBehavior () {
@@ -225,31 +236,42 @@ define(['./kernel-goodies'], function ($goodies) {
             };
 
             SmalltalkMetaclass.prototype.meta = true;
-
-            installMethodOfJsClass(SmalltalkClass, "toString", function () {
+            declareJsMethod(SmalltalkClass.prototype, "toString");
+            declareJsMethod(SmalltalkMetaclass.prototype, "toString");
+            declareJsMethod(SmalltalkClass.prototype, "added");
+            declareJsMethod(SmalltalkClass.prototype, "removed");
+            declareJsMethod(SmalltalkBehavior.prototype, "methodAdded");
+            declareJsMethod(SmalltalkBehavior.prototype, "methodRemoved");
+
+            SmalltalkClass.prototype.toString = function () {
                 return 'Smalltalk ' + this.name;
-            });
-            installMethodOfJsClass(SmalltalkMetaclass, "toString", function () {
+            };
+
+            SmalltalkMetaclass.prototype.toString = function () {
                 return 'Smalltalk Metaclass ' + this.instanceClass.name;
-            });
+            };
+
             declareEvent("classAdded");
-            installMethodOfJsClass(SmalltalkClass, "added", function () {
+            SmalltalkClass.prototype.added = function () {
                 addSubclass(this);
                 emit.classAdded(this);
-            });
+            };
+
             declareEvent("classRemoved");
-            installMethodOfJsClass(SmalltalkClass, "removed", function () {
+            SmalltalkClass.prototype.removed = function () {
                 emit.classRemoved(this);
                 removeSubclass(this);
-            });
+            };
+
             declareEvent("behaviorMethodAdded");
-            installMethodOfJsClass(SmalltalkBehavior, "methodAdded", function (method) {
+            SmalltalkBehavior.prototype.methodAdded = function (method) {
                 emit.behaviorMethodAdded(method, this);
-            });
+            };
+
             declareEvent("behaviorMethodRemove");
-            installMethodOfJsClass(SmalltalkBehavior, "methodRemoved", function (method) {
+            SmalltalkBehavior.prototype.methodRemoved = function (method) {
                 emit.behaviorMethodRemoved(method, this);
-            });
+            };
 
             // TODO remove, ["@foo"] backward compatibility
             function installIvarCompat (klass) {

+ 8 - 5
lang/base/kernel-runtime.js

@@ -3,8 +3,8 @@
 define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
-    var installMethodOfJsClass = $goodies.installMethodOfJsClass;
     var installMethodOfJsObject = $goodies.installMethodOfJsObject;
+    var declareJsMethod = $goodies.declareJsMethod;
     var st2js = $goodies.st2js;
     var js2st = $goodies.js2st;
 
@@ -292,20 +292,23 @@ define(['./kernel-goodies'], function ($goodies) {
             SmalltalkMethodContext.prototype.selector = null;
             SmalltalkMethodContext.prototype.outerContext = null;
             SmalltalkMethodContext.prototype.index = 0;
+            declareJsMethod(SmalltalkMethodContext.prototype, "fill");
+            declareJsMethod(SmalltalkMethodContext.prototype, "fillBlock");
 
-            installMethodOfJsClass(SmalltalkMethodContext, "fill", function (receiver, selector, locals) {
+            SmalltalkMethodContext.prototype.fill = function (receiver, selector, locals) {
                 this.receiver = receiver;
                 this.selector = selector;
                 if (locals != null) this.locals = locals;
                 if (this.homeContext) {
                     this.homeContext.evaluatedSelector = selector;
                 }
-            });
-            installMethodOfJsClass(SmalltalkMethodContext, "fillBlock", function (locals, ctx, index) {
+            };
+
+            SmalltalkMethodContext.prototype.fillBlock = function (locals, ctx, index) {
                 if (locals != null) this.locals = locals;
                 this.outerContext = ctx;
                 if (index) this.index = index;
-            });
+            };
 
             setClassConstructor(globals.MethodContext, SmalltalkMethodContext);