python-emscripten  Check-in [cc5f171a0c]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:emscripten.pyx: async_wget_data binding
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cc5f171a0c782fc2004831ace0389b036d8a1fd3599a64aabc03fba327559138
User & Date: Beuc 2019-10-04 16:32:37
Context
2019-10-07
09:42
webprompt: display return value, remove spurious warnings check-in: c7cce09fda user: Beuc tags: trunk
2019-10-04
16:32
emscripten.pyx: async_wget_data binding check-in: cc5f171a0c user: Beuc tags: trunk
2019-10-02
21:31
webprompt: initialization now better suited for the web check-in: 19428dfe5e user: Beuc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to emscripten.pyx.

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
# Python wrapper for emscripten_* C functions

# Copyright (C) 2018  Sylvain Beucler

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

cdef extern from "emscripten.h":
    ctypedef void (*em_callback_func)()
    ctypedef void (*em_arg_callback_func)(void*)


    void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop)
    void emscripten_set_main_loop_arg(em_arg_callback_func func, void *arg, int fps, int simulate_infinite_loop)
    void emscripten_exit_with_live_runtime()
    void emscripten_sleep(unsigned int ms)
    void emscripten_sleep_with_yield(unsigned int ms)
    void emscripten_run_script(const char *script)
    int emscripten_run_script_int(const char *script)
    char *emscripten_run_script_string(const char *script)
    void emscripten_async_call(em_arg_callback_func func, void *arg, int millis)



# https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html
from libc.stdlib cimport malloc, free
# https://github.com/cython/cython/wiki/FAQ#what-is-the-difference-between-pyobject-and-object
from cpython.ref cimport PyObject, Py_XINCREF, Py_XDECREF




|









>
>









>
>







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
# Python wrapper for emscripten_* C functions

# Copyright (C) 2018, 2019  Sylvain Beucler

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

cdef extern from "emscripten.h":
    ctypedef void (*em_callback_func)()
    ctypedef void (*em_arg_callback_func)(void*)
    ctypedef void (*em_async_wget_onload_func)(void*, void*, int)

    void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop)
    void emscripten_set_main_loop_arg(em_arg_callback_func func, void *arg, int fps, int simulate_infinite_loop)
    void emscripten_exit_with_live_runtime()
    void emscripten_sleep(unsigned int ms)
    void emscripten_sleep_with_yield(unsigned int ms)
    void emscripten_run_script(const char *script)
    int emscripten_run_script_int(const char *script)
    char *emscripten_run_script_string(const char *script)
    void emscripten_async_call(em_arg_callback_func func, void *arg, int millis)
    #void emscripten_async_wget(const char* url, const char* file, em_str_callback_func onload, em_str_callback_func onerror)
    void emscripten_async_wget_data(const char* url, void *arg, em_async_wget_onload_func onload, em_arg_callback_func onerror)

# https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html
from libc.stdlib cimport malloc, free
# https://github.com/cython/cython/wiki/FAQ#what-is-the-difference-between-pyobject-and-object
from cpython.ref cimport PyObject, Py_XINCREF, Py_XDECREF


76
77
78
79
80
81
82









































83
84
85
86
87
88
89
90
91

def run_script_int(script):
    return emscripten_run_script_int(script);

def run_script_string(script):
    return emscripten_run_script_string(script);










































def syncfs():
    emscripten_run_script(r"""
        FS.syncfs(false, function(err) {
            if (err) {
                console.trace(); console.log(err, err.message);
                Module.print("Warning: write error: " + err.message + "\n");
            }
        })
    """);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

def run_script_int(script):
    return emscripten_run_script_int(script);

def run_script_string(script):
    return emscripten_run_script_string(script);

# async_wget
# Requires a C function without parameter, while we need to set
# callpyfunc_arg as callback (so we can call a Python function)
# Perhaps doable if we maintain a list of Python callbacks indexed by 'file' (and ignore potential conflict)
# Or dynamically generate C callbacks in WebAssembly but I doubt that's simple.
# Or implement it with async_wget_data + write output file manually
#def async_wget(url, file, onload, onerror):
#    pass

cdef struct callpyfunc_async_wget_s:
    PyObject* onload
    PyObject* onerror
    PyObject* arg
cdef void callpyfunc_async_wget_onload(void* p, void* buf, int size):
    s = <callpyfunc_async_wget_s*>p
    # https://cython.readthedocs.io/en/latest/src/tutorial/strings.html#passing-byte-strings
    py_buf = (<char*>buf)[:size]
    (<object>(s.onload))(<object>(s.arg), py_buf)
    Py_XDECREF(s.onload)
    Py_XDECREF(s.onerror)
    Py_XDECREF(s.arg)
    free(s)
cdef void callpyfunc_async_wget_onerror(void* p):
    s = <callpyfunc_async_wget_s*>p
    (<object>(s.onerror))(<object>(s.arg))
    Py_XDECREF(s.onload)
    Py_XDECREF(s.onerror)
    Py_XDECREF(s.arg)
    free(s)

def async_wget_data(url, arg, onload, onerror):
    cdef callpyfunc_async_wget_s* s = <callpyfunc_async_wget_s*> malloc(sizeof(callpyfunc_async_wget_s))
    s.onload = <PyObject*>onload
    s.onerror = <PyObject*>onerror
    s.arg = <PyObject*>arg
    Py_XINCREF(s.onload)
    Py_XINCREF(s.onerror)
    Py_XINCREF(s.arg)
    emscripten_async_wget_data(url, <void*>s, callpyfunc_async_wget_onload, callpyfunc_async_wget_onerror)
# emscripten.async_wget_data('http://localhost:8000/', None, lambda arg,buf: sys.stdout.write(repr(buf)+"\n"), lambda arg: sys.stdout.write("d/l error\n"))

def syncfs():
    emscripten_run_script(r"""
        FS.syncfs(false, function(err) {
            if (err) {
                console.trace(); console.log(err, err.message);
                Module.print("Warning: write error: " + err.message + "\n");
            }
        })
    """);