changeset 6:97adec8c8127

Now passing all CommonJS SecurableModule compliance tests.
author Atul Varma <varmaa@toolness.com>
date Thu, 10 Sep 2009 01:04:04 -0700
parents c11c84274192
children 2117265e4dfe
files .hgignore pydertron.py test.js
diffstat 3 files changed, 48 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu Sep 10 01:04:04 2009 -0700
@@ -0,0 +1,3 @@
+syntax: glob
+*.pyc
+interoperablejs-read-only
--- a/pydertron.py	Thu Sep 10 00:15:57 2009 -0700
+++ b/pydertron.py	Thu Sep 10 01:04:04 2009 -0700
@@ -1,3 +1,4 @@
+import os
 import sys
 import time
 import threading
@@ -446,17 +447,17 @@
                                          code, filename, lineno)
         return self.wrap_jsobject(retval)
 
-    def run_script(self, filename, callback=None):
+    def run_script(self, contents, filename='<string>', lineno=1,
+                   callback=None):
         """
         Runs the given JS script, returning 0 on success, -1 on failure.
         """
 
         retval = -1
-        contents = open(filename).read()
         cx = self.cx
         try:
             result = cx.evaluate_script(self.root.wrapped_jsobject,
-                                        contents, filename, 1)
+                                        contents, filename, lineno)
             if callback:
                 callback(self.wrap_jsobject(result))
             retval = 0
@@ -470,6 +471,10 @@
         return retval
 
 class SecurableModuleLoader(object):
+    """
+    Loader for CommonJS SecurableModules.
+    """
+
     def __init__(self, sandbox, root_dir, globals):
         self.root_dir = root_dir
         self.sandbox = sandbox
@@ -515,9 +520,11 @@
         curr_script = self._get_calling_script()
         curr_dir = os.path.split(curr_script)[0]
 
-        if not curr_dir.startswith(self.root_dir):
+        if (not path.startswith(".") or
+            not curr_dir.startswith(self.root_dir)):
             curr_dir = self.root_dir
-        filename = os.path.join(curr_dir, "%s.js" % path)
+        ospath = path.replace('/', os.path.sep)
+        filename = os.path.join(curr_dir, "%s.js" % ospath)
         filename = os.path.normpath(filename)
         if (not filename.startswith(self.root_dir) or
             not (os.path.exists(filename) and
@@ -528,24 +535,44 @@
             self._load_module(filename)
         return self.modules[filename]
 
-if __name__ == '__main__':
-    import os
+def run_test(name, libpath):
+    print name
     sandbox = JsSandbox()
 
     @jsexposed(name='print')
-    def jsprint(string):
-        print string
+    def jsprint(message, label):
+        print "%s %s" % (message, label)
+
+    system = sandbox.new_object()
+    system['print'] = jsprint
+    globals = {'sys': system,
+               'environment': sandbox.new_object()}
+    loader = SecurableModuleLoader(sandbox, libpath, globals)
+    retval = sandbox.run_script("require('program')")
+    sandbox.finish()
+    print
+    return retval
 
-    globals = {'print': jsprint}
-    loader = SecurableModuleLoader(sandbox, "modules", globals)
-    retval = sandbox.run_script('test.js')
-    sandbox.finish()
-    del sandbox
-    del loader
+if __name__ == '__main__':
+    base_libpath = os.path.join("interoperablejs-read-only",
+                                "compliance")
+    if not os.path.exists(base_libpath):
+        print "Please run the following command and then re-run "
+        print "this script:"
+        print
+        print ("svn checkout "
+               "http://interoperablejs.googlecode.com/svn/trunk/ "
+               "interoperablejs-read-only")
+        sys.exit(1)
+
+    dirs = [(os.path.join(base_libpath, name), name)
+            for name in os.listdir(base_libpath)
+            if name not in ['.svn', 'ORACLE']]
+
+    for libpath, name in dirs:
+        run_test(name, libpath)
 
     import gc
     gc.collect()
     if pydermonkey.get_debug_info()['runtime_count']:
         print "WARNING: JS runtime was not destroyed."
-
-    sys.exit(retval)
--- a/test.js	Thu Sep 10 00:15:57 2009 -0700
+++ b/test.js	Thu Sep 10 01:04:04 2009 -0700
@@ -1,9 +1,1 @@
-var foo = require('foo');
-
-foo.bar();
-
-foo = require('foo');
-
-foo.bar();
-
-print('hello');
+require("program");