comparison utils.c @ 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.
author Atul Varma <varmaa@toolness.com>
date Tue, 30 Jun 2009 22:42:48 -0700
parents abf14cba9ef5
children 5d3d3b25f23f
comparison
equal deleted inserted replaced
32:abf14cba9ef5 33:3f8a2db496f5
2 #include "undefined.h" 2 #include "undefined.h"
3 #include "object.h" 3 #include "object.h"
4 4
5 PyObject *PYM_error; 5 PyObject *PYM_error;
6 6
7 static PyObject * 7 static int
8 PYM_doubleToJsval(JSContext *cx, 8 PYM_doubleToJsval(JSContext *cx,
9 double number, 9 double number,
10 jsval *rval) 10 jsval *rval)
11 { 11 {
12 jsdouble *numberAsJsdouble = JS_NewDouble(cx, number); 12 jsdouble *numberAsJsdouble = JS_NewDouble(cx, number);
13 if (numberAsJsdouble == NULL) { 13 if (numberAsJsdouble == NULL) {
14 PyErr_SetString(PYM_error, "JS_NewDouble() failed"); 14 PyErr_SetString(PYM_error, "JS_NewDouble() failed");
15 return NULL; 15 return -1;
16 } 16 }
17 *rval = DOUBLE_TO_JSVAL(numberAsJsdouble); 17 *rval = DOUBLE_TO_JSVAL(numberAsJsdouble);
18 Py_RETURN_NONE; 18 return 0;
19 } 19 }
20 20
21 PyObject * 21 int
22 PYM_pyObjectToJsval(JSContext *cx, 22 PYM_pyObjectToJsval(JSContext *cx,
23 PyObject *object, 23 PyObject *object,
24 jsval *rval) 24 jsval *rval)
25 { 25 {
26 #ifndef Py_UNICODE_WIDE 26 #ifndef Py_UNICODE_WIDE
28 Py_UNICODE *string = PyUnicode_AsUnicode(object); 28 Py_UNICODE *string = PyUnicode_AsUnicode(object);
29 JSString *jsString = JS_NewUCStringCopyZ(cx, 29 JSString *jsString = JS_NewUCStringCopyZ(cx,
30 (const jschar *) string); 30 (const jschar *) string);
31 if (jsString == NULL) { 31 if (jsString == NULL) {
32 PyErr_SetString(PYM_error, "JS_NewUCStringCopyZ() failed"); 32 PyErr_SetString(PYM_error, "JS_NewUCStringCopyZ() failed");
33 return NULL; 33 return -1;
34 } 34 }
35 35
36 *rval = STRING_TO_JSVAL(jsString); 36 *rval = STRING_TO_JSVAL(jsString);
37 Py_RETURN_NONE; 37 return 0;
38 } 38 }
39 #endif 39 #endif
40 40
41 if (PyInt_Check(object)) { 41 if (PyInt_Check(object)) {
42 long number = PyInt_AS_LONG(object); 42 long number = PyInt_AS_LONG(object);
43 if (INT_FITS_IN_JSVAL(number)) { 43 if (INT_FITS_IN_JSVAL(number)) {
44 *rval = INT_TO_JSVAL(number); 44 *rval = INT_TO_JSVAL(number);
45 Py_RETURN_NONE; 45 return 0;
46 } else 46 } else
47 return PYM_doubleToJsval(cx, number, rval); 47 return PYM_doubleToJsval(cx, number, rval);
48 } 48 }
49 49
50 if (PyFloat_Check(object)) 50 if (PyFloat_Check(object))
51 return PYM_doubleToJsval(cx, PyFloat_AS_DOUBLE(object), rval); 51 return PYM_doubleToJsval(cx, PyFloat_AS_DOUBLE(object), rval);
52 52
53 // TODO: Support more types. 53 // TODO: Support more types.
54 PyErr_SetString(PyExc_NotImplementedError, 54 PyErr_SetString(PyExc_NotImplementedError,
55 "Data type conversion not implemented."); 55 "Data type conversion not implemented.");
56 return NULL; 56 return -1;
57 } 57 }
58 58
59 PyObject * 59 PyObject *
60 PYM_jsvalToPyObject(PYM_JSContextObject *context, 60 PYM_jsvalToPyObject(PYM_JSContextObject *context,
61 jsval value) { 61 jsval value) {