Mercurial > scratch
comparison pydershell/test.py @ 24:dace90a7f5e3
Added a 'jsexposed' decorator and, for security purposes, required that __jsexposed__ be True on all callables passed into JS-land as well.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 07 Sep 2009 16:53:41 -0700 |
parents | 9413bebf2ee6 |
children | b7037cd0f375 |
comparison
equal
deleted
inserted
replaced
23:35ab0ad3c294 | 24:dace90a7f5e3 |
---|---|
3 # Trivial embedding of pydershell. | 3 # Trivial embedding of pydershell. |
4 | 4 |
5 import sys | 5 import sys |
6 | 6 |
7 import pydermonkey | 7 import pydermonkey |
8 from pydershell import JsSandbox, JsExposedObject | 8 from pydershell import JsSandbox, JsExposedObject, jsexposed |
9 | 9 |
10 if __name__ == '__main__': | 10 if __name__ == '__main__': |
11 sandbox = JsSandbox() | 11 sandbox = JsSandbox() |
12 | 12 |
13 class Baz(JsExposedObject): | 13 class Baz(JsExposedObject): |
14 @jsexposed | |
14 def woozle(self, blap): | 15 def woozle(self, blap): |
15 return blap + 5 | 16 return blap + 5 |
16 woozle.__jsexposed__ = True | |
17 | 17 |
18 @jsexposed(on=sandbox.root) | |
18 def baz(): | 19 def baz(): |
19 return Baz() | 20 return Baz() |
20 sandbox.root.baz = baz | |
21 | 21 |
22 @jsexposed(on=sandbox.root) | |
22 def bar(obj): | 23 def bar(obj): |
23 print obj.bar() | 24 print obj.bar() |
24 sandbox.root.bar = bar | |
25 | 25 |
26 @jsexposed(on=sandbox.root) | |
26 def foo(callback): | 27 def foo(callback): |
27 return callback() | 28 return callback() |
28 sandbox.root.foo = foo | |
29 | 29 |
30 @jsexposed(on=sandbox.root) | |
30 def ensureBaz(baz): | 31 def ensureBaz(baz): |
31 if not isinstance(baz, Baz): | 32 if not isinstance(baz, Baz): |
32 print "Uhoh" | 33 print "Uhoh" |
33 else: | 34 else: |
34 print "ok" | 35 print "ok" |
35 sandbox.root.ensureBaz = ensureBaz | |
36 | 36 |
37 @jsexposed(name="print", on=sandbox.root) | |
37 def jsprint(string): | 38 def jsprint(string): |
38 print string | 39 print string |
39 sandbox.root['print'] = jsprint | |
40 | 40 |
41 retval = sandbox.run_script('test.js') | 41 retval = sandbox.run_script('test.js') |
42 | 42 |
43 sandbox.finish() | 43 sandbox.finish() |
44 del sandbox | 44 del sandbox |