Skip to content

Commit

Permalink
Extended to enable #28
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Sep 3, 2016
1 parent c7ed869 commit 1b8082d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static async Task<String> RunScriptComponent(String script)
var cfg = Configuration.Default.With(service);
var html = String.Concat("<!doctype html><script>", script, "</script>");
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();
}

Expand Down
8 changes: 4 additions & 4 deletions src/AngleSharp.Scripting.JavaScript.Tests/FireEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task InvokeLoadEventFromJsAndCustomEventFromJsAndCs()
</script>
</body>";
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) =>
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task AddClickHandlerClassicallyWillExecute()
</script>
</body>";
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);
}

Expand All @@ -108,7 +108,7 @@ public async Task AddAndRemoveClickHandlerWontExecute()
</script>
</body>";
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);
}

Expand All @@ -129,7 +129,7 @@ public async Task AddAndInvokeClickHandlerWillChangeCapturedValue()
</script>
</body>";
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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/AngleSharp.Scripting.JavaScript.Tests/InteractionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task ReadStoredJavaScriptValueFromCSharp()
var cfg = Configuration.Default.With(service);
var html = "<!doctype html><script>var foo = 'test';</script>";
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());
}
Expand All @@ -28,7 +28,7 @@ public async Task RunJavaScriptFunctionFromCSharp()
var cfg = Configuration.Default.With(service);
var html = "<!doctype html><script>function square(x) { return x * x; }</script>";
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());
Expand All @@ -54,7 +54,7 @@ public async Task AccessCSharpInstanceMembersFromJavaScript()
service.Engine.External["person"] = new Person { Age = 20, Name = "Foobar" };
var html = "<!doctype html><script>var str = person.Name + ' is ' + person.Age + ' years old';</script>";
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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/AngleSharp.Scripting.JavaScript.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<copyright>Copyright 2016, AngleSharp</copyright>
<tags>html html5 css css3 dom javascript scripting library js scripts runtime jint anglesharp angle</tags>
<dependencies>
<dependency id="AngleSharp" version="0.9.7" />
<dependency id="Jint" version="2.8.0" />
<dependency id="AngleSharp" version="0.9.8" />
<dependency id="Jint" version="2.9.1" />
</dependencies>
</metadata>
</package>
12 changes: 6 additions & 6 deletions src/AngleSharp.Scripting.JavaScript/EngineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ sealed class EngineInstance
{
#region Fields

readonly Dictionary<Object, DomNodeInstance> _objects;
readonly Engine _engine;
readonly LexicalEnvironment _lexicals;
readonly LexicalEnvironment _variables;
readonly DomNodeInstance _window;
readonly DomConstructors _constructors;
private readonly Dictionary<Object, DomNodeInstance> _objects;
private readonly Engine _engine;
private readonly LexicalEnvironment _lexicals;
private readonly LexicalEnvironment _variables;
private readonly DomNodeInstance _window;
private readonly DomConstructors _constructors;

#endregion

Expand Down
44 changes: 23 additions & 21 deletions src/AngleSharp.Scripting.JavaScript/JavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class JavaScriptEngine : IScriptEngine
{
#region Fields

readonly ConditionalWeakTable<IWindow, EngineInstance> _contexts;
readonly Dictionary<String, Object> _external;
private readonly ConditionalWeakTable<IWindow, EngineInstance> _contexts;
private readonly Dictionary<String, Object> _external;

#endregion

Expand Down Expand Up @@ -60,20 +60,13 @@ public String Type
#region Methods

/// <summary>
/// Gets the associated Jint engine, if any.
/// Gets the associated Jint engine or creates it.
/// </summary>
/// <param name="document">The current document.</param>
/// <returns>The engine object, if any.</returns>
public Engine GetJint(IDocument document)
/// <returns>The engine object.</returns>
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;
}

/// <summary>
Expand All @@ -84,20 +77,29 @@ public Engine GetJint(IDocument document)
/// <param name="cancel">The cancellation token to transport.</param>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// </summary>
public class JavaScriptProvider : IScriptingProvider
{
readonly JavaScriptEngine _engine;
private readonly JavaScriptEngine _engine;

/// <summary>
/// Creates a new scripting service.
Expand Down

0 comments on commit 1b8082d

Please sign in to comment.