annotate docs/src/pymonkey.txt @ 161:28d067082390

Added docs for function.is_python.
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 15:50:56 -0700
parents e57d8083fb3d
children 4edaa0e6f382
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
78
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
48 .. method:: get_runtime()
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
49
c48b393f6461 Added docs for Object.get_runtime().
Atul Varma <varmaa@toolness.com>
parents: 58
diff changeset
50 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
51
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
52 .. class:: Function
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
53
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
54 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
55 :class:`Object`.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
56
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
57 .. data:: filename
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
58
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
59 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
60 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
61 ``None``.
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
62
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
63 .. data:: base_lineno
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
64
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
65 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
66
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
67 .. data:: line_extent
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 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
70
161
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
71 .. data:: is_python
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
72
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
73 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
74 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
75 retrieve this Python function.
28d067082390 Added docs for function.is_python.
Atul Varma <varmaa@toolness.com>
parents: 159
diff changeset
76
139
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
77 .. class:: Script
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
78
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
79 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
80 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
81 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
82
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
83 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
84 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
85 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
86
144
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
87 .. data:: filename
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
88
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
89 The filename of the script's original source code.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
90
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
91 .. data:: base_lineno
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
92
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
93 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: 140
diff changeset
94 begins.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
95
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
96 .. data:: line_extent
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
97
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
98 The number of lines comprising the original source code.
ab612d2ad96a Updated docs to reflect new script members.
Atul Varma <varmaa@toolness.com>
parents: 140
diff changeset
99
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
100 .. 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
101
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
102 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
103 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
104 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
105 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
106
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
107 >>> 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
108 >>> 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
109 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
110
130
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
111 JS contexts are weak-referencable.
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
112
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
113 .. 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
114
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
115 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
116
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
117 .. method:: get_stack()
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
118
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
119 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
120 current stack.
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
121
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
122 The dictionary contains the following string keys:
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
123
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
124 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
125 | key | value |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
126 +==============================+=====================================+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
127 | :const:`script` | :class:`Script` of the frame. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
128 | | 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
129 | | 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
130 | | not possible to expose the script |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
131 | | to Python for some reason. |
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
132 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
133 | :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
134 | | frame is scripted. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
135 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
136 | :const:`pc` | Program counter of the frame; this |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
137 | | is an index into the bytecode of |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
138 | | the frame's script, if the frame |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
139 | | is scripted. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
140 +------------------------------+-------------------------------------+
159
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
141 | :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
142 | | frame is executing, if any. |
e57d8083fb3d Added more documentation and a unit test.
Atul Varma <varmaa@toolness.com>
parents: 150
diff changeset
143 +------------------------------+-------------------------------------+
148
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
144 | :const:`caller` | Dictionary containing information |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
145 | | about the stack frame of the |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
146 | | caller of this frame. If this frame |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
147 | | has no caller, this value is |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
148 | | ``None``. |
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
149 +------------------------------+-------------------------------------+
0b1020c817b3 Added docs for context.get_stack().
Atul Varma <varmaa@toolness.com>
parents: 144
diff changeset
150
150
aea82140758c Added more to get_stack() docs.
Atul Varma <varmaa@toolness.com>
parents: 148
diff changeset
151 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
152 is empty), this method returns ``None``.
aea82140758c Added more to get_stack() docs.
Atul Varma <varmaa@toolness.com>
parents: 148
diff changeset
153
79
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
154 .. method:: new_object([private_obj])
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
155
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
156 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
157 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
158 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
159 :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
160
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
161 .. method:: new_function(func, name)
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
162
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
163 Creates a new :class:`Function` instance that wraps the
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
164 given Python callable. In JS-land, the function will
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
165 have the given name.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
166
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
167 When the function is executed from JavaScript, `func`
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
168 will be passed three positional arguments.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
169
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
170 The first argument is a :class:`Context` that represents the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
171 JS context which is calling the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
172
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
173 The second argument is an :class:`Object` that represents the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
174 value of ``this`` for the duration of the call.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
175
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
176 The third argument is a tuple containing the arguments
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
177 passed to the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
178
111
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
179 For instance:
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
180
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
181 >>> def add(cx, this, args):
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
182 ... return args[0] + args[1]
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
183 >>> cx = pymonkey.Runtime().new_context()
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
184 >>> obj = cx.new_object()
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
185 >>> cx.define_property(obj, 'add', cx.new_function(add, 'add'))
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
186 >>> cx.evaluate_script(obj, 'add(1, 1);', '<string>', 1)
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
187 2
df607254de2d Added a doctest.
Atul Varma <varmaa@toolness.com>
parents: 90
diff changeset
188
114
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
189 .. method:: define_property(object, name, value)
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
190
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
191 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
192
134
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
193 .. method:: has_property(object, name)
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
194
0c81cb18c935 Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents: 130
diff changeset
195 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
196
114
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
197 .. method:: get_property(object, name)
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
198
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
199 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
200 possibly invoking a JavaScript getter.
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
201
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
202 Example:
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
203
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
204 >>> cx = pymonkey.Runtime().new_context()
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
205 >>> obj = cx.new_object()
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
206 >>> 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
207 >>> cx.get_property(obj, 'beets')
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
208 u'i like beets.'
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
209
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
210 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
211 yields :data:`undefined`:
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
212
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
213 >>> cx.get_property(obj, 'carrots')
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
214 pymonkey.undefined
87147faa031a Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents: 113
diff changeset
215
79
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
216 .. method:: get_object_private(object)
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
217
80
4945e4073110 Fixed typos in docs.
Atul Varma <varmaa@toolness.com>
parents: 79
diff changeset
218 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
219 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
220 is returned.
228a56e7e793 Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
221
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
222 If `object` was created with :meth:`new_function()`, then this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
223 method returns the Python callable wrapped by `object`.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
224
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
225 This functionality is useful if you want to securely represent
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
226 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
227
85
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
228 .. method:: clear_object_private(object)
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
229
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
230 Clears the ``private_obj`` passed to :meth:`new_object()`
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
231 when `object` was first created. If it doesn't exist, this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
232 function returns nothing.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
233
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
234 If `object` was created with :meth:`new_function()`, then this
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
235 method effectively "unbinds" the Python callable wrapped by
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
236 `object`. If `object` is later called, an exception will be
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
237 raised.
e9f450d30c0e Added more documentation.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
238
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
239 .. method:: evaluate_script(globalobj, code, filename, lineno)
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
240
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
241 Evaluates the text `code` using `globalobj` as the global
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
242 object/scope.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
243
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
244 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
245 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
246 `filename`. This metadata is very useful for debugging stack traces,
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
247 exceptions, and so forth.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
248
113
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
249 For example:
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
250
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
251 >>> cx = pymonkey.Runtime().new_context()
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
252 >>> obj = cx.new_object()
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
253 >>> cx.init_standard_classes(obj)
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
254 >>> cx.evaluate_script(obj, '5 * Math', '<string>', 1)
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
255 nan
e616b4605db0 Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents: 112
diff changeset
256
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: 139
diff changeset
257 .. 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
258
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
259 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
260 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
261
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
262 `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
263 :meth:`evaluate_script()`.
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
264
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
265 .. method:: execute_script(globalobj, script)
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
266
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
267 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
268 `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
269
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
270 For example:
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 >>> cx = pymonkey.Runtime().new_context()
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
273 >>> obj = cx.new_object()
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
274 >>> 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: 139
diff changeset
275 >>> 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
276 >>> cx.execute_script(obj, script)
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
277 nan
d08cb5a06c66 Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents: 134
diff changeset
278
90
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
279 .. method:: call_function(thisobj, func, args)
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
280
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
281 Calls a JavaScript function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
282
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
283 `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
284 of ``this`` when the function executes, `func` is the
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
285 :class:`Function` to execute, and `args` is a tuple of arguments
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
286 to pass to the function.
c41f1d2e8f9d Added more docs.
Atul Varma <varmaa@toolness.com>
parents: 85
diff changeset
287
112
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
288 For instance:
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
289
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
290 >>> cx = pymonkey.Runtime().new_context()
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
291 >>> obj = cx.new_object()
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
292 >>> cx.init_standard_classes(obj)
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
293 >>> Math = cx.get_property(obj, 'Math')
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
294 >>> floor = cx.get_property(Math, 'floor')
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
295 >>> cx.call_function(Math, floor, (5.3,))
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
296 5
2086377c0abe Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents: 111
diff changeset
297
58
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
298 .. 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
299
7a3461ccaf1d Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents: 57
diff changeset
300 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
301 :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
302 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
303 `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
304 <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
305 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
306
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
307 .. method:: gc()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
308
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
309 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
310
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
311 .. method:: set_operation_callback(func)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
312
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
313 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
314 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
315 :meth:`trigger_operation_callback()`.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
316
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
317 `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
318
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
319 .. method:: trigger_operation_callback()
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 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
322 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
323
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
324 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
325 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
326 :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
327 execution of long-running code.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
328
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
329 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
330 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
331
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
332 >>> import time, threading
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
333 >>> cx = pymonkey.Runtime().new_context()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
334 >>> def stop_running_code(cx):
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
335 ... 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
336 >>> 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
337
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
338 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
339 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
340
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
341 >>> def watchdog_thread():
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
342 ... 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
343 ... cx.trigger_operation_callback()
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
344 >>> thread = threading.Thread(target=watchdog_thread)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
345 >>> thread.start()
116
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
346
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
347 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
348 aborted:
06269ca0b36c Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents: 115
diff changeset
349
115
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
350 >>> try:
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
351 ... 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
352 ... '<string>', 1)
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
353 ... except pymonkey.error, e:
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
354 ... 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
355 JS took too long to execute.
f4c550369332 Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents: 114
diff changeset
356
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
357 .. 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
358
53
2055d853b995 Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents: 51
diff changeset
359 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
360 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
361
119
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
362 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
363 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
364 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
365 has support for thread safety.
d30576edf797 Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents: 116
diff changeset
366
130
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
367 JS runtimes are weak-referencable.
4705522c7431 Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents: 119
diff changeset
368
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
369 .. 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
370
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 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
372 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
373 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
374 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
375 that are associated with the same JS runtime as the objects.