Skip to content

Commit

Permalink
fix: add all of the required duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
matteojug committed Sep 9, 2024
1 parent 010768a commit 8d191d9
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions clarity/src/vm/clarity_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use stacks_common::util::secp256k1::{secp256k1_recover, secp256k1_verify, Secp25
use wasmtime::{AsContextMut, Caller, Engine, Linker, Memory, Module, Store, Trap, Val, ValType};

use super::analysis::{CheckError, CheckErrors};
use super::ast::parse;
use super::callables::{DefineType, DefinedFunction};
use super::contracts::Contract;
use super::costs::{constants as cost_constants, CostTracker, LimitedCostTracker};
Expand Down Expand Up @@ -1673,6 +1674,25 @@ fn pass_argument_to_wasm(
}
}

pub fn signature_from_string(
val: &str,
version: ClarityVersion,
epoch: StacksEpochId,
) -> Result<TypeSignature, Error> {
let expr = parse(
&QualifiedContractIdentifier::transient(),
val,
version,
epoch,
)?;
let expr = expr.first().ok_or(CheckErrors::InvalidTypeDescription)?;
Ok(TypeSignature::parse_type_repr(
StacksEpochId::latest(),
expr,
&mut (),
)?)
}

/// Reserve space on the Wasm stack for the return value of a function, if
/// needed, and return a vector of `Val`s that can be passed to `call`, as a
/// place to store the return value, along with the new offset, which is the
Expand Down Expand Up @@ -5548,10 +5568,9 @@ fn link_print_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error> {
"print",
|mut caller: Caller<'_, ClarityWasmContext>,
value_offset: i32,
value_length: i32,
_serialized_ty_offset: i32,
_serialized_ty_length: i32| {
// NOTE: _serialized_ty_offset and _serialized_ty_length are used in `stacks-network/clarity-wasm`, here we only care about the function stub
_value_length: i32,
serialized_ty_offset: i32,
serialized_ty_length: i32| {
// runtime_cost(ClarityCostFunction::Print, env, input.size())?;

// Get the memory from the caller
Expand All @@ -5560,14 +5579,19 @@ fn link_print_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error> {
.and_then(|export| export.into_memory())
.ok_or(Error::Wasm(WasmError::MemoryNotFound))?;

// Read in the bytes from the Wasm memory
let bytes = read_bytes_from_wasm(memory, &mut caller, value_offset, value_length)?;
let serialized_ty = String::from_utf8(read_bytes_from_wasm(
memory,
&mut caller,
serialized_ty_offset,
serialized_ty_length,
)?)?;

let clarity_val = Value::deserialize_read(&mut bytes.as_slice(), None, false)?;
let epoch = caller.data().global_context.epoch_id;
let version = caller.data().contract_context().get_clarity_version();

if cfg!(feature = "developer-mode") {
debug!("{}", &clarity_val);
}
let value_ty = signature_from_string(&serialized_ty, *version, epoch)?;
let clarity_val =
read_from_wasm_indirect(memory, &mut caller, &value_ty, value_offset, epoch)?;

caller.data_mut().register_print_event(clarity_val)?;

Expand Down

0 comments on commit 8d191d9

Please sign in to comment.