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 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(new[] { sourceFile }, references, null);; var md = new MetadataImporter(er, compilation.Compilation, new CompilerOptions()); var rtl = new CoreLib.Plugin.RuntimeLibrary(md, er, compilation.Compilation, n); md.Prepare(compilation.Compilation.GetAllTypeDefinitions()); var compiler = new Saltarelle.Compiler.Compiler.Compiler(md, n, rtl, er); var compiledTypes = compiler.Compile(compilation); 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 OOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, null); js = new Linker(md, n, compilation.Compilation).Process(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); return(Tuple.Create(script, er)); }
protected void Compile(IEnumerable <string> sources, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action <IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList <string> defineConstants = null, bool allowUserDefinedStructs = true, IEnumerable <IAssemblyReference> references = null) { var sourceFiles = sources.Select((s, i) => new MockSourceFile("File" + i + ".cs", s)).ToList(); bool defaultErrorHandling = false; if (errorReporter == null) { defaultErrorHandling = true; errorReporter = new MockErrorReporter(true); } var compiler = new Saltarelle.Compiler.Compiler.Compiler(metadataImporter ?? new MockMetadataImporter(), namer ?? new MockNamer(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter); compiler.AllowUserDefinedStructs = allowUserDefinedStructs; if (methodCompiled != null) { compiler.MethodCompiled += methodCompiled; } var c = PreparedCompilation.CreateCompilation(sourceFiles, references ?? new[] { Common.Mscorlib }, defineConstants); CompiledTypes = compiler.Compile(c).AsReadOnly(); if (defaultErrorHandling) { ((MockErrorReporter)errorReporter).AllMessages.Should().BeEmpty("Compile should not generate errors"); } }
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 void Compile(IEnumerable<string> sources, IMetadataImporter metadataImporter = null, INamer namer = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action<IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList<string> defineConstants = null, IEnumerable<IAssemblyReference> references = null) { var sourceFiles = sources.Select((s, i) => new MockSourceFile("File" + i + ".cs", s)).ToList(); bool defaultErrorHandling = false; if (errorReporter == null) { defaultErrorHandling = true; errorReporter = new MockErrorReporter(true); } var compiler = new Saltarelle.Compiler.Compiler.Compiler(metadataImporter ?? new MockMetadataImporter(), namer ?? new MockNamer(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter); if (methodCompiled != null) compiler.MethodCompiled += methodCompiled; var c = PreparedCompilation.CreateCompilation("x", sourceFiles, references ?? new[] { Common.Mscorlib }, defineConstants); CompiledTypes = compiler.Compile(c).AsReadOnly(); if (defaultErrorHandling) { ((MockErrorReporter)errorReporter).AllMessages.Should().BeEmpty("Compile should not generate errors"); } }
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); }
protected void Compile(IEnumerable<string> sources, INamingConventionResolver namingConvention = null, IRuntimeLibrary runtimeLibrary = null, IErrorReporter errorReporter = null, Action<IMethod, JsFunctionDefinitionExpression, MethodCompiler> methodCompiled = null, IList<string> defineConstants = null, bool allowUnsupportedConstructs = true, bool referenceSystemCore = false) { var sourceFiles = sources.Select((s, i) => new MockSourceFile("File" + i + ".cs", s)).ToList(); bool defaultErrorHandling = false; if (errorReporter == null) { defaultErrorHandling = true; errorReporter = new MockErrorReporter(true); } var compiler = new Saltarelle.Compiler.Compiler.Compiler(namingConvention ?? new MockNamingConventionResolver(), runtimeLibrary ?? new MockRuntimeLibrary(), errorReporter) { AllowUnsupportedConstructs = allowUnsupportedConstructs }; if (methodCompiled != null) compiler.MethodCompiled += methodCompiled; var references = referenceSystemCore ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; var c = compiler.CreateCompilation(sourceFiles, references, defineConstants); CompiledTypes = compiler.Compile(c).AsReadOnly(); if (defaultErrorHandling) { ((MockErrorReporter)errorReporter).AllMessagesText.Should().BeEmpty("Compile should not generate errors"); } }
private Tuple<string, ICompilation, INamingConventionResolver> Compile(string source, bool includeLinq = false) { var sourceFile = new MockSourceFile("file.cs", source); var nc = new MetadataImporter.ScriptSharpMetadataImporter(false); var er = new MockErrorReporter(true); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(nc, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, nc.GetTypeSemantics(t).Name); }); var compiler = new Saltarelle.Compiler.Compiler.Compiler(nc, rtl, er); er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); var js = new OOPEmulator.ScriptSharpOOPEmulator(nc, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) Console.WriteLine(script); return Tuple.Create(script, compilation.Compilation, (INamingConventionResolver)nc); }
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); }
public bool Compile(CompilerOptions options, ErrorReporterWrapper er) { string intermediateAssemblyFile = Path.GetTempFileName(), intermediateDocFile = Path.GetTempFileName(); try { // Compile the assembly var settings = MapSettings(options, intermediateAssemblyFile, intermediateDocFile, er); if (er.HasErrors) return false; var ctx = new CompilerContext(settings, new ConvertingReportPrinter(er)); var d = new Mono.CSharp.Driver(ctx); d.Compile(); if (er.HasErrors) return false; // Compile the script var nc = new MetadataImporter.ScriptSharpMetadataImporter(options.MinimizeScript); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(nc, tr => { var t = tr.Resolve(compilation.Compilation).GetDefinition(); return new JsTypeReferenceExpression(t.ParentAssembly, nc.GetTypeSemantics(t).Name); }); var compiler = new Saltarelle.Compiler.Compiler.Compiler(nc, rtl, er); var references = LoadReferences(ctx.Settings.AssemblyReferences, er); if (references == null) return false; compilation = compiler.CreateCompilation(options.SourceFiles.Select(f => new SimpleSourceFile(f)), references, options.DefineConstants); var compiledTypes = compiler.Compile(compilation); var js = new OOPEmulator.ScriptSharpOOPEmulator(nc, er).Rewrite(compiledTypes, compilation.Compilation); js = new GlobalNamespaceReferenceImporter().ImportReferences(js); if (er.HasErrors) return false; string outputAssemblyPath = !string.IsNullOrEmpty(options.OutputAssemblyPath) ? options.OutputAssemblyPath : Path.ChangeExtension(options.SourceFiles[0], ".dll"); string outputScriptPath = !string.IsNullOrEmpty(options.OutputScriptPath) ? options.OutputScriptPath : Path.ChangeExtension(options.SourceFiles[0], ".js"); try { File.Copy(intermediateAssemblyFile, outputAssemblyPath, true); } catch (IOException ex) { er.Message(7950, null, TextLocation.Empty, ex.Message); return false; } if (!string.IsNullOrEmpty(options.DocumentationFile)) { try { File.Copy(intermediateDocFile, options.DocumentationFile, true); } catch (IOException ex) { er.Message(7952, null, TextLocation.Empty, ex.Message); return false; } } string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); try { File.WriteAllText(outputScriptPath, script); } catch (IOException ex) { er.Message(7951, null, TextLocation.Empty, ex.Message); return false; } return true; } catch (Exception ex) { er.InternalError(ex.ToString(), DomRegion.Empty); return false; } finally { try { File.Delete(intermediateAssemblyFile); } catch {} try { File.Delete(intermediateDocFile); } catch {} } }