@@ -16,25 +16,98 @@ namespace py = pybind11;
16
16
namespace em = emscripten;
17
17
18
18
namespace pyjs
19
- {
19
+ {
20
+
21
+ // struct LoopContext
22
+ // {
23
+ // py::object m_callback;
24
+ // bool m_cancel_loop_on_error = true; // default to true
25
+ // bool m_exit_loop = false;
26
+
27
+ // LoopContext(py::object callback, bool cancel_loop_on_error)
28
+ // : m_callback(std::move(callback)), m_cancel_loop_on_error(cancel_loop_on_error), m_exit_loop(false) {}
29
+ // };
30
+
31
+
32
+ void wrapped_callback (void * cb_ptr) {
33
+ // Reinterpret the void pointer back to a PyObject pointer
34
+ auto py_object = reinterpret_cast <PyObject*>(cb_ptr);
35
+ if (!py_object) {
36
+ std::cerr << " Error: callback pointer is null." << std::endl;
37
+ }
38
+ // We can use PyObject_CallObject to call the Python function
39
+ if (PyObject_CallNoArgs (py_object) == nullptr ) {
40
+ // If the call fails, we can print the error
41
+ std::cerr << " Error calling Python callback:" << std::endl;
42
+ PyErr_Print ();
43
+ }
44
+ };
45
+
46
+ void noop_callback () {
47
+ // This is a no-op callback, it does nothing
48
+
49
+ // we see a strange error when we run emscripten_cancel_main_loop
50
+ // **WITHOUT setting a new loop right away**
51
+ // so instead of just cancelling the loop, we need
52
+ // to cancel and right away set a new loop
53
+ }
54
+
55
+ void self_cancel_callback () {
56
+ emscripten_cancel_main_loop ();
57
+ };
58
+
59
+
60
+ void export_main_loop_callbacks (py::module_& pyjs_module)
61
+ {
62
+
63
+
64
+
65
+ // // class for loop context
66
+ // py::class_<LoopContext>(pyjs_module, "LoopContext")
67
+ // .def(py::init<py::object, bool>(), py::arg("callback"), py::arg("cancel_loop_on_error") = true)
68
+ // .def_readwrite("exit_loop", &LoopContext::m_exit_loop)
69
+ // ;
70
+
71
+
72
+
73
+ // Export main loop callbacks
74
+ pyjs_module.def (" _set_main_loop_callback" , [](py::handle callback, int fps) {
75
+
76
+ // get a PyObject * from the handle
77
+ auto py_object = callback.ptr ();
78
+
79
+ // convert the PyObject to void*
80
+ void * callback_ptr = reinterpret_cast <void *>(py_object);
81
+
82
+
83
+ // use emscripten_set_main_loop_arg
84
+ emscripten_set_main_loop_arg (
85
+ wrapped_callback,
86
+ callback_ptr, // pass the callback pointer as argument
87
+ fps, // frames per second
88
+ false
89
+ );
90
+ });
91
+
92
+ // explicit cancel main loop
93
+ pyjs_module.def (" _cancel_main_loop" , []() {
94
+ // This will cancel the main loop if it is currently running
95
+ emscripten_cancel_main_loop ();
96
+ });
97
+
98
+ pyjs_module.def (" _set_noop_main_loop" , []() {
99
+ // This will set a no-op main loop
100
+ emscripten_set_main_loop (noop_callback, 1 , false ); // set a no-op loop to avoid errors
101
+ });
102
+
103
+ }
104
+
105
+
106
+
20
107
void export_pyjs_module (py::module_& pyjs_module)
21
108
{
22
109
export_js_proxy (pyjs_module);
23
- try
24
- {
25
- // pyjs_core_pseudo_init(pyjs_module);
26
- // pyjs_extend_js_val_pseudo_init(pyjs_module);
27
- // pyjs_error_handling_pseudo_init(pyjs_module);
28
- // pyjs_convert_pseudo_init(pyjs_module);
29
- // pyjs_convert_py_to_js_pseudo_init(pyjs_module);
30
- // pyjs_webloop_pseudo_init(pyjs_module);
31
- // pyjs_pyodide_polyfill_pseudo_init(pyjs_module);
32
- }
33
- catch (py::error_already_set& e)
34
- {
35
- std::cout << " error: " << e.what () << " \n " ;
36
- throw e;
37
- }
110
+ export_main_loop_callbacks (pyjs_module);
38
111
39
112
}
40
113
}
0 commit comments