def test_benchmark_headless_time_nbody_singlepass_native(benchmark): store = Store(engine.Native(Singlepass)) module = Module(store, TEST_BYTES) serialized = module.serialize() @benchmark def bench(): deserialized = Module.deserialize(store, serialized) _ = Instance(deserialized)
def test_benchmark_execution_time_nbody_singlepass_native(benchmark): store = Store(engine.Native(Singlepass)) module = Module(store, TEST_BYTES) instance = Instance(module) main = instance.exports.main @benchmark def bench(): _ = main(N)
def test_cross_compilation_roundtrip(): triple = target.Triple('x86_64-linux-musl') cpu_features = target.CpuFeatures() cpu_features.add('sse2') target_ = target.Target(triple, cpu_features) engine_ = engine.Native(Compiler, target_) store = Store(engine_) module = Module( store, """ (module (type $sum_t (func (param i32 i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) local.get $x local.get $y i32.add) (export "sum" (func $sum_f))) """) assert isinstance(module, Module)
""" (module (type $sum_t (func (param i32 i32) (result i32))) (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) local.get $x local.get $y i32.add) (export "sum" (func $sum_f))) """ ) # Define the engine that will drive everything. # # In this case, the engine is `wasmer.engine.Native` which means that # a native object is going to be generated. engine = engine.Native(Compiler) # Create a store, that holds the engine. store = Store(engine) # Here we go. # # Let's compile the Wasm module. It is at this step that the Wasm text # is transformed into Wasm bytes (if necessary), and then compiled to # executable code by the compiler, which is then stored in memory by # the engine. module = Module(store, wasm_bytes) # Congrats, the Wasm module is compiled! Now let's execute it for the # sake of having a complete example. #
def test_store_deprecated_native(): store = Store(engine.Native()) assert store.engine_name == 'dylib' assert store.compiler_name == None
def test_benchmark_compilation_time_nbody_singlepass_native(benchmark): store = Store(engine.Native(Singlepass)) @benchmark def bench(): _ = Module(store, TEST_BYTES)
def test_benchmark_compilation_time_nbody_llvm_native(benchmark): store = Store(engine.Native(LLVM)) @benchmark def bench(): _ = Module(store, TEST_BYTES)
def test_benchmark_compilation_time_nbody_cranelift_native(benchmark): store = Store(engine.Native(Cranelift)) @benchmark def bench(): _ = Module(store, TEST_BYTES)
cpu_features = target.CpuFeatures() cpu_features.add('sse2') # Here we go finally. # # Let's build the target. target = target.Target(triple, cpu_features) # Define the engine that will drive everything. # # In this case, the engine is `wasmer.engine.Native` which means that # a native object is going to be generated. # # That's where we specify the target for the compiler. # Use the native engine. engine = engine.Native(Compiler, target) # Create a store, that holds the engine. store = Store(engine) # Let's compile the Wasm module. module = Module(store, wasm_bytes) assert isinstance(module, Module) # Congrats, the Wasm module is cross-compiled! # # What to do with that? It is possible to use an engine (probably a # headless engine) to execute the cross-compiled Wasm module an the # targeted platform.