Skip to content

Add Type Convert#196

Merged
lightszero merged 12 commits intomasterfrom
Branch_lights_utconvert
Mar 11, 2020
Merged

Add Type Convert#196
lightszero merged 12 commits intomasterfrom
Branch_lights_utconvert

Conversation

@lightszero
Copy link
Copy Markdown
Member

fixed #195

@lightszero lightszero requested review from Tommo-L and shargon March 5, 2020 04:43


[OpCode(SmartContract.Framework.OpCode.CONVERT, "0x21")]
public extern static BigInteger ConvertToInteger(byte[] from);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we still use the AsByteArray as the function name?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move these to SmartContract.Framework

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do not care about old contract code,we can direct change AsByteArray.

Copy link
Copy Markdown
Contributor

@Tommo-L Tommo-L Mar 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just ignore the old contract, as the previous version could not invoke contract successful. 😂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,let us add more convert code in framework and compiler

shargon and others added 4 commits March 5, 2020 06:53
pushNumber
pushBoolean
pushDataArray

過去合約中所有的數值是統一的Push
小於等於16的數字 和 byte[0] 和  true false 是Integer
>16的數字 和 byte[n>0] 是 ByteArray

現在在合約中使用數值將直接得到正確的類型

new byte[0] 是ByteArray
true        是Boolean
17          是Integer
因爲現在會返回integer類型了,不再是DataArray

2.修改了TypeConvert
應該現在 new byte[0] 直接是ByteArray類型了
@lightszero lightszero changed the title add a convert unittest. Add Type Convert Mar 5, 2020
@lightszero
Copy link
Copy Markdown
Member Author

I had do some fix

now
new byte[0] =>ByteArray
new byte[3]{1,2,3}=>ByteArray
true =>Boolean
1=>Integer
2=>Integer
17 =>Integer

const value will got right type without convert.

2.Use Helper.AsBigInteger() to convert to BigInteger
3.fix unittest
@lightszero
Copy link
Copy Markdown
Member Author

Helper.AsByteArray() fixed.

@lightszero lightszero requested a review from Tommo-L March 5, 2020 13:41
@lightszero lightszero requested a review from shargon March 9, 2020 02:34
shargon
shargon previously approved these changes Mar 9, 2020
@lightszero
Copy link
Copy Markdown
Member Author

@shargon has a format problem,fix it.
@Tommo-L what do you think?should we merge this?

@Tommo-L
Copy link
Copy Markdown
Contributor

Tommo-L commented Mar 10, 2020

I'll review it today.

/// </summary>
[Script]
[OpCode(OpCode.CONVERT, StackItemType_ByteArray)]
public extern static byte[] AsByteArray(this BigInteger source);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to update the comments later.

var int3 = bts3.AsBigInteger();

var arrobj = new object[8];
arrobj[0] = int0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a test case for _ConvertPushBoolean.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's hard,in IL sometimes bool is int too.

@lightszero lightszero merged commit 8acf90f into master Mar 11, 2020
@lightszero lightszero deleted the Branch_lights_utconvert branch March 11, 2020 03:41
/// </summary>
[Script]
[OpCode(OpCode.CONVERT, StackItemType_ByteArray)]
public extern static byte[] AsByteArray(this byte source);
Copy link
Copy Markdown
Member

@erikzhang erikzhang Mar 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use CONVERT, we should rename the methods from As*** to To***. Because "As" implies the meaning of not performing the actual type conversion.

@shargon shargon mentioned this pull request Mar 11, 2020
lightszero added a commit that referenced this pull request Mar 12, 2020
#196

because Insert a Opcode Convert

but newarr still get last opcode for a PushNumber

2.add some unittest for it.
lightszero added a commit that referenced this pull request Mar 13, 2020
#196

because Insert a Opcode Convert

but newarr still get last opcode for a PushNumber

2.add some unittest for it.
Jim8y pushed a commit that referenced this pull request Aug 3, 2025
* this unittest for
#195
* Clean code
* 區分push
pushNumber
pushBoolean
pushDataArray
過去合約中所有的數值是統一的Push
小於等於16的數字 和 byte[0] 和  true false 是Integer
>16的數字 和 byte[n>0] 是 ByteArray
現在在合約中使用數值將直接得到正確的類型
new byte[0] 是ByteArray
true        是Boolean
17          是Integer
* 1.修改了unittest null
因爲現在會返回integer類型了,不再是DataArray
2.修改了TypeConvert
應該現在 new byte[0] 直接是ByteArray類型了
* 1.Use Helper.AsByteArray() to convert to ByteArray
2.Use Helper.AsBigInteger() to convert to BigInteger
3.fix unittest
* fix one small fault.
* add value equal test
* Clean code
* Clean code
* fix format
Co-authored-by: Shargon <shargon@gmail.com>
Jim8y pushed a commit that referenced this pull request Aug 3, 2025
#196
because Insert a Opcode Convert
but newarr still get last opcode for a PushNumber
2.add some unittest for it.
Jim8y pushed a commit that referenced this pull request Aug 18, 2025
* this unittest for
#195

* Clean code

* 區分push
pushNumber
pushBoolean
pushDataArray

過去合約中所有的數值是統一的Push
小於等於16的數字 和 byte[0] 和  true false 是Integer
>16的數字 和 byte[n>0] 是 ByteArray

現在在合約中使用數值將直接得到正確的類型

new byte[0] 是ByteArray
true        是Boolean
17          是Integer

* 1.修改了unittest null
因爲現在會返回integer類型了,不再是DataArray

2.修改了TypeConvert
應該現在 new byte[0] 直接是ByteArray類型了

* 1.Use Helper.AsByteArray() to convert to ByteArray
2.Use Helper.AsBigInteger() to convert to BigInteger
3.fix unittest

* fix one small fault.

* add value equal test

* Clean code

* Clean code

* fix format

Co-authored-by: Shargon <shargon@gmail.com>
Jim8y pushed a commit that referenced this pull request Aug 18, 2025
#196

because Insert a Opcode Convert

but newarr still get last opcode for a PushNumber

2.add some unittest for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsByteArray() isn't able to convert BigInteger to ByteArray

4 participants