Mercurial > pymonkey
annotate utils.c @ 37:d4efcbb06964
Added a new PYM_JSFunction type, PYM_JSContext.define_property(), and PYM_JSContext.new_function(). Also fixed a memory leak.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 02 Jul 2009 22:42:31 -0700 |
parents | 5d3d3b25f23f |
children | 0b9a316ce4ef |
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 |
32
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
8 PYM_doubleToJsval(JSContext *cx, |
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 { |
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
12 jsdouble *numberAsJsdouble = JS_NewDouble(cx, number); |
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 |
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
|
22 PYM_pyObjectToJsval(JSContext *cx, |
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); |
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
|
29 JSString *jsString = JS_NewUCStringCopyZ(cx, |
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 |
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
47 return PYM_doubleToJsval(cx, 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)) |
abf14cba9ef5
JS wrapped Python functions can now return floats.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
51 return PYM_doubleToJsval(cx, PyFloat_AS_DOUBLE(object), rval); |
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; |
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
|
55 JSRuntime *rt = JS_GetRuntime(cx); |
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 |
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
|
76 // 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
|
77 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
|
78 "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
|
79 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
|
80 } |
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 |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
82 PyObject * |
21
fc04e5f1c675
Changed object constructor to take a context instead of a runtime.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
83 PYM_jsvalToPyObject(PYM_JSContextObject *context, |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
84 jsval value) { |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
85 if (JSVAL_IS_INT(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
86 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
|
87 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
88 if (JSVAL_IS_DOUBLE(value)) { |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
89 jsdouble *doubleRef = JSVAL_TO_DOUBLE(value); |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
90 return PyFloat_FromDouble(*doubleRef); |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
91 } |
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 (value == JSVAL_FALSE) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
94 Py_RETURN_FALSE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
95 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
96 if (value == JSVAL_TRUE) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
97 Py_RETURN_TRUE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
98 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
99 if (JSVAL_IS_NULL(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
100 Py_RETURN_NONE; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
101 |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
102 if (JSVAL_IS_VOID(value)) |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
103 Py_RETURN_UNDEFINED; |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
104 |
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
|
105 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
|
106 // 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
|
107 // 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
|
108 // 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
|
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 // 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
|
111 // 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
|
112 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
|
113 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
|
114 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
|
115 |
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 // 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
|
117 // 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
|
118 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
|
119 } |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
120 |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
121 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
|
122 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
|
123 NULL); |
20
abede8af8cf5
PYM_jsvalToPyObject() can now deal with JSObjects.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
124 |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
125 // TODO: Support more types. |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
126 PyErr_SetString(PyExc_NotImplementedError, |
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
127 "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
|
128 return NULL; |
11
551ba05fe6ad
factored out Runtime, Context, and utils into separate files.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
129 } |