[pypy-svn] r27891 - pypy/dist/pypy/translator/js2/jssrc

fijal at codespeak.net fijal at codespeak.net
Tue May 30 10:11:51 CEST 2006


Author: fijal
Date: Tue May 30 10:11:39 2006
New Revision: 27891

Modified:
   pypy/dist/pypy/translator/js2/jssrc/misc.js
Log:
Syntactic sugar for inheritance.


Modified: pypy/dist/pypy/translator/js2/jssrc/misc.js
==============================================================================
--- pypy/dist/pypy/translator/js2/jssrc/misc.js	(original)
+++ pypy/dist/pypy/translator/js2/jssrc/misc.js	Tue May 30 10:11:39 2006
@@ -1,19 +1,39 @@
-var in_browser;
-try {
-    dummy = alert;
-    in_browser = true;
-} catch (e) {
-    in_browser = false;
-}
-
-function log(s) {
-    if (in_browser) {
-        var logdiv = document.getElementById('logdiv');
-        logdiv.innerHTML = new Date().getTime() + ': ' + s + "<br/>" + logdiv.innerHTML;
-    } else {
-        print('log: ' + s);
+Function.prototype.method = function (name, func) {
+    this.prototype[name] = func;
+    return this;
+};
+
+Function.method('inherits', function (parent) {
+    var d = 0, p = (this.prototype = new parent());
+    this.method('uber', function uber(name) {
+        var f, r, t = d, v = parent.prototype;
+        if (t) {
+            while (t) {
+                v = v.constructor.prototype;
+                t -= 1;
+            }
+            f = v[name];
+        } else {
+            f = p[name];
+            if (f == this[name]) {
+                f = v[name];
+            }
+        }
+        d += 1;
+        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
+        d -= 1;
+        return r;
+    });
+    return this;
+});
+
+Function.method('swiss', function (parent) {
+    for (var i = 1; i < arguments.length; i += 1) {
+        var name = arguments[i];
+        this.prototype[name] = parent.prototype[name];
     }
-}
+    return this;
+});
 
 function alloc_and_set(L0, len, elem) {
     l = [];



More information about the Pypy-commit mailing list