def test_python_to_wasm(): m = python_to_wasm(find_prime) imports = {'env': {'f64_print': f64_print}} # TODO: for some reason, target='native' ends up in an infinite loop? ob = wasm.instantiate(m, imports, target='python') result = ob.exports.find_prime(10) assert result == 29
def wasmify(func, target="native"): """Convert a Python function to a WASM function, compiled to native code. Assumes that all variables are floats. Can be used as a decorator, like Numba! """ from ppci.lang.python import python_to_wasm def f64_print(x: float) -> None: print(x) wa = python_to_wasm(func) imports = {"env": {"f64_print": f64_print}} mod = instantiate(wa, imports=imports, target=target) wasmfunc = getattr(mod.exports, func.__name__) return wasmfunc
def xx_test_compiling(): # Convert Python to wasm wasm_module = python_to_wasm(py3) # Convert wasm to ppci ppci_module = wasm.wasm_to_ir(wasm_module) # Optimizer fails, or makes it slower ;) # optimize(ppci_module, 2) # Compile to native object arch = get_current_arch() ob = ir_to_object([ppci_module], arch, debug=True) # Run in memory native_module = codepage.load_obj(ob) result = native_module.main() assert result == 2741
gotit = 0 break if gotit == 1: n = n + 1 # print(perf_counter() - t0) # print(i) return i """ ## Run in memory arch = get_current_arch() # Convert Python to wasm wasm_module = python_to_wasm(py3) print(wasm_module.to_string()) wasm_module.show_interface() # Convert wasm to ppci ppci_module = wasm_to_ir(wasm_module, arch.info.get_type_info('ptr')) # Optimizer fails, or makes it slower ;) # optimize(ppci_module, 2) this_dir = os.path.dirname(os.path.abspath(__file__)) # Generate a report: html_report = os.path.join(this_dir, 'compilation_report.html') with reporting.html_reporter(html_report) as reporter: # Write IR code f = StringIO()
import io from ppci.lang.python import python_to_wasm # draw_rectangle = None #src = """ def draw(): draw_rectangle(10, 10, 100, 100) draw_rectangle(110, 110, 80, 20) draw_rectangle(10, 110, 50, 50) #""" # f = io.StringIO(src) imports = ['(import "env" "draw_rectangle" (func $draw_rectangle (param f64 f64 f64 f64)))'] mod = python_to_wasm(draw, imports=imports) print(mod) with open('pzn/shader.wasm', 'wb') as f: mod.to_file(f)