forked from bytecodealliance/wit-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmoonbit.rs
More file actions
107 lines (96 loc) · 3.25 KB
/
Copy pathmoonbit.rs
File metadata and controls
107 lines (96 loc) · 3.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::LanguageMethods;
use anyhow::bail;
use serde::Deserialize;
use std::process::Command;
/// MoonBit configuration of project files
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct LangConfig {
#[serde(default)]
path: String,
}
pub struct MoonBit;
impl LanguageMethods for MoonBit {
fn display(&self) -> &str {
"moonbit"
}
fn comment_prefix_for_test_config(&self) -> Option<&str> {
Some("//@")
}
fn default_bindgen_args(&self) -> &[&str] {
&["--derive-show", "--derive-eq", "--derive-error"]
}
fn prepare(&self, runner: &mut crate::Runner<'_>) -> anyhow::Result<()> {
println!("Testing if MoonBit toolchain exists...");
if runner
.run_command(Command::new("moon").arg("version"))
.is_err()
{
bail!("MoonBit toolchain not found. Check out <https://www.moonbitlang.com/download>");
}
Ok(())
}
fn compile(&self, runner: &crate::Runner<'_>, compile: &crate::Compile) -> anyhow::Result<()> {
let config = compile.component.deserialize_lang_config::<LangConfig>()?;
// Copy the file to the bindings directory
if !config.path.is_empty() {
let src_path = &compile.component.path;
let dest_path = compile.bindings_dir.join(config.path);
std::fs::copy(src_path, dest_path)?;
}
// Compile the MoonBit bindings to a wasm file
let mut cmd = Command::new("moon");
cmd.arg("build")
.arg("--no-strip") // for debugging
.arg("-C")
.arg(compile.bindings_dir);
runner.run_command(&mut cmd)?;
// Build the component
let artifact = compile
.bindings_dir
.join("target/wasm/release/build/gen/gen.wasm");
// Embed WIT files
let manifest_dir = compile.component.path.parent().unwrap();
let mut cmd = Command::new("wasm-tools");
cmd.arg("component")
.arg("embed")
.args(["--encoding", "utf16"])
.args(["-o", artifact.to_str().unwrap()])
.args(["-w", &compile.component.bindgen.world])
.arg(manifest_dir)
.arg(&artifact);
runner.run_command(&mut cmd)?;
// Componentize the Wasm
let mut cmd = Command::new("wasm-tools");
cmd.arg("component")
.arg("new")
.args(["-o", compile.output.to_str().unwrap()])
.arg(&artifact);
runner.run_command(&mut cmd)?;
Ok(())
}
fn should_fail_verify(
&self,
_name: &str,
config: &crate::config::WitConfig,
_args: &[String],
) -> bool {
config.async_
}
fn verify(&self, runner: &crate::Runner<'_>, verify: &crate::Verify) -> anyhow::Result<()> {
let mut cmd = Command::new("moon");
cmd.arg("check")
.arg("--warn-list")
.arg("-28")
.arg("--deny-warn")
.arg("--source-dir")
.arg(verify.bindings_dir);
runner.run_command(&mut cmd)?;
let mut cmd = Command::new("moon");
cmd.arg("build")
.arg("--source-dir")
.arg(verify.bindings_dir);
runner.run_command(&mut cmd)?;
Ok(())
}
}