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