forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_ios_headless.rs
75 lines (67 loc) · 2.48 KB
/
platform_ios_headless.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Defining an engine in Wasmer is one of the fundamental steps.
//!
//! This example builds on that of 'engine_headless.rs' but instead of
//! serializing a module and then deserializing it again for your host machines target,
//! We instead create an engine for our target architecture (In this case an ARM64 iOS device),
//! serialize a simple module to a .dylib file that can be copied to an iOS project and
//! deserialized/ran using the 'Headless C-API'.
//!
//! ```shell
//! cargo run --example platform-headless-ios --release --features "cranelift"
//! ```
//!
//! Ready?
#![allow(unused)]
use std::path::Path;
use std::str::FromStr;
use wasmer::{wat2wasm, Module, RuntimeError, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_types::{CpuFeature, Target, Triple};
/*
use wasmer_engine_dylib::Dylib;
*/
fn main() -> Result<(), Box<dyn std::error::Error>> {
/*
// Let's declare the Wasm module with the text representation.
let wasm_bytes = wat2wasm(
r#"
(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)))
"#
.as_bytes(),
)?;
// Create a compiler for iOS
let compiler_config = Cranelift::default();
// Change it to `x86_64-apple-ios` if you want to target the iOS simulator
let triple = Triple::from_str("aarch64-apple-ios")
.map_err(|error| RuntimeError::new(error.to_string()))?;
// Let's build the target.
let mut cpu_feature = CpuFeature::set();
cpu_feature.insert(CpuFeature::from_str("sse2")?);
let target = Target::new(triple, cpu_feature);
println!("Chosen target: {:?}", target);
println!("Creating Dylib engine...");
let engine = Dylib::new(compiler_config).target(target);
// Create a store, that holds the engine.
let mut store = Store::new(engine);
println!("Compiling module...");
// Let's compile the Wasm module.
let module = Module::new(&store, wasm_bytes)?;
// Here we go. Let's serialize the compiled Wasm module in a
// file.
println!("Serializing module...");
let dylib_file = Path::new("./sum.dylib");
module.serialize_to_file(dylib_file)?;
*/
Ok(())
}
#[test]
#[cfg(target_os = "macos")]
fn test_engine_headless_ios() -> Result<(), Box<dyn std::error::Error>> {
main()
}