public string GenerateJavascript( string[] args, out string generatedJavascript, out long elapsedTranslation, Func <Configuration> makeConfiguration = null ) { var tempFilename = Path.GetTempFileName(); Configuration configuration; if (makeConfiguration != null) { configuration = makeConfiguration(); } else { configuration = MakeDefaultConfiguration(); } if (StubbedAssemblies != null) { configuration.Assemblies.Stubbed.AddRange(StubbedAssemblies); } var translator = new JSIL.AssemblyTranslator(configuration, TypeInfo, null, AssemblyCache); string translatedJs; var translationStarted = DateTime.UtcNow.Ticks; var assemblyPath = Util.GetPathOfAssembly(Assembly); translatedJs = null; try { var result = translator.Translate( assemblyPath, TypeInfo == null ); AssemblyTranslator.GenerateManifest(translator.Manifest, assemblyPath, result); translatedJs = result.WriteToString(); // If we're using a preconstructed type information provider, we need to remove the type information // from the assembly we just translated if (TypeInfo != null) { Assert.AreEqual(1, result.Assemblies.Count); TypeInfo.Remove(result.Assemblies.ToArray()); } // If we're using a preconstructed assembly cache, make sure the test case assembly didn't get into // the cache, since that would leak memory. if (AssemblyCache != null) { AssemblyCache.TryRemove(Assembly.FullName); } } finally { translator.Dispose(); } elapsedTranslation = DateTime.UtcNow.Ticks - translationStarted; var testMethod = GetTestMethod(); var declaringType = JSIL.Internal.Util.EscapeIdentifier( testMethod.DeclaringType.FullName, Internal.EscapingMode.TypeIdentifier ); string argsJson; if (MainAcceptsArguments.Value) { var jsonSerializer = new DataContractJsonSerializer(typeof(string[])); using (var ms2 = new MemoryStream()) { jsonSerializer.WriteObject(ms2, args); argsJson = Encoding.UTF8.GetString(ms2.GetBuffer(), 0, (int)ms2.Length); } } else { if ((args != null) && (args.Length > 0)) { throw new ArgumentException("Test case does not accept arguments"); } argsJson = ""; } var prefixJs = @"JSIL.SuppressInterfaceWarnings = true; "; var invocationJs = String.Format( @"if (typeof (timeout) !== 'function') timeout = function () {{}};" + @"if (typeof (elapsed) !== 'function') {{ if (typeof (Date) === 'object') elapsed = Date.now; else elapsed = function () {{ return 0; }} }}" + @"timeout({0});" + @"JSIL.Initialize(); var started = elapsed(); " + @"JSIL.GetAssembly({1}).{2}.{3}({4}); " + @"var ended = elapsed(); print('// elapsed: ' + (ended - started));", JavascriptExecutionTimeout, Util.EscapeString(testMethod.Module.Assembly.FullName), declaringType, Util.EscapeIdentifier(testMethod.Name), argsJson ); generatedJavascript = translatedJs; File.WriteAllText(tempFilename, prefixJs + Environment.NewLine + translatedJs + Environment.NewLine + invocationJs); var jsFile = OutputPath; if (File.Exists(jsFile)) { File.Delete(jsFile); } File.Copy(tempFilename, jsFile); File.Delete(tempFilename); return(OutputPath); }
public TOutput Translate <TOutput> ( Func <TranslationResult, TOutput> processResult, Func <Configuration> makeConfiguration = null, Action <Exception> onTranslationFailure = null, Action <AssemblyTranslator> initializeTranslator = null, bool?scanForProxies = null, IEnumerable <IAnalyzer> analyzers = null ) { Configuration configuration; if (makeConfiguration != null) { configuration = makeConfiguration(); } else { configuration = MakeDefaultConfiguration(); } configuration = ApplyMetacomments(configuration); if (StubbedAssemblies != null) { configuration.Assemblies.Stubbed.AddRange(StubbedAssemblies); } TOutput result; using (var translator = new JSIL.AssemblyTranslator( configuration, TypeInfo, null, assemblyDataResolver: new AssemblyDataResolver(configuration, AssemblyCache), analyzers: analyzers )) { if (initializeTranslator != null) { initializeTranslator(translator); } var assemblyPath = AssemblyUtility.AssemblyLocation; TranslationResult translationResult = null; try { translationResult = translator.Translate( assemblyPath, scanForProxies == null ? TypeInfo == null : (bool)scanForProxies ); AssemblyTranslator.GenerateManifest(translator.Manifest, assemblyPath, translationResult); result = processResult(translationResult); } finally { if (onTranslationFailure != null) { foreach (var failure in translator.Failures) { onTranslationFailure(failure); } } // If we're using a preconstructed type information provider, we need to remove the type information // from the assembly we just translated if ((TypeInfo != null) && (translationResult != null)) { Assert.AreEqual(1, translationResult.Assemblies.Count); TypeInfo.Remove(translationResult.Assemblies.ToArray()); } // If we're using a preconstructed assembly cache, make sure the test case assembly didn't get into // the cache, since that would leak memory. if (AssemblyCache != null) { AssemblyCache.TryRemove(AssemblyUtility.AssemblyFullName); } } } return(result); }
public TOutput Translate <TOutput> ( Func <TranslationResult, TOutput> processResult, Func <Configuration> makeConfiguration = null, Action <Exception> onTranslationFailure = null ) { Configuration configuration; if (makeConfiguration != null) { configuration = makeConfiguration(); } else { configuration = MakeDefaultConfiguration(); } configuration = ApplyMetacomments(configuration); if (StubbedAssemblies != null) { configuration.Assemblies.Stubbed.AddRange(StubbedAssemblies); } TOutput result; using (var translator = new JSIL.AssemblyTranslator(configuration, TypeInfo, null, AssemblyCache)) { var assemblyPath = Util.GetPathOfAssembly(Assembly); TranslationResult translationResult = null; try { translationResult = translator.Translate( assemblyPath, TypeInfo == null ); AssemblyTranslator.GenerateManifest(translator.Manifest, assemblyPath, translationResult); result = processResult(translationResult); } finally { if (onTranslationFailure != null) { foreach (var failure in translator.Failures) { onTranslationFailure(failure); } } // If we're using a preconstructed type information provider, we need to remove the type information // from the assembly we just translated if ((TypeInfo != null) && (translationResult != null)) { Assert.AreEqual(1, translationResult.Assemblies.Count); TypeInfo.Remove(translationResult.Assemblies.ToArray()); } // If we're using a preconstructed assembly cache, make sure the test case assembly didn't get into // the cache, since that would leak memory. if (AssemblyCache != null) { AssemblyCache.TryRemove(Assembly.FullName); } } } return(result); }