Browse Source

kernel: Extract kernel-goodies.

Herby Vojčík 4 years ago
parent
commit
6a6a438e28

+ 2 - 10
lang/base/kernel-fundamentals.js

@@ -1,17 +1,9 @@
 //jshint eqnull:true
 
-define(function () {
+define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
-    function inherits (child, parent) {
-        child.prototype = Object.create(parent.prototype, {
-            constructor: {
-                value: child,
-                enumerable: false, configurable: true, writable: true
-            }
-        });
-        return child;
-    }
+    var inherits = $goodies.inherits;
 
     function SelectorsBrik (brikz, st) {
         var selectorSet = Object.create(null);

+ 88 - 0
lang/base/kernel-goodies.js

@@ -0,0 +1,88 @@
+//jshint eqnull:true
+
+define(function () {
+    "use strict";
+
+    function inherits (child, parent) {
+        child.prototype = Object.create(parent.prototype, {
+            constructor: {
+                value: child,
+                enumerable: false, configurable: true, writable: true
+            }
+        });
+        return child;
+    }
+
+    function defineMethod (klass, name, method) {
+        Object.defineProperty(klass.prototype, name, {
+            value: method,
+            enumerable: false, configurable: true, writable: true
+        });
+    }
+
+    function installJSMethod (obj, jsSelector, fn) {
+        Object.defineProperty(obj, jsSelector, {
+            value: fn,
+            enumerable: false, configurable: true, writable: true
+        });
+    }
+
+    /* Convert a Smalltalk selector into a JS selector */
+    function st2js (string) {
+        return '_' + string
+            .replace(/:/g, '_')
+            .replace(/[\&]/g, '_and')
+            .replace(/[\|]/g, '_or')
+            .replace(/[+]/g, '_plus')
+            .replace(/-/g, '_minus')
+            .replace(/[*]/g, '_star')
+            .replace(/[\/]/g, '_slash')
+            .replace(/[\\]/g, '_backslash')
+            .replace(/[\~]/g, '_tild')
+            .replace(/%/g, '_percent')
+            .replace(/>/g, '_gt')
+            .replace(/</g, '_lt')
+            .replace(/=/g, '_eq')
+            .replace(/,/g, '_comma')
+            .replace(/[@]/g, '_at');
+    };
+
+    function js2st (selector) {
+        if (selector.match(/^__/)) {
+            return binaryJsToSt(selector);
+        } else {
+            return keywordJsToSt(selector);
+        }
+    }
+
+    function keywordJsToSt (selector) {
+        return selector.replace(/^_/, '').replace(/_/g, ':');
+    }
+
+    function binaryJsToSt (selector) {
+        return selector
+            .replace(/^_/, '')
+            .replace(/_and/g, '&')
+            .replace(/_or/g, '|')
+            .replace(/_plus/g, '+')
+            .replace(/_minus/g, '-')
+            .replace(/_star/g, '*')
+            .replace(/_slash/g, '/')
+            .replace(/_backslash/g, '\\')
+            .replace(/_tild/g, '~')
+            .replace(/_percent/g, '%')
+            .replace(/_gt/g, '>')
+            .replace(/_lt/g, '<')
+            .replace(/_eq/g, '=')
+            .replace(/_comma/g, ',')
+            .replace(/_at/g, '@');
+    }
+
+    return {
+        js2st: js2st,
+        st2js: st2js,
+        installJSMethod: installJSMethod,
+        defineMethod: defineMethod,
+        inherits: inherits
+    }
+});

+ 3 - 17
lang/base/kernel-language.js

@@ -1,24 +1,10 @@
 //jshint eqnull:true
 
-define(function () {
+define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
-    function inherits (child, parent) {
-        child.prototype = Object.create(parent.prototype, {
-            constructor: {
-                value: child,
-                enumerable: false, configurable: true, writable: true
-            }
-        });
-        return child;
-    }
-
-    function defineMethod (klass, name, method) {
-        Object.defineProperty(klass.prototype, name, {
-            value: method,
-            enumerable: false, configurable: true, writable: true
-        });
-    }
+    var inherits = $goodies.inherits;
+    var defineMethod = $goodies.defineMethod;
 
     MethodCompositionBrik.deps = ["methods", "arraySet"];
 

+ 6 - 64
lang/base/kernel-runtime.js

@@ -1,45 +1,16 @@
 //jshint eqnull:true
 
-define(function () {
+define(['./kernel-goodies'], function ($goodies) {
     "use strict";
 
-    function defineMethod (klass, name, method) {
-        Object.defineProperty(klass.prototype, name, {
-            value: method,
-            enumerable: false, configurable: true, writable: true
-        });
-    }
-
-    function installJSMethod (obj, jsSelector, fn) {
-        Object.defineProperty(obj, jsSelector, {
-            value: fn,
-            enumerable: false, configurable: true, writable: true
-        });
-    }
+    var defineMethod = $goodies.defineMethod;
+    var installJSMethod = $goodies.installJSMethod;
+    var st2js = $goodies.st2js;
+    var js2st = $goodies.js2st;
 
     function SelectorConversionBrik (brikz, st) {
         var st2jsMemo = Object.create(null);
 
-        /* Convert a Smalltalk selector into a JS selector */
-        function st2js (string) {
-            return '_' + string
-                .replace(/:/g, '_')
-                .replace(/[\&]/g, '_and')
-                .replace(/[\|]/g, '_or')
-                .replace(/[+]/g, '_plus')
-                .replace(/-/g, '_minus')
-                .replace(/[*]/g, '_star')
-                .replace(/[\/]/g, '_slash')
-                .replace(/[\\]/g, '_backslash')
-                .replace(/[\~]/g, '_tild')
-                .replace(/%/g, '_percent')
-                .replace(/>/g, '_gt')
-                .replace(/</g, '_lt')
-                .replace(/=/g, '_eq')
-                .replace(/,/g, '_comma')
-                .replace(/[@]/g, '_at');
-        };
-
         st.st2js = function (stSelector) {
             return st2jsMemo[stSelector] || st2js(stSelector);
         };
@@ -51,36 +22,7 @@ define(function () {
         /* Convert a string to a valid smalltalk selector.
          if you modify the following functions, also change st2js
          accordingly */
-        st.js2st = function (selector) {
-            if (selector.match(/^__/)) {
-                return binaryJsToSt(selector);
-            } else {
-                return keywordJsToSt(selector);
-            }
-        };
-
-        function keywordJsToSt (selector) {
-            return selector.replace(/^_/, '').replace(/_/g, ':');
-        }
-
-        function binaryJsToSt (selector) {
-            return selector
-                .replace(/^_/, '')
-                .replace(/_and/g, '&')
-                .replace(/_or/g, '|')
-                .replace(/_plus/g, '+')
-                .replace(/_minus/g, '-')
-                .replace(/_star/g, '*')
-                .replace(/_slash/g, '/')
-                .replace(/_backslash/g, '\\')
-                .replace(/_tild/g, '~')
-                .replace(/_percent/g, '%')
-                .replace(/_gt/g, '>')
-                .replace(/_lt/g, '<')
-                .replace(/_eq/g, '=')
-                .replace(/_comma/g, ',')
-                .replace(/_at/g, '@');
-        }
+        st.js2st = js2st;
 
         st.st2prop = function (stSelector) {
             var colonPosition = stSelector.indexOf(':');