Skip to content

Commit

Permalink
wasm friendly imports
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Nov 13, 2024
1 parent d93460e commit c1991f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
23 changes: 11 additions & 12 deletions packages/fuels-programs/src/assembly/script_and_predicate_loader.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
use fuel_asm::{op, Instruction, RegId};
use fuels_core::{
constants::WORD_SIZE,
types::{
errors::Result,
transaction_builders::{Blob, BlobId},
},
};
use fuels_core::{constants::WORD_SIZE, types::errors::Result};
use itertools::Itertools;

use crate::assembly::cursor::WasmFriendlyCursor;

pub struct LoaderCode {
blob_id: BlobId,
blob_id: [u8; 32],
code: Vec<u8>,
data_offset: usize,
}

impl LoaderCode {
// std gated because of Blob usage which is in transaction_builders which are currently not
// nostd friendly
#[cfg(feature = "std")]
pub fn from_normal_binary(binary: Vec<u8>) -> Result<Self> {
let (original_code, data_section) = split_at_data_offset(&binary)?;

let blob_id = Blob::from(original_code.to_vec()).id();
let blob_id =
fuels_core::types::transaction_builders::Blob::from(original_code.to_vec()).id();
let (loader_code, data_offset) = Self::generate_loader_code(blob_id, data_section);

Ok(Self {
Expand Down Expand Up @@ -75,9 +73,10 @@ impl LoaderCode {
}))
}

pub fn extract_blob(binary: &[u8]) -> Result<Blob> {
#[cfg(feature = "std")]
pub fn extract_blob(binary: &[u8]) -> Result<fuels_core::types::transaction_builders::Blob> {
let (code, _) = split_at_data_offset(binary)?;
Ok(Blob::from(code.to_vec()))
Ok(code.to_vec().into())
}

pub fn as_bytes(&self) -> &[u8] {
Expand All @@ -88,7 +87,7 @@ impl LoaderCode {
self.data_offset
}

fn generate_loader_code(blob_id: BlobId, data_section: &[u8]) -> (Vec<u8>, usize) {
fn generate_loader_code(blob_id: [u8; 32], data_section: &[u8]) -> (Vec<u8>, usize) {
if !data_section.is_empty() {
generate_loader_w_data_section(blob_id, data_section)
} else {
Expand Down
3 changes: 1 addition & 2 deletions wasm-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate alloc;

// #[cfg(all(test, target_arch = "wasm32"))]
#[cfg(test)]
#[cfg(all(test, target_arch = "wasm32"))]
mod tests {
use std::{default::Default, str::FromStr};

Expand Down

0 comments on commit c1991f8

Please sign in to comment.