Mercurial > scratch
comparison pydershell/pydershell.py @ 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 |
comparison
equal
deleted
inserted
replaced
10:fb25af17bae6 | 11:74f27983a350 |
---|---|
1 #! /usr/bin/env python | |
2 | |
1 import sys | 3 import sys |
2 import time | 4 import time |
3 import threading | 5 import threading |
4 import traceback | 6 import traceback |
5 import pydermonkey | 7 import pydermonkey |
6 | 8 |
9 class InternalError(BaseException): | |
10 def __init__(self): | |
11 BaseException.__init__(self) | |
12 self.exc_info = sys.exc_info() | |
13 | |
7 rt = pydermonkey.Runtime() | 14 rt = pydermonkey.Runtime() |
8 cx = rt.new_context() | 15 cx = rt.new_context() |
9 globalobj = cx.new_object() | 16 globalobj = cx.new_object() |
10 cx.init_standard_classes(globalobj) | 17 cx.init_standard_classes(globalobj) |
11 | 18 |
19 def safejsfunc(cx, on_obj, name=None): | |
20 def make_wrapper(func): | |
21 if name is None: | |
22 func_name = func.__name__ | |
23 else: | |
24 func_name = name | |
25 def wrapper(func_cx, this, args): | |
26 try: | |
27 return func(func_cx, this, args) | |
28 except pydermonkey.error: | |
29 raise | |
30 except Exception: | |
31 raise InternalError() | |
32 cx.define_property( | |
33 on_obj, | |
34 func_name, | |
35 cx.new_function(wrapper, func_name) | |
36 ) | |
37 return func | |
38 return make_wrapper | |
39 | |
40 @safejsfunc(cx, globalobj) | |
12 def foo(cx, this, args): | 41 def foo(cx, this, args): |
13 return cx.call_function(this, args[0], ()) | 42 return cx.call_function(this, args[0], ()) |
14 | 43 |
15 cx.define_property(globalobj, | 44 @safejsfunc(cx, globalobj, 'print') |
16 'foo', | |
17 cx.new_function(foo, 'foo')) | |
18 | |
19 def jsprint(cx, this, args): | 45 def jsprint(cx, this, args): |
20 if len(args) > 0: | 46 if len(args) > 0: |
21 print args[0] | 47 print args[0] |
22 | |
23 cx.define_property(globalobj, | |
24 'print', | |
25 cx.new_function(jsprint, 'print')) | |
26 | 48 |
27 def opcb(cx): | 49 def opcb(cx): |
28 # Don't do anything; if a keyboard interrupt was triggered, | 50 # Don't do anything; if a keyboard interrupt was triggered, |
29 # it'll get raised here automatically. | 51 # it'll get raised here automatically. |
30 pass | 52 pass |
83 try: | 105 try: |
84 cx.evaluate_script(globalobj, open(filename).read(), filename, 1) | 106 cx.evaluate_script(globalobj, open(filename).read(), filename, 1) |
85 except pydermonkey.error, e: | 107 except pydermonkey.error, e: |
86 print make_stack(state.js_stack) | 108 print make_stack(state.js_stack) |
87 print e.args[1] | 109 print e.args[1] |
110 except InternalError, e: | |
111 print "An internal error occurred." | |
112 traceback.print_tb(e.exc_info[2]) | |
113 print e.exc_info[1] |