r9721 - in helma-ng/trunk: modules src/org/helma/javascript

[email protected] Fri, 8 May 2009 12:22:16 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090508102216.D9DAA3D0D6@mia>
Author: hannes
Date: 2009-05-08 12:22:16 +0200 (Fri, 08 May 2009)
New Revision: 9721

Modified:
   helma-ng/trunk/modules/helmaglobal.js
   helma-ng/trunk/src/org/helma/javascript/ModuleScope.java
   helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java
Log:
Switch to getter-based implementation of export()
This makes it unnecessary to copy over exported properties manually after the module has been evaluated, and fixed a bug where exported properties aren't available while module evaluation is still ongoing, such as in the if(__name__ == "__main__") statement at the end of helma/unittest and possibly other modules.

Details at http://dev.helma.org/trac/helma/changeset/9721

Modified: helma-ng/trunk/modules/helmaglobal.js
===================================================================
--- helma-ng/trunk/modules/helmaglobal.js	2009-05-08 10:22:14 UTC (rev 9720)
+++ helma-ng/trunk/modules/helmaglobal.js	2009-05-08 10:22:16 UTC (rev 9721)
@@ -16,6 +16,7 @@
             var module = getRhinoEngine().loadModule(getRhinoContext(), moduleName, this);
             var exports = module.exports;
             if (!exports || typeof exports != "object") {
+                // should never happen with helma modules
                 exports = {};
                 Object.defineProperty(module, "exports", { value: exports });
             }
@@ -66,13 +67,21 @@
      */
     Object.defineProperty(this, "export", {
         value: function() {
-            var list = this.__exports__ || [];
-            for (var i = 0; i < arguments.length; i++) {
-                list.push(arguments[i]);
+            var module = this;
+            var exports = this.exports;
+            if (!exports || typeof exports != "object") {
+                // this should never happen with helma modules
+                exports = {};
+                Object.defineProperty(module, "exports", { value: exports });
             }
-            if (this.__exports__ != list) {
-                Object.defineProperty(this, "__exports__", { value: list });
-            }
+            Array.forEach(arguments, function(name) {
+                Object.defineProperty(exports, name, {
+                    get: function() {
+                        return module[name];
+                    },
+                    enumerable: true
+                });
+            });
         }
     });
 

Modified: helma-ng/trunk/src/org/helma/javascript/ModuleScope.java
===================================================================
--- helma-ng/trunk/src/org/helma/javascript/ModuleScope.java	2009-05-08 10:22:14 UTC (rev 9720)
+++ helma-ng/trunk/src/org/helma/javascript/ModuleScope.java	2009-05-08 10:22:16 UTC (rev 9721)
@@ -42,7 +42,6 @@
         this.name = moduleName;
         this.exports = new ExportsObject();
         defineProperty("exports", exports,  DONTENUM);
-        defineProperty("__exports__", cx.newArray(this, 0), DONTENUM);
         defineProperty("__name__", moduleName, DONTENUM);
         defineProperty("__path__", source.getRelativePath(), DONTENUM);
     }
@@ -55,7 +54,6 @@
         this.exports = new ExportsObject();
         defineProperty("exports", exports,  DONTENUM);
         delete("__shared__");     
-        defineProperty("__exports__", cx.newArray(this, 0), DONTENUM);
     }
 
     public long getChecksum() {
@@ -74,26 +72,6 @@
         return exports;
     }
 
-    protected void processExports() {
-        // the __exports__ array is an alternative way for a module to export properties.
-        // it contains a list of property names that will be copied to the exports object.
-        Object e = get("__exports__", this);
-        if (e instanceof NativeArray) {
-            NativeArray array = (NativeArray) e;
-            long length = array.getLength();
-            int flags = READONLY | PERMANENT;
-            for (int i = 0; i < length; i++) {
-                // print("Exporting", moduleName, key, "->", typeof(module[key]));
-                Object key = array.get(i, array);
-                if (key instanceof String) {
-                    Object value = get((String) key, this);
-                    ScriptableObject.defineProperty(exports, (String) key, value, flags);
-                }
-            }
-        }
-
-    }
-
     @Override
     public String toString() {
         return "[ModuleScope " + source + "]";

Modified: helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java
===================================================================
--- helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java	2009-05-08 10:22:14 UTC (rev 9720)
+++ helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java	2009-05-08 10:22:16 UTC (rev 9721)
@@ -209,7 +209,6 @@
         }
         modules.put(source, module);
         script.exec(cx, module);
-        module.processExports();
         checkShared(module);
         module.setChecksum(getChecksum());
         return module;