Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative paths in %include_hex() #147

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion etk-asm/src/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@
fn pop_path(&mut self) {
self.sources.pop();
}

/// Expands the path relative to last pushed path without pushing new path
fn resolve_path(&self, path: &PathBuf) -> Result<PathBuf, Error> {
let last = self.sources.last().unwrap();
let dir = match last.parent() {
Some(s) => s,
None => Path::new("./"),
};
let resolved = dir.join(path.to_owned());

Check failure on line 230 in etk-asm/src/ingest.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unnecessary use of `to_owned`
self.root.as_ref().unwrap().check(&resolved)?;

Ok(resolved)
}
}

/// A high-level interface for assembling files into EVM bytecode.
Expand Down Expand Up @@ -321,7 +334,8 @@
raws.push(RawOp::Scope(inc_raws));
}
Node::IncludeHex(hex_path) => {
let file = std::fs::read_to_string(&hex_path).with_context(|_| error::Io {
let source = program.resolve_path(&hex_path)?;
let file = std::fs::read_to_string(&source).with_context(|_| error::Io {
message: "reading hex include",
path: hex_path.to_owned(),
})?;
Expand Down
11 changes: 11 additions & 0 deletions etk-asm/tests/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,14 @@ fn test_variable_sized_push2() -> Result<(), Error> {

Ok(())
}

#[test]
fn test_include_hex() -> Result<(), Error> {
let mut output = Vec::new();
let mut ingester = Ingest::new(&mut output);
ingester.ingest_file(source(&["include-hex", "main.etk"]))?;

assert_eq!(output, hex!("6002600c5f3960025ff300 cafeb0ba cafebabe"));

Ok(())
}
1 change: 1 addition & 0 deletions etk-asm/tests/asm/include-hex/hex1.etk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cafeb0ba
11 changes: 11 additions & 0 deletions etk-asm/tests/asm/include-hex/main.etk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
push1 2
push1 12
push0
codecopy
push1 2
push0
return
stop

%include_hex("hex1.etk")
%include_hex("subdirectory/hex2.etk")
1 change: 1 addition & 0 deletions etk-asm/tests/asm/include-hex/subdirectory/hex2.etk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cafebabe
Loading