changeset 11:74f27983a350

Added more to pydershell.
author Atul Varma <varmaa@toolness.com>
date Sun, 06 Sep 2009 20:12:48 +0000
parents fb25af17bae6
children f024e41d0fb9
files pydershell/pydershell.py
diffstat 1 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/pydershell/pydershell.py	Sun Sep 06 11:12:35 2009 -0700
+++ b/pydershell/pydershell.py	Sun Sep 06 20:12:48 2009 +0000
@@ -1,29 +1,51 @@
+#! /usr/bin/env python
+
 import sys
 import time
 import threading
 import traceback
 import pydermonkey
 
+class InternalError(BaseException):
+    def __init__(self):
+        BaseException.__init__(self)
+        self.exc_info = sys.exc_info()
+
 rt = pydermonkey.Runtime()
 cx = rt.new_context()
 globalobj = cx.new_object()
 cx.init_standard_classes(globalobj)
 
+def safejsfunc(cx, on_obj, name=None):
+    def make_wrapper(func):
+        if name is None:
+            func_name = func.__name__
+        else:
+            func_name = name
+        def wrapper(func_cx, this, args):
+            try:
+                return func(func_cx, this, args)
+            except pydermonkey.error:
+                raise
+            except Exception:
+                raise InternalError()
+        cx.define_property(
+            on_obj,
+            func_name, 
+            cx.new_function(wrapper, func_name)
+            )
+        return func
+    return make_wrapper
+
+@safejsfunc(cx, globalobj)
 def foo(cx, this, args):
     return cx.call_function(this, args[0], ())
 
-cx.define_property(globalobj,
-                   'foo',
-                   cx.new_function(foo, 'foo'))
-
+@safejsfunc(cx, globalobj, 'print')
 def jsprint(cx, this, args):
     if len(args) > 0:
         print args[0]
 
-cx.define_property(globalobj,
-                   'print',
-                   cx.new_function(jsprint, 'print'))
-
 def opcb(cx):
     # Don't do anything; if a keyboard interrupt was triggered,
     # it'll get raised here automatically.
@@ -85,3 +107,7 @@
 except pydermonkey.error, e:
     print make_stack(state.js_stack)
     print e.args[1]
+except InternalError, e:
+    print "An internal error occurred."
+    traceback.print_tb(e.exc_info[2])
+    print e.exc_info[1]