Merge pull request #2791 from peterhuene/compile-command
Add a compile command to Wasmtime.
This commit is contained in:
@@ -1,57 +1,78 @@
|
||||
use anyhow::Result;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
fn checks_incompatible_target() -> Result<()> {
|
||||
let mut target = target_lexicon::Triple::host();
|
||||
target.operating_system = target_lexicon::OperatingSystem::Unknown;
|
||||
match Module::new(
|
||||
&Engine::new(Config::new().target(&target.to_string())?)?,
|
||||
"(module)",
|
||||
) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => assert!(e
|
||||
.to_string()
|
||||
.contains("configuration does not match the host")),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caches_across_engines() {
|
||||
let mut c = Config::new();
|
||||
c.cranelift_clear_cpu_flags();
|
||||
let c = Config::new();
|
||||
|
||||
let bytes = Module::new(&Engine::new(&c).unwrap(), "(module)")
|
||||
.unwrap()
|
||||
.serialize()
|
||||
.unwrap();
|
||||
|
||||
let res = Module::deserialize(
|
||||
&Engine::new(&Config::new().cranelift_clear_cpu_flags()).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
let res = Module::new(&Engine::new(&Config::new()).unwrap(), &bytes);
|
||||
assert!(res.is_ok());
|
||||
|
||||
// differ in shared cranelift flags
|
||||
let res = Module::deserialize(
|
||||
&Engine::new(
|
||||
&Config::new()
|
||||
.cranelift_clear_cpu_flags()
|
||||
.cranelift_nan_canonicalization(true),
|
||||
)
|
||||
.unwrap(),
|
||||
let res = Module::new(
|
||||
&Engine::new(Config::new().cranelift_nan_canonicalization(true)).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
assert!(res.is_err());
|
||||
|
||||
// differ in cranelift settings
|
||||
let res = Module::deserialize(
|
||||
&Engine::new(
|
||||
&Config::new()
|
||||
.cranelift_clear_cpu_flags()
|
||||
.cranelift_opt_level(OptLevel::None),
|
||||
)
|
||||
.unwrap(),
|
||||
let res = Module::new(
|
||||
&Engine::new(Config::new().cranelift_opt_level(OptLevel::None)).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
assert!(res.is_err());
|
||||
|
||||
// differ in cpu-specific flags
|
||||
// Missing required cpu flags
|
||||
if cfg!(target_arch = "x86_64") {
|
||||
let res = Module::deserialize(
|
||||
&Engine::new(unsafe {
|
||||
&Config::new()
|
||||
.cranelift_clear_cpu_flags()
|
||||
.cranelift_other_flag("has_sse3", "true")
|
||||
.unwrap()
|
||||
})
|
||||
let res = Module::new(
|
||||
&Engine::new(
|
||||
Config::new()
|
||||
.target(&target_lexicon::Triple::host().to_string())
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aot_compiles() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let bytes = engine.precompile_module(
|
||||
"(module (func (export \"f\") (param i32) (result i32) local.get 0))".as_bytes(),
|
||||
)?;
|
||||
|
||||
let module = Module::from_binary(&engine, &bytes)?;
|
||||
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
let f = instance.get_typed_func::<i32, i32>("f")?;
|
||||
assert_eq!(f.call(101).unwrap(), 101);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ fn compile() -> Result<()> {
|
||||
assert_eq!(m.imports().len(), 0);
|
||||
assert_eq!(m.exports().len(), 0);
|
||||
let bytes = m.serialize()?;
|
||||
Module::deserialize(&engine, &bytes)?;
|
||||
Module::new(&engine, &bytes)?;
|
||||
assert_eq!(m.imports().len(), 0);
|
||||
assert_eq!(m.exports().len(), 0);
|
||||
Ok(())
|
||||
|
||||
@@ -7,10 +7,27 @@ fn serialize(engine: &Engine, wat: &'static str) -> Result<Vec<u8>> {
|
||||
}
|
||||
|
||||
fn deserialize_and_instantiate(store: &Store, buffer: &[u8]) -> Result<Instance> {
|
||||
let module = Module::deserialize(store.engine(), buffer)?;
|
||||
let module = Module::new(store.engine(), buffer)?;
|
||||
Ok(Instance::new(&store, &module, &[])?)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_version_mismatch() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let mut buffer = serialize(&engine, "(module)")?;
|
||||
buffer[13 /* header length */ + 1 /* version length */] = 'x' as u8;
|
||||
|
||||
match Module::new(&engine, &buffer) {
|
||||
Ok(_) => bail!("expected deserialization to fail"),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
"Module was compiled with incompatible Wasmtime version 'x.25.0'"
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_module_serialize_simple() -> Result<()> {
|
||||
let buffer = serialize(
|
||||
|
||||
Reference in New Issue
Block a user