diff --git a/Makefile b/Makefile index 96d187a..906b971 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,8 @@ -run-controller: - venv/bin/mypy --strict phasmplatform - venv/bin/pyflakes phasmplatform - venv/bin/pycodestyle phasmplatform +run-controller: sast venv/bin/python -m phasmplatform.controller -init: - docker compose up -d +run-worker: sast + venv/bin/python -m phasmplatform.worker redis-sh: docker compose exec -it redis redis-cli @@ -15,5 +12,15 @@ setup: venv/bin/pip install --upgrade pip wheel setuptools venv/bin/pip install -r requirements.txt -r requirements-dev.txt +init: + docker compose up -d + update: venv/bin/pip install -r requirements.txt -r requirements-dev.txt + +sast: + venv/bin/mypy --strict phasmplatform + venv/bin/pyflakes phasmplatform + venv/bin/pycodestyle --max-line-length=140 phasmplatform + +.PHONY: init redis-sh run-controller run-worker setup sast update diff --git a/phasmplatform/common/exceptions.py b/phasmplatform/common/exceptions.py new file mode 100644 index 0000000..5da196a --- /dev/null +++ b/phasmplatform/common/exceptions.py @@ -0,0 +1,10 @@ +class PhasmPlatformError(Exception): + pass + + +class PhashPlatformRuntimeError(PhasmPlatformError): + pass + + +class PhashPlatformNonIntMainReturnError(PhashPlatformRuntimeError): + pass diff --git a/phasmplatform/worker/__init__.py b/phasmplatform/worker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/phasmplatform/worker/__main__.py b/phasmplatform/worker/__main__.py new file mode 100644 index 0000000..f036886 --- /dev/null +++ b/phasmplatform/worker/__main__.py @@ -0,0 +1,21 @@ +import sys + +from phasmplatform.common.config import from_toml + +from .runner import run_once + + +def main() -> int: + with open('config.toml', 'rb') as fil: + config = from_toml(fil) + + del config + + with open('/home/johan/projects/idea/phasm/examples/crc32.wasm', 'rb') as fil: + run_once(fil.read()) + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/phasmplatform/worker/runner.py b/phasmplatform/worker/runner.py new file mode 100644 index 0000000..7e0c921 --- /dev/null +++ b/phasmplatform/worker/runner.py @@ -0,0 +1,17 @@ +import wasm3 # type: ignore + +from phasmplatform.common.exceptions import PhashPlatformNonIntMainReturnError + + +def run_once(wasm_bin: bytes) -> int: + env = wasm3.Environment() + rtime = env.new_runtime(1024 * 1024) + rtime.load(env.parse_module(wasm_bin)) + result = rtime.find_function('main')() + if result is None: + return 0 + + if not isinstance(result, int): + raise PhashPlatformNonIntMainReturnError(result) + + return result diff --git a/requirements.txt b/requirements.txt index 7f60c54..f572e23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +pywasm3==0.5.0 redis==4.5.4 tomli==2.0.1 ; python_version < '3.11'