annotate utils.c @ 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.
author Atul Varma <varmaa@toolness.com>
date Mon, 06 Jul 2009 08:13:45 -0700
parents a0f677cfc679
children bc4263c6ae82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
1 #include "utils.h"
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 #include "undefined.h"
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
3 #include "object.h"
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
4
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
5 PyObject *PYM_error;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
6
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
7 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
8 PYM_doubleToJsval(PYM_JSContextObject *context,
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
9 double number,
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
10 jsval *rval)
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
11 {
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
12 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
13 if (numberAsJsdouble == NULL) {
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
14 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
15 return -1;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
16 }
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
17 *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
18 return 0;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
19 }
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
20
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
21 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
22 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
23 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
24 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
25 {
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
26 #ifndef Py_UNICODE_WIDE
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
27 if (PyUnicode_Check(object)) {
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
28 Py_UNICODE *string = PyUnicode_AsUnicode(object);
44
0b9a316ce4ef Changed function signature of PYM_pyObjectToJsval() to be consistent w/ the rest of the API.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
29 JSString *jsString = JS_NewUCStringCopyZ(context->cx,
29
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
30 (const jschar *) string);
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
31 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
32 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
33 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
34 }
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
35
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
36 *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
37 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
38 }
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
39 #endif
608d086d12e3 Added a new PYM_pyObjectToJsval() function that only supports unicode for the moment. Also, whereever we're assuming that Py_UNICODE is UCS-2, we're surrounding such code with #ifndef Py_UNICODE_WIDE.
Atul Varma <varmaa@toolness.com>
parents: 27
diff changeset
40
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
41 if (PyInt_Check(object)) {
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
42 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
43 if (INT_FITS_IN_JSVAL(number)) {
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
44 *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
45 return 0;
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
46 } 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
47 return PYM_doubleToJsval(context, number, rval);
31
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
48 }
d0a3f358072a gcc now shows all warnings (-Wall).
Atul Varma <varmaa@toolness.com>
parents: 30
diff changeset
49
32
abf14cba9ef5 JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents: 31
diff changeset
50 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
51 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
52
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
53 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
54 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
55 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
56 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
57 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
58 "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
59 "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
60 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
61 }
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
62 *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
63 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
64 }
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
65
34
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
66 if (object == Py_True) {
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
67 *rval = JSVAL_TRUE;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
68 return 0;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
69 }
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
70
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
71 if (object == Py_False) {
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
72 *rval = JSVAL_FALSE;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
73 return 0;
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
74 }
5d3d3b25f23f JS wrapped Python functions can now return booleans.
Atul Varma <varmaa@toolness.com>
parents: 33
diff changeset
75
45
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
76 if (object == Py_None) {
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
77 *rval = JSVAL_NULL;
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
78 return 0;
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
79 }
03aec8572461 Python-wrapped JS functions can now return null/None.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
80
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
81 // 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
82 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
83 "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
84 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
85 }
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
86
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
87 PyObject *
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
88 PYM_jsvalToPyObject(PYM_JSContextObject *context,
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
89 jsval value) {
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
90 if (JSVAL_IS_INT(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
91 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
92
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
93 if (JSVAL_IS_DOUBLE(value)) {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
94 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
95 return PyFloat_FromDouble(*doubleRef);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
96 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
97
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
98 if (value == JSVAL_FALSE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
99 Py_RETURN_FALSE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
100
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
101 if (value == JSVAL_TRUE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
102 Py_RETURN_TRUE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
103
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
104 if (JSVAL_IS_NULL(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
105 Py_RETURN_NONE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
106
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
107 if (JSVAL_IS_VOID(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
108 Py_RETURN_UNDEFINED;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
109
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
110 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
111 // 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
112 // 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
113 // 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
114
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
115 // 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
116 // 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
117 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
118 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
119 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
120
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
121 // 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
122 // 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
123 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
124 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
125
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
126 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
127 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
128 NULL);
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
129
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
130 // TODO: Support more types.
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
131 PyErr_SetString(PyExc_NotImplementedError,
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
132 "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
133 return NULL;
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
134 }
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
135
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
136 void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
137 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
138 {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
139 PyObject *type;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
140 PyObject *value;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
141 PyObject *traceback;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
142
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
143 PyErr_Fetch(&type, &value, &traceback);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
144 PyObject *str = PyObject_Unicode(value);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
145 if (str == NULL)
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
146 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
147 else {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
148 jsval val;
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
149 if (PYM_pyObjectToJsval(context, str, &val) == 0) {
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
150 // TODO: Wrap Python traceback info in JS exception so the client
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
151 // can examine it.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
152 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
153 } else
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
154 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
155 }
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
156
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
157 Py_DECREF(type);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
158 Py_DECREF(value);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
159 Py_DECREF(traceback);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
160 }
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
161
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
162 void
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
163 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
164 {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
165 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
166 PyErr_Occurred())
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
167 return;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
168
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
169 jsval val;
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
170 if (JS_GetPendingException(context->cx, &val)) {
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
171 JSString *str = JS_ValueToString(context->cx, val);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
172 if (str != NULL) {
47
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
173 // TODO: Wrap the original JS exception so that the client can
3f4982759e55 Converting JS exceptions into Python exceptions is now doable, albeit not yet implemented, thanks to the discovery of JSOPTION_DONT_REPORT_UNCAUGHT. Also, JS warnings are now converted into Python warnings.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
174 // examine it.
46
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
175 const char *chars = JS_GetStringBytes(str);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
176 PyErr_SetString(PYM_error, chars);
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
177 } else
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
178 PyErr_SetString(PYM_error, "JS exception occurred");
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
179 } else
a0f677cfc679 Added basic functionality for passing useful exceptions between Python and JS code.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
180 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
181 }