Skip to content

Commit 8d191d9

Browse files
committed
fix: add all of the required duplicated code
1 parent 010768a commit 8d191d9

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use stacks_common::util::secp256k1::{secp256k1_recover, secp256k1_verify, Secp25
1111
use wasmtime::{AsContextMut, Caller, Engine, Linker, Memory, Module, Store, Trap, Val, ValType};
1212

1313
use super::analysis::{CheckError, CheckErrors};
14+
use super::ast::parse;
1415
use super::callables::{DefineType, DefinedFunction};
1516
use super::contracts::Contract;
1617
use super::costs::{constants as cost_constants, CostTracker, LimitedCostTracker};
@@ -1673,6 +1674,25 @@ fn pass_argument_to_wasm(
16731674
}
16741675
}
16751676

1677+
pub fn signature_from_string(
1678+
val: &str,
1679+
version: ClarityVersion,
1680+
epoch: StacksEpochId,
1681+
) -> Result<TypeSignature, Error> {
1682+
let expr = parse(
1683+
&QualifiedContractIdentifier::transient(),
1684+
val,
1685+
version,
1686+
epoch,
1687+
)?;
1688+
let expr = expr.first().ok_or(CheckErrors::InvalidTypeDescription)?;
1689+
Ok(TypeSignature::parse_type_repr(
1690+
StacksEpochId::latest(),
1691+
expr,
1692+
&mut (),
1693+
)?)
1694+
}
1695+
16761696
/// Reserve space on the Wasm stack for the return value of a function, if
16771697
/// needed, and return a vector of `Val`s that can be passed to `call`, as a
16781698
/// place to store the return value, along with the new offset, which is the
@@ -5548,10 +5568,9 @@ fn link_print_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Error> {
55485568
"print",
55495569
|mut caller: Caller<'_, ClarityWasmContext>,
55505570
value_offset: i32,
5551-
value_length: i32,
5552-
_serialized_ty_offset: i32,
5553-
_serialized_ty_length: i32| {
5554-
// NOTE: _serialized_ty_offset and _serialized_ty_length are used in `stacks-network/clarity-wasm`, here we only care about the function stub
5571+
_value_length: i32,
5572+
serialized_ty_offset: i32,
5573+
serialized_ty_length: i32| {
55555574
// runtime_cost(ClarityCostFunction::Print, env, input.size())?;
55565575

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

5563-
// Read in the bytes from the Wasm memory
5564-
let bytes = read_bytes_from_wasm(memory, &mut caller, value_offset, value_length)?;
5582+
let serialized_ty = String::from_utf8(read_bytes_from_wasm(
5583+
memory,
5584+
&mut caller,
5585+
serialized_ty_offset,
5586+
serialized_ty_length,
5587+
)?)?;
55655588

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

5568-
if cfg!(feature = "developer-mode") {
5569-
debug!("{}", &clarity_val);
5570-
}
5592+
let value_ty = signature_from_string(&serialized_ty, *version, epoch)?;
5593+
let clarity_val =
5594+
read_from_wasm_indirect(memory, &mut caller, &value_ty, value_offset, epoch)?;
55715595

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

0 commit comments

Comments
 (0)