comparison docs/rendered/pymonkey.html @ 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
128 stored within the new JS object; it can be retrieved using 128 stored within the new JS object; it can be retrieved using
129 <a title="pymonkey.Context.get_object_private" class="reference" href="#pymonkey.Context.get_object_private"><tt class="xref docutils literal"><span class="pre">get_object_private()</span></tt></a>.</dd></dl> 129 <a title="pymonkey.Context.get_object_private" class="reference" href="#pymonkey.Context.get_object_private"><tt class="xref docutils literal"><span class="pre">get_object_private()</span></tt></a>.</dd></dl>
130 130
131 <dl class="method"> 131 <dl class="method">
132 <dt id="pymonkey.Context.new_function"> 132 <dt id="pymonkey.Context.new_function">
133 <tt class="descname">new_function</tt><big>(</big><em>callable</em>, <em>name</em><big>)</big><a class="headerlink" href="#pymonkey.Context.new_function" title="Permalink to this definition">¶</a></dt> 133 <tt class="descname">new_function</tt><big>(</big><em>func</em>, <em>name</em><big>)</big><a class="headerlink" href="#pymonkey.Context.new_function" title="Permalink to this definition">¶</a></dt>
134 <dd><p>Creates a new <a title="pymonkey.Function" class="reference" href="#pymonkey.Function"><tt class="xref docutils literal"><span class="pre">Function</span></tt></a> instance that wraps the 134 <dd><p>Creates a new <a title="pymonkey.Function" class="reference" href="#pymonkey.Function"><tt class="xref docutils literal"><span class="pre">Function</span></tt></a> instance that wraps the
135 given Python callable. In JS-land, the function will 135 given Python callable. In JS-land, the function will
136 have the given name.</p> 136 have the given name.</p>
137 <p>When the function is executed from JavaScript, <cite>callable</cite> 137 <p>When the function is executed from JavaScript, <cite>func</cite>
138 will be passed three positional arguments.</p> 138 will be passed three positional arguments.</p>
139 <p>The first argument is a <a title="pymonkey.Context" class="reference" href="#pymonkey.Context"><tt class="xref docutils literal"><span class="pre">Context</span></tt></a> that represents the 139 <p>The first argument is a <a title="pymonkey.Context" class="reference" href="#pymonkey.Context"><tt class="xref docutils literal"><span class="pre">Context</span></tt></a> that represents the
140 JS context which is calling the function.</p> 140 JS context which is calling the function.</p>
141 <p>The second argument is an <a title="pymonkey.Object" class="reference" href="#pymonkey.Object"><tt class="xref docutils literal"><span class="pre">Object</span></tt></a> that represents the 141 <p>The second argument is an <a title="pymonkey.Object" class="reference" href="#pymonkey.Object"><tt class="xref docutils literal"><span class="pre">Object</span></tt></a> that represents the
142 value of <tt class="docutils literal"><span class="pre">this</span></tt> for the duration of the call.</p> 142 value of <tt class="docutils literal"><span class="pre">this</span></tt> for the duration of the call.</p>
249 <dd>Defines the standard JavaScript classes on the given 249 <dd>Defines the standard JavaScript classes on the given
250 <a title="pymonkey.Object" class="reference" href="#pymonkey.Object"><tt class="xref docutils literal"><span class="pre">Object</span></tt></a>, such as <tt class="docutils literal"><span class="pre">Array</span></tt>, <tt class="docutils literal"><span class="pre">eval</span></tt>, <tt class="docutils literal"><span class="pre">undefined</span></tt>, and 250 <a title="pymonkey.Object" class="reference" href="#pymonkey.Object"><tt class="xref docutils literal"><span class="pre">Object</span></tt></a>, such as <tt class="docutils literal"><span class="pre">Array</span></tt>, <tt class="docutils literal"><span class="pre">eval</span></tt>, <tt class="docutils literal"><span class="pre">undefined</span></tt>, and
251 so forth. For more information, see the documentation to 251 so forth. For more information, see the documentation to
252 <a class="reference" href="https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_InitStandardClasses">JS_InitStandardClasses()</a>, 252 <a class="reference" href="https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_InitStandardClasses">JS_InitStandardClasses()</a>,
253 which this method wraps.</dd></dl> 253 which this method wraps.</dd></dl>
254
255 <dl class="method">
256 <dt id="pymonkey.Context.gc">
257 <tt class="descname">gc</tt><big>(</big><big>)</big><a class="headerlink" href="#pymonkey.Context.gc" title="Permalink to this definition">¶</a></dt>
258 <dd>Performs garbage collection on the context&#8217;s JavaScript runtime.</dd></dl>
259
260 <dl class="method">
261 <dt id="pymonkey.Context.set_operation_callback">
262 <tt class="descname">set_operation_callback</tt><big>(</big><em>func</em><big>)</big><a class="headerlink" href="#pymonkey.Context.set_operation_callback" title="Permalink to this definition">¶</a></dt>
263 <dd><p>Sets the operation callback for the context to the given Python
264 callable. The callback can be triggered via
265 <a title="pymonkey.Context.trigger_operation_callback" class="reference" href="#pymonkey.Context.trigger_operation_callback"><tt class="xref docutils literal"><span class="pre">trigger_operation_callback()</span></tt></a>.</p>
266 <p><cite>func</cite> takes one argument: the context that triggered it.</p>
267 </dd></dl>
268
269 <dl class="method">
270 <dt id="pymonkey.Context.trigger_operation_callback">
271 <tt class="descname">trigger_operation_callback</tt><big>(</big><big>)</big><a class="headerlink" href="#pymonkey.Context.trigger_operation_callback" title="Permalink to this definition">¶</a></dt>
272 <dd><p>Triggers the context&#8217;s operation callback. If no callback has
273 yet been set, this function does nothing.</p>
274 <p>This function is one of the few thread-safe functions available
275 to a JS runtime, and together with
276 <a title="pymonkey.Context.set_operation_callback" class="reference" href="#pymonkey.Context.set_operation_callback"><tt class="xref docutils literal"><span class="pre">set_operation_callback()</span></tt></a> can be used to abort the
277 execution of long-running code.</p>
278 <p>For instance:</p>
279 <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">threading</span>
280 <span class="gp">&gt;&gt;&gt; </span><span class="n">cx</span> <span class="o">=</span> <span class="n">pymonkey</span><span class="o">.</span><span class="n">Runtime</span><span class="p">()</span><span class="o">.</span><span class="n">new_context</span><span class="p">()</span>
281 <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">stop_running_code</span><span class="p">(</span><span class="n">cx</span><span class="p">):</span>
282 <span class="gp">... </span> <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;JS took too long to execute.&#39;</span><span class="p">)</span>
283 <span class="gp">&gt;&gt;&gt; </span><span class="n">cx</span><span class="o">.</span><span class="n">set_operation_callback</span><span class="p">(</span><span class="n">stop_running_code</span><span class="p">)</span>
284 <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">watchdog_thread</span><span class="p">():</span>
285 <span class="gp">... </span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
286 <span class="gp">... </span> <span class="n">cx</span><span class="o">.</span><span class="n">trigger_operation_callback</span><span class="p">()</span>
287 <span class="gp">&gt;&gt;&gt; </span><span class="n">thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">watchdog_thread</span><span class="p">)</span>
288 <span class="gp">&gt;&gt;&gt; </span><span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
289 <span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
290 <span class="gp">... </span> <span class="n">cx</span><span class="o">.</span><span class="n">evaluate_script</span><span class="p">(</span><span class="n">cx</span><span class="o">.</span><span class="n">new_object</span><span class="p">(),</span> <span class="s">&#39;while (1) {}&#39;</span><span class="p">,</span>
291 <span class="gp">... </span> <span class="s">&#39;&lt;string&gt;&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
292 <span class="gp">... </span><span class="k">except</span> <span class="n">pymonkey</span><span class="o">.</span><span class="n">error</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
293 <span class="gp">... </span> <span class="k">print</span> <span class="n">cx</span><span class="o">.</span><span class="n">get_object_private</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mf">0</span><span class="p">])</span>
294 <span class="go">JS took too long to execute.</span>
295 </pre></div>
296 </div>
297 </dd></dl>
254 298
255 </dd></dl> 299 </dd></dl>
256 300
257 <dl class="class"> 301 <dl class="class">
258 <dt id="pymonkey.Runtime"> 302 <dt id="pymonkey.Runtime">