annotate tests/test_pydermonkey.py @ 173:5cfb47c9e916 default tip

Minor line wrap and spacing fixes.
author Atul Varma <varmaa@toolness.com>
date Tue, 01 Sep 2009 03:22:33 -0700
parents dd32a92f6b4f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
1 import gc
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
2 import sys
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
3 import unittest
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
4 import weakref
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
5 import time
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
6 import threading
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
7
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
8 import pydermonkey
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
9
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
10 class PydermonkeyTests(unittest.TestCase):
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
11 def setUp(self):
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
12 self._teardowns = []
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
13
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
14 def tearDown(self):
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
15 self.last_exception = None
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
16 while self._teardowns:
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
17 obj = self._teardowns.pop()
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
18 runtime = obj.get_runtime()
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
19 runtime.new_context().clear_object_private(obj)
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
20 del runtime
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
21 del obj
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
22 self.assertEqual(pydermonkey.get_debug_info()['runtime_count'], 0)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
23
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
24 def _clearOnTeardown(self, obj):
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
25 self._teardowns.append(obj)
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
26
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
27 def _evaljs(self, code):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
28 rt = pydermonkey.Runtime()
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
29 cx = rt.new_context()
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
30 obj = cx.new_object()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
31 cx.init_standard_classes(obj)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
32 return cx.evaluate_script(obj, code, '<string>', 1)
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
33
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
34 def _execjs(self, code):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
35 rt = pydermonkey.Runtime()
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
36 cx = rt.new_context()
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
37 obj = cx.new_object()
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
38 cx.init_standard_classes(obj)
140
ce8099238c01 As per a discussion with jorendorff, it looks like we can pass NULL to compile_script(), since we're not using object principals (and instead just using an object capability model for now).
Atul Varma <varmaa@toolness.com>
parents: 138
diff changeset
39 script = cx.compile_script(code, '<string>', 1)
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
40 return cx.execute_script(obj, script)
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
41
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
42 def _evalJsWrappedPyFunc(self, func, code):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
43 cx = pydermonkey.Runtime().new_context()
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
44 obj = cx.new_object()
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
45 cx.init_standard_classes(obj)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
46 jsfunc = cx.new_function(func, func.__name__)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
47 self._clearOnTeardown(jsfunc)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
48 cx.define_property(obj, func.__name__, jsfunc)
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
49 return cx.evaluate_script(obj, code, '<string>', 1)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
50
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
51 def assertRaises(self, exctype, func, *args):
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
52 was_raised = False
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
53 try:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
54 func(*args)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
55 except exctype, e:
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
56 self.last_exception = e
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
57 was_raised = True
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
58 self.assertTrue(was_raised)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
59
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
60 def testSyntaxErrorsAreRaised(self):
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
61 for run in [self._evaljs, self._execjs]:
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
62 self.assertRaises(pydermonkey.error, run, '5f')
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
63 self.assertEqual(
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
64 self.last_exception.args[1],
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
65 u'SyntaxError: missing ; before statement'
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
66 )
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
67
147
ebc0ff767290 Ensure that get_stack() returns None if the stack is empty.
Atul Varma <varmaa@toolness.com>
parents: 146
diff changeset
68 def testGetStackOnEmptyStackReturnsNone(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
69 cx = pydermonkey.Runtime().new_context()
147
ebc0ff767290 Ensure that get_stack() returns None if the stack is empty.
Atul Varma <varmaa@toolness.com>
parents: 146
diff changeset
70 self.assertEqual(cx.get_stack(), None)
ebc0ff767290 Ensure that get_stack() returns None if the stack is empty.
Atul Varma <varmaa@toolness.com>
parents: 146
diff changeset
71
145
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
72 def testGetStackWorks(self):
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
73 stack_holder = []
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
74
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
75 def func(cx, this, args):
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
76 stack_holder.append(cx.get_stack())
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
77
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
78 cx = pydermonkey.Runtime().new_context()
145
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
79 obj = cx.new_object()
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
80 cx.init_standard_classes(obj)
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
81 jsfunc = cx.new_function(func, func.__name__)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
82 self._clearOnTeardown(jsfunc)
145
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
83 cx.define_property(obj, func.__name__, jsfunc)
157
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
84 cx.evaluate_script(obj, '(function closure() { \nfunc() })()',
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
85 '<string>', 1)
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
86 stack = stack_holder[0]
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
87 script = stack['caller']['caller']['script']
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
88 pc = stack['caller']['caller']['pc']
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
89 closure = stack['caller']['function']
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
90 self.assertEqual(closure.name, 'closure')
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
91 self.assertEqual(closure.filename, '<string>')
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
92 self.assertEqual(stack['caller']['script'], None)
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
93 self.assertEqual(stack['caller']['lineno'], 2)
146
b1cf9decc36f Added 'lineno' and 'pc' attributes to stack frame information.
Atul Varma <varmaa@toolness.com>
parents: 145
diff changeset
94 self.assertEqual(script.filename, '<string>')
157
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
95 self.assertEqual(stack['caller']['caller']['lineno'], 1)
146
b1cf9decc36f Added 'lineno' and 'pc' attributes to stack frame information.
Atul Varma <varmaa@toolness.com>
parents: 145
diff changeset
96 self.assertTrue(pc >= 0 and pc < len(buffer(script)))
157
a31ff2de6017 Added 'function' key to stack frame dicts; lineno and pc information is now provided even if script is not.
Atul Varma <varmaa@toolness.com>
parents: 155
diff changeset
97 self.assertEqual(stack['caller']['caller']['caller'], None)
145
5d53f6293a81 Added a basic context.get_stack() function.
Atul Varma <varmaa@toolness.com>
parents: 143
diff changeset
98
142
a2c1db5ece2b Added a 'filename' member to script objects.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
99 def testScriptHasFilenameMember(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
100 cx = pydermonkey.Runtime().new_context()
142
a2c1db5ece2b Added a 'filename' member to script objects.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
101 script = cx.compile_script('foo', '<string>', 1)
a2c1db5ece2b Added a 'filename' member to script objects.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
102 self.assertEqual(script.filename, '<string>')
a2c1db5ece2b Added a 'filename' member to script objects.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
103
143
df97699fc104 Added base_lineno and line_extent members to the script object.
Atul Varma <varmaa@toolness.com>
parents: 142
diff changeset
104 def testScriptHasLineInfo(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
105 cx = pydermonkey.Runtime().new_context()
143
df97699fc104 Added base_lineno and line_extent members to the script object.
Atul Varma <varmaa@toolness.com>
parents: 142
diff changeset
106 script = cx.compile_script('foo\nbar', '<string>', 1)
df97699fc104 Added base_lineno and line_extent members to the script object.
Atul Varma <varmaa@toolness.com>
parents: 142
diff changeset
107 self.assertEqual(script.base_lineno, 1)
df97699fc104 Added base_lineno and line_extent members to the script object.
Atul Varma <varmaa@toolness.com>
parents: 142
diff changeset
108 self.assertEqual(script.line_extent, 2)
df97699fc104 Added base_lineno and line_extent members to the script object.
Atul Varma <varmaa@toolness.com>
parents: 142
diff changeset
109
138
1a982ee56458 A script's bytecode is now exposed as a buffer object.
Atul Varma <varmaa@toolness.com>
parents: 137
diff changeset
110 def testScriptIsExposedAsBuffer(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
111 rt = pydermonkey.Runtime()
138
1a982ee56458 A script's bytecode is now exposed as a buffer object.
Atul Varma <varmaa@toolness.com>
parents: 137
diff changeset
112 cx = rt.new_context()
140
ce8099238c01 As per a discussion with jorendorff, it looks like we can pass NULL to compile_script(), since we're not using object principals (and instead just using an object capability model for now).
Atul Varma <varmaa@toolness.com>
parents: 138
diff changeset
113 script = cx.compile_script('foo', '<string>', 1)
138
1a982ee56458 A script's bytecode is now exposed as a buffer object.
Atul Varma <varmaa@toolness.com>
parents: 137
diff changeset
114 self.assertTrue(len(buffer(script)) > 0)
1a982ee56458 A script's bytecode is now exposed as a buffer object.
Atul Varma <varmaa@toolness.com>
parents: 137
diff changeset
115
136
b4e216d06e83 Added pymonkey.Script, which wraps a JSScript object, and context.compile_script()/execute_script().
Atul Varma <varmaa@toolness.com>
parents: 133
diff changeset
116 def testCompileScriptWorks(self):
137
f83a1603c417 Added a syntax error test.
Atul Varma <varmaa@toolness.com>
parents: 136
diff changeset
117 self.assertEqual(self._execjs('5 + 1'), 6)
136
b4e216d06e83 Added pymonkey.Script, which wraps a JSScript object, and context.compile_script()/execute_script().
Atul Varma <varmaa@toolness.com>
parents: 133
diff changeset
118
120
856ca7a139e4 Pymonkey errors raised as a result of JS exceptions now contain both the string representation and the original Python-wrapped JS object that was thrown (i.e., the exception).
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
119 def testErrorsRaisedIncludeStrings(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
120 self.assertRaises(pydermonkey.error, self._evaljs, 'boop()')
120
856ca7a139e4 Pymonkey errors raised as a result of JS exceptions now contain both the string representation and the original Python-wrapped JS object that was thrown (i.e., the exception).
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
121 self.assertEqual(self.last_exception.args[1],
856ca7a139e4 Pymonkey errors raised as a result of JS exceptions now contain both the string representation and the original Python-wrapped JS object that was thrown (i.e., the exception).
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
122 u'ReferenceError: boop is not defined')
856ca7a139e4 Pymonkey errors raised as a result of JS exceptions now contain both the string representation and the original Python-wrapped JS object that was thrown (i.e., the exception).
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
123
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
124 def testThreadSafetyExceptionIsRaised(self):
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
125 stuff = {}
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
126 def make_runtime():
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
127 stuff['rt'] = pydermonkey.Runtime()
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
128 thread = threading.Thread(target = make_runtime)
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
129 thread.start()
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
130 thread.join()
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
131 self.assertRaises(pydermonkey.error,
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
132 stuff['rt'].new_context)
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
133 self.assertEqual(self.last_exception.args[0],
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
134 'Function called from wrong thread')
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
135 del stuff['rt']
87
345d4c0e3dd3 Thread safety exceptions are now properly raised by all relevant pymonkey functions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
136
81
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
137 def testClearObjectPrivateWorks(self):
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
138 class Foo(object):
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
139 pass
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
140 pyobj = Foo()
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
141 cx = pydermonkey.Runtime().new_context()
81
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
142 obj = cx.new_object(pyobj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
143 pyobj = weakref.ref(pyobj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
144 self.assertEqual(pyobj(), cx.get_object_private(obj))
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
145 cx.clear_object_private(obj)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
146 self.assertEqual(cx.get_object_private(obj), None)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
147 self.assertEqual(pyobj(), None)
99138265d4b9 Added a context.clear_object_private() function.
Atul Varma <varmaa@toolness.com>
parents: 77
diff changeset
148
71
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
149 def testGetObjectPrivateWorks(self):
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
150 class Foo(object):
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
151 pass
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
152 pyobj = Foo()
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
153 cx = pydermonkey.Runtime().new_context()
71
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
154 obj = cx.new_object(pyobj)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
155 pyobj = weakref.ref(pyobj)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
156 self.assertEqual(pyobj(), cx.get_object_private(obj))
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
157 del obj
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
158 del cx
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
159 self.assertEqual(pyobj(), None)
9b3f4e53e365 Added context.get_object_private() and an optional private object parameter to context.new_object().
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
160
127
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
161 def testContextSupportsCyclicGc(self):
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
162 def makecx():
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
163 cx = pydermonkey.Runtime().new_context()
127
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
164
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
165 def opcb(othercx):
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
166 return cx
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
167
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
168 cx.set_operation_callback(opcb)
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
169 return cx
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
170
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
171 gc.disable()
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
172 cx = makecx()
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
173 wcx = weakref.ref(cx)
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
174 self.assertEqual(wcx(), cx)
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
175 del cx
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
176 self.assertTrue(wcx())
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
177 gc.enable()
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
178 gc.collect()
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
179 self.assertEqual(wcx(), None)
4179d1e1a75c Added a test for context cyclic gc support and fixed a bug revealed by it.
Atul Varma <varmaa@toolness.com>
parents: 126
diff changeset
180
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
181 def testOperationCallbackIsCalled(self):
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
182 def opcb(cx):
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
183 raise Exception("stop eet!")
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
184
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
185 cx = pydermonkey.Runtime().new_context()
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
186 cx.set_operation_callback(opcb)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
187 obj = cx.new_object()
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
188 cx.init_standard_classes(obj)
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
189
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
190 def watchdog():
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
191 time.sleep(0.1)
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
192 cx.trigger_operation_callback()
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
193
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
194 thread = threading.Thread(target = watchdog)
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 64
diff changeset
195 thread.start()
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
196
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
197 self.assertRaises(
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
198 pydermonkey.error,
64
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
199 cx.evaluate_script,
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
200 obj, 'while (1) {}', '<string>', 1
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
201 )
fb7e11dec538 Added context.set_operation_callback() and trigger_operation_callback() methods.
Atul Varma <varmaa@toolness.com>
parents: 62
diff changeset
202
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
203 def testUndefinedStrIsUndefined(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
204 self.assertEqual(str(pydermonkey.undefined),
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
205 "pydermonkey.undefined")
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
206
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
207 def testScriptedJsFuncHasIsPythonFalse(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
208 cx = pydermonkey.Runtime().new_context()
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
209 jsfunc = cx.evaluate_script(cx.new_object(),
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
210 '(function(){})', '<string>', 1)
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
211 self.assertFalse(jsfunc.is_python)
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
212
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
213 def testJsWrappedPythonFuncHasIsPythonTrue(self):
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
214 def foo(cx, this, args):
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
215 pass
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
216
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
217 cx = pydermonkey.Runtime().new_context()
160
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
218 jsfunc = cx.new_function(foo, foo.__name__)
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
219 self.assertTrue(jsfunc.is_python)
56181fb5fc7e Added a function.is_python property indicating whether the js function is implemented in python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
220
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
221 def testJsWrappedPythonFuncHasNoFilename(self):
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
222 def foo(cx, this, args):
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
223 pass
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
224
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
225 cx = pydermonkey.Runtime().new_context()
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
226 jsfunc = cx.new_function(foo, foo.__name__)
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
227 self.assertEqual(jsfunc.filename, None)
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 157
diff changeset
228
162
d1606a6cf1c0 Made get_object_private() and clear_object_private() more robust.
Atul Varma <varmaa@toolness.com>
parents: 160
diff changeset
229 def testJsScriptedFuncHasNoPrivate(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
230 cx = pydermonkey.Runtime().new_context()
162
d1606a6cf1c0 Made get_object_private() and clear_object_private() more robust.
Atul Varma <varmaa@toolness.com>
parents: 160
diff changeset
231 jsfunc = cx.evaluate_script(cx.new_object(),
d1606a6cf1c0 Made get_object_private() and clear_object_private() more robust.
Atul Varma <varmaa@toolness.com>
parents: 160
diff changeset
232 '(function(){})', '<string>', 1)
d1606a6cf1c0 Made get_object_private() and clear_object_private() more robust.
Atul Varma <varmaa@toolness.com>
parents: 160
diff changeset
233 self.assertEqual(cx.get_object_private(jsfunc), None)
d1606a6cf1c0 Made get_object_private() and clear_object_private() more robust.
Atul Varma <varmaa@toolness.com>
parents: 160
diff changeset
234
166
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
235 def testGetPendingExceptionReturnsNone(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
236 cx = pydermonkey.Runtime().new_context()
166
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
237 self.assertFalse(cx.is_exception_pending())
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
238 self.assertEqual(cx.get_pending_exception(), None)
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
239
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
240 def testThrowHookWorks(self):
166
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
241 exceptions = []
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
242 def throwhook(cx):
166
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
243 self.assertTrue(cx.is_exception_pending())
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
244 exceptions.append(cx.get_pending_exception())
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
245
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
246 cx = pydermonkey.Runtime().new_context()
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
247 cx.set_throw_hook(throwhook)
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
248 self.assertRaises(
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
249 pydermonkey.error,
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
250 cx.evaluate_script,
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
251 cx.new_object(),
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
252 '(function() { throw "hi"; })()',
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
253 '<string>', 1
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
254 )
166
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
255 self.assertEqual(exceptions, ['hi', 'hi'])
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
256 self.assertFalse(cx.is_exception_pending())
2eaae00d5e30 Added is_exception_pending() and get_pending_exception().
Atul Varma <varmaa@toolness.com>
parents: 165
diff changeset
257 self.assertEqual(cx.get_pending_exception(), None)
165
1f9a5982db9c Added a context.set_throw_hook() method.
Atul Varma <varmaa@toolness.com>
parents: 162
diff changeset
258
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
259 def testJsWrappedPythonFuncHasPrivate(self):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
260 def foo(cx, this, args):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
261 pass
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
262
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
263 cx = pydermonkey.Runtime().new_context()
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
264 jsfunc = cx.new_function(foo, foo.__name__)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
265 self.assertEqual(cx.get_object_private(jsfunc), foo)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
266
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
267 def testJsWrappedPythonFuncIsNotGCd(self):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
268 def define(cx, obj):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
269 def func(cx, this, args):
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
270 return u'func was called'
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
271 jsfunc = cx.new_function(func, func.__name__)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
272 cx.define_property(obj, func.__name__, jsfunc)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
273 return weakref.ref(func)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
274 rt = pydermonkey.Runtime()
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
275 cx = rt.new_context()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
276 obj = cx.new_object()
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
277 cx.init_standard_classes(obj)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
278 ref = define(cx, obj)
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
279 cx.gc()
59
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
280 self.assertNotEqual(ref(), None)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
281 result = cx.evaluate_script(obj, 'func()', '<string>', 1)
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
282 self.assertEqual(result, u'func was called')
fb97bed55789 Added a test that fails b/c of a known bug in pymonkey.
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
283
61
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
284 # Now ensure that the wrapped function is GC'd when it's
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
285 # no longer reachable from JS space.
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
286 cx.define_property(obj, 'func', 0)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
287 cx.gc()
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
288 self.assertEqual(ref(), None)
1506350991d4 JS Wrapped python functions are now only GC'd by python once they've first been GC'd by JS.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
289
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
290 def testCircularJsWrappedPythonFuncIsGCdIfPrivateCleared(self):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
291 def define(cx, obj):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
292 rt = cx.get_runtime()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
293 def func(cx, this, args):
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
294 # Oh noes, a circular reference is born!
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
295 rt
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
296 jsfunc = cx.new_function(func, func.__name__)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
297 cx.define_property(obj, func.__name__, jsfunc)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
298 return (jsfunc, weakref.ref(func))
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
299 rt = pydermonkey.Runtime()
84
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
300 cx = rt.new_context()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
301 obj = cx.new_object()
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
302 cx.init_standard_classes(obj)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
303 jsfunc, ref = define(cx, obj)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
304
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
305 # This will break the circular reference.
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
306 cx.clear_object_private(jsfunc)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
307
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
308 del jsfunc
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
309 del rt
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
310 del cx
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
311 del obj
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
312 self.assertEqual(ref(), None)
10205d88f6ff JS-wrapped Python functions can now be passed to context.get_object_private() and context.clear_object_private(), which allows cycles to be manually broken from Python. Far from ideal, but easier than writing a cycle collector for now.
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
313
153
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
314 def testFunctionsWithClosuresAreNotIdentical(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
315 cx = pydermonkey.Runtime().new_context()
153
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
316 obj = cx.new_object()
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
317 cx.init_standard_classes(obj)
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
318 cx.evaluate_script(
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
319 obj, "function build(x) { return function foo() { return x; } }",
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
320 "<string>", 1
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
321 )
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
322 func1 = cx.evaluate_script(obj, "build(1)", "<string>", 1)
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
323 func2 = cx.evaluate_script(obj, "build(2)", "<string>", 1)
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
324 self.assertNotEqual(func1, func2)
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
325 self.assertEqual(func1.name, 'foo')
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
326 self.assertEqual(func1.name, func2.name)
9ea6a9a566e2 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 152
diff changeset
327
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
328 def testAnonymousJsFunctionHasNullNameAttribute(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
329 cx = pydermonkey.Runtime().new_context()
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
330 obj = cx.new_object()
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
331 cx.init_standard_classes(obj)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
332 jsfunc = cx.evaluate_script(obj, "(function() {})",
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
333 "<string>", 1)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
334 self.assertEqual(jsfunc.name, None)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
335
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
336 def testJsFunctionHasNameAttribute(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
337 cx = pydermonkey.Runtime().new_context()
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
338 obj = cx.new_object()
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
339 cx.init_standard_classes(obj)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
340 jsfunc = cx.evaluate_script(obj, "(function blarg() {})",
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
341 "<string>", 1)
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
342 self.assertEqual(jsfunc.name, "blarg")
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
343
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
344 def testJsWrappedPythonFuncHasNameAttribute(self):
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
345 def func(cx, this, args):
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
346 return True
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
347
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
348 cx = pydermonkey.Runtime().new_context()
152
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
349 jsfunc = cx.new_function(func, "foo")
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
350 self.assertEqual(jsfunc.name, "foo")
80463c8c7930 Added a 'name' property to function obects. Fixed a TODO.
Atul Varma <varmaa@toolness.com>
parents: 151
diff changeset
351
62
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
352 def testJsWrappedPythonFuncIsGCdAtRuntimeDestruction(self):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
353 def define(cx, obj):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
354 def func(cx, this, args):
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
355 return u'func was called'
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
356 jsfunc = cx.new_function(func, func.__name__)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
357 cx.define_property(obj, func.__name__, jsfunc)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
358 return weakref.ref(func)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
359 rt = pydermonkey.Runtime()
62
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
360 cx = rt.new_context()
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
361 obj = cx.new_object()
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
362 cx.init_standard_classes(obj)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
363 ref = define(cx, obj)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
364 del rt
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
365 del cx
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
366 del obj
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
367 self.assertEqual(ref(), None)
2b5696b91b01 Fixed a bug whereby some objects wouldn't be finalized when a runtime was destroyed.
Atul Varma <varmaa@toolness.com>
parents: 61
diff changeset
368
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
369 def testJsWrappedPythonFuncThrowsExcIfPrivateCleared(self):
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
370 def func(cx, this, args):
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
371 return True
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
372
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
373 code = "func()"
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
374 cx = pydermonkey.Runtime().new_context()
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
375 obj = cx.new_object()
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
376 cx.init_standard_classes(obj)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
377 jsfunc = cx.new_function(func, func.__name__)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
378 cx.define_property(obj, func.__name__, jsfunc)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
379 cx.clear_object_private(jsfunc)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
380 self.assertRaises(pydermonkey.error,
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
381 cx.evaluate_script,
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
382 obj, code, '<string>', 1)
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
383 self.assertEqual(
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
384 self._tostring(cx, self.last_exception.args[0]),
86
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
385 "Error: Wrapped Python function no longer exists"
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
386 )
16a3e99e9b77 JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
387
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
388 def testJsWrappedPythonFuncPassesContext(self):
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
389 contexts = []
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
390
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
391 def func(cx, this, args):
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
392 contexts.append(cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
393 return True
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
394
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
395 code = "func()"
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
396 cx = pydermonkey.Runtime().new_context()
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
397 obj = cx.new_object()
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
398 cx.init_standard_classes(obj)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
399 jsfunc = cx.new_function(func, func.__name__)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
400 self._clearOnTeardown(jsfunc)
43
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
401 cx.define_property(obj, func.__name__, jsfunc)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
402 cx.evaluate_script(obj, code, '<string>', 1)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
403 self.assertEqual(contexts[0], cx)
5727675b1bcb JS-wrapped python functions now take a context object as their first parameter.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
404
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
405 def testJsWrappedPythonFuncPassesThisArg(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
406 thisObjs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
407
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
408 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
409 thisObjs.append(this)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
410 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
411
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
412 code = "func()"
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
413 cx = pydermonkey.Runtime().new_context()
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
414 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
415 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
416 jsfunc = cx.new_function(func, func.__name__)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
417 self._clearOnTeardown(jsfunc)
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
418 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
419 cx.evaluate_script(obj, code, '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
420 self.assertEqual(thisObjs[0], obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
421
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
422 def testJsWrappedPythonFuncPassesFuncArgs(self):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
423 funcArgs = []
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
424
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
425 def func(cx, this, args):
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
426 funcArgs.append(args)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
427 return True
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
428
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
429 cx = pydermonkey.Runtime().new_context()
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
430 obj = cx.new_object()
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
431 cx.init_standard_classes(obj)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
432 jsfunc = cx.new_function(func, func.__name__)
151
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
433 self._clearOnTeardown(jsfunc)
657afb7307eb Added a pymonkey.get_debug_info() function and used it on test teardown to ensure that unit tests don't leak.
Atul Varma <varmaa@toolness.com>
parents: 149
diff changeset
434
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
435 cx.define_property(obj, func.__name__, jsfunc)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
436
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
437 cx.evaluate_script(obj, "func()", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
438 self.assertEqual(len(funcArgs[0]), 0)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
439 self.assertTrue(isinstance(funcArgs[0], tuple))
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
440
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
441 cx.evaluate_script(obj, "func(1, 'foo')", '<string>', 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
442 self.assertEqual(len(funcArgs[1]), 2)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
443 self.assertEqual(funcArgs[1][0], 1)
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
444 self.assertEqual(funcArgs[1][1], u'foo')
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
445
92
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
446 def testJsWrappedPythonFunctionReturnsUnicodeWithEmbeddedNULs(self):
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
447 def hai2u(cx, this, args):
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
448 return args[0] + u"o hai"
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
449 self.assertEqual(self._evalJsWrappedPyFunc(hai2u,
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
450 'hai2u("blah\x00 ")'),
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
451 u"blah\x00 o hai")
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
452
95
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
453 def testJsWrappedPythonFunctionReturnsString(self):
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
454 def hai2u(cx, this, args):
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
455 return "o hai"
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
456 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
457 "o hai")
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
458
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
459 def testJsWrappedPythonFunctionReturnsUnicode(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
460 def hai2u(cx, this, args):
95
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
461 return u"o hai\u2026"
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
462 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
95
0701aee1b0cd JS-wrapped python functions can now return normal strings (not just unicode).
Atul Varma <varmaa@toolness.com>
parents: 94
diff changeset
463 u"o hai\u2026")
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
464
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
465 def testJsWrappedPythonFunctionThrowsJsException(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
466 def hai2u(cx, this, args):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
467 raise pydermonkey.error(u"blarg")
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
468 self.assertRaises(pydermonkey.error,
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
469 self._evalJsWrappedPyFunc,
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
470 hai2u, 'hai2u()')
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
471 self.assertEqual(self.last_exception.args[0], u"blarg")
75
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
472
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
473 def testJsWrappedPythonFunctionThrowsPyException(self):
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
474 thecx = []
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
475 def hai2u(cx, this, args):
4b1149d818e8 Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
476 thecx.append(cx)
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
477 raise Exception("hello")
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
478 self.assertRaises(pydermonkey.error,
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
479 self._evalJsWrappedPyFunc,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
480 hai2u, 'hai2u()')
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
481 exc = thecx[0].get_object_private(self.last_exception.args[0])
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
482 self.assertEqual(exc.args[0], "hello")
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
483
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
484 def testJsWrappedPythonFunctionReturnsNone(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
485 def hai2u(cx, this, args):
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
486 pass
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
487 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
488 None)
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 43
diff changeset
489
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
490 def testJsWrappedPythonFunctionReturnsTrue(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
491 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
492 return True
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
493 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
494 True)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
495
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
496 def testJsWrappedPythonFunctionReturnsFalse(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
497 def hai2u(cx, this, args):
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
498 return False
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
499 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
500 False)
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 32
diff changeset
501
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
502 def testJsWrappedPythonFunctionReturnsSmallInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
503 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
504 return 5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
505 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
506 5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
507
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
508 def testJsWrappedPythonFunctionReturnsFloat(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
509 def hai2u(cx, this, args):
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
510 return 5.1
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
511 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
512 5.1)
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
513
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
514 def testJsWrappedPythonFunctionReturnsNegativeInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
515 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
516 return -5
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
517 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
518 -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
519
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
520 def testJsWrappedPythonFunctionReturnsBigInt(self):
52
427b01954b22 The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
521 def hai2u(cx, this, args):
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
522 return 2147483647
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
523 self.assertEqual(self._evalJsWrappedPyFunc(hai2u, 'hai2u()'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
524 2147483647)
24
74b7ad049542 Added very primitive support for calling python functions from JS.
Atul Varma <varmaa@toolness.com>
parents: 22
diff changeset
525
133
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
526 def testHasPropertyWorks(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
527 cx = pydermonkey.Runtime().new_context()
133
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
528 obj = cx.new_object()
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
529 cx.init_standard_classes(obj)
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
530 foo = cx.new_object()
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
531 cx.define_property(obj, u"foo\u2026", foo)
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
532 self.assertTrue(cx.has_property(obj, u"foo\u2026"))
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
533 self.assertFalse(cx.has_property(obj, "bar"))
a9c0db56e06b Added a context.has_property() function.
Atul Varma <varmaa@toolness.com>
parents: 129
diff changeset
534
104
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
535 def testDefinePropertyWorksWithUnicodePropertyNames(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
536 cx = pydermonkey.Runtime().new_context()
104
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
537 obj = cx.new_object()
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
538 cx.init_standard_classes(obj)
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
539 foo = cx.new_object()
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
540 cx.define_property(obj, u"foo\u2026", foo)
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
541 self.assertEqual(
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
542 cx.get_property(obj, u"foo\u2026"),
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
543 foo
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
544 )
00c1351b3e82 Added support for unicode property names in context.define_property().
Atul Varma <varmaa@toolness.com>
parents: 99
diff changeset
545
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
546 def testDefinePropertyWorksWithObject(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
547 cx = pydermonkey.Runtime().new_context()
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
548 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
549 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
550 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
551 cx.define_property(obj, "foo", foo)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
552 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
553 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
554 foo
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
555 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
556
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
557 def testDefinePropertyWorksWithString(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
558 cx = pydermonkey.Runtime().new_context()
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
559 obj = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
560 cx.init_standard_classes(obj)
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
561 foo = cx.new_object()
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
562 cx.define_property(obj, "foo", u"hello")
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
563 self.assertEqual(
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
564 cx.evaluate_script(obj, 'foo', '<string>', 1),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
565 u"hello"
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
566 )
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
567
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
568 def testObjectIsIdentityPreserving(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
569 cx = pydermonkey.Runtime().new_context()
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
570 obj = cx.new_object()
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
571 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
572 cx.evaluate_script(obj, 'var foo = {bar: 1}', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
573 self.assertTrue(isinstance(cx.get_property(obj, u"foo"),
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
574 pydermonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
575 self.assertTrue(cx.get_property(obj, u"foo") is
94
c66d7da09c95 Tweaked a test to also ensure that context.get_property() can take unicode or strings.
Atul Varma <varmaa@toolness.com>
parents: 92
diff changeset
576 cx.get_property(obj, "foo"))
22
988a8998c75f JS objects reflected into Python are now identity-preserving, though the implementation for this is pretty bad right now.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
577
73
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
578 def testObjectGetattrThrowsException(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
579 cx = pydermonkey.Runtime().new_context()
73
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
580 obj = cx.new_object()
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
581 cx.init_standard_classes(obj)
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
582 result = cx.evaluate_script(obj, '({get foo() { throw "blah"; }})',
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
583 '<string>', 1)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
584 self.assertRaises(pydermonkey.error,
73
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
585 cx.get_property,
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
586 result,
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
587 u"foo")
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
588 self.assertEqual(self.last_exception.args[0], u"blah")
73
efa0cfe6fc03 Resolved two TODOs.
Atul Varma <varmaa@toolness.com>
parents: 71
diff changeset
589
76
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
590 def testInfiniteRecursionRaisesError(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
591 cx = pydermonkey.Runtime().new_context()
76
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
592 obj = cx.new_object()
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
593 cx.init_standard_classes(obj)
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
594 self.assertRaises(
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
595 pydermonkey.error,
76
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
596 cx.evaluate_script,
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
597 obj, '(function foo() { foo(); })();', '<string>', 1
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
598 )
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
599 self.assertEqual(
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
600 self._tostring(cx, self.last_exception.args[0]),
76
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
601 "InternalError: too much recursion"
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
602 )
4cb89d48b068 Added test for infinite recursion.
Atul Varma <varmaa@toolness.com>
parents: 75
diff changeset
603
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
604 def testObjectGetattrWorks(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
605 cx = pydermonkey.Runtime().new_context()
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
606 obj = cx.new_object()
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
607 cx.init_standard_classes(obj)
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
608 cx.evaluate_script(obj, 'var boop = 5', '<string>', 1)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
609 cx.evaluate_script(obj, 'this["blarg\u2026"] = 5', '<string>', 1)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
610 self.assertEqual(cx.get_property(obj, u"beans"),
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
611 pydermonkey.undefined)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
612 self.assertEqual(cx.get_property(obj, u"blarg\u2026"), 5)
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
613 self.assertEqual(cx.get_property(obj, u"boop"), 5)
17
0812422ec99e Added a context.get_property() method.
Atul Varma <varmaa@toolness.com>
parents: 16
diff changeset
614
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
615 def testContextIsInstance(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
616 cx = pydermonkey.Runtime().new_context()
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
617 self.assertTrue(isinstance(cx, pydermonkey.Context))
9
032cfc448079 Added pymonkey.Runtime and pymonkey.Context as new types.
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
618
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
619 def testContextTypeCannotBeInstantiated(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
620 self.assertRaises(TypeError, pydermonkey.Context)
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
621
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
622 def testObjectIsInstance(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
623 obj = pydermonkey.Runtime().new_context().new_object()
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
624 self.assertTrue(isinstance(obj, pydermonkey.Object))
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
625 self.assertFalse(isinstance(obj, pydermonkey.Function))
13
ca17531e8c81 Added an object class.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
626
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
627 def testObjectTypeCannotBeInstantiated(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
628 self.assertRaises(TypeError, pydermonkey.Object)
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
629
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
630 def testFunctionIsInstance(self):
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
631 def boop():
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
632 pass
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
633 obj = pydermonkey.Runtime().new_context().new_function(boop, "boop")
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
634 self.assertTrue(isinstance(obj, pydermonkey.Object))
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
635 self.assertTrue(isinstance(obj, pydermonkey.Function))
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
636
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
637 def testFunctionTypeCannotBeInstantiated(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
638 self.assertRaises(TypeError, pydermonkey.Function)
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
639
77
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
640 def testObjectGetRuntimeWorks(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
641 rt = pydermonkey.Runtime()
77
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
642 obj = rt.new_context().new_object()
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
643 self.assertEqual(obj.get_runtime(), rt)
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
644
755af0a94c94 Added a pymonkey.Object.get_runtime() method.
Atul Varma <varmaa@toolness.com>
parents: 76
diff changeset
645 def testContextGetRuntimeWorks(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
646 rt = pydermonkey.Runtime()
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
647 cx = rt.new_context()
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
648 self.assertEqual(cx.get_runtime(), rt)
14
9edcdb4ab12d added an init_standard_classes() method to context objects.
Atul Varma <varmaa@toolness.com>
parents: 13
diff changeset
649
129
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
650 def testRuntimesAreWeakReferencable(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
651 rt = pydermonkey.Runtime()
129
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
652 wrt = weakref.ref(rt)
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
653 self.assertEqual(rt, wrt())
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
654 del rt
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
655 self.assertEqual(wrt(), None)
7b7a23615873 Added weakref support for runtimes.
Atul Varma <varmaa@toolness.com>
parents: 127
diff changeset
656
126
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
657 def testContextsAreWeakReferencable(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
658 rt = pydermonkey.Runtime()
126
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
659 cx = rt.new_context()
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
660 wcx = weakref.ref(cx)
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
661 self.assertEqual(cx, wcx())
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
662 del cx
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
663 self.assertEqual(wcx(), None)
8371983fee63 Added support for weakrefs to contexts.
Atul Varma <varmaa@toolness.com>
parents: 123
diff changeset
664
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
665 def testUndefinedCannotBeInstantiated(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
666 self.assertRaises(TypeError, pydermonkey.undefined)
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
667
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
668 def testEvaluateThrowsException(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
669 cx = pydermonkey.Runtime().new_context()
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
670 obj = cx.new_object()
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
671 self.assertRaises(pydermonkey.error,
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
672 cx.evaluate_script,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
673 obj, 'hai2u()', '<string>', 1)
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
674 self.assertEqual(self._tostring(cx,
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
675 self.last_exception.args[0]),
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
676 'ReferenceError: hai2u is not defined')
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
677
123
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
678 def testThrowingObjWithBadToStringWorks(self):
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
679 self.assertRaises(
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
680 pydermonkey.error,
123
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
681 self._evaljs,
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
682 "throw {toString: function() { throw 'dujg' }}"
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
683 )
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
684 self.assertEqual(
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
685 self.last_exception.args[1],
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
686 "<string conversion failed>"
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
687 )
08a012e96f62 Added another test and fixed a bug it revealed.
Atul Varma <varmaa@toolness.com>
parents: 122
diff changeset
688
122
5eda03c8e756 context.evaluate_script() can now take unicode code strings.
Atul Varma <varmaa@toolness.com>
parents: 120
diff changeset
689 def testEvaluateTakesUnicodeCode(self):
5eda03c8e756 context.evaluate_script() can now take unicode code strings.
Atul Varma <varmaa@toolness.com>
parents: 120
diff changeset
690 self.assertEqual(self._evaljs(u"'foo\u2026'"),
5eda03c8e756 context.evaluate_script() can now take unicode code strings.
Atul Varma <varmaa@toolness.com>
parents: 120
diff changeset
691 u"foo\u2026")
5eda03c8e756 context.evaluate_script() can now take unicode code strings.
Atul Varma <varmaa@toolness.com>
parents: 120
diff changeset
692
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
693 def testEvaluateReturnsUndefined(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
694 retval = self._evaljs("")
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
695 self.assertTrue(retval is pydermonkey.undefined)
8
6647870380cc Added support for undefined.
Atul Varma <varmaa@toolness.com>
parents: 7
diff changeset
696
92
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
697 def testEvaludateReturnsUnicodeWithEmbeddedNULs(self):
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
698 retval = self._evaljs("'\x00hi'")
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
699 self.assertEqual(retval, u'\x00hi')
d2891ca1b89c Added tests involving unicode with embedded NUL characters.
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
700
28
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
701 def testEvaluateReturnsSMPUnicode(self):
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
702 # This is 'LINEAR B SYLLABLE B008 A', in the supplementary
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
703 # multilingual plane (SMP).
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
704 retval = self._evaljs("'\uD800\uDC00'")
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
705 self.assertEqual(retval, u'\U00010000')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
706 self.assertEqual(retval.encode('utf-16'),
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
707 '\xff\xfe\x00\xd8\x00\xdc')
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
708
bd30f5c02fc3 Added a new test for supplementary multilingual plane unicode.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
709 def testEvaluateReturnsBMPUnicode(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
710 retval = self._evaljs("'o hai\u2026'")
7
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
711 self.assertTrue(type(retval) == unicode)
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
712 self.assertEqual(retval, u'o hai\u2026')
0d0ce6415b66 Added support for unicode strings.
Atul Varma <varmaa@toolness.com>
parents: 5
diff changeset
713
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
714 def testEvaluateReturnsObject(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
715 cx = pydermonkey.Runtime().new_context()
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
716 obj = cx.new_object()
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
717 cx.init_standard_classes(obj)
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
718 obj = cx.evaluate_script(obj, '({boop: 1})', '<string>', 1)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
719 self.assertTrue(isinstance(obj, pydermonkey.Object))
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
720 self.assertEqual(cx.get_property(obj, u"boop"), 1)
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
721
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
722 def testScriptedFunctionsHaveFilenameInfo(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
723 cx = pydermonkey.Runtime().new_context()
154
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
724 obj = cx.new_object()
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
725 cx.init_standard_classes(obj)
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
726 jsfunc = cx.evaluate_script(obj,
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
727 '(function boop() { \nreturn 1; })',
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
728 'somefile', 5)
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
729 self.assertEqual(jsfunc.filename, 'somefile')
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
730 self.assertEqual(jsfunc.base_lineno, 5)
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
731 self.assertEqual(jsfunc.line_extent, 2)
5ec81091cb89 Added filename/line-number metadata to functions. Wanted to just add a 'script' property but this proved problematic--see the TODO in the code for more information.
Atul Varma <varmaa@toolness.com>
parents: 153
diff changeset
732
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
733 def testEvaluateReturnsFunction(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
734 cx = pydermonkey.Runtime().new_context()
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
735 obj = cx.new_object()
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
736 cx.init_standard_classes(obj)
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
737 obj = cx.evaluate_script(obj, '(function boop() { return 1; })',
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
738 '<string>', 1)
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
739 self.assertTrue(isinstance(obj, pydermonkey.Function))
40
8a7abd0bb48d Renamed PYM_newJSFunction() to PYM_newJSFunctionFromCallable(). PYM_newJSObject() now returns objects of type PYM_JSFunctionType as needed.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
740
88
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
741 def testJsExceptionStateIsClearedAfterExceptionIsCaught(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
742 cx = pydermonkey.Runtime().new_context()
88
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
743 obj = cx.new_object()
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
744 self.assertRaises(pydermonkey.error,
88
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
745 cx.evaluate_script,
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
746 obj, 'blah()', '<string>', 1)
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
747 self.assertEqual(cx.evaluate_script(obj, '5+3', '<string>', 1),
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
748 8)
0b2970e8cd67 Added a test.
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
749
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
750 def testCallFunctionRaisesErrorOnBadFuncArgs(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
751 cx = pydermonkey.Runtime().new_context()
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
752 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
753 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
754 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
755 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
756 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
757 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
758 self.assertRaises(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
759 NotImplementedError,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
760 cx.call_function,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
761 obj, obj, (1, self)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
762 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
763
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
764 def _tostring(self, cx, obj):
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
765 return cx.call_function(obj,
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
766 cx.get_property(obj, u"toString"),
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
767 ())
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
768
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
769 def testCallFunctionRaisesErrorFromJS(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
770 cx = pydermonkey.Runtime().new_context()
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
771 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
772 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
773 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
774 '(function boop(a, b) { blarg(); })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
775 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
776 )
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
777 self.assertRaises(pydermonkey.error,
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
778 cx.call_function,
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
779 obj, obj, (1,))
74
e06376295170 JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
780 self.assertEqual(self._tostring(cx,
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
781 self.last_exception.args[0]),
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
782 'ReferenceError: blarg is not defined')
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
783
89
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
784 def testInitStandardClassesRaisesExcOnRuntimeMismatch(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
785 cx2 = pydermonkey.Runtime().new_context()
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
786 cx = pydermonkey.Runtime().new_context()
89
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
787 obj = cx.new_object()
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
788 self.assertRaises(ValueError,
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
789 cx2.init_standard_classes,
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
790 obj)
99
e4f7cc6beafe Changed exception.message to exception.args[0], to satisfy a deprecation warning in Python 2.6.
Atul Varma <varmaa@toolness.com>
parents: 95
diff changeset
791 self.assertEqual(self.last_exception.args[0],
89
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
792 'JS runtime mismatch')
e77bc7c799e8 JS runtime mismatches are now checked for and enforced so they won't cause segfaults.
Atul Varma <varmaa@toolness.com>
parents: 88
diff changeset
793
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
794 def testCallFunctionWorks(self):
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 166
diff changeset
795 cx = pydermonkey.Runtime().new_context()
41
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
796 obj = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
797 thisArg = cx.new_object()
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
798 cx.define_property(thisArg, "c", 3)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
799 cx.init_standard_classes(obj)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
800 obj = cx.evaluate_script(
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
801 obj,
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
802 '(function boop(a, b) { return a+b+this.c; })',
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
803 '<string>', 1
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
804 )
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
805 self.assertEqual(cx.call_function(thisArg, obj, (1,2)), 6)
71ab5e977dd3 Added a context.call_function() method.
Atul Varma <varmaa@toolness.com>
parents: 40
diff changeset
806
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
807 def testEvaluateReturnsTrue(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
808 self.assertTrue(self._evaljs('true') is True)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
809
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
810 def testEvaluateReturnsFalse(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
811 self.assertTrue(self._evaljs('false') is False)
5
aae78eac86d6 Added support for booleans.
Atul Varma <varmaa@toolness.com>
parents: 4
diff changeset
812
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
813 def testEvaluateReturnsNone(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
814 self.assertTrue(self._evaljs('null') is None)
4
2711b636f8e6 Added support for NULL return values.
Atul Varma <varmaa@toolness.com>
parents: 3
diff changeset
815
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
816 def testEvaluateReturnsIntegers(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
817 self.assertEqual(self._evaljs('1+3'), 4)
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
818
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
819 def testEvaluateReturnsNegativeIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
820 self.assertEqual(self._evaljs('-5'), -5)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
821
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
822 def testEvaluateReturnsBigIntegers(self):
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
823 self.assertEqual(self._evaljs('2147483647*2'),
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
824 2147483647*2)
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
825
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
826 def testEvaluateReturnsFloats(self):
16
532b7ddca616 Created a new context.evaluate_script() function, replacing pymonkey.evaluate(). Separated out one of the big unit tests into several.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
827 self.assertEqual(self._evaljs('1.1+3'), 4.1)
3
d6a0819ca6ca Added return value support for doubles.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
828
0
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
829 if __name__ == '__main__':
21aa6e3abb49 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
830 unittest.main()