Mercurial > pymonkey
annotate utils.c @ 91:97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sat, 15 Aug 2009 00:10:29 -0700 |
parents | 4b1149d818e8 |
children | 3beb55bedd92 |
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 if (PyUnicode_Check(object)) { |
91
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
63 PyObject *string = PyUnicode_AsUTF16String(object); |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
64 if (string == NULL) |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
65 return -1; |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
66 |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
67 char *buffer = PyString_AS_STRING(string); |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
68 Py_ssize_t size = PyString_GET_SIZE(string); |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
69 |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
70 // Note that we're manipulating buffer and size here to get rid of |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
71 // the BOM. |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
72 JSString *jsString = JS_NewUCStringCopyN(context->cx, |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
73 (const jschar *) (buffer + 2), |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
74 (size / 2) - 1); |
97d1faf02460
Got rid of Py_UNICODE_WIDE ifdefs.
Atul Varma <varmaa@toolness.com>
parents:
75
diff
changeset
|
75 Py_DECREF(string); |
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
|
76 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
|
77 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
|
78 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
|
79 } |
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
|
80 |
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
|
81 *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
|
82 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
|
83 } |
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
|
84 |
31
d0a3f358072a
gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents:
30
diff
changeset
|
85 if (PyInt_Check(object)) { |
d0a3f358072a
gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents:
30
diff
changeset
|
86 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
|
87 if (INT_FITS_IN_JSVAL(number)) { |
31
d0a3f358072a
gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents:
30
diff
changeset
|
88 *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
|
89 return 0; |
32
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
90 } 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
|
91 return PYM_doubleToJsval(context, number, rval); |
31
d0a3f358072a
gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents:
30
diff
changeset
|
92 } |
d0a3f358072a
gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents:
30
diff
changeset
|
93 |
32
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
94 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
|
95 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
|
96 |
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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 "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
|
103 "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
|
104 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
|
105 } |
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
|
106 *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
|
107 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
|
108 } |
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
|
109 |
34
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
110 if (object == Py_True) { |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
111 *rval = JSVAL_TRUE; |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
112 return 0; |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
113 } |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
114 |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
115 if (object == Py_False) { |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
116 *rval = JSVAL_FALSE; |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
117 return 0; |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
118 } |
5d3d3b25f23f
JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
119 |
45
03aec8572461
Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
120 if (object == Py_None) { |
03aec8572461
Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
121 *rval = JSVAL_NULL; |
03aec8572461
Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
122 return 0; |
03aec8572461
Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
123 } |
03aec8572461
Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
124 |
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
|
125 // 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
|
126 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
|
127 "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
|
128 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
|
129 } |
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
|
130 |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
131 PyObject * |
21
fc04e5f1c675
Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
132 PYM_jsvalToPyObject(PYM_JSContextObject *context, |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
133 jsval value) { |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
134 if (JSVAL_IS_INT(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
135 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
|
136 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
137 if (JSVAL_IS_DOUBLE(value)) { |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
138 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value); |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
139 return PyFloat_FromDouble(*doubleRef); |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
140 } |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
141 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
142 if (value == JSVAL_FALSE) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
143 Py_RETURN_FALSE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
144 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
145 if (value == JSVAL_TRUE) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
146 Py_RETURN_TRUE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
147 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
148 if (JSVAL_IS_NULL(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
149 Py_RETURN_NONE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
150 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
151 if (JSVAL_IS_VOID(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
152 Py_RETURN_UNDEFINED; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
153 |
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 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
|
155 // 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
|
156 // 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
|
157 // 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
|
158 |
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
|
159 // 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
|
160 // 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
|
161 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
|
162 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
|
163 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
|
164 |
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
|
165 // 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
|
166 // 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
|
167 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
|
168 } |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
169 |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
170 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
|
171 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
|
172 NULL); |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
173 |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
174 // TODO: Support more types. |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
175 PyErr_SetString(PyExc_NotImplementedError, |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
176 "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
|
177 return NULL; |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
178 } |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
179 |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
180 void |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
181 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
|
182 { |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
183 PyObject *type; |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
184 PyObject *value; |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
185 PyObject *traceback; |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
186 |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
187 PyErr_Fetch(&type, &value, &traceback); |
75
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
188 |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
189 if (type == PYM_error && value && |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
190 PyObject_HasAttrString(value, "message")) { |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
191 jsval val; |
75
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
192 PyObject *message = PyObject_GetAttrString(value, "message"); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
193 if (message && PYM_pyObjectToJsval(context, message, &val) == 0) { |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
194 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
|
195 } else |
75
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
196 JS_ReportError(context->cx, |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
197 "Python exception occurred, but exception " |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
198 "couldn't be converted"); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
199 Py_XDECREF(message); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
200 } else { |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
201 if (value) { |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
202 JSObject *exception = PYM_JS_newObject(context->cx, value); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
203 if (exception) |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
204 JS_SetPendingException(context->cx, OBJECT_TO_JSVAL(exception)); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
205 else |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
206 JS_ReportOutOfMemory(context->cx); |
4b1149d818e8
Exceptions work a bit more securely now.
Atul Varma <varmaa@toolness.com>
parents:
74
diff
changeset
|
207 } else |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
208 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
|
209 } |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
210 |
52
427b01954b22
The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
211 Py_XDECREF(type); |
427b01954b22
The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
212 Py_XDECREF(value); |
427b01954b22
The 'this' argument for a js-wrapped python function, as well as the function's arguments, are now passed to the python function.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
213 Py_XDECREF(traceback); |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
214 } |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
215 |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
216 void |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
217 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
|
218 { |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
219 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
|
220 PyErr_Occurred()) |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
221 return; |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
222 |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
223 jsval val; |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
224 if (JS_GetPendingException(context->cx, &val)) { |
74
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
225 PyObject *obj = PYM_jsvalToPyObject(context, val); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
226 if (obj) { |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
227 PyErr_SetObject(PYM_error, obj); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
228 Py_DECREF(obj); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
229 } else { |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
230 PyErr_Clear(); |
69
c2972e58fbb6
Made the conversion of JS exceptions into strings release the GIL.
Atul Varma <varmaa@toolness.com>
parents:
52
diff
changeset
|
231 |
74
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
232 JSString *str = NULL; |
69
c2972e58fbb6
Made the conversion of JS exceptions into strings release the GIL.
Atul Varma <varmaa@toolness.com>
parents:
52
diff
changeset
|
233 |
74
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
234 Py_BEGIN_ALLOW_THREADS; |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
235 str = JS_ValueToString(context->cx, val); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
236 Py_END_ALLOW_THREADS; |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
237 |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
238 if (str != NULL) { |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
239 const char *chars = JS_GetStringBytes(str); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
240 PyErr_SetString(PYM_error, chars); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
241 } else |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
242 PyErr_SetString(PYM_error, "JS exception occurred"); |
e06376295170
JS exceptions thrown out to Python now include the wrapped original exception. This fixes a TODO.
Atul Varma <varmaa@toolness.com>
parents:
69
diff
changeset
|
243 } |
46
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
244 } else |
a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
245 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
|
246 } |