Skip to content

Commit

Permalink
Improve hygiene of script_with_data_offset! (#136)
Browse files Browse the repository at this point in the history
* make `script_data_with_offset!` hygienic and fully re-export some prelude deps.
  • Loading branch information
Voxelot authored Jun 13, 2022
1 parent 8440128 commit 52cbe36
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,29 @@ pub mod util;
#[cfg(feature = "profile-any")]
pub mod profiler;

// Fully re-export fuel dependencies
#[doc(no_inline)]
pub use fuel_asm;
#[doc(no_inline)]
pub use fuel_crypto;
#[doc(no_inline)]
pub use fuel_merkle;
#[doc(no_inline)]
pub use fuel_storage;
#[doc(no_inline)]
pub use fuel_tx;
#[doc(no_inline)]
pub use fuel_types;

pub mod prelude {
//! Required implementations for full functionality

#[doc(no_inline)]
pub use fuel_asm::{Instruction, InstructionResult, Opcode, OpcodeRepr, PanicReason};
#[doc(no_inline)]
pub use fuel_storage::{MerkleRoot, MerkleStorage, Storage};
#[doc(no_inline)]
pub use fuel_tx::{Contract, Input, Output, Receipt, Transaction, UtxoId, ValidationError, Witness};
#[doc(no_inline)]
pub use fuel_types::{
bytes::{Deserializable, SerializableVec, SizedBytes},
Address, AssetId, Bytes32, Bytes4, Bytes64, Bytes8, ContractId, Immediate06, Immediate12, Immediate18,
Expand Down
24 changes: 17 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,23 @@
#[macro_export]
macro_rules! script_with_data_offset {
($offset:ident, $script:expr) => {{
use fuel_types::{bytes, Immediate18};
use $crate::consts::VM_TX_MEMORY;
use $crate::prelude::Transaction;
let $offset = 0 as Immediate18;
let script_bytes: Vec<u8> = { $script }.into_iter().collect();
let data_offset = VM_TX_MEMORY + Transaction::script_offset() + bytes::padded_len(script_bytes.as_slice());
let $offset = data_offset as Immediate18;
let $offset = {
// first set offset to 0 before evaluating script expression
let $offset = {
use $crate::prelude::Immediate18;
0 as Immediate18
};
// evaluate script expression with zeroed data offset to get the script length
let script_bytes: ::std::vec::Vec<u8> = ::std::iter::IntoIterator::into_iter({ $script }).collect();
// compute the script data offset within the VM memory given the script length
{
use $crate::consts::VM_TX_MEMORY;
use $crate::fuel_types::bytes::padded_len;
use $crate::prelude::{Immediate18, Transaction};
(VM_TX_MEMORY + Transaction::script_offset() + padded_len(script_bytes.as_slice())) as Immediate18
}
};
// re-evaluate and return the finalized script with the correct data offset length set.
($script, $offset)
}};
}
Expand Down

0 comments on commit 52cbe36

Please sign in to comment.