-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstring.c
More file actions
89 lines (75 loc) · 1.89 KB
/
Copy pathstring.c
File metadata and controls
89 lines (75 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2009 Paul J. Davis <paul.joseph.davis@gmail.com>
*
* This file is part of the python-spidermonkey package released
* under the MIT license.
*
*/
#include "spidermonkey.h"
JSString*
py2js_string_obj(Context* cx, PyObject* str)
{
PyObject* conv = NULL;
PyObject* encoded = NULL;
JSString* ret = NULL;
char* bytes;
Py_ssize_t len;
if(PyString_Check(str))
{
conv = PyUnicode_FromEncodedObject(str, "utf-8", "replace");
if(conv == NULL) goto error;
str = conv;
}
else if(!PyUnicode_Check(str))
{
PyErr_SetString(PyExc_TypeError, "Invalid string conversion.");
goto error;
}
encoded = PyUnicode_AsEncodedString(str, "utf-16", "strict");
if(encoded == NULL) goto error;
if(PyString_AsStringAndSize(encoded, &bytes, &len) < 0) goto error;
if(len < 2)
{
PyErr_SetString(PyExc_ValueError, "Failed to find byte-order mark.");
goto error;
}
if(((unsigned short*) bytes)[0] != 0xFEFF)
{
PyErr_SetString(PyExc_ValueError, "Invalid UTF-16 BOM");
goto error;
}
ret = JS_NewUCStringCopyN(cx->cx, (jschar*) (bytes+2), (len/2)-1);
goto success;
error:
success:
Py_XDECREF(conv);
Py_XDECREF(encoded);
return ret;
}
jsval
py2js_string(Context* cx, PyObject* str)
{
JSString* val = py2js_string_obj(cx, str);
if(val == NULL)
{
PyErr_Clear();
return JSVAL_VOID;
}
return STRING_TO_JSVAL(val);
}
PyObject*
js2py_string(Context* cx, jsval val)
{
JSString* str;
jschar* bytes;
size_t len;
if(!JSVAL_IS_STRING(val))
{
PyErr_SetString(PyExc_TypeError, "Value is not a JS String.");
return NULL;
}
str = JSVAL_TO_STRING(val);
len = JS_GetStringLength(str);
bytes = JS_GetStringChars(str);
return PyUnicode_Decode((const char*) bytes, len*2, "utf-16", "strict");
}