Skip to content

Commit

Permalink
Simplify setup code (pyodide#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hood Chatham authored Feb 10, 2021
1 parent 747152a commit f64bee4
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 179 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ build/pyodide.asm.js: \
src/core/pyproxy.o \
src/core/python2js_buffer.o \
src/core/python2js.o \
src/core/runpython.o \
src/pystone.py \
src/_testcapi.py \
src/webbrowser.py \
Expand Down
6 changes: 2 additions & 4 deletions docs/development/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ This file is intended as guidelines to help contributors trying to modify the C
## What the files do
The primary purpose of `core` is to implement {ref}`type conversions <type_conversions>` between Python and Javascript. Here is a breakdown of the purposes of the files.

* `main.c` -- responsible for configuring and initializing the python interpreter, initializing the other source files, and creating the `_pyodide_core` module which is used to expose Python objects to `pyodide_py`. `main.c` also tries to generate fatal initialization error messages to help with debugging when there is a mistake in the initialization code.

* `runpython` -- Defines the `_runPythonDebug` entrypoint to help in case there is a bug in `PyProxy.apply`.

* `main` -- responsible for configuring and initializing the python interpreter, initializing the other source files, and creating the `_pyodide_core` module which is used to expose Python objects to `pyodide_py`. `main.c` also tries to generate fatal initialization error messages to help with debugging when there is a mistake in the initialization code.
* `keyboard_interrupt` -- This sets up the keyboard interrupts system for using Pyodide with a webworker.

### Backend utilities
* `hiwire` -- A helper framework. It is impossible for wasm to directly hold owning references to javascript objects. The primary purpose of hiwire is to act as a surrogate owner for javascript references by holding the references in a javascript `Map`. `hiwire` also defines a wide variety of `EM_JS` helper functions to do javascript operations on the held objects. The primary type that hiwire exports is `JsRef`. References are created with `Module.hiwire.new_value` (only can be done from javascript) and must be destroyed from C with `hiwire_decref` or `hiwire_CLEAR`, or from javascript with `Module.hiwire.decref`.
Expand Down
4 changes: 1 addition & 3 deletions src/core/jsproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ JsProxy_init(PyObject* core_module)
bool success = false;

PyObject* asyncio_module = NULL;
PyObject* pyodide_module = NULL;

asyncio_module = PyImport_ImportModule("asyncio");
FAIL_IF_NULL(asyncio_module);
Expand All @@ -901,7 +900,7 @@ JsProxy_init(PyObject* core_module)

JsMethodType.tp_base = &JsProxyType;
JsBufferType.tp_base = &JsProxyType;
// Add JsException to the pyodide module so people can catch it if they want.
// Add JsException to core_module so people can catch it if they want.
FAIL_IF_MINUS_ONE(PyModule_AddType(core_module, &JsProxyType));
FAIL_IF_MINUS_ONE(PyModule_AddType(core_module, &JsBufferType));
FAIL_IF_MINUS_ONE(PyModule_AddType(core_module, &JsMethodType));
Expand All @@ -910,6 +909,5 @@ JsProxy_init(PyObject* core_module)
success = true;
finally:
Py_CLEAR(asyncio_module);
Py_CLEAR(pyodide_module);
return success ? 0 : -1;
}
35 changes: 30 additions & 5 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "keyboard_interrupt.h"
#include "pyproxy.h"
#include "python2js.h"
#include "runpython.h"

#define FATAL_ERROR(args...) \
do { \
Expand Down Expand Up @@ -73,6 +72,19 @@ static struct PyModuleDef core_module_def = {
.m_size = -1,
};

PyObject* init_dict;

void
run_python_simple_inner(char* code)
{
PyObject* result = PyRun_String(code, Py_file_input, init_dict, init_dict);
if (result == NULL) {
pythonexc2js();
} else {
Py_DECREF(result);
}
}

int
main(int argc, char** argv)
{
Expand All @@ -96,7 +108,7 @@ main(int argc, char** argv)
TRY_INIT(hiwire);
TRY_INIT(error_handling);
TRY_INIT(js2python);
TRY_INIT_WITH_CORE_MODULE(JsProxy); // JsProxy needs to be before JsImport
TRY_INIT_WITH_CORE_MODULE(JsProxy);
TRY_INIT(pyproxy);
TRY_INIT(python2js);
TRY_INIT(keyboard_interrupt);
Expand All @@ -106,9 +118,22 @@ main(int argc, char** argv)
FATAL_ERROR("Failed to add '_pyodide_core' module to modules dict.");
}

// pyodide.py imported for these two.
// They should appear last so that core_module is ready.
TRY_INIT(runpython);
init_dict = PyDict_New();
JsRef init_dict_proxy = python2js(init_dict);
EM_ASM(
{
Module.init_dict = Module.hiwire.pop_value($0);
Module.runPythonSimple = function(code)
{
let code_c_string = stringToNewUTF8(code);
try {
_run_python_simple_inner(code_c_string);
} finally {
_free(code_c_string);
}
};
},
init_dict_proxy);

Py_CLEAR(core_module);
printf("Python initialization complete\n");
Expand Down
129 changes: 0 additions & 129 deletions src/core/runpython.c

This file was deleted.

12 changes: 0 additions & 12 deletions src/core/runpython.h

This file was deleted.

Loading

0 comments on commit f64bee4

Please sign in to comment.