Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pyodide
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
pyodide
Commits
3831cf86
Commit
3831cf86
authored
May 30, 2018
by
Michael Droettboom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Consistent capitalization
parent
c1e75744
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
112 additions
and
125 deletions
+112
-125
src/js2python.c
src/js2python.c
+20
-20
src/js2python.h
src/js2python.h
+2
-2
src/jsimport.c
src/jsimport.c
+2
-2
src/jsimport.h
src/jsimport.h
+1
-1
src/jsproxy.c
src/jsproxy.c
+12
-12
src/jsproxy.h
src/jsproxy.h
+1
-1
src/main.c
src/main.c
+11
-19
src/pyimport.c
src/pyimport.c
+5
-5
src/pyimport.h
src/pyimport.h
+1
-2
src/pyproxy.c
src/pyproxy.c
+29
-29
src/pyproxy.h
src/pyproxy.h
+1
-1
src/python2js.c
src/python2js.c
+17
-17
src/python2js.h
src/python2js.h
+3
-3
src/runpython.c
src/runpython.c
+6
-6
src/runpython.h
src/runpython.h
+1
-5
No files found.
src/js2python.c
View file @
3831cf86
...
...
@@ -8,76 +8,76 @@
// Since we're going *to* Python, just let any Python exceptions at conversion
// bubble out to Python
int
_js
StringToPython
(
char
*
val
)
{
int
_js
2python_string
(
char
*
val
)
{
return
(
int
)
PyUnicode_FromString
(
val
);
}
int
_js
NumberToPython
(
double
val
)
{
int
_js
2python_number
(
double
val
)
{
return
(
int
)
PyFloat_FromDouble
(
val
);
}
int
_
pythonN
one
()
{
int
_
js2python_n
one
()
{
Py_INCREF
(
Py_None
);
return
(
int
)
Py_None
;
}
int
_
pythonT
rue
()
{
int
_
js2python_t
rue
()
{
Py_INCREF
(
Py_True
);
return
(
int
)
Py_True
;
}
int
_
pythonF
alse
()
{
int
_
js2python_f
alse
()
{
Py_INCREF
(
Py_False
);
return
(
int
)
Py_False
;
}
int
_js
PyProxyToPython
(
PyObject
*
val
)
{
int
_js
2python_pyproxy
(
PyObject
*
val
)
{
Py_INCREF
(
val
);
return
(
int
)
val
;
}
int
_js
BytesToPython
(
char
*
bytes
,
int
length
)
{
int
_js
2python_bytes
(
char
*
bytes
,
int
length
)
{
return
(
int
)
PyBytes_FromStringAndSize
(
bytes
,
length
);
}
int
_js
ProxyToPython
(
int
id
)
{
int
_js
2python_jsproxy
(
int
id
)
{
return
(
int
)
JsProxy_cnew
(
id
);
}
// TODO: Add some meaningful order
EM_JS
(
int
,
__js
ToP
ython
,
(
int
id
),
{
EM_JS
(
int
,
__js
2p
ython
,
(
int
id
),
{
var
value
=
Module
.
hiwire_get_value
(
id
);
var
type
=
typeof
value
;
if
(
type
===
'
string
'
)
{
var
charptr
=
allocate
(
intArrayFromString
(
value
),
'
i8
'
,
ALLOC_NORMAL
);
var
result
=
__js
StringToPython
(
charptr
);
var
result
=
__js
2python_string
(
charptr
);
_free
(
charptr
);
return
result
;
}
else
if
(
type
===
'
number
'
)
{
return
__js
NumberToPython
(
value
);
return
__js
2python_number
(
value
);
}
else
if
(
value
===
undefined
||
value
===
null
)
{
return
__
pythonN
one
();
return
__
js2python_n
one
();
}
else
if
(
value
===
true
)
{
return
__
pythonT
rue
();
return
__
js2python_t
rue
();
}
else
if
(
value
===
false
)
{
return
__
pythonF
alse
();
return
__
js2python_f
alse
();
}
else
if
(
Module
.
PyProxy
.
isPyProxy
(
value
))
{
return
__js
PyProxyToPython
(
Module
.
PyProxy
.
getPtr
(
value
));
return
__js
2python_pyproxy
(
Module
.
PyProxy
.
getPtr
(
value
));
}
else
if
(
value
[
'
byteLength
'
]
!==
undefined
)
{
var
bytes
=
allocate
(
value
,
'
i8
'
,
ALLOC_NORMAL
);
var
result
=
__js
BytesToPython
(
bytes
,
value
[
'
byteLength
'
]);
var
result
=
__js
2python_bytes
(
bytes
,
value
[
'
byteLength
'
]);
_free
(
bytes
);
return
result
;
}
else
{
return
__js
ProxyToPython
(
id
);
return
__js
2python_jsproxy
(
id
);
}
});
PyObject
*
js
ToP
ython
(
int
id
)
{
return
(
PyObject
*
)
__js
ToP
ython
(
id
);
PyObject
*
js
2p
ython
(
int
id
)
{
return
(
PyObject
*
)
__js
2p
ython
(
id
);
}
int
js
ToPython_Ready
()
{
int
js
2python_init
()
{
return
0
;
}
src/js2python.h
View file @
3831cf86
...
...
@@ -13,9 +13,9 @@
* occurred during the conversion, and the Python exception API should be used
* to obtain the exception.
*/
PyObject
*
js
ToP
ython
(
int
x
);
PyObject
*
js
2p
ython
(
int
x
);
/** Initialize any global variables used by this module. */
int
js
ToPython_Ready
();
int
js
2python_init
();
#endif
/* JS2PYTHON_H */
src/jsimport.c
View file @
3831cf86
...
...
@@ -46,7 +46,7 @@ static PyObject *JsImport_Call(PyObject *self, PyObject *args, PyObject *kwargs)
return
NULL
;
}
int
jsval
=
hiwire_get_global
((
int
)
c
);
PyObject
*
pyval
=
js
ToP
ython
(
jsval
);
PyObject
*
pyval
=
js
2p
ython
(
jsval
);
hiwire_decref
(
jsval
);
if
(
PyDict_SetItem
(
d
,
key
,
pyval
))
{
Py_DECREF
(
key
);
...
...
@@ -79,7 +79,7 @@ static PyObject *JsImport_New() {
return
(
PyObject
*
)
self
;
}
int
JsImport_
Ready
()
{
int
JsImport_
init
()
{
if
(
PyType_Ready
(
&
JsImportType
))
{
return
1
;
}
...
...
src/jsimport.h
View file @
3831cf86
...
...
@@ -6,7 +6,7 @@
#include <Python.h>
/** Install the import hook to support "from js import …". */
int
JsImport_
Ready
();
int
JsImport_
init
();
extern
PyObject
*
globals
;
...
...
src/jsproxy.c
View file @
3831cf86
...
...
@@ -24,7 +24,7 @@ static void JsProxy_dealloc(JsProxy *self) {
static
PyObject
*
JsProxy_Repr
(
PyObject
*
o
)
{
JsProxy
*
self
=
(
JsProxy
*
)
o
;
int
idrepr
=
hiwire_to_string
(
self
->
js
);
PyObject
*
pyrepr
=
js
ToP
ython
(
idrepr
);
PyObject
*
pyrepr
=
js
2p
ython
(
idrepr
);
return
pyrepr
;
}
...
...
@@ -51,7 +51,7 @@ static PyObject *JsProxy_GetAttr(PyObject *o, PyObject *attr_name) {
return
JsBoundMethod_cnew
(
self
->
js
,
key
);
}
PyObject
*
pyresult
=
js
ToP
ython
(
idresult
);
PyObject
*
pyresult
=
js
2p
ython
(
idresult
);
hiwire_decref
(
idresult
);
return
pyresult
;
}
...
...
@@ -64,7 +64,7 @@ static int JsProxy_SetAttr(PyObject *o, PyObject *attr_name, PyObject *pyvalue)
return
-
1
;
}
char
*
key
=
PyUnicode_AsUTF8
(
attr_name_py_str
);
int
idvalue
=
python
ToJ
s
(
pyvalue
);
int
idvalue
=
python
2j
s
(
pyvalue
);
hiwire_set_member_string
(
self
->
js
,
(
int
)
key
,
idvalue
);
hiwire_decref
(
idvalue
);
Py_DECREF
(
attr_name_py_str
);
...
...
@@ -80,14 +80,14 @@ static PyObject* JsProxy_Call(PyObject *o, PyObject *args, PyObject *kwargs) {
int
idargs
=
hiwire_array
();
for
(
Py_ssize_t
i
;
i
<
nargs
;
++
i
)
{
int
idarg
=
python
ToJ
s
(
PyTuple_GET_ITEM
(
args
,
i
));
int
idarg
=
python
2j
s
(
PyTuple_GET_ITEM
(
args
,
i
));
hiwire_push_array
(
idargs
,
idarg
);
hiwire_decref
(
idarg
);
}
int
idresult
=
hiwire_call
(
self
->
js
,
idargs
);
hiwire_decref
(
idargs
);
PyObject
*
pyresult
=
js
ToP
ython
(
idresult
);
PyObject
*
pyresult
=
js
2p
ython
(
idresult
);
hiwire_decref
(
idresult
);
return
pyresult
;
}
...
...
@@ -100,14 +100,14 @@ static PyObject* JsProxy_New(PyObject *o, PyObject *args, PyObject *kwargs) {
int
idargs
=
hiwire_array
();
for
(
Py_ssize_t
i
;
i
<
nargs
;
++
i
)
{
int
idarg
=
python
ToJ
s
(
PyTuple_GET_ITEM
(
args
,
i
));
int
idarg
=
python
2j
s
(
PyTuple_GET_ITEM
(
args
,
i
));
hiwire_push_array
(
idargs
,
idarg
);
hiwire_decref
(
idarg
);
}
int
idresult
=
hiwire_new
(
self
->
js
,
idargs
);
hiwire_decref
(
idargs
);
PyObject
*
pyresult
=
js
ToP
ython
(
idresult
);
PyObject
*
pyresult
=
js
2p
ython
(
idresult
);
hiwire_decref
(
idresult
);
return
pyresult
;
}
...
...
@@ -122,14 +122,14 @@ PyObject* JsProxy_item(PyObject *o, Py_ssize_t idx) {
JsProxy
*
self
=
(
JsProxy
*
)
o
;
int
idresult
=
hiwire_get_member_int
(
self
->
js
,
idx
);
PyObject
*
pyresult
=
js
ToP
ython
(
idresult
);
PyObject
*
pyresult
=
js
2p
ython
(
idresult
);
hiwire_decref
(
idresult
);
return
pyresult
;
}
int
JsProxy_ass_item
(
PyObject
*
o
,
Py_ssize_t
idx
,
PyObject
*
value
)
{
JsProxy
*
self
=
(
JsProxy
*
)
o
;
int
idvalue
=
python
ToJ
s
(
value
);
int
idvalue
=
python
2j
s
(
value
);
hiwire_set_member_int
(
self
->
js
,
idx
,
idvalue
);
hiwire_decref
(
idvalue
);
return
0
;
...
...
@@ -200,14 +200,14 @@ static PyObject* JsBoundMethod_Call(PyObject *o, PyObject *args, PyObject *kwarg
int
idargs
=
hiwire_array
();
for
(
Py_ssize_t
i
=
0
;
i
<
nargs
;
++
i
)
{
int
idarg
=
python
ToJ
s
(
PyTuple_GET_ITEM
(
args
,
i
));
int
idarg
=
python
2j
s
(
PyTuple_GET_ITEM
(
args
,
i
));
hiwire_push_array
(
idargs
,
idarg
);
hiwire_decref
(
idarg
);
}
int
idresult
=
hiwire_call_member
(
self
->
this_
,
(
int
)
self
->
name
,
idargs
);
hiwire_decref
(
idargs
);
PyObject
*
pyresult
=
js
ToP
ython
(
idresult
);
PyObject
*
pyresult
=
js
2p
ython
(
idresult
);
hiwire_decref
(
idresult
);
return
pyresult
;
}
...
...
@@ -242,7 +242,7 @@ int JsProxy_AsJs(PyObject *x) {
return
hiwire_incref
(
js_proxy
->
js
);
}
int
JsProxy_
Ready
()
{
int
JsProxy_
init
()
{
return
(
PyType_Ready
(
&
JsProxyType
)
||
PyType_Ready
(
&
JsBoundMethodType
));
}
src/jsproxy.h
View file @
3831cf86
...
...
@@ -26,6 +26,6 @@ int JsProxy_Check(PyObject *x);
int
JsProxy_AsJs
(
PyObject
*
x
);
/** Initialize global state for the JsProxy functionality. */
int
JsProxy_
Ready
();
int
JsProxy_
init
();
#endif
/* JSPROXY_H */
src/main.c
View file @
3831cf86
...
...
@@ -10,16 +10,6 @@
#include "python2js.h"
#include "runpython.h"
// TODO: Use static functions where appropriate
////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////
// Conversions
/*
TODO: This is a workaround for a weird emscripten compiler bug. The
matplotlib/_qhull.so extension makes function pointer calls with these
...
...
@@ -53,15 +43,17 @@ int main(int argc, char** argv) {
Py_InitializeEx
(
0
);
// cleanup naming of these functions
if
(
JsProxy_Ready
()
||
jsToPython_Ready
()
||
pythonToJs_Ready
()
||
JsImport_Ready
()
||
runPython_Ready
()
||
pyimport_Ready
()
||
PyProxy_Ready
())
{
// TODO cleanup naming of these functions
if
(
js2python_init
()
||
JsImport_init
()
||
JsProxy_init
()
||
pyimport_init
()
||
pyproxy_init
()
||
python2js_init
()
||
runpython_init
()
)
{
return
1
;
}
...
...
src/pyimport.c
View file @
3831cf86
...
...
@@ -7,24 +7,24 @@
extern
PyObject
*
globals
;
int
pyimport
(
char
*
name
)
{
int
_
pyimport
(
char
*
name
)
{
PyObject
*
pyname
=
PyUnicode_FromString
(
name
);
PyObject
*
pyval
=
PyDict_GetItem
(
globals
,
pyname
);
if
(
pyval
==
NULL
)
{
Py_DECREF
(
pyname
);
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
Py_DECREF
(
pyname
);
int
idval
=
python
ToJ
s
(
pyval
);
int
idval
=
python
2j
s
(
pyval
);
Py_DECREF
(
pyval
);
return
idval
;
}
EM_JS
(
int
,
pyimport_
Ready
,
(),
{
EM_JS
(
int
,
pyimport_
init
,
(),
{
Module
.
pyimport
=
function
(
name
)
{
var
pyname
=
allocate
(
intArrayFromString
(
name
),
'
i8
'
,
ALLOC_NORMAL
);
var
idresult
=
Module
.
_pyimport
(
pyname
);
var
idresult
=
Module
.
_
_
pyimport
(
pyname
);
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
_free
(
pyname
);
...
...
src/pyimport.h
View file @
3831cf86
...
...
@@ -4,7 +4,6 @@
/** Makes `var foo = pyodide.pyimport('foo')` work in Javascript.
*/
int
pyimport
(
char
*
name
);
int
pyimport_Ready
();
int
pyimport_init
();
#endif
/* PYIMPORT_H */
src/pyproxy.c
View file @
3831cf86
...
...
@@ -5,17 +5,17 @@
#include "js2python.h"
#include "python2js.h"
int
pyproxy_has
(
int
ptrobj
,
int
idkey
)
{
int
_
pyproxy_has
(
int
ptrobj
,
int
idkey
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
PyObject
*
pykey
=
js
ToP
ython
(
idkey
);
PyObject
*
pykey
=
js
2p
ython
(
idkey
);
int
result
=
PyObject_HasAttr
(
pyobj
,
pykey
)
?
hiwire_true
()
:
hiwire_false
();
Py_DECREF
(
pykey
);
return
result
;
}
int
pyproxy_get
(
int
ptrobj
,
int
idkey
)
{
int
_
pyproxy_get
(
int
ptrobj
,
int
idkey
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
PyObject
*
pykey
=
js
ToP
ython
(
idkey
);
PyObject
*
pykey
=
js
2p
ython
(
idkey
);
PyObject
*
pyattr
=
PyObject_GetAttr
(
pyobj
,
pykey
);
Py_DECREF
(
pykey
);
if
(
pyattr
==
NULL
)
{
...
...
@@ -23,52 +23,52 @@ int pyproxy_get(int ptrobj, int idkey) {
return
hiwire_undefined
();
}
int
idattr
=
python
ToJ
s
(
pyattr
);
int
idattr
=
python
2j
s
(
pyattr
);
Py_DECREF
(
pyattr
);
return
idattr
;
};
int
pyproxy_set
(
int
ptrobj
,
int
idkey
,
int
idval
)
{
int
_
pyproxy_set
(
int
ptrobj
,
int
idkey
,
int
idval
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
PyObject
*
pykey
=
js
ToP
ython
(
idkey
);
PyObject
*
pyval
=
js
ToP
ython
(
idval
);
PyObject
*
pykey
=
js
2p
ython
(
idkey
);
PyObject
*
pyval
=
js
2p
ython
(
idval
);
int
result
=
PyObject_SetAttr
(
pyobj
,
pykey
,
pyval
);
Py_DECREF
(
pykey
);
Py_DECREF
(
pyval
);
if
(
result
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
idval
;
}
int
pyproxy_deleteProperty
(
int
ptrobj
,
int
idkey
)
{
int
_
pyproxy_deleteProperty
(
int
ptrobj
,
int
idkey
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
PyObject
*
pykey
=
js
ToP
ython
(
idkey
);
PyObject
*
pykey
=
js
2p
ython
(
idkey
);
int
ret
=
PyObject_DelAttr
(
pyobj
,
pykey
);
Py_DECREF
(
pykey
);
if
(
ret
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
hiwire_undefined
();
}
int
pyproxy_ownKeys
(
int
ptrobj
)
{
int
_
pyproxy_ownKeys
(
int
ptrobj
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
PyObject
*
pydir
=
PyObject_Dir
(
pyobj
);
if
(
pydir
==
NULL
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
int
iddir
=
hiwire_array
();
Py_ssize_t
n
=
PyList_Size
(
pydir
);
for
(
Py_ssize_t
i
=
0
;
i
<
n
;
++
i
)
{
PyObject
*
pyentry
=
PyList_GetItem
(
pydir
,
i
);
int
identry
=
python
ToJ
s
(
pyentry
);
int
identry
=
python
2j
s
(
pyentry
);
hiwire_push_array
(
iddir
,
identry
);
hiwire_decref
(
identry
);
}
...
...
@@ -77,26 +77,26 @@ int pyproxy_ownKeys(int ptrobj) {
return
iddir
;
}
int
pyproxy_enumerate
(
int
ptrobj
)
{
return
pyproxy_ownKeys
(
ptrobj
);
int
_
pyproxy_enumerate
(
int
ptrobj
)
{
return
_
pyproxy_ownKeys
(
ptrobj
);
}
int
pyproxy_apply
(
int
ptrobj
,
int
idargs
)
{
int
_
pyproxy_apply
(
int
ptrobj
,
int
idargs
)
{
PyObject
*
pyobj
=
(
PyObject
*
)
ptrobj
;
Py_ssize_t
length
=
hiwire_get_length
(
idargs
);
PyObject
*
pyargs
=
PyTuple_New
(
length
);
for
(
Py_ssize_t
i
=
0
;
i
<
length
;
++
i
)
{
int
iditem
=
hiwire_get_member_int
(
idargs
,
i
);
PyObject
*
pyitem
=
js
ToP
ython
(
iditem
);
PyObject
*
pyitem
=
js
2p
ython
(
iditem
);
PyTuple_SET_ITEM
(
pyargs
,
i
,
pyitem
);
hiwire_decref
(
iditem
);
}
PyObject
*
pyresult
=
PyObject_Call
(
pyobj
,
pyargs
,
NULL
);
if
(
pyresult
==
NULL
)
{
Py_DECREF
(
pyargs
);
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
int
idresult
=
python
ToJ
s
(
pyresult
);
int
idresult
=
python
2j
s
(
pyresult
);
Py_DECREF
(
pyresult
);
Py_DECREF
(
pyargs
);
return
idresult
;
...
...
@@ -108,7 +108,7 @@ EM_JS(int, pyproxy_new, (int ptrobj), {
return
Module
.
hiwire_new_value
(
new
Proxy
(
target
,
Module
.
PyProxy
));
});
EM_JS
(
int
,
PyProxy_Ready
,
(),
{
EM_JS
(
int
,
pyproxy_init
,
(),
{
Module
.
PyProxy
=
{
getPtr:
function
(
jsobj
)
{
return
jsobj
[
'$$'
][
'
ptr
'
];
...
...
@@ -120,7 +120,7 @@ EM_JS(int, PyProxy_Ready, (), {
has:
function
(
jsobj
,
jskey
)
{
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idkey
=
Module
.
hiwire_new_value
(
jskey
);
var
result
=
_pyproxy_has
(
ptrobj
,
idkey
)
!=
0
;
var
result
=
_
_
pyproxy_has
(
ptrobj
,
idkey
)
!=
0
;
Module
.
hiwire_decref
(
idkey
);
return
result
;
},
...
...
@@ -136,7 +136,7 @@ EM_JS(int, PyProxy_Ready, (), {
}
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idkey
=
Module
.
hiwire_new_value
(
jskey
);
var
idresult
=
_pyproxy_get
(
ptrobj
,
idkey
);
var
idresult
=
_
_
pyproxy_get
(
ptrobj
,
idkey
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idkey
);
Module
.
hiwire_decref
(
idresult
);
...
...
@@ -146,7 +146,7 @@ EM_JS(int, PyProxy_Ready, (), {
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idkey
=
Module
.
hiwire_new_value
(
jskey
);
var
idval
=
Module
.
hiwire_new_value
(
jsval
);
var
idresult
=
_pyproxy_set
(
ptrobj
,
idkey
,
idval
);
var
idresult
=
_
_
pyproxy_set
(
ptrobj
,
idkey
,
idval
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idkey
);
Module
.
hiwire_decref
(
idval
);
...
...
@@ -156,7 +156,7 @@ EM_JS(int, PyProxy_Ready, (), {
deleteProperty:
function
(
jsobj
,
jskey
)
{
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idkey
=
Module
.
hiwire_new_value
(
jskey
);
var
idresult
=
_pyproxy_deleteProperty
(
ptrobj
,
idkey
);
var
idresult
=
_
_
pyproxy_deleteProperty
(
ptrobj
,
idkey
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
Module
.
hiwire_decref
(
idkey
);
...
...
@@ -164,7 +164,7 @@ EM_JS(int, PyProxy_Ready, (), {
},
ownKeys:
function
(
jsobj
)
{
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idresult
=
_pyproxy_ownKeys
(
ptrobj
);
var
idresult
=
_
_
pyproxy_ownKeys
(
ptrobj
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
jsresult
.
push
(
'
toString
'
);
...
...
@@ -173,7 +173,7 @@ EM_JS(int, PyProxy_Ready, (), {
},
enumerate:
function
(
jsobj
)
{
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idresult
=
_pyproxy_enumerate
(
ptrobj
);
var
idresult
=
_
_
pyproxy_enumerate
(
ptrobj
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
jsresult
.
push
(
'
toString
'
);
...
...
@@ -183,7 +183,7 @@ EM_JS(int, PyProxy_Ready, (), {
apply:
function
(
jsobj
,
jsthis
,
jsargs
)
{
ptrobj
=
this
.
getPtr
(
jsobj
);
var
idargs
=
Module
.
hiwire_new_value
(
jsargs
);
var
idresult
=
_pyproxy_apply
(
ptrobj
,
idargs
);
var
idresult
=
_
_
pyproxy_apply
(
ptrobj
,
idargs
);
var
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
Module
.
hiwire_decref
(
idargs
);
...
...
src/pyproxy.h
View file @
3831cf86
...
...
@@ -8,6 +8,6 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
int
pyproxy_new
(
int
obj
);
int
PyProxy_Ready
();
int
pyproxy_init
();
#endif
/* PYPROXY_H */
src/python2js.c
View file @
3831cf86
...
...
@@ -6,7 +6,7 @@
#include "jsproxy.h"
#include "pyproxy.h"
int
python
ExcToJ
s
()
{
int
python
exc2j
s
()
{
PyObject
*
type
;
PyObject
*
value
;
PyObject
*
traceback
;
...
...
@@ -32,7 +32,7 @@ int pythonExcToJs() {
if
(
repr
==
NULL
)
{
excval
=
hiwire_string_utf8
((
int
)
"Could not get repr for exception"
);
}
else
{
excval
=
python
ToJ
s
(
repr
);
excval
=
python
2j
s
(
repr
);
Py_DECREF
(
repr
);
}
}
else
{
...
...
@@ -64,7 +64,7 @@ int pythonExcToJs() {
PyObject
*
pystr
=
PyUnicode_Join
(
newline
,
pylines
);
printf
(
"Python exception:
\n
"
);
printf
(
"%s
\n
"
,
PyUnicode_AsUTF8
(
pystr
));
excval
=
python
ToJ
s
(
pystr
);
excval
=
python
2j
s
(
pystr
);
Py_DECREF
(
pystr
);
Py_DECREF
(
newline
);
Py_DECREF
(
pylines
);
...
...
@@ -86,7 +86,7 @@ int pythonExcToJs() {
return
-
1
;
}
static
int
is
TypeN
ame
(
PyObject
*
x
,
const
char
*
name
)
{
static
int
is
_type_n
ame
(
PyObject
*
x
,
const
char
*
name
)
{
PyObject
*
x_type
=
PyObject_Type
(
x
);
if
(
x_type
==
NULL
)
{
// If we can't get a type, that's probably ok in this case...
...
...
@@ -103,7 +103,7 @@ static int isTypeName(PyObject *x, const char *name) {
return
result
;
}
int
python
ToJ
s
(
PyObject
*
x
)
{
int
python
2j
s
(
PyObject
*
x
)
{
if
(
x
==
Py_None
)
{
return
hiwire_undefined
();
}
else
if
(
x
==
Py_True
)
{
...
...
@@ -113,32 +113,32 @@ int pythonToJs(PyObject *x) {
}
else
if
(
PyLong_Check
(
x
))
{
long
x_long
=
PyLong_AsLongLong
(
x
);
if
(
x_long
==
-
1
&&
PyErr_Occurred
())
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
hiwire_int
(
x_long
);
}
else
if
(
PyFloat_Check
(
x
))
{
double
x_double
=
PyFloat_AsDouble
(
x
);
if
(
x_double
==
-
1
.
0
&&
PyErr_Occurred
())
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
hiwire_double
(
x_double
);
}
else
if
(
PyUnicode_Check
(
x
))
{
Py_ssize_t
length
;
char
*
chars
=
PyUnicode_AsUTF8AndSize
(
x
,
&
length
);
if
(
chars
==
NULL
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
hiwire_string_utf8_length
((
int
)(
void
*
)
chars
,
length
);
}
else
if
(
PyBytes_Check
(
x
))
{
char
*
x_buff
;
Py_ssize_t
length
;
if
(
PyBytes_AsStringAndSize
(
x
,
&
x_buff
,
&
length
))
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
return
hiwire_bytes
((
int
)(
void
*
)
x_buff
,
length
);
}
else
if
(
JsProxy_Check
(
x
))
{
return
JsProxy_AsJs
(
x
);
}
else
if
(
PyList_Check
(
x
)
||
is
TypeN
ame
(
x
,
"<class 'numpy.ndarray'>"
))
{
}
else
if
(
PyList_Check
(
x
)
||
is
_type_n
ame
(
x
,
"<class 'numpy.ndarray'>"
))
{
int
jsarray
=
hiwire_array
();
size_t
length
=
PySequence_Size
(
x
);
for
(
size_t
i
=
0
;
i
<
length
;
++
i
)
{
...
...
@@ -151,11 +151,11 @@ int pythonToJs(PyObject *x) {
Py_INCREF
(
x
);
return
pyproxy_new
((
int
)
x
);
}
int
jsitem
=
python
ToJ
s
(
pyitem
);
int
jsitem
=
python
2j
s
(
pyitem
);
if
(
jsitem
==
-
1
)
{
Py_DECREF
(
pyitem
);
hiwire_decref
(
jsarray
);
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
Py_DECREF
(
pyitem
);
hiwire_push_array
(
jsarray
,
jsitem
);
...
...
@@ -167,18 +167,18 @@ int pythonToJs(PyObject *x) {
PyObject
*
pykey
,
*
pyval
;
Py_ssize_t
pos
=
0
;
while
(
PyDict_Next
(
x
,
&
pos
,
&
pykey
,
&
pyval
))
{
int
jskey
=
python
ToJ
s
(
pykey
);
int
jskey
=
python
2j
s
(
pykey
);
if
(
jskey
==
-
1
)
{
hiwire_decref
(
jsdict
);
// TODO: Return a proxy instead here???
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
int
jsval
=
python
ToJ
s
(
pyval
);
int
jsval
=
python
2j
s
(
pyval
);
if
(
jsval
==
-
1
)
{
// TODO: Return a proxy instead here???
hiwire_decref
(
jskey
);
hiwire_decref
(
jsdict
);
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
hiwire_push_object_pair
(
jsdict
,
jskey
,
jsval
);
hiwire_decref
(
jskey
);
...
...
@@ -191,6 +191,6 @@ int pythonToJs(PyObject *x) {
}
}
int
python
ToJs_Ready
()
{
int
python
2js_init
()
{
return
0
;
}
src/python2js.h
View file @
3831cf86
...
...
@@ -9,16 +9,16 @@
/** Convert the active Python exception into a Javascript Error object.
* \return A Javascript Error object
*/
int
python
ExcToJ
s
();
int
python
exc2j
s
();
/** Convert a Python object to a Javascript object.
* \param The Python object
* \return The Javascript object -- might be an Error object in the case of an exception.
*/
int
python
ToJ
s
(
PyObject
*
x
);
int
python
2j
s
(
PyObject
*
x
);
/** Set up the global state for this module.
*/
int
python
ToJs_Ready
();
int
python
2js_init
();
#endif
/* PYTHON2JS_H */
src/runpython.c
View file @
3831cf86
...
...
@@ -21,7 +21,7 @@ static int is_whitespace(char x) {
}
}
int
runPython
(
char
*
code
)
{
int
_
runPython
(
char
*
code
)
{
char
*
last_line
=
code
;
while
(
*
last_line
!=
0
)
{
++
last_line
;
...
...
@@ -59,7 +59,7 @@ int runPython(char *code) {
}
ret
=
PyRun_StringFlags
(
code
,
Py_file_input
,
globals
,
globals
,
&
cf
);
if
(
ret
==
NULL
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
Py_DECREF
(
ret
);
}
...
...
@@ -78,18 +78,18 @@ int runPython(char *code) {
}
if
(
ret
==
NULL
)
{
return
python
ExcToJ
s
();
return
python
exc2j
s
();
}
int
id
=
python
ToJ
s
(
ret
);
int
id
=
python
2j
s
(
ret
);
Py_DECREF
(
ret
);
return
id
;
}
EM_JS
(
int
,
run
Python_Ready
,
(),
{
EM_JS
(
int
,
run
python_init
,
(),
{
Module
.
runPython
=
function
(
code
)
{
var
pycode
=
allocate
(
intArrayFromString
(
code
),
'
i8
'
,
ALLOC_NORMAL
);
var
idresult
=
Module
.
_runPython
(
pycode
);
var
idresult
=
Module
.
_
_
runPython
(
pycode
);
jsresult
=
Module
.
hiwire_get_value
(
idresult
);
Module
.
hiwire_decref
(
idresult
);
_free
(
pycode
);
...
...
src/runpython.h
View file @
3831cf86
...
...
@@ -5,10 +5,6 @@
*/
/** The primary entry point function that runs Python code.
*/
int
runPython
(
char
*
code
);
int
runPython_Ready
();
int
runpython_init
();
#endif
/* RUNPYTHON_H */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment