Skip to content

Commit

Permalink
[API] MoveConverter::try_into_vm_value() special-cases ASCII::String
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse authored and aptos-bot committed Apr 26, 2022
1 parent 3ac7c04 commit 6260f2f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
8 changes: 7 additions & 1 deletion api/src/tests/converter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{current_function_name, tests::new_test_context};
use aptos_api_types::{AsConverter, MoveConverter, MoveType};
use aptos_api_types::{new_vm_ascii_string, AsConverter, MoveConverter, MoveType};
use aptos_vm::data_cache::AsMoveResolver;
use move_core_types::{
account_address::AccountAddress,
Expand All @@ -27,6 +27,12 @@ async fn test_value_conversion() {
assert_value_conversion(&converter, "u128", "1", VmMoveValue::U128(1));
assert_value_conversion(&converter, "bool", true, VmMoveValue::Bool(true));
assert_value_conversion(&converter, "address", "0x1", VmMoveValue::Address(address));
assert_value_conversion(
&converter,
"0x1::ASCII::String",
"hello",
new_vm_ascii_string("hello"),
);
assert_value_conversion(
&converter,
"vector<u8>",
Expand Down
39 changes: 30 additions & 9 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
type_tag: &TypeTag,
val: Value,
) -> Result<move_core_types::value::MoveValue> {
let layout = self.inner.get_type_layout_with_fields(type_tag)?;
let layout = self.inner.get_type_layout_with_types(type_tag)?;

self.try_into_vm_value_from_layout(&layout, val)
}
Expand Down Expand Up @@ -496,14 +496,21 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
layout: &MoveStructLayout,
val: Value,
) -> Result<move_core_types::value::MoveValue> {
let field_layouts = if let MoveStructLayout::WithFields(fields) = layout {
fields
} else {
bail!(
"Expecting `MoveStructLayout::WithFields, getting {:?}",
layout
);
};
let (struct_tag, field_layouts) =
if let MoveStructLayout::WithTypes { type_, fields } = layout {
(type_, fields)
} else {
bail!(
"Expecting `MoveStructLayout::WithTypes`, getting {:?}",
layout
);
};
if MoveValue::is_ascii_string(struct_tag) {
let string = val
.as_str()
.ok_or_else(|| format_err!("failed to parse ASCII::String."))?;
return Ok(new_vm_ascii_string(string));
}

let mut field_values = if let Value::Object(fields) = val {
fields
Expand Down Expand Up @@ -596,3 +603,17 @@ impl<R: MoveResolver> AsConverter<R> for R {
MoveConverter::new(self)
}
}

pub fn new_vm_ascii_string(string: &str) -> move_core_types::value::MoveValue {
use move_core_types::value::{MoveStruct, MoveValue};

let byte_vector = MoveValue::Vector(
string
.as_bytes()
.iter()
.map(|byte| MoveValue::U8(*byte))
.collect(),
);
let move_string = MoveStruct::Runtime(vec![byte_vector]);
MoveValue::Struct(move_string)
}
2 changes: 1 addition & 1 deletion api/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod transaction;
pub use account::AccountData;
pub use address::Address;
pub use bytecode::Bytecode;
pub use convert::{AsConverter, MoveConverter};
pub use convert::{new_vm_ascii_string, AsConverter, MoveConverter};
pub use error::Error;
pub use event_key::EventKey;
pub use hash::HashValue;
Expand Down

0 comments on commit 6260f2f

Please sign in to comment.