Skip to content

Commit 7974dcf

Browse files
committed
runtime code
1 parent 9076e68 commit 7974dcf

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/HuffConfig.sol

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,81 @@ contract HuffConfig {
216216
// set `msg.sender` for upcoming create context
217217
vm.prank(deployer);
218218

219+
vm.ffi(cleanup);
220+
}
221+
222+
function runtime_code(string memory file) public payable returns (bytes memory bytecode) {
223+
binary_check();
224+
225+
// Split the file into its parts
226+
strings.slice memory s = file.toSlice();
227+
strings.slice memory delim = "/".toSlice();
228+
string[] memory parts = new string[](s.count(delim) + 1);
229+
for (uint256 i = 0; i < parts.length; i++) {
230+
parts[i] = s.split(delim).toString();
231+
}
232+
233+
// Get the system time with our script
234+
string[] memory time = new string[](1);
235+
time[0] = "./lib/foundry-huff/scripts/rand_bytes.sh";
236+
bytes memory retData = vm.ffi(time);
237+
string memory rand_bytes = bytes32ToString(keccak256(abi.encode(bytes32(retData))));
238+
239+
// Re-concatenate the file with a "__TEMP__" prefix
240+
string memory tempFile = parts[0];
241+
if (parts.length <= 1) {
242+
tempFile = string.concat("__TEMP__", rand_bytes, tempFile);
243+
} else {
244+
for (uint256 i = 1; i < parts.length - 1; i++) {
245+
tempFile = string.concat(tempFile, "/", parts[i]);
246+
}
247+
tempFile = string.concat(tempFile, "/", "__TEMP__", rand_bytes, parts[parts.length - 1]);
248+
}
249+
250+
// Paste the code in a new temp file
251+
string[] memory create_cmds = new string[](3);
252+
// TODO: create_cmds[0] = "$(find . -name \"file_writer.sh\")";
253+
create_cmds[0] = "./lib/foundry-huff/scripts/file_writer.sh";
254+
create_cmds[1] = string.concat("src/", tempFile, ".huff");
255+
create_cmds[2] = string.concat(code, "\n");
256+
vm.ffi(create_cmds);
257+
258+
// Append the real code to the temp file
259+
string[] memory append_cmds = new string[](3);
260+
append_cmds[0] = "./lib/foundry-huff/scripts/read_and_append.sh";
261+
append_cmds[1] = string.concat("src/", tempFile, ".huff");
262+
append_cmds[2] = string.concat("src/", file, ".huff");
263+
vm.ffi(append_cmds);
264+
265+
/// Create a list of strings with the commands necessary to compile Huff contracts
266+
string[] memory cmds = new string[](5);
267+
if (const_overrides.length > 0) {
268+
cmds = new string[](6 + const_overrides.length);
269+
cmds[5] = "-c";
270+
271+
Constant memory cur_const;
272+
for (uint256 i; i < const_overrides.length; i++) {
273+
cur_const = const_overrides[i];
274+
cmds[6 + i] = string.concat(cur_const.key, "=", cur_const.value);
275+
}
276+
}
277+
278+
cmds[0] = "huffc";
279+
cmds[1] = string(string.concat("src/", tempFile, ".huff"));
280+
cmds[2] = "-r";
281+
cmds[3] = "-e";
282+
cmds[4] = get_evm_version();
283+
284+
/// @notice compile the Huff contract and return the bytecode
285+
bytecode = vm.ffi(cmds);
286+
287+
// Clean up temp files
288+
string[] memory cleanup = new string[](2);
289+
cleanup[0] = "rm";
290+
cleanup[1] = string.concat("src/", tempFile, ".huff");
291+
292+
// set `msg.sender` for upcoming create context
293+
vm.prank(deployer);
219294

220295
vm.ffi(cleanup);
221296
}

0 commit comments

Comments
 (0)