Skip to content

Commit

Permalink
Fix issue with assigning default values to user types breaking the heap
Browse files Browse the repository at this point in the history
- Fix issue with assigning default values on user-defined types using the user defined type to assign to the heap which would put the wrong type into the heap and die in game.
  • Loading branch information
MerlinVR committed Apr 3, 2020
1 parent 24f6262 commit 4092443
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Assets/UdonSharp/Editor/UdonSharpCompilationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class CompilationModule

public HashSet<FieldDeclarationSyntax> fieldsWithInitializers;

public ClassDefinition compiledClassDefinition = null;

public int ErrorCount { get; private set; } = 0;

public CompilationModule(UdonSharpProgramAsset sourceAsset)
Expand Down Expand Up @@ -133,6 +135,8 @@ public int Compile(List<ClassDefinition> classDefinitions)

if (errorCount == 0)
{
compiledClassDefinition = classDefinitions.Where(e => e.userClassType == visitor.visitorContext.behaviourUserType).FirstOrDefault();

Profiler.BeginSample("Build assembly");
string dataBlock = BuildHeapDataBlock();
string codeBlock = visitor.GetCompiledUasm();
Expand Down
16 changes: 14 additions & 2 deletions Assets/UdonSharp/Editor/UdonSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,28 @@ private int RunFieldInitalizers(CompilationModule[] compiledModules)
bool isConst = fieldDeclarationSyntax.Modifiers.Any(t => t.ToString() == "const");
foreach (var variable in fieldDeclarationSyntax.Declaration.Variables)
{
FieldDefinition fieldDef = module.compiledClassDefinition?.fieldDefinitions?.Find(e => (e.fieldSymbol.declarationType == SymbolDeclTypeFlags.Private || e.fieldSymbol.declarationType == SymbolDeclTypeFlags.Public) &&
e.fieldSymbol.symbolOriginalName == variable.Identifier.ToString());

string typeQualifiedName = type.ToString();
if (fieldDef != null)
{
if (fieldDef.fieldSymbol.symbolCsType.Namespace.Length == 0)
typeQualifiedName = fieldDef.fieldSymbol.symbolCsType.Name;
else
typeQualifiedName = fieldDef.fieldSymbol.symbolCsType.Namespace + "." + fieldDef.fieldSymbol.symbolCsType.Name;
}

if (variable.Initializer != null)
{
string name = variable.Identifier.ToString();
if (isConst)
{
_class.Members.Add(new CodeSnippetTypeMember($"const {type} {name} {variable.Initializer};"));
_class.Members.Add(new CodeSnippetTypeMember($"const {typeQualifiedName} {name} {variable.Initializer};"));
}
else
{
method.Statements.Add(new CodeSnippetStatement($"{type} {name} {variable.Initializer};"));
method.Statements.Add(new CodeSnippetStatement($"{typeQualifiedName} {name} {variable.Initializer};"));
}

method.Statements.Add(new CodeSnippetStatement(
Expand Down

0 comments on commit 4092443

Please sign in to comment.