From b79d8ad6403b710e585611ce07375df83cbaa3ad Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 18 Dec 2020 23:44:45 +0800 Subject: [PATCH] nac3embedded: basic source extraction --- .gitignore | 1 + nac3embedded/demo.py | 11 +++++++++++ nac3embedded/language.py | 17 +++++++++++++++++ nac3embedded/src/lib.rs | 15 +++++++++++---- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 nac3embedded/demo.py create mode 100644 nac3embedded/language.py diff --git a/.gitignore b/.gitignore index ea8c4bf7..691f4944 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +__pycache__ /target diff --git a/nac3embedded/demo.py b/nac3embedded/demo.py new file mode 100644 index 00000000..9264b960 --- /dev/null +++ b/nac3embedded/demo.py @@ -0,0 +1,11 @@ +from language import * + + +class Demo: + @kernel + def run(self): + pass + + +if __name__ == "__main__": + Demo().run() diff --git a/nac3embedded/language.py b/nac3embedded/language.py new file mode 100644 index 00000000..57e10d93 --- /dev/null +++ b/nac3embedded/language.py @@ -0,0 +1,17 @@ +from functools import wraps + +import nac3embedded + + +__all__ = ["kernel", "portable"] + + +def kernel(function): + @wraps(function) + def run_on_core(self, *args, **kwargs): + nac3embedded.add_host_object(self) + return run_on_core + + +def portable(function): + return fn diff --git a/nac3embedded/src/lib.rs b/nac3embedded/src/lib.rs index c0e864d8..213f15e6 100644 --- a/nac3embedded/src/lib.rs +++ b/nac3embedded/src/lib.rs @@ -2,13 +2,20 @@ use pyo3::prelude::*; use pyo3::wrap_pyfunction; #[pyfunction] -fn sum_as_string(a: usize, b: usize) -> PyResult { - Ok((a + b).to_string()) +fn add_host_object(obj: PyObject) -> PyResult<()> { + Python::with_gil(|py| -> PyResult<()> { + let obj: &PyAny = obj.extract(py)?; + let inspect = PyModule::import(py, "inspect")?; + let source = inspect.call1("getsource", (obj.get_type(), ))?; + println!("source:\n{}", source); + Ok(()) + })?; + Ok(()) } #[pymodule] -fn nac3embedded(py: Python, m: &PyModule) -> PyResult<()> { - m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; +fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(add_host_object, m)?)?; Ok(()) }