forked from bytecodealliance/ComponentizeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
83 lines (75 loc) · 2.17 KB
/
lib.rs
File metadata and controls
83 lines (75 loc) · 2.17 KB
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
76
77
78
79
80
81
82
83
use std::path::Path;
use anyhow::{Context, Result, bail};
use wit_parser::{PackageId, Resolve};
pub mod bindgen;
pub mod splice;
pub mod stub_wasi;
pub mod wit;
use wit::exports::local::spidermonkey_embedding_splicer::splicer::{CoreFn, CoreTy};
/// Calls [`write!`] with the passed arguments and unwraps the result.
///
/// Useful for writing to things with infallible `Write` implementations like
/// `Source` and `String`.
///
/// [`write!`]: std::write
#[macro_export]
macro_rules! uwrite {
($dst:expr, $($arg:tt)*) => {
write!($dst, $($arg)*).unwrap()
};
}
/// Calls [`writeln!`] with the passed arguments and unwraps the result.
///
/// Useful for writing to things with infallible `Write` implementations like
/// `Source` and `String`.
///
/// [`writeln!`]: std::writeln
#[macro_export]
macro_rules! uwriteln {
($dst:expr, $($arg:tt)*) => {
writeln!($dst, $($arg)*).unwrap()
};
}
fn map_core_ty(cty: &bindgen::CoreTy) -> CoreTy {
match cty {
bindgen::CoreTy::I32 => CoreTy::I32,
bindgen::CoreTy::I64 => CoreTy::I64,
bindgen::CoreTy::F32 => CoreTy::F32,
bindgen::CoreTy::F64 => CoreTy::F64,
}
}
fn map_core_fn(cfn: &bindgen::CoreFn) -> CoreFn {
let bindgen::CoreFn {
params,
ret,
retptr,
retsize,
paramptr,
} = cfn;
CoreFn {
params: params.iter().map(&map_core_ty).collect(),
ret: ret.as_ref().map(map_core_ty),
retptr: *retptr,
retsize: *retsize,
paramptr: *paramptr,
}
}
fn parse_wit(path: impl AsRef<Path>) -> Result<(Resolve, PackageId)> {
let mut resolve = Resolve::default();
let path = path.as_ref();
let id = if path.is_dir() {
resolve
.push_dir(path)
.with_context(|| format!("resolving WIT in {}", path.display()))?
.0
} else {
let contents =
std::fs::read(path).with_context(|| format!("reading file {}", path.display()))?;
let text = match std::str::from_utf8(&contents) {
Ok(s) => s,
Err(_) => bail!("input file is not valid utf-8"),
};
resolve.push_str(path, text)?
};
Ok((resolve, id))
}