annotate src/utils.h @ 173:5cfb47c9e916 default tip

Minor line wrap and spacing fixes.
author Atul Varma <varmaa@toolness.com>
date Tue, 01 Sep 2009 03:22:33 -0700
parents dd32a92f6b4f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
3 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
4 * The contents of this file are subject to the Mozilla Public License Version
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
5 * 1.1 (the "License"); you may not use this file except in compliance with
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
6 * the License. You may obtain a copy of the License at
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
7 * http://www.mozilla.org/MPL/
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
8 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
9 * Software distributed under the License is distributed on an "AS IS" basis,
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
11 * for the specific language governing rights and limitations under the
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
12 * License.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
13 *
170
dd32a92f6b4f Initial attempt at renaming pymonkey to pydermonkey.
Atul Varma <varmaa@toolness.com>
parents: 132
diff changeset
14 * The Original Code is Pydermonkey.
48
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
15 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
16 * The Initial Developer of the Original Code is Mozilla.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
17 * Portions created by the Initial Developer are Copyright (C) 2007
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
18 * the Initial Developer. All Rights Reserved.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
19 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
20 * Contributor(s):
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
21 * Atul Varma <atul@mozilla.com>
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
22 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
23 * Alternatively, the contents of this file may be used under the terms of
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
26 * in which case the provisions of the GPL or the LGPL are applicable instead
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
27 * of those above. If you wish to allow use of your version of this file only
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
28 * under the terms of either the GPL or the LGPL, and not to allow others to
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
29 * use your version of this file under the terms of the MPL, indicate your
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
30 * decision by deleting the provisions above and replace them with the notice
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
31 * and other provisions required by the GPL or the LGPL. If you do not delete
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
32 * the provisions above, a recipient may use your version of this file under
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
33 * the terms of any one of the MPL, the GPL or the LGPL.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
34 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
35 * ***** END LICENSE BLOCK ***** */
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
36
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 #ifndef PYM_UTILS_H
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38 #define PYM_UTILS_H
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
39
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
40 #include "context.h"
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
41
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42 #include <jsapi.h>
23
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
43 #include <jsdhash.h>
98
3baa418b7ba8 Changed includes from Python/Python.h to just Python.h.
Atul Varma <varmaa@toolness.com>
parents: 66
diff changeset
44 #include <Python.h>
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
45
132
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
46 // Simple class that holds on to a UTF-16 string created by
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
47 // PyArg_ParseTuple() for as long as it's in scope. It also
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
48 // provides easy BOM-stripping accessors for JS-land.
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
49 class PYM_UTF16String {
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
50 public:
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
51 PYM_UTF16String(char *buffer, int size) : jsbuffer((jschar *) (buffer + 2)),
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
52 jslen(size / 2 - 1), pybuffer(buffer), pysize(size) {
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
53 }
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
54
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
55 ~PYM_UTF16String() {
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
56 PyMem_Free(pybuffer);
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
57 }
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
58
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
59 jschar *jsbuffer;
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
60 size_t jslen;
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
61
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
62 protected:
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
63 char *pybuffer;
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
64 int pysize;
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
65 };
537cf7deadc9 Added a new PYM_UTF16String C++ class to make calling UC JSAPI functions a little easier and less error-prone.
Atul Varma <varmaa@toolness.com>
parents: 118
diff changeset
66
118
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
67 // Simple class that holds the Python global interpreter lock (GIL)
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
68 // for as long as it's in scope.
66
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
69 class PYM_PyAutoEnsureGIL {
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
70 public:
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
71 PYM_PyAutoEnsureGIL() {
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
72 state = PyGILState_Ensure();
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
73 }
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
74
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
75 ~PYM_PyAutoEnsureGIL() {
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
76 PyGILState_Release(state);
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
77 }
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
78
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
79 protected:
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
80 PyGILState_STATE state;
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
81 };
b49180c39d0a Pymonkey now handles the GIL properly so that Python code can run while JS code does.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
82
23
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
83 typedef struct {
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
84 JSDHashEntryStub base;
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
85 void *value;
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
86 } PYM_HashEntry;
951ad1b15587 The hashtable of reflected JS objects now uses a JS_DHashTable instead of a PyDict. Also removed weakref-ability of the JSObject class since it didn't make any sense to use Python's weakref support for JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
87
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
88 extern PyObject *PYM_error;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
89
118
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
90 // Convert a PyObject to a jsval. Returns 0 on success,
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
91 // -1 on error. If an error occurs, a Python exception is
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
92 // set.
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
93 //
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
94 // The jsval is placed in rval.
33
3f8a2db496f5 Changed PYM_pyObjectToJsval() to simply return an int status/error code; this way callers don't have to worry about decreasing the reference count of a Py_NONE.
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
95 extern int
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
96 PYM_pyObjectToJsval(PYM_JSContextObject *context,
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 23
diff changeset
97 PyObject *object,
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 23
diff changeset
98 jsval *rval);
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 23
diff changeset
99
118
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
100 // Convert a jsval to a PyObject, returning a new reference.
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
101 // If this function fails, it sets a Python exception and
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
102 // returns NULL.
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
103 extern PyObject *
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
104 PYM_jsvalToPyObject(PYM_JSContextObject *context, jsval value);
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
105
118
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
106 // Converts the currently-pending Python exception to a
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
107 // pending JS exception on the given JS context.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
108 extern void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
109 PYM_pythonExceptionToJs(PYM_JSContextObject *context);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
110
118
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
111 // Converts the currently-pending exception on the given
00e874d9a6a7 Added some documentation to utils.h.
Atul Varma <varmaa@toolness.com>
parents: 117
diff changeset
112 // JS context into a pending Python exception.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
113 void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
114 PYM_jsExceptionToPython(PYM_JSContextObject *context);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
115
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
116 #endif