Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimize generation of static ctors w/o var declarations

  • Loading branch information...
commit 4cd0a4b172c87591f28866c6679664b8ac0f9d05 1 parent 53f4374
@nikhilk authored
View
23 src/Core/Compiler/Generator/TypeGenerator.cs
@@ -321,15 +321,22 @@ internal static class TypeGenerator {
if (classSymbol.StaticConstructor != null) {
ScriptTextWriter writer = generator.Writer;
- writer.Write("(function");
- writer.WriteTrimmed(" () ");
- writer.Write("{");
- writer.WriteNewLine();
- writer.Indent++;
+ SymbolImplementation implementation = classSymbol.StaticConstructor.Implementation;
+ bool requiresFunctionScope = implementation.DeclaresVariables;
+
+ if (requiresFunctionScope) {
+ writer.Write("(function");
+ writer.WriteTrimmed(" () ");
+ writer.Write("{");
+ writer.WriteNewLine();
+ writer.Indent++;
+ }
CodeGenerator.GenerateScript(generator, classSymbol.StaticConstructor);
- writer.Indent--;
- writer.Write("})();");
- writer.WriteSignificantNewLine();
+ if (requiresFunctionScope) {
+ writer.Indent--;
+ writer.Write("})();");
+ writer.WriteSignificantNewLine();
+ }
}
}
View
11 src/Core/Compiler/ScriptModel/Symbols/SymbolImplementation.cs
@@ -21,6 +21,17 @@ internal sealed class SymbolImplementation {
_statements = statements;
}
+ public bool DeclaresVariables {
+ get {
+ foreach (Statement statement in _statements) {
+ if (statement is VariableDeclarationStatement) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
public SymbolScope Scope {
get {
return _scope;
View
8 tests/TestCases/Member/Constructors/Baseline.txt
@@ -38,10 +38,6 @@ test.MyClass2.registerClass('test.MyClass2');
test.MyClass3.registerClass('test.MyClass3');
test.MyClass4.registerClass('test.MyClass4', test.MyClass3);
test.MyClass2.x = null;
-(function () {
- test.MyClass2.x = 'Hello';
-})();
+test.MyClass2.x = 'Hello';
test.MyClass3.c = null;
-(function () {
- test.MyClass3.c = new test.MyClass();
-})();
+test.MyClass3.c = new test.MyClass();
View
4 tests/TestCases/Member/Methods/Baseline.txt
@@ -87,9 +87,7 @@ $.fn.extend = function(x, i) {
test.Test.registerClass('test.Test');
test.X.registerClass('test.X');
-(function () {
- alert('Startup code in FooBar');
-})();
+alert('Startup code in FooBar');
(function () {
var timeStamp = new Date().getMilliseconds();
alert('Startup code in FooBar: ' + timeStamp);
View
31 tests/TestCases/Member/StaticConstructors/Baseline.txt
@@ -15,6 +15,27 @@ test.MyClass = function(d) {
////////////////////////////////////////////////////////////////////////////////
+// test.MyClassEmpty
+
+test.MyClassEmpty = function() {
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+// test.MyClassSimple
+
+test.MyClassSimple = function() {
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+// test.MyClassSimpleMulti
+
+test.MyClassSimpleMulti = function() {
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
// test.MyBehavior
test.MyBehavior = function(element) {
@@ -24,11 +45,15 @@ test.MyBehavior = function(element) {
test.Behavior.registerClass('test.Behavior');
test.MyClass.registerClass('test.MyClass');
+test.MyClassEmpty.registerClass('test.MyClassEmpty');
+test.MyClassSimple.registerClass('test.MyClassSimple');
+test.MyClassSimpleMulti.registerClass('test.MyClassSimpleMulti');
test.MyBehavior.registerClass('test.MyBehavior', test.Behavior);
test.MyClass.instance = null;
-(function () {
- test.MyClass.instance = new test.MyClass(Date.get_now());
-})();
+test.MyClass.instance = new test.MyClass(Date.get_now());
+alert('simple static ctor');
+alert('simple static ctor with multiple statements');
+document.getElementById('foo').innerHTML = '...';
(function () {
var e = document.body;
var b = true;
View
21 tests/TestCases/Member/StaticConstructors/Code.cs
@@ -25,6 +25,27 @@ public sealed class MyClass {
}
}
+ public sealed class MyClassEmpty {
+
+ static MyClassEmpty() {
+ }
+ }
+
+ public sealed class MyClassSimple {
+
+ static MyClassSimple() {
+ Window.Alert("simple static ctor");
+ }
+ }
+
+ public sealed class MyClassSimpleMulti {
+
+ static MyClassSimpleMulti() {
+ Window.Alert("simple static ctor with multiple statements");
+ Document.GetElementById("foo").InnerHTML = "...";
+ }
+ }
+
public sealed class MyBehavior : Behavior {
static MyBehavior() {
Please sign in to comment.
Something went wrong with that request. Please try again.