Skip to content

Commit

Permalink
Fix for assigning values to value types from array indexers
Browse files Browse the repository at this point in the history
- Fix situations where you have a struct array like `Vector3[] vecArray;`and try to assign a value to the struct in that array like `vecArray[0].x = 4f;` now we copy the struct value back into the array if it's a value type.
- Change name return to "UnknownType" on GetUdonTypeName when a name could not be found since UdonBehaviours will fail to return valid values from GetProgramVariable when they have not been enabled yet.
  • Loading branch information
MerlinVR committed Mar 11, 2020
1 parent 068cc49 commit e254c97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Assets/UdonSharp/Editor/UdonSharpExpressionCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ExpressionCaptureScope : System.IDisposable
public SymbolDefinition accessSymbol { get; private set; } = null;

// Used for array indexers
private SymbolDefinition arrayBacktraceSymbol = null;
private SymbolDefinition arrayIndexerIndexSymbol = null;

// Used for resolving generic methods, and eventually types when Udon adds support
Expand Down Expand Up @@ -273,6 +274,8 @@ private MethodInfo GetUdonSetMethodInfo()
// Inserts uasm instructions to get the value stored in the current localSymbol, property, or field
public SymbolDefinition ExecuteGet()
{
arrayBacktraceSymbol = null;

if (captureArchetype == ExpressionCaptureArchetype.LocalSymbol)
return captureLocalSymbol;

Expand Down Expand Up @@ -340,6 +343,8 @@ public SymbolDefinition ExecuteGet()
elementType = accessSymbol.userCsType.GetElementType();
}

arrayBacktraceSymbol = accessSymbol;

outSymbol = visitorContext.topTable.CreateUnnamedSymbol(elementType, SymbolDeclTypeFlags.Internal);

visitorContext.uasmBuilder.AddPush(accessSymbol);
Expand Down Expand Up @@ -429,6 +434,18 @@ public void ExecuteSet(SymbolDefinition value, bool explicitCast = false)
{
throw new System.Exception("Set can only be run on Fields, Properties, and local Symbols");
}

// Copy the result back into the array if it's a value type
if (captureArchetype != ExpressionCaptureArchetype.ArrayIndexer && arrayBacktraceSymbol != null && accessSymbol.symbolCsType.IsValueType)
{
using (ExpressionCaptureScope arraySetScope = new ExpressionCaptureScope(visitorContext, null))
{
arraySetScope.SetToLocalSymbol(arrayBacktraceSymbol);
arraySetScope.HandleArrayIndexerAccess(arrayIndexerIndexSymbol);

arraySetScope.ExecuteSet(accessSymbol);
}
}
}

private string PrettifyTypeName(System.Type type)
Expand Down
2 changes: 1 addition & 1 deletion Assets/UdonSharp/Editor/UdonSharpInternalMethodHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public SymbolDefinition Invoke(SymbolDefinition[] invokeParams)
if (captureFunc == InternalFunc.TypeIDInstance)
resultSetterScope.ExecuteSet(visitorContext.topTable.CreateConstSymbol(typeof(long), 0L));
else
resultSetterScope.ExecuteSet(visitorContext.topTable.CreateConstSymbol(typeof(string), "GraphType"));
resultSetterScope.ExecuteSet(visitorContext.topTable.CreateConstSymbol(typeof(string), "UnknownType"));

visitorContext.uasmBuilder.AddJumpLabel(exitBranchJump);
}
Expand Down

0 comments on commit e254c97

Please sign in to comment.