Mercurial > pymonkey
annotate docs/rendered/_sources/pymonkey.txt @ 139:d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 24 Aug 2009 11:39:58 -0700 |
parents | 0c81cb18c935 |
children | 96dc1beefc00 |
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 | 52 .. class:: Function |
53 | |
54 This is the type of JavaScript functions, which is a subtype of | |
55 :class:`Object`. | |
56 | |
139
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
57 .. class:: Script |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
58 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 |
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
|
67 .. 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
|
68 |
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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 |
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
|
74 >>> 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
|
75 >>> 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
|
76 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
|
77 |
130
4705522c7431
Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents:
119
diff
changeset
|
78 JS contexts are weak-referencable. |
4705522c7431
Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents:
119
diff
changeset
|
79 |
58
7a3461ccaf1d
Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents:
57
diff
changeset
|
80 .. 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
|
81 |
7a3461ccaf1d
Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents:
57
diff
changeset
|
82 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
|
83 |
79
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
84 .. method:: new_object([private_obj]) |
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
85 |
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
86 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
|
87 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
|
88 stored within the new JS object; it can be retrieved using |
80 | 89 :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
|
90 |
115
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
91 .. method:: new_function(func, name) |
85 | 92 |
93 Creates a new :class:`Function` instance that wraps the | |
90 | 94 given Python callable. In JS-land, the function will |
85 | 95 have the given name. |
96 | |
115
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
97 When the function is executed from JavaScript, `func` |
90 | 98 will be passed three positional arguments. |
99 | |
100 The first argument is a :class:`Context` that represents the | |
101 JS context which is calling the function. | |
102 | |
103 The second argument is an :class:`Object` that represents the | |
104 value of ``this`` for the duration of the call. | |
105 | |
106 The third argument is a tuple containing the arguments | |
107 passed to the function. | |
108 | |
111 | 109 For instance: |
110 | |
111 >>> def add(cx, this, args): | |
112 ... return args[0] + args[1] | |
113 >>> cx = pymonkey.Runtime().new_context() | |
114 >>> obj = cx.new_object() | |
115 >>> cx.define_property(obj, 'add', cx.new_function(add, 'add')) | |
116 >>> cx.evaluate_script(obj, 'add(1, 1);', '<string>', 1) | |
117 2 | |
118 | |
114
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
119 .. method:: define_property(object, name, value) |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
120 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
121 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
|
122 |
134
0c81cb18c935
Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents:
130
diff
changeset
|
123 .. method:: has_property(object, name) |
0c81cb18c935
Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents:
130
diff
changeset
|
124 |
0c81cb18c935
Added docs for context.has_property().
Atul Varma <varmaa@toolness.com>
parents:
130
diff
changeset
|
125 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
|
126 |
114
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
127 .. method:: get_property(object, name) |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
128 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
129 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
|
130 possibly invoking a JavaScript getter. |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
131 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
132 Example: |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
133 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
134 >>> cx = pymonkey.Runtime().new_context() |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
135 >>> obj = cx.new_object() |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
136 >>> 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
|
137 >>> cx.get_property(obj, 'beets') |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
138 u'i like beets.' |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
139 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
140 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
|
141 yields :data:`undefined`: |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
142 |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
143 >>> cx.get_property(obj, 'carrots') |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
144 pymonkey.undefined |
87147faa031a
Added docs for define_property() and get_property().
Atul Varma <varmaa@toolness.com>
parents:
113
diff
changeset
|
145 |
79
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
146 .. method:: get_object_private(object) |
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
147 |
80 | 148 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
|
149 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
|
150 is returned. |
228a56e7e793
Added docs for context.get_object_private().
Atul Varma <varmaa@toolness.com>
parents:
78
diff
changeset
|
151 |
85 | 152 If `object` was created with :meth:`new_function()`, then this |
153 method returns the Python callable wrapped by `object`. | |
154 | |
90 | 155 This functionality is useful if you want to securely represent |
156 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
|
157 |
85 | 158 .. method:: clear_object_private(object) |
159 | |
160 Clears the ``private_obj`` passed to :meth:`new_object()` | |
161 when `object` was first created. If it doesn't exist, this | |
162 function returns nothing. | |
163 | |
164 If `object` was created with :meth:`new_function()`, then this | |
165 method effectively "unbinds" the Python callable wrapped by | |
166 `object`. If `object` is later called, an exception will be | |
167 raised. | |
168 | |
90 | 169 .. method:: evaluate_script(globalobj, code, filename, lineno) |
170 | |
171 Evaluates the text `code` using `globalobj` as the global | |
172 object/scope. | |
173 | |
174 It's assumed that `code` is coming from the file named by `filename`; | |
175 the first line of `code` is assumed to be line number `lineno` of | |
176 `filename`. This metadata is very useful for debugging stack traces, | |
177 exceptions, and so forth. | |
178 | |
113
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
179 For example: |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
180 |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
181 >>> cx = pymonkey.Runtime().new_context() |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
182 >>> obj = cx.new_object() |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
183 >>> cx.init_standard_classes(obj) |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
184 >>> cx.evaluate_script(obj, '5 * Math', '<string>', 1) |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
185 nan |
e616b4605db0
Added doctest for evaluate_script().
Atul Varma <varmaa@toolness.com>
parents:
112
diff
changeset
|
186 |
139
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
187 .. method:: compile_script(obj, code, filename, lineno) |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
188 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
189 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
|
190 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
|
191 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
192 `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
|
193 :meth:`evaluate_script()`. |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
194 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
195 `obj` is defined in the SpiderMonkey documentation to be |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
196 the "object with which the script is associated", though it's |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
197 unclear what its actual role is. |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
198 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
199 .. method:: execute_script(globalobj, script) |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
200 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
201 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
|
202 `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
|
203 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
204 For example: |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
205 |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
206 >>> cx = pymonkey.Runtime().new_context() |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
207 >>> obj = cx.new_object() |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
208 >>> cx.init_standard_classes(obj) |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
209 >>> script = cx.compile_script(cx.new_object(), '5 * Math', |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
210 ... '<string>', 1) |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
211 >>> cx.execute_script(obj, script) |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
212 nan |
d08cb5a06c66
Added docs for Script, compile_script() and execute_script().
Atul Varma <varmaa@toolness.com>
parents:
134
diff
changeset
|
213 |
90 | 214 .. method:: call_function(thisobj, func, args) |
215 | |
216 Calls a JavaScript function. | |
217 | |
218 `thisobj` is an :class:`Object` that will be used as the value | |
219 of ``this`` when the function executes, `func` is the | |
220 :class:`Function` to execute, and `args` is a tuple of arguments | |
221 to pass to the function. | |
222 | |
112
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
223 For instance: |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
224 |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
225 >>> cx = pymonkey.Runtime().new_context() |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
226 >>> obj = cx.new_object() |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
227 >>> cx.init_standard_classes(obj) |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
228 >>> Math = cx.get_property(obj, 'Math') |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
229 >>> floor = cx.get_property(Math, 'floor') |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
230 >>> cx.call_function(Math, floor, (5.3,)) |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
231 5 |
2086377c0abe
Added doctest for call_function().
Atul Varma <varmaa@toolness.com>
parents:
111
diff
changeset
|
232 |
58
7a3461ccaf1d
Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents:
57
diff
changeset
|
233 .. 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
|
234 |
7a3461ccaf1d
Added documentation for Context methods get_runtime(), new_object(), and init_standard_classes().
Atul Varma <varmaa@toolness.com>
parents:
57
diff
changeset
|
235 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
|
236 :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
|
237 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
|
238 `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
|
239 <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
|
240 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
|
241 |
115
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
242 .. method:: gc() |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
243 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
244 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
|
245 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
246 .. method:: set_operation_callback(func) |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
247 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
248 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
|
249 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
|
250 :meth:`trigger_operation_callback()`. |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
251 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
252 `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
|
253 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
254 .. method:: trigger_operation_callback() |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
255 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
256 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
|
257 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
|
258 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
259 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
|
260 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
|
261 :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
|
262 execution of long-running code. |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
263 |
116
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
264 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
|
265 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
|
266 |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
267 >>> import time, threading |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
268 >>> cx = pymonkey.Runtime().new_context() |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
269 >>> def stop_running_code(cx): |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
270 ... 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
|
271 >>> 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
|
272 |
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
273 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
|
274 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
|
275 |
115
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
276 >>> def watchdog_thread(): |
116
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
277 ... 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
|
278 ... cx.trigger_operation_callback() |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
279 >>> thread = threading.Thread(target=watchdog_thread) |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
280 >>> thread.start() |
116
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
281 |
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
282 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
|
283 aborted: |
06269ca0b36c
Made the operation callback doctest easier to read.
Atul Varma <varmaa@toolness.com>
parents:
115
diff
changeset
|
284 |
115
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
285 >>> try: |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
286 ... 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
|
287 ... '<string>', 1) |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
288 ... except pymonkey.error, e: |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
289 ... 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
|
290 JS took too long to execute. |
f4c550369332
Added documentation for gc() and operation callback functions.
Atul Varma <varmaa@toolness.com>
parents:
114
diff
changeset
|
291 |
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
|
292 .. 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
|
293 |
53
2055d853b995
Added docs for pymonkey.Object.
Atul Varma <varmaa@toolness.com>
parents:
51
diff
changeset
|
294 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
|
295 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
|
296 |
119
d30576edf797
Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents:
116
diff
changeset
|
297 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
|
298 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
|
299 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
|
300 has support for thread safety. |
d30576edf797
Added notes on thread safety to documentation.
Atul Varma <varmaa@toolness.com>
parents:
116
diff
changeset
|
301 |
130
4705522c7431
Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents:
119
diff
changeset
|
302 JS runtimes are weak-referencable. |
4705522c7431
Added notes about weak-referencability to docs.
Atul Varma <varmaa@toolness.com>
parents:
119
diff
changeset
|
303 |
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
|
304 .. 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
|
305 |
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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 that are associated with the same JS runtime as the objects. |