Ejemplo n.º 1
0
		protected string Process(string source, string[] typeNames = null, string entryPoint = null, IEnumerable<IAssemblyResource> resources = null, IErrorReporter errorReporter = null) {
			bool assertNoErrors = errorReporter == null;
			errorReporter = errorReporter ?? new MockErrorReporter(true);
			var sourceFile = new MockSourceFile("file.cs", source);
			var n = new Namer();
			var references = new[] { Files.Mscorlib };
			var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null, resources);
			var md = new MetadataImporter(errorReporter, compilation.Compilation, new CompilerOptions());
			var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n);
			var l = new MockLinker();
			md.Prepare(compilation.Compilation.GetAllTypeDefinitions());
			var compiler = new Compiler(md, n, rtl, errorReporter);
			var compiledTypes = compiler.Compile(compilation).ToList();
			var obj = new OOPEmulator(compilation.Compilation, md, rtl, n, l, errorReporter);
			IMethod ep;
			if (entryPoint != null) {
				var type = compiledTypes.Single(c => c.CSharpTypeDefinition.FullName == entryPoint.Substring(0, entryPoint.IndexOf('.')));
				ep = type.CSharpTypeDefinition.Methods.Single(m => m.FullName == entryPoint);
			}
			else {
				ep = null;
			}
			var rewritten = obj.Process(compiledTypes.Where(t => typeNames == null || typeNames.Contains(t.CSharpTypeDefinition.FullName)), ep);

			if (assertNoErrors)
				Assert.That(((MockErrorReporter)errorReporter).AllMessages, Is.Empty, "Should not have errors");

			return string.Join("", rewritten.Select(s => OutputFormatter.Format(s, allowIntermediates: true)));
		}
		public static Tuple<string, MockErrorReporter> Compile(string source, bool expectErrors = false) {
			var sourceFile = new MockSourceFile("file.cs", source);
			var er = new MockErrorReporter(!expectErrors);
			var n = new Namer();
			var references = new[] { Files.Mscorlib };
			var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null);;
			var s = new AttributeStore(compilation.Compilation, er);
			var md = new MetadataImporter(er, compilation.Compilation, s, new CompilerOptions());
			var rtl = new RuntimeLibrary(md, er, compilation.Compilation, n, s);
			var l = new MockLinker();
			md.Prepare(compilation.Compilation.GetAllTypeDefinitions());
			var compiler = new Compiler(md, n, rtl, er);

			var compiledTypes = compiler.Compile(compilation).ToList();

			if (expectErrors) {
				Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors");
				return Tuple.Create((string)null, er);
			}

			Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors");

			var js = new OOPEmulatorInvoker(new OOPEmulator(compilation.Compilation, md, rtl, n, l, s, er), md, er).Process(compiledTypes, null);
			js = new Linker(md, n, s, compilation.Compilation).Process(js);

			string script = OutputFormatter.Format(js, allowIntermediates: false);

			return Tuple.Create(script, er);
		}
		public void ReturnsDollarTPlusAnIncrementingDigitWhenNoDesiredNameWasSpecified() {
			var n = new Namer();
			Assert.That(n.GetVariableName(null, new HashSet<string> { }), Is.EqualTo("$t1"));
			Assert.That(n.GetVariableName(null, new HashSet<string> { "$t1" }), Is.EqualTo("$t2"));
			Assert.That(n.GetVariableName(null, new HashSet<string> { "$t1", "$t2" }), Is.EqualTo("$t3"));
			Assert.That(n.GetVariableName(null, new HashSet<string> { "$t1", "$t2", "$t3" }), Is.EqualTo("$t4"));
		}
		protected OOPEmulator CreateEmulator(ICompilation compilation, IErrorReporter errorReporter = null) {
			var n = new Namer();
			errorReporter = errorReporter ?? new MockErrorReporter();
			var md = new MetadataImporter(errorReporter, compilation, new CompilerOptions());
			md.Prepare(compilation.GetAllTypeDefinitions());
			var rtl = new RuntimeLibrary(md, errorReporter, compilation, n);
			return new OOPEmulator(compilation, md, rtl, n, new MockLinker(), errorReporter);
		}
		protected Tuple<ICompilation, List<JsType>> Compile(string source, IEnumerable<IAssemblyResource> resources = null) {
			var errorReporter = new MockErrorReporter(true);
			var sourceFile = new MockSourceFile("file.cs", source);
			var n = new Namer();
			var references = new[] { Files.Mscorlib };
			var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null, resources);
			var md = new MetadataImporter(errorReporter, compilation.Compilation, new CompilerOptions());
			var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n);
			md.Prepare(compilation.Compilation.GetAllTypeDefinitions());
			var compiler = new Compiler(md, n, rtl, errorReporter);
			var compiledTypes = compiler.Compile(compilation).ToList();

			return Tuple.Create(compilation.Compilation, compiledTypes);

		}
		protected Tuple<ICompilation, IOOPEmulator, List<JsType>> Compile(string source, IEnumerable<IAssemblyResource> resources = null, IErrorReporter errorReporter = null) {
			errorReporter = errorReporter ?? new MockErrorReporter(true);
			var sourceFile = new MockSourceFile("file.cs", source);
			var n = new Namer();
			var references = new[] { Files.Mscorlib };
			var compilation = PreparedCompilation.CreateCompilation("x", new[] { sourceFile }, references, null, resources);
			var s = new AttributeStore(compilation.Compilation, errorReporter);
			RunAutomaticMetadataAttributeAppliers(s, compilation.Compilation);
			s.RunAttributeCode();
			var md = new MetadataImporter(errorReporter, compilation.Compilation, s, new CompilerOptions());
			var rtl = new RuntimeLibrary(md, errorReporter, compilation.Compilation, n, s);
			md.Prepare(compilation.Compilation.GetAllTypeDefinitions());
			var compiler = new Compiler(md, n, rtl, errorReporter);
			var compiledTypes = compiler.Compile(compilation).ToList();

			return Tuple.Create(compilation.Compilation, (IOOPEmulator)new OOPEmulator(compilation.Compilation, md, rtl, n, new MockLinker(), s, errorReporter), compiledTypes);

		}
		public void ReturnsTheVariableNameWhenPossible() {
			var n = new Namer();
			Assert.That(n.GetVariableName("variable", new HashSet<string>()), Is.EqualTo("variable"));
		}
		public void ReturnsAValidJavaScriptIdentifierWithNameStartingWithDollarForTransparentIdentifers() {
			var n = new Namer();
			Assert.That(n.GetVariableName("<>identifier", new HashSet<string>()), Is.EqualTo("$identifier"));
		}
		public void DoesNotUseTheNameSS() {
			var n = new Namer();
			Assert.That(n.GetVariableName("ss", new HashSet<string>()), Is.EqualTo("ss1"));
		}
		public void DoesNotReturnKeywords() {
			var n = new Namer();
			Assert.That(n.GetVariableName("finally", new HashSet<string>()), Is.EqualTo("finally1"));
		}
		public void ReturnsTheVariableNameSuffixedWithAnIncrementingDigitWhenTheNameIsAlreadyUsed() {
			var n = new Namer();
			Assert.That(n.GetVariableName("variable", new HashSet<string> { "variable" }), Is.EqualTo("variable1"));
			Assert.That(n.GetVariableName("variable", new HashSet<string> { "variable", "variable1" }), Is.EqualTo("variable2"));
			Assert.That(n.GetVariableName("variable", new HashSet<string> { "variable", "variable1", "variable2" }), Is.EqualTo("variable3"));
		}
		public void ReturnsUniqueLoopLabels() {
			var n = new Namer();
			Assert.That(n.GetStateMachineLoopLabel(new HashSet<string>()), Is.EqualTo("$sm1"));
			Assert.That(n.GetStateMachineLoopLabel(new HashSet<string> { "$sm1" }), Is.EqualTo("$sm2"));
			Assert.That(n.GetStateMachineLoopLabel(new HashSet<string> { "$sm1", "$sm2" }), Is.EqualTo("$sm3"));
		}
Ejemplo n.º 13
0
        private Tuple<JsClass, MockErrorReporter> Compile(string source, bool expectErrors = false)
        {
            var sourceFile = new MockSourceFile("file.cs", source);
            var er = new MockErrorReporter(!expectErrors);
            var n = new Namer();
            var references = new[] { Mscorlib, QUnit };
            var compilation = PreparedCompilation.CreateCompilation("Test", new[] { sourceFile }, references, null);
            var s = new AttributeStore(compilation.Compilation, er);
            s.RunAttributeCode();
            var md = new MetadataImporter(er, compilation.Compilation, s, new CompilerOptions());
            var rtl = new RuntimeLibrary(md, er, compilation.Compilation, n, s);
            md.Prepare(compilation.Compilation.GetAllTypeDefinitions());
            var compiler = new Compiler(md, n, rtl, er);

            var result = compiler.Compile(compilation).ToList();
            Assert.That(result, Has.Count.EqualTo(1), "Should compile exactly one type");
            Assert.That(er.AllMessages, Is.Empty, "Compile should not generate errors");

            result = new TestRewriter(er, rtl, s).Rewrite(result).ToList();
            Assert.That(result, Has.Count.EqualTo(1), "Should have one type after rewrite");
            Assert.That(result[0], Is.InstanceOf<JsClass>(), "Compiled type should be a class after rewrite");

            if (expectErrors) {
                Assert.That(er.AllMessages, Is.Not.Empty);
            }
            else {
                Assert.That(er.AllMessages, Is.Empty);
            }

            return Tuple.Create((JsClass)result[0], er);
        }