Skip to content

Commit

Permalink
[Feature] Public Native name_of function (#107)
Browse files Browse the repository at this point in the history
* public native fun name_of

* Add TypeInfo module

* Remove T store
  • Loading branch information
WGB5445 authored and sanlee42 committed Mar 16, 2023
1 parent 535b368 commit 2cbdb88
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 4 deletions.
5 changes: 4 additions & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: TreasuryWithdrawDaoProposal
: StarcoinFramework
? address: "0x00000000000000000000000000000001"
name: TypeInfo
: StarcoinFramework
? address: "0x00000000000000000000000000000001"
name: U256
: StarcoinFramework
Expand All @@ -264,7 +267,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: 59DC490803CD4B71186078A318FC182DC8EDB9C73B5752B79229318EF86631C8
source_digest: 55FD31A7EE545AD6C249CA3C06160B3B905B41CC3EB014362C9A1182E3CB0922
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file modified build/StarcoinFramework/bytecode_modules/Token.mv
Binary file not shown.
Binary file added build/StarcoinFramework/bytecode_modules/TypeInfo.mv
Binary file not shown.
1 change: 1 addition & 0 deletions build/StarcoinFramework/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ This is the root document for the Move StarcoinFramework module documentation. T
- [`0x1::Treasury`](Treasury.md#0x1_Treasury)
- [`0x1::TreasuryScripts`](TreasuryScripts.md#0x1_TreasuryScripts)
- [`0x1::TreasuryWithdrawDaoProposal`](TreasuryWithdrawDaoProposal.md#0x1_TreasuryWithdrawDaoProposal)
- [`0x1::TypeInfo`](TypeInfo.md#0x1_TypeInfo)
- [`0x1::U256`](U256.md#0x1_U256)
- [`0x1::UpgradeModuleDaoProposal`](UpgradeModuleDaoProposal.md#0x1_UpgradeModuleDaoProposal)
- [`0x1::VMConfig`](VMConfig.md#0x1_VMConfig)
Expand Down
29 changes: 27 additions & 2 deletions build/StarcoinFramework/docs/Token.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Token implementation of Starcoin.
- [Function `is_same_token`](#0x1_Token_is_same_token)
- [Function `token_address`](#0x1_Token_token_address)
- [Function `token_code`](#0x1_Token_token_code)
- [Function `type_of`](#0x1_Token_type_of)
- [Function `name_of`](#0x1_Token_name_of)
- [Function `name_of_token`](#0x1_Token_name_of_token)
- [Module Specification](#@Module_Specification_1)
Expand Down Expand Up @@ -1709,6 +1710,30 @@ does not matter for the verification of callers.



</details>

<a name="0x1_Token_type_of"></a>

## Function `type_of`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="Token.md#0x1_Token_type_of">type_of</a>&lt;T&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> (<b>friend</b>) <b>fun</b> <a href="Token.md#0x1_Token_type_of">type_of</a>&lt;T&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;){
<a href="Token.md#0x1_Token_name_of">name_of</a>&lt;T&gt;()
}
</code></pre>



</details>

<a name="0x1_Token_name_of"></a>
Expand All @@ -1718,7 +1743,7 @@ does not matter for the verification of callers.
Return Token's module address, module name, and type name of <code>TokenType</code>.


<pre><code><b>fun</b> <a href="Token.md#0x1_Token_name_of">name_of</a>&lt;TokenType: store&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;)
<pre><code><b>fun</b> <a href="Token.md#0x1_Token_name_of">name_of</a>&lt;TokenType&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;)
</code></pre>


Expand All @@ -1727,7 +1752,7 @@ Return Token's module address, module name, and type name of <code>TokenType</co
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="Token.md#0x1_Token_name_of">name_of</a>&lt;TokenType: store&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;);
<pre><code><b>native</b> <b>fun</b> <a href="Token.md#0x1_Token_name_of">name_of</a>&lt;TokenType&gt;(): (<b>address</b>, vector&lt;u8&gt;, vector&lt;u8&gt;);
</code></pre>


Expand Down
158 changes: 158 additions & 0 deletions build/StarcoinFramework/docs/TypeInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

<a name="0x1_TypeInfo"></a>

# Module `0x1::TypeInfo`



- [Struct `TypeInfo`](#0x1_TypeInfo_TypeInfo)
- [Function `account_address`](#0x1_TypeInfo_account_address)
- [Function `module_name`](#0x1_TypeInfo_module_name)
- [Function `struct_name`](#0x1_TypeInfo_struct_name)
- [Function `type_of`](#0x1_TypeInfo_type_of)


<pre><code><b>use</b> <a href="Token.md#0x1_Token">0x1::Token</a>;
</code></pre>



<a name="0x1_TypeInfo_TypeInfo"></a>

## Struct `TypeInfo`



<pre><code><b>struct</b> <a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a> <b>has</b> <b>copy</b>, drop, store
</code></pre>



<details>
<summary>Fields</summary>


<dl>
<dt>
<code>account_address: <b>address</b></code>
</dt>
<dd>

</dd>
<dt>
<code>module_name: vector&lt;u8&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>struct_name: vector&lt;u8&gt;</code>
</dt>
<dd>

</dd>
</dl>


</details>

<a name="0x1_TypeInfo_account_address"></a>

## Function `account_address`



<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_account_address">account_address</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo_TypeInfo">TypeInfo::TypeInfo</a>): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_account_address">account_address</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a>):<b>address</b>{
type_info.account_address
}
</code></pre>



</details>

<a name="0x1_TypeInfo_module_name"></a>

## Function `module_name`



<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_module_name">module_name</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo_TypeInfo">TypeInfo::TypeInfo</a>): vector&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_module_name">module_name</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a>):vector&lt;u8&gt;{
*&type_info.module_name
}
</code></pre>



</details>

<a name="0x1_TypeInfo_struct_name"></a>

## Function `struct_name`



<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_struct_name">struct_name</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo_TypeInfo">TypeInfo::TypeInfo</a>): vector&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_struct_name">struct_name</a>(type_info: &<a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a>):vector&lt;u8&gt;{
*&type_info.struct_name
}
</code></pre>



</details>

<a name="0x1_TypeInfo_type_of"></a>

## Function `type_of`



<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_type_of">type_of</a>&lt;T&gt;(): <a href="TypeInfo.md#0x1_TypeInfo_TypeInfo">TypeInfo::TypeInfo</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TypeInfo.md#0x1_TypeInfo_type_of">type_of</a>&lt;T&gt;():<a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a>{
<b>let</b> (account_address, module_name, struct_name) = <a href="Token.md#0x1_Token_type_of">Token::type_of</a>&lt;T&gt;();
<a href="TypeInfo.md#0x1_TypeInfo">TypeInfo</a> {
account_address,
module_name,
struct_name
}
}
</code></pre>



</details>
Binary file modified build/StarcoinFramework/source_maps/Token.mvsm
Binary file not shown.
Binary file added build/StarcoinFramework/source_maps/TypeInfo.mvsm
Binary file not shown.
7 changes: 6 additions & 1 deletion sources/Token.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Token {
use StarcoinFramework::Errors;
use StarcoinFramework::Math;

friend StarcoinFramework::TypeInfo;
spec module {
pragma verify;
pragma aborts_if_is_strict;
Expand Down Expand Up @@ -504,8 +505,12 @@ module Token {
/// does not matter for the verification of callers.
spec fun spec_token_code<TokenType>(): TokenCode;

public (friend) fun type_of<T>(): (address, vector<u8>, vector<u8>){
name_of<T>()
}

/// Return Token's module address, module name, and type name of `TokenType`.
native fun name_of<TokenType: store>(): (address, vector<u8>, vector<u8>);
native fun name_of<TokenType>(): (address, vector<u8>, vector<u8>);

spec name_of {
pragma opaque = true;
Expand Down
30 changes: 30 additions & 0 deletions sources/TypeInfo.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module StarcoinFramework::TypeInfo{
use StarcoinFramework::Token;

struct TypeInfo has copy, drop, store{
account_address: address,
module_name: vector<u8>,
struct_name: vector<u8>
}

public fun account_address(type_info: &TypeInfo):address{
type_info.account_address
}

public fun module_name(type_info: &TypeInfo):vector<u8>{
*&type_info.module_name
}

public fun struct_name(type_info: &TypeInfo):vector<u8>{
*&type_info.struct_name
}

public fun type_of<T>():TypeInfo{
let (account_address, module_name, struct_name) = Token::type_of<T>();
TypeInfo {
account_address,
module_name,
struct_name
}
}
}

0 comments on commit 2cbdb88

Please sign in to comment.