annotate utils.c @ 49:7401af070bb8

Moved README to Sphinx documentation. Keeping the rendered docs in the repository so that they can be viewed via HG and locally by people who don't have Sphinx installed.
author Atul Varma <varmaa@toolness.com>
date Tue, 07 Jul 2009 09:26:36 -0700
parents bc4263c6ae82
children 427b01954b22
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: 47
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
diff changeset
3 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
diff changeset
7 * http://www.mozilla.org/MPL/
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
8 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
diff changeset
12 * License.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
13 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
14 * The Original Code is Pymonkey.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
15 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
diff changeset
18 * the Initial Developer. All Rights Reserved.
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
19 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
20 * Contributor(s):
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
21 * Atul Varma <atul@mozilla.com>
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
22 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
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: 47
diff changeset
34 *
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
35 * ***** END LICENSE BLOCK ***** */
bc4263c6ae82 Added license blocks to all .c/.h files.
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
36
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 #include "utils.h"
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38 #include "undefined.h"
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
39 #include "object.h"
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
40
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41 PyObject *PYM_error;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42
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: 32
diff changeset
43 static int
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
44 PYM_doubleToJsval(PYM_JSContextObject *context,
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
45 double number,
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
46 jsval *rval)
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
47 {
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
48 jsdouble *numberAsJsdouble = JS_NewDouble(context->cx, number);
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
49 if (numberAsJsdouble == NULL) {
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
50 PyErr_SetString(PYM_error, "JS_NewDouble() failed");
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: 32
diff changeset
51 return -1;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
52 }
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
53 *rval = DOUBLE_TO_JSVAL(numberAsJsdouble);
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: 32
diff changeset
54 return 0;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
55 }
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
56
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: 32
diff changeset
57 int
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
58 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: 27
diff changeset
59 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: 27
diff changeset
60 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: 27
diff changeset
61 {
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: 27
diff changeset
62 #ifndef Py_UNICODE_WIDE
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: 27
diff changeset
63 if (PyUnicode_Check(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: 27
diff changeset
64 Py_UNICODE *string = PyUnicode_AsUnicode(object);
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
65 JSString *jsString = JS_NewUCStringCopyZ(context->cx,
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: 27
diff changeset
66 (const jschar *) string);
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: 27
diff changeset
67 if (jsString == NULL) {
30
3b2bdf2823bb Changed PYM_pyObjectToJsval() to assume the caller is in Python-space for consistency.
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
68 PyErr_SetString(PYM_error, "JS_NewUCStringCopyZ() failed");
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: 32
diff changeset
69 return -1;
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: 27
diff changeset
70 }
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: 27
diff changeset
71
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: 27
diff changeset
72 *rval = STRING_TO_JSVAL(jsString);
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: 32
diff changeset
73 return 0;
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: 27
diff changeset
74 }
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: 27
diff changeset
75 #endif
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: 27
diff changeset
76
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
77 if (PyInt_Check(object)) {
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
78 long number = PyInt_AS_LONG(object);
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
79 if (INT_FITS_IN_JSVAL(number)) {
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
80 *rval = INT_TO_JSVAL(number);
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: 32
diff changeset
81 return 0;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
82 } else
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
83 return PYM_doubleToJsval(context, number, rval);
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
84 }
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
85
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
86 if (PyFloat_Check(object))
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
87 return PYM_doubleToJsval(context, PyFloat_AS_DOUBLE(object), rval);
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
88
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
89 if (PyObject_TypeCheck(object, &PYM_JSObjectType)) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
90 PYM_JSObject *jsObject = (PYM_JSObject *) object;
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
91 JSRuntime *rt = JS_GetRuntime(context->cx);
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
92 if (rt != jsObject->runtime->rt) {
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
93 PyErr_SetString(PyExc_ValueError,
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
94 "JS object and JS context are from different "
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
95 "JS runtimes");
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
96 return -1;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
97 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
98 *rval = OBJECT_TO_JSVAL(jsObject->obj);
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
99 return 0;
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
100 }
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
101
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
102 if (object == Py_True) {
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
103 *rval = JSVAL_TRUE;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
104 return 0;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
105 }
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
106
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
107 if (object == Py_False) {
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
108 *rval = JSVAL_FALSE;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
109 return 0;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
110 }
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
111
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
112 if (object == Py_None) {
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
113 *rval = JSVAL_NULL;
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
114 return 0;
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
115 }
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
116
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: 27
diff changeset
117 // TODO: Support more types.
30
3b2bdf2823bb Changed PYM_pyObjectToJsval() to assume the caller is in Python-space for consistency.
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
118 PyErr_SetString(PyExc_NotImplementedError,
3b2bdf2823bb Changed PYM_pyObjectToJsval() to assume the caller is in Python-space for consistency.
Atul Varma <varmaa@toolness.com>
parents: 29
diff changeset
119 "Data type conversion not implemented.");
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: 32
diff changeset
120 return -1;
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: 27
diff changeset
121 }
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: 27
diff changeset
122
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
123 PyObject *
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
124 PYM_jsvalToPyObject(PYM_JSContextObject *context,
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
125 jsval value) {
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
126 if (JSVAL_IS_INT(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
127 return PyInt_FromLong(JSVAL_TO_INT(value));
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
128
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
129 if (JSVAL_IS_DOUBLE(value)) {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
130 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
131 return PyFloat_FromDouble(*doubleRef);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
132 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
133
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
134 if (value == JSVAL_FALSE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
135 Py_RETURN_FALSE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
136
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
137 if (value == JSVAL_TRUE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
138 Py_RETURN_TRUE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
139
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
140 if (JSVAL_IS_NULL(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
141 Py_RETURN_NONE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
142
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
143 if (JSVAL_IS_VOID(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
144 Py_RETURN_UNDEFINED;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
145
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
146 if (JSVAL_IS_STRING(value)) {
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
147 // Strings in JS are funky: think of them as 16-bit versions of
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
148 // Python 2.x's 'str' type. Whether or not they're valid UTF-16
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
149 // is entirely up to the client code.
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
150
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
151 // TODO: Instead of ignoring errors, consider actually treating
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
152 // the string as a raw character buffer.
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
153 JSString *str = JSVAL_TO_STRING(value);
27
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
154 const char *chars = (const char *) JS_GetStringChars(str);
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
155 size_t length = JS_GetStringLength(str);
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
156
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
157 // We're multiplying length by two since Python wants the number
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
158 // of bytes, not the number of 16-bit characters.
21045074139f Based on my new understanding of JSString */jschar * thanks to folks on #jsapi, I've removed the requirement that SpiderMonkey be in UTF-8 mode to translate strings between Python and SpiderMonkey.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
159 return PyUnicode_DecodeUTF16(chars, length * 2, "ignore", NULL);
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
160 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
161
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
162 if (JSVAL_IS_OBJECT(value))
37
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
163 return (PyObject *) PYM_newJSObject(context, JSVAL_TO_OBJECT(value),
d4efcbb06964 Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
Atul Varma <varmaa@toolness.com>
parents: 34
diff changeset
164 NULL);
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
165
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
166 // TODO: Support more types.
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
167 PyErr_SetString(PyExc_NotImplementedError,
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
168 "Data type conversion not implemented.");
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: 27
diff changeset
169 return NULL;
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
170 }
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
171
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
172 void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
173 PYM_pythonExceptionToJs(PYM_JSContextObject *context)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
174 {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
175 PyObject *type;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
176 PyObject *value;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
177 PyObject *traceback;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
178
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
179 PyErr_Fetch(&type, &value, &traceback);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
180 PyObject *str = PyObject_Unicode(value);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
181 if (str == NULL)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
182 JS_ReportError(context->cx, "Python exception occurred");
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
183 else {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
184 jsval val;
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
185 if (PYM_pyObjectToJsval(context, str, &val) == 0) {
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
186 // TODO: Wrap Python traceback info in JS exception so the client
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
187 // can examine it.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
188 JS_SetPendingException(context->cx, val);
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
189 } else
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
190 JS_ReportError(context->cx, "Python exception occurred");
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
191 }
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
192
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
193 Py_DECREF(type);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
194 Py_DECREF(value);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
195 Py_DECREF(traceback);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
196 }
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
197
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
198 void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
199 PYM_jsExceptionToPython(PYM_JSContextObject *context)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
200 {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
201 if (!JS_IsExceptionPending(context->cx) &&
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
202 PyErr_Occurred())
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
203 return;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
204
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
205 jsval val;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
206 if (JS_GetPendingException(context->cx, &val)) {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
207 JSString *str = JS_ValueToString(context->cx, val);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
208 if (str != NULL) {
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
209 // TODO: Wrap the original JS exception so that the client can
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
210 // examine it.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
211 const char *chars = JS_GetStringBytes(str);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
212 PyErr_SetString(PYM_error, chars);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
213 } else
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
214 PyErr_SetString(PYM_error, "JS exception occurred");
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
215 } else
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
216 PyErr_SetString(PYM_error, "JS_GetPendingException() failed");
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
217 }