Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Initialize non-builtin structs with a default value (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-kallen committed Apr 15, 2015
1 parent 60c1e16 commit e109221
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public void VariableDeclarationsWithInitializerWork() {
");
}

[Test]
public void VariableDeclarationsWithoutInitializerWorkStruct() {
AssertCorrect(
@"struct S {}
public void M() {
S s1 = default(S), s2 = default(S);
// BEGIN
S i, j;
// END
}",
@" var $i = $Default({def_S}), $j = $Default({def_S});
", mutableValueTypes: true);
}


[Test]
public void VariableDeclarationsWithInitializerWorkStruct() {
AssertCorrect(
Expand Down Expand Up @@ -71,7 +86,7 @@ public void M() {
// END
OtherMethod(out i, out j);
}",
@" var $i = { $: $Clone($s, {to_S}) }, $j = {};
@" var $i = { $: $Clone($s, {to_S}) }, $j = { $: $Default({def_S}) };
", mutableValueTypes: true);
}

Expand Down
20 changes: 15 additions & 5 deletions Compiler/Saltarelle.Compiler/Compiler/StatementCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private void HandleLocalDeclarations(IEnumerable<VariableDeclaratorSyntax> varia
var declarations = new List<JsVariableDeclaration>();
foreach (var d in variables) {
SetLocation(d.GetLocation());
var variable = _semanticModel.GetDeclaredSymbol(d);
var variable = (ILocalSymbol)_semanticModel.GetDeclaredSymbol(d);
var data = _variables[variable];
JsExpression jsInitializer;
if (d.Initializer != null) {
Expand All @@ -456,13 +456,19 @@ private void HandleLocalDeclarations(IEnumerable<VariableDeclaratorSyntax> varia
_result.Add(s);
}
}
jsInitializer = (data.UseByRefSemantics ? JsExpression.ObjectLiteral(new[] { new JsObjectLiteralProperty("$", exprCompileResult.Expression) }) : exprCompileResult.Expression);
jsInitializer = exprCompileResult.Expression;
}
else {
if (data.UseByRefSemantics)
jsInitializer = JsExpression.ObjectLiteral();
else
if (DoesTypeneedDefaultInitialization(variable)) {
jsInitializer = _runtimeLibrary.Default(variable.Type, this);
}
else {
jsInitializer = null;
}
}

if (data.UseByRefSemantics) {
jsInitializer = (jsInitializer != null ? JsExpression.ObjectLiteral(new JsObjectLiteralProperty("$", jsInitializer)) : JsExpression.ObjectLiteral());
}
declarations.Add(JsStatement.Declaration(data.Name, jsInitializer));
}
Expand All @@ -471,6 +477,10 @@ private void HandleLocalDeclarations(IEnumerable<VariableDeclaratorSyntax> varia
_result.Add(JsStatement.Var(declarations));
}

private static bool DoesTypeneedDefaultInitialization(ILocalSymbol variable) {
return variable.Type.TypeKind == TypeKind.Struct && variable.Type.OriginalDefinition.SpecialType == SpecialType.None;
}

public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) {
HandleLocalDeclarations(node.Declaration.Variables);
}
Expand Down

0 comments on commit e109221

Please sign in to comment.