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