comparison docs/rendered/_sources/pymonkey.txt @ 115:f4c550369332

Added documentation for gc() and operation callback functions.
author Atul Varma <varmaa@toolness.com>
date Mon, 17 Aug 2009 03:33:40 -0700
parents 87147faa031a
children 06269ca0b36c
comparison
equal deleted inserted replaced
114:87147faa031a 115:f4c550369332
74 Creates a new :class:`Object` instance and returns 74 Creates a new :class:`Object` instance and returns
75 it. ``private_obj`` is any Python object that is privately 75 it. ``private_obj`` is any Python object that is privately
76 stored within the new JS object; it can be retrieved using 76 stored within the new JS object; it can be retrieved using
77 :meth:`get_object_private()`. 77 :meth:`get_object_private()`.
78 78
79 .. method:: new_function(callable, name) 79 .. method:: new_function(func, name)
80 80
81 Creates a new :class:`Function` instance that wraps the 81 Creates a new :class:`Function` instance that wraps the
82 given Python callable. In JS-land, the function will 82 given Python callable. In JS-land, the function will
83 have the given name. 83 have the given name.
84 84
85 When the function is executed from JavaScript, `callable` 85 When the function is executed from JavaScript, `func`
86 will be passed three positional arguments. 86 will be passed three positional arguments.
87 87
88 The first argument is a :class:`Context` that represents the 88 The first argument is a :class:`Context` that represents the
89 JS context which is calling the function. 89 JS context which is calling the function.
90 90
194 so forth. For more information, see the documentation to 194 so forth. For more information, see the documentation to
195 `JS_InitStandardClasses() 195 `JS_InitStandardClasses()
196 <https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_InitStandardClasses>`_, 196 <https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_InitStandardClasses>`_,
197 which this method wraps. 197 which this method wraps.
198 198
199 .. method:: gc()
200
201 Performs garbage collection on the context's JavaScript runtime.
202
203 .. method:: set_operation_callback(func)
204
205 Sets the operation callback for the context to the given Python
206 callable. The callback can be triggered via
207 :meth:`trigger_operation_callback()`.
208
209 `func` takes one argument: the context that triggered it.
210
211 .. method:: trigger_operation_callback()
212
213 Triggers the context's operation callback. If no callback has
214 yet been set, this function does nothing.
215
216 This function is one of the few thread-safe functions available
217 to a JS runtime, and together with
218 :meth:`set_operation_callback()` can be used to abort the
219 execution of long-running code.
220
221 For instance:
222
223 >>> import time, threading
224 >>> cx = pymonkey.Runtime().new_context()
225 >>> def stop_running_code(cx):
226 ... raise Exception('JS took too long to execute.')
227 >>> cx.set_operation_callback(stop_running_code)
228 >>> def watchdog_thread():
229 ... time.sleep(0.1)
230 ... cx.trigger_operation_callback()
231 >>> thread = threading.Thread(target=watchdog_thread)
232 >>> thread.start()
233 >>> try:
234 ... cx.evaluate_script(cx.new_object(), 'while (1) {}',
235 ... '<string>', 1)
236 ... except pymonkey.error, e:
237 ... print cx.get_object_private(e.args[0])
238 JS took too long to execute.
239
199 .. class:: Runtime() 240 .. class:: Runtime()
200 241
201 Creates a new JavaScript runtime. JS objects created by the runtime 242 Creates a new JavaScript runtime. JS objects created by the runtime
202 may only interact with other JS objects of the same runtime. 243 may only interact with other JS objects of the same runtime.
203 244