├── images ├── Github.png └── logo.svg ├── examples ├── c │ ├── test_c.wasm │ ├── Readme.md │ ├── test_c.c │ └── test_c.py ├── c++ │ ├── test_cpp.wasm │ ├── Readme.md │ ├── test_cpp.py │ └── test_cpp.cpp ├── wasm │ ├── test_wasm.wasm │ ├── readme.md │ └── test_wasm.py └── wat │ ├── readme.md │ ├── test_wat.wat │ └── test_wat.py ├── requirements.txt ├── wasmite ├── wat.py ├── wasm.py ├── __init__.py ├── wasi.py └── globals.py ├── MANIFEST.in ├── setup.py ├── Readme.md └── LICENSE /images/Github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yusuf8ahmed/Wasmite/HEAD/images/Github.png -------------------------------------------------------------------------------- /examples/c/test_c.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yusuf8ahmed/Wasmite/HEAD/examples/c/test_c.wasm -------------------------------------------------------------------------------- /examples/c++/test_cpp.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yusuf8ahmed/Wasmite/HEAD/examples/c++/test_cpp.wasm -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiocontextvars==0.2.2 2 | contextvars==2.4 3 | immutables==0.14 4 | loguru==0.5.3 5 | wasmer==1.0.0 6 | wasmer-compiler-cranelift==1.0.0 -------------------------------------------------------------------------------- /examples/wasm/test_wasm.wasm: -------------------------------------------------------------------------------- 1 | asm```mathsummathseven3 read_global write_globaladdsubaddsum 2 | &#  $  j  k   Unamesumaddsubaddsum4lhslhsrhslhsrhslhsrhs -------------------------------------------------------------------------------- /examples/wat/readme.md: -------------------------------------------------------------------------------- 1 | ## WAT example 2 | 3 | * [test_wat.py](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/wat/test_wat.py): Wasmite python unit testing code 4 | * [test_wat.wat](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/wat/test_wat.wat): wat code -------------------------------------------------------------------------------- /examples/wasm/readme.md: -------------------------------------------------------------------------------- 1 | ## Compiled WASM example 2 | 3 | * [test_wasm.py](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/wasm/test_wasm.py): Wasmite python unit testing code 4 | * [test_wasm.wasm](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/wasm/test_wasm.wasm): complied wasm code -------------------------------------------------------------------------------- /examples/c/Readme.md: -------------------------------------------------------------------------------- 1 | ## C language example 2 | 3 | * [test_c.py](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c/test_c.py): Wasmite python unit testing code 4 | * [test_c.c](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c/test_c.c): c code to be complied 5 | * [test_c.wasm](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c/test_c.wasm): complied wasm code 6 | -------------------------------------------------------------------------------- /wasmite/wat.py: -------------------------------------------------------------------------------- 1 | from wasmer import Store, wat2wasm, Module 2 | 3 | from .globals import BaseModule 4 | 5 | 6 | class WatModule(BaseModule): 7 | TYPE = "WAT" 8 | 9 | def _import_module(self): 10 | """ import a WAT module """ 11 | self.store = Store() 12 | self.wasm_bytes = wat2wasm(open(self.path, "r").read()) 13 | self.module = Module(self.store, self.wasm_bytes) 14 | -------------------------------------------------------------------------------- /examples/c++/Readme.md: -------------------------------------------------------------------------------- 1 | ## C++ language example 2 | 3 | * [test_cpp.py](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c%2B%2B/test_cpp.py): Wasmite python unit testing code 4 | * [test_cpp.cpp](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c%2B%2B/test_cpp.cpp): c++ code to be complied 5 | * [test_cpp.wasm](https://github.com/yusuf8ahmed/Wasmite/blob/master/testing/c%2B%2B/test_cpp.wasm): complied wasm code -------------------------------------------------------------------------------- /wasmite/wasm.py: -------------------------------------------------------------------------------- 1 | from wasmer import engine, Store, Module 2 | from wasmer_compiler_cranelift import Compiler 3 | 4 | from .globals import BaseModule 5 | 6 | 7 | class WasmModule(BaseModule): 8 | TYPE = "WASM" 9 | 10 | def _import_module(self): 11 | """ import a WASM module """ 12 | self.store = Store(engine.JIT(Compiler)) 13 | self.wasm_bytes = open(self.path, "rb").read() 14 | self.module = Module(self.store, self.wasm_bytes) 15 | -------------------------------------------------------------------------------- /wasmite/__init__.py: -------------------------------------------------------------------------------- 1 | from .globals import WasmiteCase 2 | from .globals import FunctionTypes 3 | from .globals import main 4 | 5 | from .globals import I32 6 | from .globals import I64 7 | from .globals import F32 8 | from .globals import F64 9 | from .globals import V128 10 | from .globals import EXTERN_REF 11 | from .globals import FUNC_REF 12 | 13 | from .wasm import WasmModule 14 | from .wasi import WasiModule 15 | from .wat import WatModule 16 | 17 | from wasmer import Function 18 | from wasmer import Global 19 | from wasmer import Value 20 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | // Copyright 2021 abdulwahid 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | include LICENSE 16 | include README.md 17 | -------------------------------------------------------------------------------- /examples/c++/test_cpp.py: -------------------------------------------------------------------------------- 1 | from wasmite import WasiModule, WasmiteCase 2 | from wasmite import main 3 | 4 | class Test(WasmiteCase): 5 | module = WasiModule("test_cpp.wasm") 6 | exports = module.get_exports() 7 | 8 | def test_main(self): 9 | # test and check main function 10 | self.module.run_main() 11 | 12 | def test_add(self): 13 | # test the "addone" function 14 | result = self.exports.addone(1) 15 | self.assertEqual(result, 2) 16 | 17 | def test_sub(self): 18 | # test the "cubed" function 19 | result = self.exports.cubed(1) 20 | self.assertEqual(result, 1) 21 | 22 | # Hi don't forget to add me 23 | if __name__ == "__main__": 24 | main() -------------------------------------------------------------------------------- /examples/c++/test_cpp.cpp: -------------------------------------------------------------------------------- 1 | // test_cpp.wasm 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // em++ test_cpp.cpp -o test_cpp.wasm -s EXPORTED_FUNCTIONS='["_addone", "_cubed"]' 10 | // functions addone and cubed are exported ------------------^^^^^^^^^^^^^^^^^^^^^ 11 | 12 | extern "C" { 13 | 14 | int addone(int value){ 15 | return value + 1; 16 | } 17 | 18 | int cubed(int value){ 19 | return value * value * value; 20 | } 21 | 22 | int main() { 23 | printf("\n"); 24 | 25 | int number = addone(3); 26 | printf("addone(3): %d\n", number); 27 | 28 | int number2 = cubed(3); 29 | printf("cubed(3) %d\n", number2); 30 | 31 | return 0; 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /examples/wat/test_wat.wat: -------------------------------------------------------------------------------- 1 | ;; test.wat 2 | (module 3 | (import "math" "sum" (func $sum (param i32 i32) (result i32))) 4 | (import "math" "seven" (global $seven (mut i32))) 5 | (func (export "read_global") (result i32) 6 | global.get $seven) 7 | (func (export "write_global") (param $lhs i32) 8 | get_local $lhs 9 | global.set $seven) 10 | (type $t0 (func (param i32 i32) (result i32))) 11 | (func $add (export "add") (type $t0) (param $lhs i32) (param $rhs i32) (result i32) 12 | get_local $lhs 13 | get_local $rhs 14 | i32.add) 15 | (func $sub (export "sub") (type $t0) (param $lhs i32) (param $rhs i32) (result i32) 16 | get_local $lhs 17 | get_local $rhs 18 | i32.sub) 19 | (func $addsum (export "addsum") (param $lhs i32) (param $rhs i32) (result i32) 20 | get_local $lhs 21 | get_local $rhs 22 | call $sum) 23 | ) 24 | -------------------------------------------------------------------------------- /wasmite/wasi.py: -------------------------------------------------------------------------------- 1 | from wasmer import engine, wasi, Store, Module 2 | from wasmer_compiler_cranelift import Compiler 3 | 4 | from .globals import BaseModule 5 | 6 | 7 | class WasiModule(BaseModule): 8 | TYPE = "WASI" 9 | 10 | def _import_module(self): 11 | """ import a module: implemented in subclasses """ 12 | self._IMPORT_FLAG = True 13 | self.store = Store(engine.JIT(Compiler)) 14 | self.wasm_bytes = open(self.path, "rb").read() 15 | self.module = Module(self.store, self.wasm_bytes) 16 | 17 | wasi_version = wasi.get_version(self.module, strict=True) 18 | wasi_env = wasi.StateBuilder("test-program").finalize() 19 | self.import_object = wasi_env.generate_import_object(self.store, wasi_version) 20 | 21 | def run_main(self): 22 | """ could raise runtime Error """ 23 | self.exports._start() 24 | -------------------------------------------------------------------------------- /examples/c/test_c.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // emcc test_c.c -o test_c.wasm -s EXPORTED_FUNCTIONS='["_even", "_squared", "_inverse"]' 4 | // functions even and squared are exported ------------^^^^^^^^^^^^^^^^^^^^^ 5 | 6 | int even(int value){ 7 | if (value % 2 == 0) { 8 | return 1; 9 | } else { 10 | return 0; 11 | } 12 | } 13 | 14 | int squared(int value){ 15 | return value * value; 16 | } 17 | 18 | float inverse(float number){ 19 | long i; 20 | float x2, y; 21 | const float threehalfs = 1.5F; 22 | 23 | x2 = number * 0.5F; 24 | y = number; 25 | i = * ( long * ) &y; 26 | i = 0x5f3759df - ( i >> 1 ); 27 | y = * ( float * ) &i; 28 | y = y * ( threehalfs - ( x2 * y * y ) ); 29 | 30 | return y; 31 | } 32 | 33 | int main() { 34 | printf("\n"); 35 | 36 | float number = inverse(3.5); 37 | printf("inverse(3.5) %f\n", number); 38 | 39 | int number1 = even(3); 40 | printf("even(3): %d\n", number1); 41 | 42 | int number2 = squared(3); 43 | printf("squared(3) %d\n", number2); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /examples/wat/test_wat.py: -------------------------------------------------------------------------------- 1 | from wasmite import WasmiteCase, WatModule 2 | from wasmite import Function, Global, Value, main 3 | 4 | def sum(x: int, y: int) -> int: 5 | return x + y 6 | 7 | class Test(WasmiteCase): 8 | module = WatModule("test_wat.wat") 9 | module.register("math", { 10 | "sum": Function(module.store, sum), 11 | "seven": Global(module.store, Value.i32(7), mutable=True) 12 | }) 13 | exports = module.get_exports() 14 | 15 | def test_add(self): 16 | # test add function 17 | add_function = self.exports.add(1,2) 18 | self.assertEqual(add_function, 3) 19 | 20 | def test_sub(self): 21 | # test sub function 22 | sub_function = self.exports.sub(1,2) 23 | self.assertEqual(sub_function, -1) 24 | 25 | def test_global_read(self): 26 | # test reading value of global 27 | read_seven = self.exports.read_global() 28 | self.assertEqual(read_seven, 7) 29 | 30 | def test_global_write(self): 31 | # text writing value of global 32 | self.exports.write_global(5) 33 | read_global = self.exports.read_global() 34 | self.assertEqual(read_global, 5) 35 | 36 | if __name__ == "__main__": 37 | main() -------------------------------------------------------------------------------- /examples/c/test_c.py: -------------------------------------------------------------------------------- 1 | from wasmite import WasiModule, WasmiteCase 2 | from wasmite import main 3 | from wasmite import FunctionTypes, F32 4 | 5 | 6 | class Test(WasmiteCase): 7 | module = WasiModule("test_c.wasm") 8 | exports = module.get_exports() 9 | 10 | def test_even(self): 11 | # test the "even" function 12 | result = self.exports.even(1) 13 | self.assertEqual(result, 0) 14 | 15 | def test_squared(self): 16 | # test the "squared" function 17 | result = self.exports.squared(1) # 2**3 18 | self.assertEqual(result, 1) 19 | 20 | def test_quake_inverse(self): 21 | # test the "inverse" function 22 | result = self.exports.inverse(float(1)) 23 | self.assertLess(result, 1) 24 | 25 | def test_quake_inverse_types(self): 26 | # test the "inverse" function types 27 | # param is I64 and result is I64 28 | add_function = self.exports.inverse 29 | self.assertTypes(add_function, FunctionTypes([F32], [F32])) # 30 | 31 | def test_main(self): 32 | # test and check main function 33 | self.module.run_main() 34 | 35 | # Hi don't forget to add me 36 | if __name__ == "__main__": 37 | main() -------------------------------------------------------------------------------- /images/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | VERSION = "0.2.1" 4 | 5 | # source env/bin/activate 6 | # python setup.py develop 7 | 8 | # rm -rf build dist wasmite.egg-info 9 | # python setup.py sdist bdist_wheel 10 | # python -m twine upload --skip-existing dist/* 11 | # python -m twine upload dist/* 12 | 13 | # python setup.py install 14 | 15 | setup( 16 | name="wasmite", 17 | version=VERSION, 18 | author="Yusuf Ahmed", 19 | author_email="yusufahmed172@gmail.com", 20 | packages=find_packages(), 21 | description="Wasmite: Webassembly is the future but now it has a testing toolchain", 22 | long_description=open('Readme.md').read(), 23 | long_description_content_type="text/markdown", 24 | install_requires=['loguru', "wasmer>=1.0.0-alpha3", "wasmer-compiler-cranelift>=1.0.0-alpha3"], 25 | url="https://github.com/yusuf8ahmed/Wasmite", 26 | classifiers=[ 27 | "Programming Language :: Python :: 3", 28 | "Development Status :: 2 - Pre-Alpha", 29 | "Operating System :: OS Independent", 30 | "Intended Audience :: Developers", 31 | "Topic :: Internet", 32 | "Topic :: Internet :: WWW/HTTP :: Browsers", 33 | "Topic :: Internet :: WWW/HTTP :: HTTP Servers", 34 | "Topic :: Internet :: WWW/HTTP :: WSGI", 35 | "Topic :: Internet :: WWW/HTTP :: WSGI :: Application", 36 | "Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware", 37 | "Topic :: Internet :: WWW/HTTP :: WSGI :: Server", 38 | "Topic :: Software Development :: Assemblers", 39 | "Topic :: Software Development :: Testing", 40 | "Topic :: Software Development :: Quality Assurance", 41 | "Topic :: System :: Monitoring", 42 | ], 43 | platforms = 'any', 44 | keywords = ["wasmer", "wasmite", "wasp", "wasm", "debug", "debugging", "unit", 45 | "unit testing", "unit tests", "unit testing wasm", "unit tests wasm", 46 | "debug wasm", "debugging wasm"], 47 | python_requires='!=3.9, !=2.*', 48 | zip_safe = False 49 | ) -------------------------------------------------------------------------------- /examples/wasm/test_wasm.py: -------------------------------------------------------------------------------- 1 | from wasmite import WasmiteCase, WasmModule 2 | from wasmite import FunctionTypes, Function, Global, Value, main 3 | from wasmite import I32, I64 4 | 5 | def sum(x: int, y: int) -> int: 6 | """ python function to be imported into WASM """ 7 | return x + y 8 | 9 | class Test(WasmiteCase): 10 | # create a variable the hold all the functions from a specific wasm file. 11 | module = WasmModule("test_wasm.wasm") 12 | # import python function into WASM 13 | # type annotations on the function is necessary 14 | module.register("math", { 15 | "sum": Function(module.store, sum), 16 | "seven": Global(module.store, Value.i32(7), mutable=True) 17 | }) 18 | # start up the module and return the exports (this is mandatory) 19 | exports = module.get_exports() 20 | 21 | def test_add(self): 22 | # test add function 23 | result = self.exports.add(1,2) 24 | self.assertEqual(result, 3) 25 | 26 | def test_sub(self): 27 | # test the sub function 28 | result = self.exports.sub(2,2) 29 | self.assertEqual(result, 0) 30 | 31 | def test_args_add(self): 32 | # check the types for results and parameter of the function "add" 33 | # param is I32, I32 and result is I32 34 | add_function = self.exports.add 35 | self.assertTypes(add_function, FunctionTypes([I32, I32], [I32])) # 36 | 37 | def test_import_sum(self): 38 | # test the imported python function sum. 39 | sum_function = self.exports.addsum(5,2) 40 | self.assertEqual(sum_function, 7) 41 | 42 | def test_global_read(self): 43 | # test reading value of global 44 | read_seven = self.exports.read_global() 45 | self.assertEqual(read_seven, 7) 46 | 47 | def test_global_write(self): 48 | # test writing value of global 49 | self.exports.write_global(5) 50 | read_seven = self.exports.read_global() 51 | self.assertEqual(read_seven, 5) 52 | 53 | # Hi don't forget to add me 54 | if __name__ == "__main__": 55 | main() -------------------------------------------------------------------------------- /wasmite/globals.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from typing import NamedTuple, List, Type 3 | 4 | from wasmer import Instance, ImportObject 5 | from wasmer import Type 6 | from loguru import logger 7 | 8 | I32 = Type.I32 9 | I64 = Type.I64 10 | F32 = Type.F32 11 | F64 = Type.F64 12 | V128 = Type.V128 13 | EXTERN_REF = Type.EXTERN_REF 14 | FUNC_REF = Type.FUNC_REF 15 | 16 | get_types = lambda x: list(map(lambda x: Type(x).name, x)) 17 | 18 | 19 | class FunctionTypes(NamedTuple): 20 | """ FuncType: holds the param types and results type. """ 21 | 22 | params: List[Type] 23 | result: List[Type] 24 | 25 | 26 | class BaseModule: 27 | IMPORT_FLAG = False 28 | TYPE = None 29 | 30 | def __init__(self, path): 31 | self.path = path 32 | self.logger = logger 33 | self._import_module() 34 | 35 | def _import_module(self): 36 | """ import a module: implemented in subclasses """ 37 | raise NotImplementedError() 38 | 39 | def run_main(self): 40 | """ run main function: only implemented in wasi subclass """ 41 | raise NotImplementedError() 42 | 43 | def register(self, name, namespace): 44 | """ register and import python functions into the module """ 45 | if not self.TYPE == "WASI": 46 | self.import_object = ImportObject() 47 | self.import_object.register(name, namespace) 48 | self._IMPORT_FLAG = True 49 | 50 | def get_exports(self): 51 | """ start module and export functions """ 52 | if self._IMPORT_FLAG: 53 | instance = Instance(self.module, self.import_object) 54 | else: 55 | instance = Instance(self.module) 56 | self.exports = instance.exports 57 | return instance.exports 58 | 59 | 60 | class CheckFunction: 61 | def _baseAssertFuncType(self, first, second, msg=None): 62 | """ The default assertEqual implementation, not type specific. """ 63 | if not first == second: 64 | msg = self._formatMessage(msg, "Types Differ") 65 | raise self.failureException(msg) 66 | 67 | def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None): 68 | """ The implementation of assertEqual. """ 69 | if not (seq1 == seq2): 70 | msg = self._formatMessage(msg, "Types Differ") 71 | raise self.failureException(msg) 72 | 73 | def _getAssertFuncType(self, first, second): 74 | if type(first) is type(second): 75 | asserter = self._type_equality_funcs.get(type(first)) 76 | if asserter is not None: 77 | if isinstance(asserter, str): 78 | asserter = getattr(self, asserter) 79 | return asserter 80 | return self._baseAssertFuncType 81 | 82 | def _assertFuncType(self, first, second, msg): 83 | first, second = get_types(first), get_types(second) 84 | assertion_func = self._getAssertFuncType(first, second) 85 | msg = """\n\nTypes Differ (on {}):\nWebAssembly Function: {}\nUnittest FunctionTypes: {}""".format( 86 | msg, first, second 87 | ) 88 | assertion_func(first, second, msg=msg) 89 | 90 | 91 | class WasmiteCase(unittest.TestCase, CheckFunction): 92 | """Wasmite Extension of unittest.TestCase""" 93 | 94 | def assertTypes(self, func, func_type): 95 | """ Check the static types of the param(s) and return of WebAssembly Function """ 96 | self._assertFuncType(func.type.params, func_type.params, "params") 97 | self._assertFuncType(func.type.results, func_type.result, "return") 98 | 99 | 100 | def main(): 101 | unittest.main(verbosity=2) 102 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | ### What is the Wasmite project 6 | Since WebAssembly is the future of the web. I decide to create Wasmite, a python package for unit-testing your wasm or wat code. Wasmite is based on **[wasmer](https://wasmerio.github.io/wasmer-python/api/wasmer/)** and the python standard library package **[unittest](https://docs.python.org/3/library/unittest.html)**. Documentation for can be found here: [documentation for unittest](https://docs.python.org/3/library/unittest.html) and [documentation for wasmer](https://wasmerio.github.io/wasmer-python/api/wasmer/) 7 | 8 | **This project was formerly an extension of my Rust/Python Web framework Wasp, so some section of the code may refer to it's earlier name (Native)** 9 | 10 | Wasmite looks for tests in python files whose names start with test_\*.py and runs every test_\* function it discovers. The testing folder has more examples. 11 | 12 | **Having any problems or questions create a [issue](https://github.com/yusuf8ahmed/Wasmite/issues/new), i will be happy to help :)** 13 | 14 | ### Installation 15 | 16 | This project requires python 3 and doesn't support 3.9 17 | ```bash 18 | pip install wasmite 19 | ``` 20 | 21 | ### Project Goals: 22 | 23 | - [x] Import wasm or wat module successfully 24 | - [x] Access functions within module 25 | - [x] Type checking of parameters and the result of functions 26 | - [x] Release to **PyPi** for public to use 27 | - [x] Allow Wasmite ... 28 | - [x] Export Python functions 29 | - [x] Export Global Instances 30 | - [x] Export Memory Instances 31 | - [x] More complex examples in testing folder 32 | - [ ] Receive community on how to improve 33 | 34 | Examples: 35 | 36 | * [c++](https://github.com/yusuf8ahmed/Wasmite/tree/master/examples/c%2B%2B) 37 | * [c](https://github.com/yusuf8ahmed/Wasmite/tree/master/examples/c) 38 | * [wasm](https://github.com/yusuf8ahmed/Wasmite/tree/examples/testing/wasm) 39 | * [wat](https://github.com/yusuf8ahmed/Wasmite/tree/examples/testing/wat) 40 | 41 | 42 | ```python 43 | from wasmite import WasmiteCase, WasmModule 44 | from wasmite import FunctionTypes, Function, Global, Value, main 45 | from wasmite import I32 46 | 47 | def sum(x: int, y: int) -> int: 48 | """ python function to be imported into WASM """ 49 | return x + y 50 | 51 | class Test(WasmiteCase): 52 | # create a variable the hold all the functions from a specific wasm file. 53 | module = WasmModule("test_wasm.wasm") 54 | # import python function into WASM 55 | # type annotations on the function is necessary 56 | module.register("math", { 57 | "sum": Function(module.store, sum), 58 | "seven": Global(module.store, Value.i32(7), mutable=True) 59 | }) 60 | # start up the module and return the exports (this is mandatory) 61 | exports = module.get_exports() 62 | 63 | def test_add(self): 64 | # test add function 65 | result = self.exports.add(1,2) 66 | self.assertEqual(result, 3) 67 | 68 | def test_sub(self): 69 | # test the sub function 70 | result = self.exports.sub(2,2) 71 | self.assertEqual(result, 0) 72 | 73 | def test_args_add(self): 74 | # check the types for results and parameter of the function "add" 75 | # param is I32, I32 and result is I32 76 | add_function = self.exports.add 77 | self.assertTypes(add_function, FunctionTypes([I32, I32], [I32])) # result will fail 78 | 79 | def test_import_sum(self): 80 | # test the imported python function sum. 81 | sum_function = self.exports.addsum(5,2) 82 | self.assertEqual(sum_function, 7) 83 | 84 | def test_global_read(self): 85 | # test reading value of global 86 | read_seven = self.exports.read_global() 87 | self.assertEqual(read_seven, 7) 88 | 89 | def test_global_write(self): 90 | # test writing value of global 91 | self.exports.write_global(5) 92 | read_seven = self.exports.read_global() 93 | self.assertEqual(read_seven, 5) 94 | 95 | # Hi don't forget to add me 96 | if __name__ == "__main__": 97 | main() 98 | ``` 99 | --> 100 | 101 | Then you can then run this test like so: 102 | ```bash 103 | # make sure you are in examples/wasm 104 | $ python test_wasm.py 105 | 106 | test_add (__main__.Test) ... ok 107 | test_args_add (__main__.Test) ... ok 108 | test_global_read (__main__.Test) ... ok 109 | test_global_write (__main__.Test) ... ok 110 | test_import_sum (__main__.Test) ... ok 111 | test_sub (__main__.Test) ... ok 112 | 113 | ---------------------------------------------------------------------- 114 | Ran 6 tests in 0.001s 115 | 116 | OK 117 | ``` 118 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------