Skip to content

Commit

Permalink
Added auto clearing of maps/lists in contract constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Relfos committed Oct 15, 2021
1 parent 6208768 commit 9d58947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
18 changes: 17 additions & 1 deletion Compiler/Declaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public void GenerateCode(CodeGenerator output)
// we need to fetch the global variables from storage and allocate registers for them
foreach (var variable in this.scope.Parent.Variables.Values)
{
// validate NFT implicit variables
if (variable.Storage != VarStorage.Global)
{
if (variable.Storage == VarStorage.NFT && this.scope.Module.Kind != ModuleKind.NFT)
Expand All @@ -647,12 +648,26 @@ public void GenerateCode(CodeGenerator output)
continue;
}

// for maps/lists/sets we clear them here. Should not be necessary, but just in case
// this is REQUIRED right now until chain implementation of Contract.Kill is improved
if (isConstructor && variable.Type.IsStorageBound)
{
var storageClassName = variable.Type.ToString().Replace("Storage_", "");

output.AppendLine(this, $"// clearing {variable.Name} storage");
output.AppendLine(this, $"LOAD r0 \"{variable.Name}\"");
output.AppendLine(this, $"PUSH r0");
output.AppendLine(this, "LOAD r0 \"" + storageClassName + ".Clear\"");
output.AppendLine(this, $"EXTCALL r0");
}

if (!this.IsNodeUsed(variable))
{
variable.Register = null;
continue;
}

// generate code for loading globals from contract storage via Data.Get extcall
if (tempReg1 == null && !isConstructor)
{
tempReg1 = Compiler.Instance.AllocRegister(output, this, "dataGet");
Expand All @@ -667,7 +682,8 @@ public void GenerateCode(CodeGenerator output)

if (isConstructor)
{
continue; // in a constructor we don't need to read the vars from storage as they dont exist yet
// don't do anything more, since in a constructor we don't need to read the vars from storage as they dont exist yet
continue;
}

//var fieldKey = SmartContract.GetKeyForField(this.scope.Root.Name, variable.Name, false);
Expand Down
5 changes: 0 additions & 5 deletions Compiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ static void Main(string[] args)

foreach (var module in modules)
{
/*if (module.Hidden)
{
continue;
}*/

ExportModule(module);

foreach (var subModule in module.SubModules)
Expand Down

0 comments on commit 9d58947

Please sign in to comment.