annotate utils.c @ 26:9e33fc5a8d92

Added a link to ServerJS.
author Atul Varma <varmaa@toolness.com>
date Mon, 29 Jun 2009 10:35:06 -0700
parents fc04e5f1c675
children 21045074139f
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
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 PyObject *
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
8 PYM_jsvalToPyObject(PYM_JSContextObject *context,
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
9 jsval value) {
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
10 if (JSVAL_IS_INT(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
11 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
12
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
13 if (JSVAL_IS_DOUBLE(value)) {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
14 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
15 return PyFloat_FromDouble(*doubleRef);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
16 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
17
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
18 if (value == JSVAL_FALSE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
19 Py_RETURN_FALSE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
20
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
21 if (value == JSVAL_TRUE)
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
22 Py_RETURN_TRUE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
23
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
24 if (JSVAL_IS_NULL(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
25 Py_RETURN_NONE;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
26
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
27 if (JSVAL_IS_VOID(value))
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
28 Py_RETURN_UNDEFINED;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
29
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
30 if (JSVAL_IS_STRING(value) && JS_CStringsAreUTF8()) {
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
31 // TODO: What to do if C strings aren't UTF-8? The jschar *
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
32 // type isn't actually UTF-16, it's just "UTF-16-ish", so
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
33 // there doesn't seem to be any other lossless way of
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
34 // transferring the string other than perhaps by transmitting
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
35 // its JSON representation.
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
36
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 JSString *str = JSVAL_TO_STRING(value);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38 const char *bytes = JS_GetStringBytes(str);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
39 const char *errors;
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
40 return PyUnicode_DecodeUTF8(bytes, strlen(bytes), errors);
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41 }
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
42
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
43 if (JSVAL_IS_OBJECT(value))
21
fc04e5f1c675 Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents: 20
diff changeset
44 return (PyObject *) PYM_newJSObject(context, JSVAL_TO_OBJECT(value));
20
abede8af8cf5 PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
45
11
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
46 // TODO: Support more types.
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
47 PyErr_SetString(PyExc_NotImplementedError,
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
48 "Data type conversion not implemented.");
551ba05fe6ad factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
49 }