Skip to content

Commit

Permalink
move stdlib: type_info::full_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Jul 21, 2022
1 parent 9593a8d commit 59be05a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
Binary file not shown.
30 changes: 30 additions & 0 deletions aptos-move/framework/aptos-stdlib/sources/type_info.move
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module aptos_std::type_info {
use std::string;

struct TypeInfo has copy, drop, store {
account_address: address,
module_name: vector<u8>,
Expand All @@ -18,6 +20,7 @@ module aptos_std::type_info {
}

public native fun type_of<T>(): TypeInfo;
public native fun full_name<T>(): string::String;

#[test]
fun test() {
Expand All @@ -26,4 +29,31 @@ module aptos_std::type_info {
assert!(module_name(&type_info) == b"type_info", 1);
assert!(struct_name(&type_info) == b"TypeInfo", 2);
}

#[test]
fun test_full_name() {
use aptos_std::table::Table;

assert!(full_name<bool>() == string::utf8(b"bool"), 0);
assert!(full_name<u8>() == string::utf8(b"u8"), 1);
assert!(full_name<u64>() == string::utf8(b"u64"), 2);
assert!(full_name<u128>() == string::utf8(b"u128"), 3);
assert!(full_name<address>() == string::utf8(b"address"), 4);
assert!(full_name<signer>() == string::utf8(b"signer"), 5);

// vector
assert!(full_name<vector<u8>>() == string::utf8(b"vector<u8>"), 6);
assert!(full_name<vector<vector<u8>>>() == string::utf8(b"vector<vector<u8>>"), 7);
assert!(full_name<vector<vector<TypeInfo>>>() == string::utf8(b"vector<vector<0x1::type_info::TypeInfo>>"), 8);


// struct
assert!(full_name<TypeInfo>() == string::utf8(b"0x1::type_info::TypeInfo"), 9);
assert!(full_name<
Table<
TypeInfo,
Table<u8, vector<TypeInfo>>
>
>() == string::utf8(b"0x1::table::Table<0x1::type_info::TypeInfo, 0x1::table::Table<u8, vector<0x1::type_info::TypeInfo>>"), 10);
}
}
2 changes: 2 additions & 0 deletions aptos-move/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use move_deps::{
pub mod cost {
pub const APTOS_CREATE_ADDRESS: u64 = 5;
pub const APTOS_LIB_TYPE_OF: u64 = 10;
pub const APTOS_LIB_TYPE_NAME: u64 = 10;
pub const APTOS_SIP_HASH: u64 = 10;
pub const APTOS_SECP256K1_RECOVER: u64 = 71;
}
Expand Down Expand Up @@ -50,6 +51,7 @@ pub fn all_natives(framework_addr: AccountAddress) -> NativeFunctionTable {
signature::native_secp256k1_recover,
),
("type_info", "type_of", type_info::type_of),
("type_info", "full_name", type_info::full_name),
("hash", "sip_hash", hash::native_sip_hash),
];
NATIVES
Expand Down
22 changes: 22 additions & 0 deletions aptos-move/framework/src/natives/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn type_of(
let cost = GasCost::new(super::cost::APTOS_LIB_TYPE_OF, 1).total();

let type_tag = context.type_to_type_tag(&ty_args[0])?;

if let TypeTag::Struct(struct_tag) = type_tag {
Ok(NativeResult::ok(
cost,
Expand All @@ -42,6 +43,27 @@ pub fn type_of(
}
}

/// Returns a string representing the TypeTag of the parameter.
pub fn full_name(
context: &mut NativeContext,
ty_args: Vec<Type>,
arguments: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(arguments.is_empty());

let cost = GasCost::new(super::cost::APTOS_LIB_TYPE_NAME, 1).total();
let type_tag = context.type_to_type_tag(&ty_args[0])?;
let type_name = type_tag.to_string();

Ok(NativeResult::ok(
cost,
smallvec![Value::struct_(Struct::pack(vec![Value::vector_u8(
type_name.as_bytes().to_vec()
)]))],
))
}

fn type_of_internal(struct_tag: &StructTag) -> Result<SmallVec<[Value; 1]>, std::fmt::Error> {
let mut name = struct_tag.name.to_string();
if let Some(first_ty) = struct_tag.type_params.first() {
Expand Down
7 changes: 6 additions & 1 deletion aptos-move/framework/tests/move_unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ pub fn aptos_test_natives() -> NativeFunctionTable {
}

#[test]
fn move_unit_tests() {
fn move_framework_unit_tests() {
run_tests_for_pkg("aptos-framework");
}

#[test]
fn move_stdlib_unit_tests() {
run_tests_for_pkg("aptos-stdlib");
}

#[test]
fn move_token_unit_tests() {
run_tests_for_pkg("aptos-token");
Expand Down

0 comments on commit 59be05a

Please sign in to comment.