Skip to content

Commit 117e4d9

Browse files
committed
Add Weak/Capability/Json encode-decode/ResourcePool intrinsics, separate special forms coverage, enable serde_json preserve_order
1 parent da2426f commit 117e4d9

15 files changed

Lines changed: 1174 additions & 111 deletions

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ path = "src/main.rs"
2323
[dependencies]
2424
reir = { path = "reir" }
2525
serde = { version = "1", features = ["derive"] }
26-
serde_json = "1"
26+
serde_json = { version = "1", features = ["preserve_order"] }
2727
sha2 = "0.10"
2828
regex = "1"
2929
toml = "0.8"

build.rs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeMap;
1+
use std::collections::{BTreeMap, BTreeSet};
22
use std::env;
33
use std::fs;
44
use std::path::{Path, PathBuf};
@@ -138,27 +138,57 @@ fn write_core_package_index() -> Result<(), String> {
138138

139139
fn write_reg_vm_runtime_intrinsics() -> Result<(), String> {
140140
println!("cargo:rerun-if-changed=src/reg_vm.rs");
141+
println!("cargo:rerun-if-changed=src/runtime_abi.rs");
141142

142143
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("manifest dir"));
143144
let source_path = manifest_dir.join("src/reg_vm.rs");
144145
let source = fs::read_to_string(&source_path)
145146
.map_err(|error| format!("failed to read {}: {error}", source_path.display()))?;
146-
let signatures = collect_reg_vm_runtime_intrinsics(&source)?;
147-
let generated = format!(
147+
let resolver_signatures = collect_reg_vm_resolver_signatures(&source)?;
148+
let runtime_abi_path = manifest_dir.join("src/runtime_abi.rs");
149+
let runtime_abi_source = fs::read_to_string(&runtime_abi_path)
150+
.map_err(|error| format!("failed to read {}: {error}", runtime_abi_path.display()))?;
151+
let runtime_abi_signatures = collect_runtime_abi_signatures(&runtime_abi_source);
152+
let signatures = resolver_signatures
153+
.iter()
154+
.filter(|signature| runtime_abi_signatures.contains(*signature))
155+
.cloned()
156+
.collect::<Vec<_>>();
157+
let special_forms = resolver_signatures
158+
.into_iter()
159+
.filter(|signature| !runtime_abi_signatures.contains(signature))
160+
.collect::<Vec<_>>();
161+
let generated_runtime = format!(
148162
"&[\n{}\n]\n",
149163
signatures
150164
.iter()
151165
.map(|signature| format!(" {signature:?},"))
152166
.collect::<Vec<_>>()
153167
.join("\n")
154168
);
169+
let generated_special_forms = format!(
170+
"&[\n{}\n]\n",
171+
special_forms
172+
.iter()
173+
.map(|signature| format!(" {signature:?},"))
174+
.collect::<Vec<_>>()
175+
.join("\n")
176+
);
155177
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("out dir"));
156-
fs::write(out_dir.join("rss-reg-vm-runtime-intrinsics.rs"), generated)
157-
.map_err(|error| format!("reg VM runtime intrinsic index should be written: {error}"))?;
178+
fs::write(
179+
out_dir.join("rss-reg-vm-runtime-intrinsics.rs"),
180+
generated_runtime,
181+
)
182+
.map_err(|error| format!("reg VM runtime intrinsic index should be written: {error}"))?;
183+
fs::write(
184+
out_dir.join("rss-reg-vm-special-forms.rs"),
185+
generated_special_forms,
186+
)
187+
.map_err(|error| format!("reg VM special form index should be written: {error}"))?;
158188
Ok(())
159189
}
160190

161-
fn collect_reg_vm_runtime_intrinsics(source: &str) -> Result<Vec<String>, String> {
191+
fn collect_reg_vm_resolver_signatures(source: &str) -> Result<Vec<String>, String> {
162192
let start_marker = "let intrinsic = match (namespace_root, name_root) {";
163193
let start = source
164194
.find(start_marker)
@@ -205,6 +235,40 @@ fn collect_reg_vm_runtime_intrinsics(source: &str) -> Result<Vec<String>, String
205235
Ok(signatures.into_keys().collect())
206236
}
207237

238+
fn collect_runtime_abi_signatures(source: &str) -> BTreeSet<String> {
239+
let mut signatures = BTreeSet::new();
240+
let mut index = 0;
241+
while let Some(relative) = source[index..].find("runtime_intrinsic") {
242+
index += relative + "runtime_intrinsic".len();
243+
let bytes = source.as_bytes();
244+
let mut cursor = index;
245+
if source[cursor..].starts_with("_with_handles") {
246+
cursor += "_with_handles".len();
247+
}
248+
cursor = skip_ascii_whitespace(bytes, cursor);
249+
if bytes.get(cursor) != Some(&b'(') {
250+
continue;
251+
}
252+
cursor += 1;
253+
cursor = skip_ascii_whitespace(bytes, cursor);
254+
let Some((namespace, after_namespace)) = parse_quoted_ascii(source, cursor) else {
255+
continue;
256+
};
257+
cursor = skip_ascii_whitespace(bytes, after_namespace);
258+
if bytes.get(cursor) != Some(&b',') {
259+
continue;
260+
}
261+
cursor += 1;
262+
cursor = skip_ascii_whitespace(bytes, cursor);
263+
let Some((name, after_name)) = parse_quoted_ascii(source, cursor) else {
264+
continue;
265+
};
266+
signatures.insert(format!("{namespace}.{name}"));
267+
index = after_name;
268+
}
269+
signatures
270+
}
271+
208272
fn parse_quoted_ascii(source: &str, quote_index: usize) -> Option<(String, usize)> {
209273
let bytes = source.as_bytes();
210274
if bytes.get(quote_index) != Some(&b'"') {

runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ path = "src/lib.rs"
1111

1212
[dependencies]
1313
serde = { version = "1", features = ["derive"] }
14-
serde_json = "1"
14+
serde_json = { version = "1", features = ["preserve_order"] }
1515
toml = "0.8"
1616
serde_yaml_ng = "0.10"
1717
sha2 = "0.10"

src/hir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,10 @@ impl Hir {
693693
self.types.get(type_root_name(name))
694694
}
695695

696+
pub fn types(&self) -> impl Iterator<Item = &TypeInfo> {
697+
self.types.values()
698+
}
699+
696700
pub fn type_kind(&self, name: &str) -> Option<HirTypeKind> {
697701
self.type_info(name).map(|info| info.kind)
698702
}

0 commit comments

Comments
 (0)