forked from davisp/python-spidermonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger.c
63 lines (52 loc) · 1.12 KB
/
integer.c
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
/*
* 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"
jsval
py2js_integer(Context* cx, PyObject* obj)
{
long pyval;
if(PyInt_Check(obj))
{
pyval = PyInt_AsLong(obj);
if(PyErr_Occurred()) return JSVAL_VOID;
}
else
{
pyval = PyLong_AsLong(obj);
if(PyErr_Occurred()) return JSVAL_VOID;
}
return long2js_integer(cx, pyval);
}
jsval
long2js_integer(Context* cx, long pyval)
{
jsval ret = JSVAL_VOID;
if(INT_FITS_IN_JSVAL(pyval))
{
ret = INT_TO_JSVAL(pyval);
goto done;
}
if(!JS_NewNumberValue(cx->cx, pyval, &ret))
{
PyErr_SetString(PyExc_ValueError, "Failed to convert number.");
goto done;
}
done:
return ret;
}
PyObject*
js2py_integer(Context* cx, jsval val)
{
int32 rval;
if(!JS_ValueToInt32(cx->cx, val, &rval))
{
PyErr_SetString(PyExc_TypeError, "Invalid JS integer value.");
return NULL;
}
return PyInt_FromLong(rval);
}