diff --git a/src/AngleSharp.Scripting.JavaScript.Tests/ComponentTests.cs b/src/AngleSharp.Scripting.JavaScript.Tests/ComponentTests.cs index 1e63057..40b6cb5 100644 --- a/src/AngleSharp.Scripting.JavaScript.Tests/ComponentTests.cs +++ b/src/AngleSharp.Scripting.JavaScript.Tests/ComponentTests.cs @@ -14,7 +14,7 @@ static async Task RunScriptComponent(String script) var cfg = Configuration.Default.With(service); var html = String.Concat(""); var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var value = service.Engine.GetJint(document).GetValue("assert"); + var value = service.Engine.GetOrCreateJint(document).GetValue("assert"); return value.AsString(); } diff --git a/src/AngleSharp.Scripting.JavaScript.Tests/FireEventTests.cs b/src/AngleSharp.Scripting.JavaScript.Tests/FireEventTests.cs index cb886ff..2a444ff 100644 --- a/src/AngleSharp.Scripting.JavaScript.Tests/FireEventTests.cs +++ b/src/AngleSharp.Scripting.JavaScript.Tests/FireEventTests.cs @@ -53,7 +53,7 @@ public async Task InvokeLoadEventFromJsAndCustomEventFromJsAndCs() "; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var log = service.Engine.GetJint(document).GetValue("log").AsArray(); + var log = service.Engine.GetOrCreateJint(document).GetValue("log").AsArray(); document.AddEventListener("hello", (s, ev) => { @@ -86,7 +86,7 @@ public async Task AddClickHandlerClassicallyWillExecute() "; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var clicked = service.Engine.GetJint(document).GetValue("clicked").AsBoolean(); + var clicked = service.Engine.GetOrCreateJint(document).GetValue("clicked").AsBoolean(); Assert.IsTrue(clicked); } @@ -108,7 +108,7 @@ public async Task AddAndRemoveClickHandlerWontExecute() "; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var clicked = service.Engine.GetJint(document).GetValue("clicked").AsBoolean(); + var clicked = service.Engine.GetOrCreateJint(document).GetValue("clicked").AsBoolean(); Assert.IsFalse(clicked); } @@ -129,7 +129,7 @@ public async Task AddAndInvokeClickHandlerWillChangeCapturedValue() "; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var clicked = service.Engine.GetJint(document).GetValue("clicked").AsBoolean(); + var clicked = service.Engine.GetOrCreateJint(document).GetValue("clicked").AsBoolean(); Assert.IsTrue(clicked); } } diff --git a/src/AngleSharp.Scripting.JavaScript.Tests/InteractionTests.cs b/src/AngleSharp.Scripting.JavaScript.Tests/InteractionTests.cs index 01a50e1..1775eb1 100644 --- a/src/AngleSharp.Scripting.JavaScript.Tests/InteractionTests.cs +++ b/src/AngleSharp.Scripting.JavaScript.Tests/InteractionTests.cs @@ -16,7 +16,7 @@ public async Task ReadStoredJavaScriptValueFromCSharp() var cfg = Configuration.Default.With(service); var html = ""; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var foo = service.Engine.GetJint(document).GetValue("foo"); + var foo = service.Engine.GetOrCreateJint(document).GetValue("foo"); Assert.AreEqual(Types.String, foo.Type); Assert.AreEqual("test", foo.AsString()); } @@ -28,7 +28,7 @@ public async Task RunJavaScriptFunctionFromCSharp() var cfg = Configuration.Default.With(service); var html = ""; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var square = service.Engine.GetJint(document).GetValue("square"); + var square = service.Engine.GetOrCreateJint(document).GetValue("square"); var result = square.Invoke(4); Assert.AreEqual(Types.Number, result.Type); Assert.AreEqual(16.0, result.AsNumber()); @@ -54,7 +54,7 @@ public async Task AccessCSharpInstanceMembersFromJavaScript() service.Engine.External["person"] = new Person { Age = 20, Name = "Foobar" }; var html = ""; var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html)); - var str = service.Engine.GetJint(document).GetValue("str"); + var str = service.Engine.GetOrCreateJint(document).GetValue("str"); Assert.AreEqual(Types.String, str.Type); Assert.AreEqual("Foobar is 20 years old", str.AsString()); } diff --git a/src/AngleSharp.Scripting.JavaScript.nuspec b/src/AngleSharp.Scripting.JavaScript.nuspec index 8227997..88e56bc 100644 --- a/src/AngleSharp.Scripting.JavaScript.nuspec +++ b/src/AngleSharp.Scripting.JavaScript.nuspec @@ -14,8 +14,8 @@ Copyright 2016, AngleSharp html html5 css css3 dom javascript scripting library js scripts runtime jint anglesharp angle - - + + diff --git a/src/AngleSharp.Scripting.JavaScript/EngineInstance.cs b/src/AngleSharp.Scripting.JavaScript/EngineInstance.cs index 68b44b2..5bbfb96 100644 --- a/src/AngleSharp.Scripting.JavaScript/EngineInstance.cs +++ b/src/AngleSharp.Scripting.JavaScript/EngineInstance.cs @@ -12,12 +12,12 @@ sealed class EngineInstance { #region Fields - readonly Dictionary _objects; - readonly Engine _engine; - readonly LexicalEnvironment _lexicals; - readonly LexicalEnvironment _variables; - readonly DomNodeInstance _window; - readonly DomConstructors _constructors; + private readonly Dictionary _objects; + private readonly Engine _engine; + private readonly LexicalEnvironment _lexicals; + private readonly LexicalEnvironment _variables; + private readonly DomNodeInstance _window; + private readonly DomConstructors _constructors; #endregion diff --git a/src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs b/src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs index ad0096a..22135fe 100644 --- a/src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs +++ b/src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs @@ -19,8 +19,8 @@ public class JavaScriptEngine : IScriptEngine { #region Fields - readonly ConditionalWeakTable _contexts; - readonly Dictionary _external; + private readonly ConditionalWeakTable _contexts; + private readonly Dictionary _external; #endregion @@ -60,20 +60,13 @@ public String Type #region Methods /// - /// Gets the associated Jint engine, if any. + /// Gets the associated Jint engine or creates it. /// /// The current document. - /// The engine object, if any. - public Engine GetJint(IDocument document) + /// The engine object. + public Engine GetOrCreateJint(IDocument document) { - var instance = default(EngineInstance); - - if (_contexts.TryGetValue(document.DefaultView, out instance)) - { - return instance.Jint; - } - - return null; + return GetOrCreateInstance(document).Jint; } /// @@ -84,20 +77,29 @@ public Engine GetJint(IDocument document) /// The cancellation token to transport. public async Task EvaluateScriptAsync(IResponse response, ScriptOptions options, CancellationToken cancel) { - var instance = default(EngineInstance); - var objectContext = options.Document.DefaultView; - using (var reader = new StreamReader(response.Content, options.Encoding ?? Encoding.UTF8, true)) { var content = await reader.ReadToEndAsync().ConfigureAwait(false); + GetOrCreateInstance(options.Document).RunScript(content); + } + } - if (!_contexts.TryGetValue(objectContext, out instance)) - { - _contexts.Add(objectContext, instance = new EngineInstance(objectContext, _external)); - } + #endregion + + #region Helpers - instance.RunScript(content); + private EngineInstance GetOrCreateInstance(IDocument document) + { + var instance = default(EngineInstance); + var objectContext = document.DefaultView; + + if (!_contexts.TryGetValue(objectContext, out instance)) + { + instance = new EngineInstance(objectContext, _external); + _contexts.Add(objectContext, instance); } + + return instance; } #endregion diff --git a/src/AngleSharp.Scripting.JavaScript/Services/JavaScriptProvider.cs b/src/AngleSharp.Scripting.JavaScript/Services/JavaScriptProvider.cs index 98cb381..3797740 100644 --- a/src/AngleSharp.Scripting.JavaScript/Services/JavaScriptProvider.cs +++ b/src/AngleSharp.Scripting.JavaScript/Services/JavaScriptProvider.cs @@ -10,7 +10,7 @@ /// public class JavaScriptProvider : IScriptingProvider { - readonly JavaScriptEngine _engine; + private readonly JavaScriptEngine _engine; /// /// Creates a new scripting service.