comparison pymonkey.c @ 7:0d0ce6415b66

Added support for unicode strings.
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Jun 2009 13:09:39 -0700
parents 42f57789f84f
children 6647870380cc
comparison
equal deleted inserted replaced
6:42f57789f84f 7:0d0ce6415b66
27 Py_RETURN_TRUE; 27 Py_RETURN_TRUE;
28 28
29 if (JSVAL_IS_NULL(value)) 29 if (JSVAL_IS_NULL(value))
30 Py_RETURN_NONE; 30 Py_RETURN_NONE;
31 31
32 if (JSVAL_IS_STRING(value) && JS_CStringsAreUTF8()) {
33 // TODO: What to do if C strings aren't UTF-8? The jschar *
34 // type isn't actually UTF-16, it's just "UTF-16-ish", so
35 // there doesn't seem to be any other lossless way of
36 // transferring the string other than perhaps by transmitting
37 // its JSON representation.
38
39 JSString *str = JSVAL_TO_STRING(value);
40 const char *bytes = JS_GetStringBytes(str);
41 const char *errors;
42 return PyUnicode_DecodeUTF8(bytes, strlen(bytes), errors);
43 }
44
32 // TODO: Support more types. 45 // TODO: Support more types.
33 PyErr_SetString(PyExc_NotImplementedError, 46 PyErr_SetString(PyExc_NotImplementedError,
34 "Data type conversion not implemented."); 47 "Data type conversion not implemented.");
35 } 48 }
36 49
43 int lineNo; 56 int lineNo;
44 57
45 if (!PyArg_ParseTuple(args, "s#si", &source, &sourceLen, 58 if (!PyArg_ParseTuple(args, "s#si", &source, &sourceLen,
46 &filename, &lineNo)) 59 &filename, &lineNo))
47 return NULL; 60 return NULL;
61
62 if (!JS_CStringsAreUTF8())
63 JS_SetCStringsAreUTF8();
48 64
49 JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L); 65 JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L);
50 if (rt == NULL) { 66 if (rt == NULL) {
51 PyErr_SetString(PYM_error, "JS_NewRuntime() failed"); 67 PyErr_SetString(PYM_error, "JS_NewRuntime() failed");
52 return NULL; 68 return NULL;