comparison utils.c @ 11:551ba05fe6ad

factored out Runtime, Context, and utils into separate files.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 17:28:57 -0700
parents
children abede8af8cf5
comparison
equal deleted inserted replaced
10:29eaa1fceff1 11:551ba05fe6ad
1 #include "utils.h"
2 #include "undefined.h"
3
4 PyObject *PYM_error;
5
6 PyObject *
7 PYM_jsvalToPyObject(jsval value) {
8 if (JSVAL_IS_INT(value))
9 return PyInt_FromLong(JSVAL_TO_INT(value));
10
11 if (JSVAL_IS_DOUBLE(value)) {
12 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value);
13 return PyFloat_FromDouble(*doubleRef);
14 }
15
16 if (value == JSVAL_FALSE)
17 Py_RETURN_FALSE;
18
19 if (value == JSVAL_TRUE)
20 Py_RETURN_TRUE;
21
22 if (JSVAL_IS_NULL(value))
23 Py_RETURN_NONE;
24
25 if (JSVAL_IS_VOID(value))
26 Py_RETURN_UNDEFINED;
27
28 if (JSVAL_IS_STRING(value) && JS_CStringsAreUTF8()) {
29 // TODO: What to do if C strings aren't UTF-8? The jschar *
30 // type isn't actually UTF-16, it's just "UTF-16-ish", so
31 // there doesn't seem to be any other lossless way of
32 // transferring the string other than perhaps by transmitting
33 // its JSON representation.
34
35 JSString *str = JSVAL_TO_STRING(value);
36 const char *bytes = JS_GetStringBytes(str);
37 const char *errors;
38 return PyUnicode_DecodeUTF8(bytes, strlen(bytes), errors);
39 }
40
41 // TODO: Support more types.
42 PyErr_SetString(PyExc_NotImplementedError,
43 "Data type conversion not implemented.");
44 }