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

A Problem of Neo.VM.Types.Struct #3518

Open
nan01ab opened this issue Oct 8, 2024 · 6 comments
Open

A Problem of Neo.VM.Types.Struct #3518

nan01ab opened this issue Oct 8, 2024 · 6 comments
Labels
discussion Initial issue state - proposed but not yet accepted

Comments

@nan01ab
Copy link
Contributor

nan01ab commented Oct 8, 2024

There are several builtin types in Neo-VM, and each type has its own characteristics(about Mutability, Reference or Value). For examples:

  • Boolean: Immutable
  • Integer: Immutable
  • ByteString: Immutable
  • InteropInterface: Immutable
  • Pointer: Immutable
  • Buffer: Reference Type and Mutable
  • Array: Reference Type and Mutable

But characteristics of Struct are somewhat ambiguous(or unclear).

According to doc from https://developers.neo.org/docs/n3/Advances/Neo%20VM%20instructions, the semantics of Neo-VM builtin types are affected by some types in C#.
Struct Inherited from Array, a Clone method is added, and Equals is not supported.

From some running logic of the Neo-VM, it seems that Struct should be a value type. For examples:

  • When a Struct add to an Array or a Map(by Append, SetItem), it will be cloned;
  • When a Struct picked from an Array or a Map(by Values), it will be cloned too.
  • When a Struct is deep copied , it's sub Struct will deep copied too.

But from other logic, struct seems to be a reference type, For examples:

  • Dup a Struct does not get a copy of the original Struct.
  • PickItem is similar to Dup operation.

so if a dupped or picked Struct is mutated by SetItem, ClearItems or PopItem, the original Struct is mutated too(because Struct Inherited from Array, swtich (x) { case Array _: ... } or if (x is Array) will be true, a struct can be mutated by SetItem, ClearItems or PopItem).

So, is Struct value type or reference type?
If Struct is value type, why not copied when Dup, PickItem or ...?
If Struct is reference type, why needs to copy when Append, Values or ..?

@Jim8y Jim8y added the discussion Initial issue state - proposed but not yet accepted label Oct 8, 2024
@Jim8y
Copy link
Contributor

Jim8y commented Oct 8, 2024

I think DUP for all types are like ref type..

@Jim8y
Copy link
Contributor

Jim8y commented Oct 8, 2024

@shargon @roman-khimov is struct be of any real use that can not be replaced with Array? isn't array already sufficient?

@roman-khimov
Copy link
Contributor

It's special and mostly follows its primary use cases. Technically it can be emulated by compilers with arrays (although deep copying is not really fun and will burn some GAS), but at the same time this type is being currently used (at least by NeoGo, likely other compilers as well, @gsmachado, @ixje) and I just wouldn't touch it unless there is some huge problem (implicit copies can be problematic, but we hope most of them were solved previously).

@shargon
Copy link
Member

shargon commented Oct 8, 2024

I always thought that we don't need the structs, but, now it's hard to remove

@gsmachado
Copy link
Contributor

I always thought that we don't need the structs, but, now it's hard to remove

Same here: I agree ☝️ .
And yes, it'd be a pain to remove.

@nan01ab
Copy link
Contributor Author

nan01ab commented Oct 11, 2024

May NEO can re-explainDup and PickItem:

  • Dup creates a reference of the stack top;
  • PickItem returns the reference of the item;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Initial issue state - proposed but not yet accepted
Projects
None yet
Development

No branches or pull requests

5 participants