nac3embedded: basic source extraction

This commit is contained in:
Sebastien Bourdeauducq 2020-12-18 23:44:45 +08:00
parent 703059adab
commit b79d8ad640
4 changed files with 40 additions and 4 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__
/target

11
nac3embedded/demo.py Normal file
View File

@ -0,0 +1,11 @@
from language import *
class Demo:
@kernel
def run(self):
pass
if __name__ == "__main__":
Demo().run()

17
nac3embedded/language.py Normal file
View File

@ -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

View File

@ -2,13 +2,20 @@ use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
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(())
}