annotate docs/rendered/_sources/pymonkey.txt @ 164:3fadba042201

Minor rewording in docs.
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 17:07:38 -0700
parents 4edaa0e6f382
children b745c9d8eef9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
1 :mod:`pymonkey` --- Access SpiderMonkey from Python
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 ===================================================
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
3
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
4 .. module:: pymonkey
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
5 :synopsis: Access SpiderMonkey from Python
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
6
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 .. testsetup:: *
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
8
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
9 import pymonkey
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
10
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
11 This module offers a low-level interface to the `Mozilla SpiderMonkey
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
12 <https://developer.mozilla.org/en/SpiderMonkey>`_ JavaScript engine.
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
13
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
14 .. exception:: error
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
15
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
16 This is the type of any SpiderMonkey-related errors thrown by this
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
17 module.
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
18
54
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
19 .. data:: undefined
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
20
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
21 This is the singleton that represents the JavaScript value
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
22 ``undefined``, as Python has no equivalent representation
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
23 (JavaScript's ``null`` is mapped to Python's ``None`` object).
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
24 For instance:
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
25
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
26 >>> cx = pymonkey.Runtime().new_context()
56
72e84bd75905 Made another doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
27 >>> cx.evaluate_script(cx.new_object(), '', '<string>', 1)
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 56
diff changeset
28 pymonkey.undefined
54
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
29
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 56
diff changeset
30 This object also has a "falsy" value:
54
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
31
57
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 56
diff changeset
32 >>> if not pymonkey.undefined:
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 56
diff changeset
33 ... print "See, it's falsy!"
a2b617731398 pymonkey.undefined now has a 'falsy' value.
Atul Varma <varmaa@toolness.com>
parents: 56
diff changeset
34 See, it's falsy!
54
234fca1c4b86 Added docs for pymonkey.undefined.
Atul Varma <varmaa@toolness.com>
parents: 53
diff changeset
35
53
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
36 .. class:: Object
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
37
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
38 This is the type of JavaScript objects. Such objects can only be
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
39 created via Pymonkey calls like :meth:`Context.new_object()` or
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
40 through the execution of JS code, but this type object can be used
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
41 with Python's built-in :func:`isinstance()` to verify that an
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
42 object is a JS object, like so:
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
43
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
44 >>> obj = pymonkey.Runtime().new_context().new_object()
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
45 >>> isinstance(obj, pymonkey.Object)
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
46 True
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
47
163
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
48 Note that :class:`Object` and all its subclasses are
164
3fadba042201 Minor rewording in docs.
Atul Varma <varmaa@toolness.com>
parents: 163
diff changeset
49 identity-preserving when passed between Python and
3fadba042201 Minor rewording in docs.
Atul Varma <varmaa@toolness.com>
parents: 163
diff changeset
50 JavaScript code. For instance:
163
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
51
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
52 >>> cx = pymonkey.Runtime().new_context()
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
53 >>> obj1 = cx.new_object()
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
54 >>> obj2 = cx.evaluate_script(obj1, 'this', '<string>', 1)
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
55 >>> obj1 is obj2
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
56 True
4edaa0e6f382 Added notes on identity perseverance for objects.
Atul Varma <varmaa@toolness.com>
parents: 161
diff changeset
57
78
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
58 .. method:: get_runtime()
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
59
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
60 Returns the :class:`Runtime` that the object belongs to.
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
61
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
62 .. class:: Function
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
63
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
64 This is the type of JavaScript functions, which is a subtype of
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
65 :class:`Object`.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
66
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
67 .. data:: filename
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
68
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
69 Name of the file that contains the function's original JS source
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
70 code. If the function isn't a JS function, this value is
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
71 ``None``.
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
72
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
73 .. data:: base_lineno
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
74
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
75 The line number at which the function's JS source code begins.
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
76
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
77 .. data:: line_extent
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
78
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
79 The number of lines comprising the function's JS source code.
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
80
161
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
81 .. data:: is_python
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
82
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
83 Whether or not the function is actually implemented in Python.
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
84 If it is, you can use :meth:`Context.get_object_private()` to
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
85 retrieve this Python function.
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
86
139
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
87 .. class:: Script
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
88
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
89 This is the type of compiled JavaScript scripts; it's actually a
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
90 subtype of :class:`Object`, though when exposed to JS code it
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
91 doesn't provide access to any special data or functionality.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
92
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
93 Script instances have a read-only buffer interface that exposes
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
94 their bytecode. This can be accessed by passing an instance to
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
95 Python's built-in ``buffer()`` function.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
96
144
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
97 .. data:: filename
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
98
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
99 The filename of the script's original source code.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
100
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
101 .. data:: base_lineno
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
102
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
103 The line number at which the script's original source code
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
104 begins.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
105
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
106 .. data:: line_extent
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
107
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
108 The number of lines comprising the original source code.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 141
diff changeset
109
51
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
110 .. class:: Context
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
111
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
112 This is the type of JavaScript context objects. Contexts can only
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
113 be created via a call to :meth:`Runtime.new_context()`, but this
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
114 type object can be used with Python's built-in :func:`isinstance()`
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
115 to verify that an object is a context, like so:
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
116
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
117 >>> cx = pymonkey.Runtime().new_context()
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
118 >>> isinstance(cx, pymonkey.Context)
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
119 True
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
120
130
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
121 JS contexts are weak-referencable.
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
122
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
123 .. method:: get_runtime()
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
124
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
125 Returns the :class:`Runtime` that the context belongs to.
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
126
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
127 .. method:: get_stack()
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
128
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
129 Returns a dictionary containing information about the context's
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
130 current stack.
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
131
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
132 The dictionary contains the following string keys:
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
133
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
134 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
135 | key | value |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
136 +==============================+=====================================+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
137 | :const:`script` | :class:`Script` of the frame. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
138 | | This may be ``None`` if the frame |
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
139 | | isn't a scripted frame, or if it's |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
140 | | not possible to expose the script |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
141 | | to Python for some reason. |
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
142 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
143 | :const:`lineno` | Line number of the frame, if the |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
144 | | frame is scripted. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
145 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
146 | :const:`pc` | Program counter of the frame; this |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
147 | | is an index into the bytecode of |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
148 | | the frame's script, if the frame |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
149 | | is scripted. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
150 +------------------------------+-------------------------------------+
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
151 | :const:`function` | The :class:`Function` in which the |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
152 | | frame is executing, if any. |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
153 +------------------------------+-------------------------------------+
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
154 | :const:`caller` | Dictionary containing information |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
155 | | about the stack frame of the |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
156 | | caller of this frame. If this frame |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
157 | | has no caller, this value is |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
158 | | ``None``. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
159 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
160
150
aea82140758c Added more to get_stack() docs.
Atul Varma <varmaa@toolness.com>
parents: 148
diff changeset
161 If the context isn't currently executing any code (i.e., the stack
aea82140758c Added more to get_stack() docs.
Atul Varma <varmaa@toolness.com>
parents: 148
diff changeset
162 is empty), this method returns ``None``.
aea82140758c Added more to get_stack() docs.
Atul Varma <varmaa@toolness.com>
parents: 148
diff changeset
163
79
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
164 .. method:: new_object([private_obj])
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
165
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
166 Creates a new :class:`Object` instance and returns
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
167 it. ``private_obj`` is any Python object that is privately
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
168 stored within the new JS object; it can be retrieved using
80
4945e4073110 Fixed typos in docs.
Atul Varma <varmaa@toolness.com>
parents: 79
diff changeset
169 :meth:`get_object_private()`.
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
170
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
171 .. method:: new_function(func, name)
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
172
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
173 Creates a new :class:`Function` instance that wraps the
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
174 given Python callable. In JS-land, the function will
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
175 have the given name.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
176
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
177 When the function is executed from JavaScript, `func`
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
178 will be passed three positional arguments.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
179
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
180 The first argument is a :class:`Context` that represents the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
181 JS context which is calling the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
182
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
183 The second argument is an :class:`Object` that represents the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
184 value of ``this`` for the duration of the call.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
185
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
186 The third argument is a tuple containing the arguments
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
187 passed to the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
188
111
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
189 For instance:
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
190
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
191 >>> def add(cx, this, args):
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
192 ... return args[0] + args[1]
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
193 >>> cx = pymonkey.Runtime().new_context()
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
194 >>> obj = cx.new_object()
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
195 >>> cx.define_property(obj, 'add', cx.new_function(add, 'add'))
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
196 >>> cx.evaluate_script(obj, 'add(1, 1);', '<string>', 1)
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
197 2
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
198
114
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
199 .. method:: define_property(object, name, value)
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
200
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
201 Creates a new property on `object`, bypassing any JavaScript setters.
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
202
134
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
203 .. method:: has_property(object, name)
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
204
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
205 Returns whether or not `object` has the specified property.
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
206
114
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
207 .. method:: get_property(object, name)
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
208
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
209 Finds the specified property on `object` and returns its value,
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
210 possibly invoking a JavaScript getter.
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
211
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
212 Example:
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
213
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
214 >>> cx = pymonkey.Runtime().new_context()
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
215 >>> obj = cx.new_object()
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
216 >>> cx.define_property(obj, 'beets', 'i like beets.')
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
217 >>> cx.get_property(obj, 'beets')
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
218 u'i like beets.'
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
219
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
220 Note also that calling this function on undefined properties
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
221 yields :data:`undefined`:
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
222
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
223 >>> cx.get_property(obj, 'carrots')
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
224 pymonkey.undefined
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
225
79
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
226 .. method:: get_object_private(object)
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
227
80
4945e4073110 Fixed typos in docs.
Atul Varma <varmaa@toolness.com>
parents: 79
diff changeset
228 Returns the ``private_obj`` passed to :meth:`new_object()`
79
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
229 when `object` was first created. If it doesn't exist, ``None``
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
230 is returned.
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
231
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
232 If `object` was created with :meth:`new_function()`, then this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
233 method returns the Python callable wrapped by `object`.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
234
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
235 This functionality is useful if you want to securely represent
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
236 Python objects in JS-land.
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
237
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
238 .. method:: clear_object_private(object)
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
239
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
240 Clears the ``private_obj`` passed to :meth:`new_object()`
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
241 when `object` was first created. If it doesn't exist, this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
242 function returns nothing.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
243
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
244 If `object` was created with :meth:`new_function()`, then this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
245 method effectively "unbinds" the Python callable wrapped by
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
246 `object`. If `object` is later called, an exception will be
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
247 raised.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
248
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
249 .. method:: evaluate_script(globalobj, code, filename, lineno)
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
250
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
251 Evaluates the text `code` using `globalobj` as the global
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
252 object/scope.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
253
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
254 It's assumed that `code` is coming from the file named by `filename`;
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
255 the first line of `code` is assumed to be line number `lineno` of
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
256 `filename`. This metadata is very useful for debugging stack traces,
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
257 exceptions, and so forth.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
258
113
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
259 For example:
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
260
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
261 >>> cx = pymonkey.Runtime().new_context()
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
262 >>> obj = cx.new_object()
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
263 >>> cx.init_standard_classes(obj)
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
264 >>> cx.evaluate_script(obj, '5 * Math', '<string>', 1)
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
265 nan
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
266
141
96dc1beefc00 Regenerated docs.
Atul Varma <varmaa@toolness.com>
parents: 139
diff changeset
267 .. method:: compile_script(code, filename, lineno)
139
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
268
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
269 Compiles the given string of code and returns a :class:`Script`
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
270 instance that can be executed via :meth:`execute_script()`.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
271
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
272 `filename` and `lineno` are used just as in
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
273 :meth:`evaluate_script()`.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
274
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
275 .. method:: execute_script(globalobj, script)
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
276
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
277 Executes the code in the given :class:`Script` object, using
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
278 `globalobj` as the global object/scope, and returns the result.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
279
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
280 For example:
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
281
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
282 >>> cx = pymonkey.Runtime().new_context()
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
283 >>> obj = cx.new_object()
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
284 >>> cx.init_standard_classes(obj)
141
96dc1beefc00 Regenerated docs.
Atul Varma <varmaa@toolness.com>
parents: 139
diff changeset
285 >>> script = cx.compile_script('5 * Math', '<string>', 1)
139
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
286 >>> cx.execute_script(obj, script)
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
287 nan
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
288
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
289 .. method:: call_function(thisobj, func, args)
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
290
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
291 Calls a JavaScript function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
292
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
293 `thisobj` is an :class:`Object` that will be used as the value
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
294 of ``this`` when the function executes, `func` is the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
295 :class:`Function` to execute, and `args` is a tuple of arguments
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
296 to pass to the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
297
112
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
298 For instance:
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
299
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
300 >>> cx = pymonkey.Runtime().new_context()
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
301 >>> obj = cx.new_object()
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
302 >>> cx.init_standard_classes(obj)
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
303 >>> Math = cx.get_property(obj, 'Math')
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
304 >>> floor = cx.get_property(Math, 'floor')
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
305 >>> cx.call_function(Math, floor, (5.3,))
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
306 5
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
307
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
308 .. method:: init_standard_classes(object)
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
309
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
310 Defines the standard JavaScript classes on the given
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
311 :class:`Object`, such as ``Array``, ``eval``, ``undefined``, and
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
312 so forth. For more information, see the documentation to
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
313 `JS_InitStandardClasses()
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
314 <https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_InitStandardClasses>`_,
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
315 which this method wraps.
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
316
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
317 .. method:: gc()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
318
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
319 Performs garbage collection on the context's JavaScript runtime.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
320
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
321 .. method:: set_operation_callback(func)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
322
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
323 Sets the operation callback for the context to the given Python
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
324 callable. The callback can be triggered via
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
325 :meth:`trigger_operation_callback()`.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
326
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
327 `func` takes one argument: the context that triggered it.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
328
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
329 .. method:: trigger_operation_callback()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
330
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
331 Triggers the context's operation callback. If no callback has
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
332 yet been set, this function does nothing.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
333
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
334 This function is one of the few thread-safe functions available
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
335 to a JS runtime, and together with
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
336 :meth:`set_operation_callback()` can be used to abort the
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
337 execution of long-running code.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
338
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
339 For instance, we first create an operation callback to stop
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
340 long-running code by throwing an exception:
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
341
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
342 >>> import time, threading
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
343 >>> cx = pymonkey.Runtime().new_context()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
344 >>> def stop_running_code(cx):
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
345 ... raise Exception('JS took too long to execute.')
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
346 >>> cx.set_operation_callback(stop_running_code)
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
347
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
348 Then we create a watchdog thread to trigger the operation
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
349 callback once a long amount of time has passed:
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
350
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
351 >>> def watchdog_thread():
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
352 ... time.sleep(0.1) # An eternity to a computer!
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
353 ... cx.trigger_operation_callback()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
354 >>> thread = threading.Thread(target=watchdog_thread)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
355 >>> thread.start()
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
356
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
357 Now, when we execute code that takes too long to run, it gets
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
358 aborted:
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
359
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
360 >>> try:
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
361 ... cx.evaluate_script(cx.new_object(), 'while (1) {}',
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
362 ... '<string>', 1)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
363 ... except pymonkey.error, e:
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
364 ... print cx.get_object_private(e.args[0])
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
365 JS took too long to execute.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
366
51
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
367 .. class:: Runtime()
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
368
53
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
369 Creates a new JavaScript runtime. JS objects created by the runtime
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
370 may only interact with other JS objects of the same runtime.
51
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
371
119
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
372 With few exceptions, objects belonging to a runtime can currently
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
373 only be used in the same thread that the runtime was created
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
374 in. This may be changed in the future, since SpiderMonkey itself
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
375 has support for thread safety.
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
376
130
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
377 JS runtimes are weak-referencable.
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
378
51
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
379 .. method:: new_context()
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
380
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
381 Creates a new Context object and returns it. Contexts are best
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
382 conceptualized as threads of execution in a JS runtme; each one
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
383 has a program counter, a current exception state, and so
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
384 forth. JS objects may be freely accessed and changed by contexts
fabd3f2271fa Added some basic, incomplete docs on the pymonkey module, which include a doctest that's run when pymonkey is built.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
385 that are associated with the same JS runtime as the objects.