public override bool TryGetModule(ModuleRequest moduleRequest, out Module result) { if (File.Exists(moduleRequest.AbsolutePath)) { var code = File.ReadAllText(moduleRequest.AbsolutePath); result = new Module(moduleRequest.AbsolutePath, code); return(true); } else { if (moduleRequest.AbsolutePath.EndsWith("Rhino.js")) { var assembly = typeof(ModuleResolver).Assembly; using (var stream = assembly.GetManifestResourceStream("RhinoHornbill.Rhino.js")) using (var reader = new StreamReader(stream)) { var code = reader.ReadToEnd(); var module = new Module(moduleRequest.AbsolutePath, code); var namespaceProvider = new NamespaceProvider("Rhino"); module.Context.DefineVariable("Rhino").Assign(namespaceProvider); result = module; return(true); } } } result = null; return(false); }
public void WhenNamespaceFileDoesNotExistProvidedNamespaceIsReturned() { var namespaceProvider = new NamespaceProvider(TestNamespace, Guid.NewGuid().ToString()); var ns = namespaceProvider.GetNamespace(); Assert.Equal(TestNamespace, ns); }
public void KeysTest() { var ns = new NamespaceProvider { { new NamespaceKey("x"), "c" }, { new NamespaceKey("y"), "d" } }; ns.Remove(new NamespaceKey("y")); AssertEx.CollectionEqual(ns.Keys.ToArray(), new[] { new NamespaceKey("x") }); }
public void GetNamespaceFromNamespaceFile() { var fileNamespace = "file-ns"; File.WriteAllText("namespace.txt", fileNamespace); var ns = new NamespaceProvider(namespacePath: "namespace.txt", ns: TestNamespace).GetNamespace(); Assert.Equal(fileNamespace, ns); }
public void IndexTest() { var ns = new NamespaceProvider(); ns[new NamespaceKey("x")] = "c"; ns.GenerateNamespace = k => k.XmlSchemaNamespace != "z" ? k.XmlSchemaNamespace : null; Assert.Equal(ns[new NamespaceKey("x")], "c"); Assert.Equal(ns[new NamespaceKey("y")], "y"); Assert.Equal(ns[new NamespaceKey("y")], "y"); Assert.Throws<KeyNotFoundException>(() => ns[new NamespaceKey("z")]); }
public void IndexTest() { var ns = new NamespaceProvider(); ns[new NamespaceKey("x")] = "c"; ns.GenerateNamespace = k => k.XmlSchemaNamespace != "z" ? k.XmlSchemaNamespace : null; Assert.Equal("c", ns[new NamespaceKey("x")]); Assert.Equal("y", ns[new NamespaceKey("y")]); Assert.Equal("y", ns[new NamespaceKey("y")]); Assert.Throws <KeyNotFoundException>(() => ns[new NamespaceKey("z")]); }
public static NamespaceProvider ToNamespaceProvider(this IEnumerable<KeyValuePair<NamespaceKey, string>> items, GenerateNamespaceDelegate generate = null) { var result = new NamespaceProvider() { GenerateNamespace = generate, }; foreach (var item in items) result.Add(item.Key, item.Value); return result; }
public void WhenNamespaceFileExistsNamespaceContainedInThatFileIsReturned() { const string fileNamespace = "file-ns"; var namespaceFilePath = Path.GetTempFileName(); File.WriteAllText(namespaceFilePath, fileNamespace); var namespaceProvider = new NamespaceProvider(TestNamespace, namespaceFilePath); var ns = namespaceProvider.GetNamespace(); Assert.Equal(fileNamespace, ns); }
public void ContainsKeyTest() { var ns = new NamespaceProvider { { new NamespaceKey("x"), "c" } }; ns.GenerateNamespace = k => k.XmlSchemaNamespace != "z" ? k.XmlSchemaNamespace : null; AssertEx.CollectionEqual(ns.Values.ToArray(), new[] { "c" }); Assert.True(ns.ContainsKey(new NamespaceKey("x"))); Assert.True(ns.ContainsKey(new NamespaceKey("y"))); Assert.True(ns.ContainsKey(new NamespaceKey("y"))); Assert.False(ns.ContainsKey(new NamespaceKey("z"))); ns.Clear(); Assert.Equal(0, ns.Count); }
public void ContainsKeyTest() { var ns = new NamespaceProvider { { new NamespaceKey("x"), "c" } }; ns.GenerateNamespace = k => k.XmlSchemaNamespace != "z" ? k.XmlSchemaNamespace : null; AssertEx.CollectionEqual(ns.Values.ToArray(), new[] { "c" }); Assert.True(ns.ContainsKey(new NamespaceKey("x"))); Assert.True(ns.ContainsKey(new NamespaceKey("y"))); Assert.True(ns.ContainsKey(new NamespaceKey("y"))); Assert.False(ns.ContainsKey(new NamespaceKey("z"))); ns.Clear(); Assert.Empty(ns); }
public override void Run() { typeof(System.Windows.Forms.Form).ToString(); // This line causes load of assembly "System.Windows.Form.dll" var namespaceProvider = new NamespaceProvider("System.Windows.Forms"); var context = new Context(); context.DefineVariable("forms").Assign(namespaceProvider); // NamespaceProvider inherits JSValue. // It is not necessary to wrap instance of this type // Form with title "Hello, World!" context.Eval(@" var form = new forms.Form(); // new keyword is optional in this case form.Text = 'Hello, World!'; form.ShowDialog(); "); }
public GeneratorConfiguration() { NamespaceProvider = new NamespaceProvider() { GenerateNamespace = key => { var xn = key.XmlSchemaNamespace; var name = string.Join(".", xn.Split('/').Where(p => Regex.IsMatch(p, @"^[A-Za-z]+$") && p != "schema") .Select(n => Generator.ToTitleCase(n, NamingScheme.PascalCase))); if (!string.IsNullOrEmpty(NamespacePrefix)) name = NamespacePrefix + (string.IsNullOrEmpty(name) ? "" : ("." + name)); return name; }, }; NamingScheme = NamingScheme.PascalCase; DataAnnotationMode = DataAnnotationMode.All; GenerateSerializableAttribute = GenerateDesignerCategoryAttribute = true; CollectionType = typeof(Collection<>); }
static void Main(string[] args) { //Parser.DefineCustomCodeFragment(typeof(NiL.JS.Test.SyntaxExtensions.UsingStatement)); Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; Context.DefaultGlobalContext.DebuggerCallback += (sender, e) => Debugger.Break(); Context.DefaultGlobalContext.DefineVariable("$").Assign(JSValue.Wrap( new { sleep = new Action <int>(time => Thread.Sleep(time)), threadId = new Func <int>(() => Thread.CurrentThread.ManagedThreadId), delay = new Func <int, Task>((x) => Task.Delay(x)) })); #if !PORTABLE Context.DefaultGlobalContext.DefineVariable("$nil").Assign(JSValue.Wrap( new { GetCtor = new Func <string, JSValue>(name => JSValue.GetConstructor(NamespaceProvider.GetType(name))) })); #endif //Context.DefaultGlobalContext.DefineVariable("alert").Assign(new ExternalFunction((t, a) => { System.Windows.Forms.MessageBox.Show(a[0].ToString()); return JSValue.Undefined; })); Context.DefaultGlobalContext.DefineVariable("print").Assign(new ExternalFunction((t, a) => { for (var i = 0; i < a.Length; i++) { Console.WriteLine(a[i]); } return(JSValue.Undefined); })); #if PORTABLE Context.DefaultGlobalContext.DefineVariable("console").Assign(JSValue.Wrap(new { log = new Action <Arguments>(arguments => { for (var i = 0; i < arguments.Length; i++) { if (i > 0) { System.Console.Write(" "); } var r = arguments[i].ToString(); System.Console.Write(r); } System.Console.WriteLine(); }) })); #endif int mode = 101 ; switch (mode) { case -5: { staticAnalyzer("modules/ftest.js"); break; } case -3: { runFile("brain-browser.js"); break; } case -1: { //var currentTimeZone = TimeZone.CurrentTimeZone; //var offset = currentTimeZone.GetType().GetField("m_ticksOffset", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); //offset.SetValue(currentTimeZone, new TimeSpan(-8, 0, 0).Ticks); //runFiles("custom/"); sputnikTests(@"tests\sputnik\ch15\15.1\"); sputnikTests(@"tests\sputnik\ch15\15.2\"); sputnikTests(@"tests\sputnik\ch15\15.3\"); sputnikTests(@"tests\sputnik\ch15\15.4\"); sputnikTests(@"tests\sputnik\ch15\15.5\"); // with some errors due double.toString() (8) sputnikTests(@"tests\sputnik\ch15\15.6\"); sputnikTests(@"tests\sputnik\ch15\15.7\"); sputnikTests(@"tests\sputnik\ch15\15.8\"); // with some errors due accuracy comparison sputnikTests(@"tests\sputnik\ch15\15.9\"); sputnikTests(@"tests\sputnik\ch15\15.10\"); // with 17 asserts sputnikTests(@"tests\sputnik\ch15\15.11\"); sputnikTests(@"tests\sputnik\ch15\15.12\"); break; } case 0: { // В текущем процессе часовой пояс будет -8:00:00. // Создатели sputnik'a не удосужились в своих тестах учитывать временную зону // и от всех требуют пребывания в указаном часовом поясе. var currentTimeZone = TimeZone.CurrentTimeZone; var offset = currentTimeZone.GetType().GetField("m_ticksOffset", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); offset.SetValue(currentTimeZone, new TimeSpan(-8, 0, 0).Ticks); //runFiles("custom/"); sputnikTests(); break; } case 1: { // runFiles("custom/"); webkitTests(); break; } case 2: { //var currentTimeZone = TimeZone.CurrentTimeZone; //var offset = currentTimeZone.GetType().GetField("m_ticksOffset", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); //offset.SetValue(currentTimeZone, new TimeSpan(-8, 0, 0).Ticks); runFile(@"modules/ftest.js"); break; } case 3: { testEx(); break; } case 4: { compileTest(); break; } case 5: { runFile("uglifyjs.js"); runFile("coffee-script.js"); runFile("linq.js"); runFile("arraytests.js"); runFile("d3.min.js"); runFile("knockout-3.3.0.js"); runFile("aes.js"); runFile("handlebars-v2.0.0.js"); break; } case 6: { runFile("pbkdf.js"); break; } case 8: { runFile("acorn_interpreter.js"); break; } case 9: { runFile("d3.min.js"); break; } case 10: { runFile("knockout-3.3.0.js"); break; } case 11: { runFile("sunspider-regexp-dna.js"); break; } case 12: { runTestFile(@"tests\sputnik\ch15\15.1\15.1.3\15.1.3.2\S15.1.3.2_A2.5_T1.js"); break; } case 151: { // Global sputnikTests(@"tests\sputnik\ch15\15.1\"); break; } case 152: { // Object sputnikTests(@"tests\sputnik\ch15\15.2\"); break; } case 153: { // Function sputnikTests(@"tests\sputnik\ch15\15.3\"); break; } case 154: { // Array sputnikTests(@"tests\sputnik\ch15\15.4\"); break; } case 155: { // String sputnikTests(@"tests\sputnik\ch15\15.5\"); break; } case 156: { // Boolean sputnikTests(@"tests\sputnik\ch15\15.6\"); break; } case 157: { // Number sputnikTests(@"tests\sputnik\ch15\15.7\"); break; } case 158: { // Math sputnikTests(@"tests\sputnik\ch15\15.8\"); break; } case 159: { //var currentTimeZone = TimeZone.CurrentTimeZone; //var offset = currentTimeZone.GetType().GetField("m_ticksOffset", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); //offset.SetValue(currentTimeZone, new TimeSpan(-8, 0, 0).Ticks); // Date sputnikTests(@"tests\sputnik\ch15\15.9\"); break; } case 1510: { // RegExp sputnikTests(@"tests\sputnik\ch15\15.10\"); break; } case 1511: { // Error sputnikTests(@"tests\sputnik\ch15\15.11\"); break; } case 1512: { // JSON sputnikTests(@"tests\sputnik\ch15\15.12\"); break; } case 100: { Context.DefaultGlobalContext.DefineVariable("load").Assign(new ExternalFunction((_th, e) => { using (var f = new FileStream("v8\\" + e["0"], FileMode.Open, FileAccess.Read)) { using (var sr = new StreamReader(f)) Context.CurrentContext.Eval(sr.ReadToEnd(), true); } return(null); })); runFile(@"v8\run.js"); break; } case 101: { sunspider(); break; } case 102: { dromaeoTests(); break; } case 103: { kraken(); break; } case 104: { cryptojs(); break; } } GC.Collect(0); GC.Collect(1); GC.Collect(2); GC.WaitForPendingFinalizers(); Console.WriteLine("GC.GetTotalMemory: " + GC.GetTotalMemory(true)); Console.WriteLine("GC.CollectionCount: " + GC.CollectionCount(0)); Console.WriteLine("GC.CollectionCount: " + GC.CollectionCount(1)); Console.WriteLine("GC.CollectionCount: " + GC.CollectionCount(2)); Console.WriteLine("GC.MaxGeneration: " + GC.MaxGeneration); //if (System.Windows.Forms.Application.OpenForms.Count != 0) //{ // while (System.Windows.Forms.Application.OpenForms.Count != 0) // { // System.Threading.Thread.Sleep(1); // System.Windows.Forms.Application.DoEvents(); // } //} //else if (Debugger.IsAttached) // Console.ReadKey(); }
public void GetNamespaceFromConstructorArguments() { var ns = new NamespaceProvider(namespacePath: Guid.NewGuid().ToString(), ns: TestNamespace).GetNamespace(); Assert.Equal(TestNamespace, ns); }