changeset 1:ab09b8a10876

Added trivial half-baked implementation of securable modules.
author Atul Varma <varmaa@toolness.com>
date Wed, 09 Sep 2009 21:09:39 -0700
parents a5b09b685df4
children b6f9d743a2b5
files modules/foo.js pydertron.py test.js
diffstat 3 files changed, 38 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/foo.js	Wed Sep 09 21:09:39 2009 -0700
@@ -0,0 +1,3 @@
+exports.bar = function bar() {
+  print('hay!');
+};
--- a/pydertron.py	Wed Sep 09 14:59:08 2009 -0700
+++ b/pydertron.py	Wed Sep 09 21:09:39 2009 -0700
@@ -233,7 +233,7 @@
     loading and executing scripts.
     """
 
-    def __init__(self, watchdog=watchdog):
+    def __init__(self, root_dir, watchdog=watchdog):
         rt = pydermonkey.Runtime()
         cx = rt.new_context()
         root = cx.new_object()
@@ -243,6 +243,7 @@
         cx.set_throw_hook(self._throwhook)
         watchdog.add_context(cx)
 
+        self.root_dir = root_dir
         self.rt = rt
         self.cx = cx
         self.curr_exc = None
@@ -466,3 +467,31 @@
             traceback.print_tb(e.exc_info[2])
             print e.exc_info[1]
         return retval
+
+if __name__ == '__main__':
+    import os
+    sandbox = JsSandbox("modules")
+
+    @jsexposed(on=sandbox.root, name='print')
+    def jsprint(string):
+        print string
+
+    @jsexposed(on=sandbox.root)
+    def require(path):
+        mcx = sandbox.rt.new_context()
+        module = mcx.new_object()
+        mcx.init_standard_classes(module)
+        exports = mcx.new_object()
+        mcx.define_property(module, 'exports', exports)
+
+        wrappedmodule = sandbox.wrap_jsobject(module)
+        wrappedmodule['print'] = jsprint
+        wrappedmodule['require'] = require
+
+        filename = os.path.join(sandbox.root_dir, "%s.js" % path)
+        contents = open(filename).read()
+        mcx.evaluate_script(module, contents, filename, 1)
+        return sandbox.wrap_jsobject(exports)
+
+    sandbox.run_script('test.js')
+    sandbox.finish()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.js	Wed Sep 09 21:09:39 2009 -0700
@@ -0,0 +1,5 @@
+var foo = require('foo');
+
+foo.bar();
+
+print('hello');