Skip to content

Commit c241855

Browse files
authored
Merge pull request #5088 from stacks-network/fix/wasm-contract-call-dynamic
Clarity-Wasm: fix for the `contract-call?` host function
2 parents 5f9c466 + c3d86cb commit c241855

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5288,6 +5288,8 @@ fn link_contract_call_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
52885288
"clarity",
52895289
"contract_call",
52905290
|mut caller: Caller<'_, ClarityWasmContext>,
5291+
trait_name_offset: i32,
5292+
trait_name_length: i32,
52915293
contract_offset: i32,
52925294
contract_length: i32,
52935295
function_offset: i32,
@@ -5403,10 +5405,26 @@ fn link_contract_call_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
54035405
}?;
54045406

54055407
// Write the result to the return buffer
5406-
let return_ty = function
5407-
.get_return_type()
5408-
.as_ref()
5409-
.ok_or(CheckErrors::DefineFunctionBadSignature)?;
5408+
let return_ty = if trait_name_length == 0 {
5409+
// This is a direct call
5410+
function.get_return_type().as_ref()
5411+
} else {
5412+
// This is a dynamic call
5413+
let trait_name = read_identifier_from_wasm(
5414+
memory,
5415+
&mut caller,
5416+
trait_name_offset,
5417+
trait_name_length,
5418+
)?;
5419+
contract
5420+
.contract_context
5421+
.defined_traits
5422+
.get(trait_name.as_str())
5423+
.and_then(|trait_functions| trait_functions.get(function_name.as_str()))
5424+
.map(|f_ty| &f_ty.returns)
5425+
}
5426+
.ok_or(CheckErrors::DefineFunctionBadSignature)?;
5427+
54105428
let memory = caller
54115429
.get_export("memory")
54125430
.and_then(|export| export.into_memory())
@@ -5419,7 +5437,7 @@ fn link_contract_call_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
54195437
return_offset,
54205438
return_offset + get_type_size(return_ty),
54215439
&result,
5422-
true,
5440+
false,
54235441
)?;
54245442

54255443
Ok(())

0 commit comments

Comments
 (0)