Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Add user property declarations support #85

Merged
merged 14 commits into from
Jun 14, 2021
Merged

Add user property declarations support #85

merged 14 commits into from
Jun 14, 2021

Conversation

mika-f
Copy link
Contributor

@mika-f mika-f commented Jun 3, 2021

The following property declarations will now be compiled by UdonSharp.

public string Property1 => "Some Value";
public string Property2 { get; }
public string Property3 { get; set; }

// This will cause synchronization to be performed on the auto-generated internal field.
[field: UdonSynced]
public string Property4 { get; private set; }

private string _backingField;
public string Property5
{
    get => _backingField;
    set
    {
        if (_backingField != value)
             NotifiyPropertyChanged(nameof(Property5));
        _backingField = value;
    }
}

In this pull request, the compiler internally converts to UdonAssembly as method declarations and method calls, just like in real C#.
For example, public string SomeProperty { get; private set; } will be converted into UdonAssembly as shown below:

.export get_SomeProperty
get_SomeProperty:
    PUSH, __0_const_intnl_SystemUInt32
    PUSH, __0_bf_intnl_kBackingField_String
    PUSH, __0_intnl_returnValSymbol_String
    COPY
    PUSH, __0_intnl_returnTarget_UInt32
    COPY
    JUMP_INDIRECT, __0_intnl_returnTarget_UInt32

set_SomeProperty:
    PUSH, __0_const_intnl_SystemUInt32
    PUSH, __0_mp_value_String
    PUSH, __0_bf_intnl_kBackingField_String
    COPY
    PUSH, __0_intnl_returnTarget_UInt32
    COPY
    JUMP_INDIRECT, __0_intnl_returnTarget_UInt32

Also, property calls (like _instance.SomeProperty) are converted as follows:

PUSH, _instance
PUSH, __1_const_intnl__SystemString
EXTERN, "VRCUdonCommonInterfacesIUdonEventReceiver.__SendCustomEvent__SystemString__SystemVoid"
PUSH, _instance
PUSH, __2_const_intnl__SystemString
PUSH, __1_intnl_SystemObject
EXTERN, "VRCUdonCommonInterfacesIUdonEventReceiver.__GetProgramVariable__SystemString__SystemObject"

The concern is that it is currently a low priority and may reduce the maintainability of the compiler; also, many users can build UdonSharp code without this feature without any problems.

@MerlinVR MerlinVR self-assigned this Jun 3, 2021
- Fix case where property return values are shared which can cause issues with stuff like `MyValue + MyValue` where the getter for `MyValue` modifies the return
- Fix redundant gets on prefix/postfix decrement and increment causing them to misbehave on properties
…izer

- Attempting to serialize anonymous backing fields breaks the serializer, so don't.
- Make setter and getter methods start with an underscore since they cannot be called via the network.
@MerlinVR
Copy link
Owner

This looks good, thank you! I fixed some edge cases that were mostly due to other systems not handling properties correctly.

@MerlinVR MerlinVR merged commit 1bdf1f6 into MerlinVR:master Jun 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants