Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type Convert #196

Merged
merged 12 commits into from
Mar 11, 2020
Merged

Add Type Convert #196

merged 12 commits into from
Mar 11, 2020

Conversation

lightszero
Copy link
Member

fixed #195



[OpCode(SmartContract.Framework.OpCode.CONVERT, "0x21")]
public extern static BigInteger ConvertToInteger(byte[] from);
Copy link
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
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
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
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
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
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
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
Member Author

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

@Tommo-L
Copy link
Contributor

Tommo-L commented Mar 10, 2020

I'll review it today.

src/Neo.Compiler.MSIL/MSIL/Conv_Common.cs Show resolved Hide resolved

/// <summary>
/// Converts BigInteger to byte[]. No guarantees are assumed regarding BigInteger working range.
/// Examples: 10 -> [0x0a]; 10 -> [0x0a00]; -128 -> [0x80]; -128 -> [0x80ff]; 0 -> []; 0 -> [0x00]; 255 -> [0xff00]
/// </summary>
[Script]
[OpCode(OpCode.CONVERT, StackItemType_ByteArray)]
public extern static byte[] AsByteArray(this BigInteger source);
Copy link
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
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
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>
/// Converts byte to byte[] considering the byte as a BigInteger (0x00 at the end)
/// </summary>
[Script]
[OpCode(OpCode.CONVERT, StackItemType_ByteArray)]
public extern static byte[] AsByteArray(this byte source);
Copy link
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.
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