Skip to content

Commit

Permalink
assembly: check library has at least one export
Browse files Browse the repository at this point in the history
  • Loading branch information
paracetamolo committed Dec 10, 2024
1 parent 06e06b0 commit cabb601
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
11 changes: 7 additions & 4 deletions assembly/src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ impl Library {
/// Constructs a new [`Library`] from the provided MAST forest and a set of exports.
///
/// # Errors
/// Returns an error if the set of exports is empty.
/// Returns an error if any of the specified exports do not have a corresponding procedure root
/// in the provided MAST forest.
pub fn new(
mast_forest: Arc<MastForest>,
exports: BTreeMap<QualifiedProcedureName, MastNodeId>,
) -> Result<Self, LibraryError> {
if exports.is_empty() {
return Err(LibraryError::NoExport);
}
for (fqn, &proc_body_id) in exports.iter() {
if !mast_forest.is_procedure_root(proc_body_id) {
return Err(LibraryError::NoProcedureRootForExport { procedure_path: fqn.clone() });
Expand Down Expand Up @@ -177,6 +181,9 @@ impl Deserializable for Library {
let mast_forest = Arc::new(MastForest::read_from(source)?);

let num_exports = source.read_usize()?;
if num_exports == 0 {
return Err(DeserializationError::InvalidValue(String::from("No exported procedures")));
};
let mut exports = BTreeMap::new();
for _ in 0..num_exports {
let proc_module = source.read()?;
Expand Down Expand Up @@ -353,10 +360,6 @@ impl TryFrom<Library> for KernelLibrary {
type Error = LibraryError;

fn try_from(library: Library) -> Result<Self, Self::Error> {
if library.exports.is_empty() {
return Err(LibraryError::NoExport);
}

let kernel_path = LibraryPath::from(LibraryNamespace::Kernel);
let mut proc_digests = Vec::with_capacity(library.exports.len());

Expand Down
3 changes: 3 additions & 0 deletions miden/masm-examples/bundle/lib_noexports/lib.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proc.lib_proc
swap
end
9 changes: 9 additions & 0 deletions miden/tests/integration/cli/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ fn cli_bundle_debug() {
cmd.arg("bundle").arg("--debug").arg("./masm-examples/bundle/lib");
cmd.assert().success();
}

#[test]
fn cli_bundle_no_exports() {
let mut cmd = bin_under_test().command();
cmd.arg("bundle").arg("./masm-examples/bundle/lib_noexports");
cmd.assert()
.failure()
.stderr(predicate::str::contains("library must contain at least one exported procedure"));
}

0 comments on commit cabb601

Please sign in to comment.