-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
JsScriptingService.cs
126 lines (103 loc) · 4.06 KB
/
JsScriptingService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace AngleSharp.Scripting
{
using AngleSharp.Browser;
using AngleSharp.Dom;
using AngleSharp.Io;
using AngleSharp.Js;
using Jint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// The JavaScript engine.
/// </summary>
public class JsScriptingService : IScriptingService
{
#region Fields
private readonly ConditionalWeakTable<IWindow, EngineInstance> _contexts;
private readonly Dictionary<String, Object> _external;
#endregion
#region ctor
/// <summary>
/// Creates a new JavaScript engine.
/// </summary>
public JsScriptingService()
{
_contexts = new ConditionalWeakTable<IWindow, EngineInstance>();
_external = new Dictionary<String, Object>();
}
#endregion
#region Properties
/// <summary>
/// Gets the external assignments.
/// </summary>
public IDictionary<String, Object> External => _external;
/// <summary>
/// Gets the engine's mime-type.
/// </summary>
public String Type => MimeTypeNames.DefaultJavaScript;
#endregion
#region Methods
Boolean IScriptingService.SupportsType(String mimeType) =>
MimeTypeNames.IsJavaScript(mimeType);
/// <summary>
/// Gets the associated Jint engine or creates it.
/// </summary>
/// <param name="document">The current document.</param>
/// <returns>The engine object.</returns>
public Engine GetOrCreateJint(IDocument document) =>
GetOrCreateInstance(document).Jint;
/// <summary>
/// Evaluates the response asynchronously.
/// </summary>
/// <param name="response">The response to parse.</param>
/// <param name="options">The options to consider.</param>
/// <param name="cancel">The cancellation token to transport.</param>
public async Task EvaluateScriptAsync(IResponse response, ScriptOptions options, CancellationToken cancel)
{
var encoding = options.Encoding ?? Encoding.UTF8;
using (var reader = new StreamReader(response.Content, encoding, true))
{
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
await options.EventLoop.EnqueueAsync(_ =>
EvaluateScript(options.Document, content), TaskPriority.Critical).ConfigureAwait(false);
}
}
/// <summary>
/// Evaluates the given script source in the engine of the document.
/// </summary>
/// <param name="document">The context of the evaluation.</param>
/// <param name="source">The source of the script.</param>
/// <returns>The result of the evaluation.</returns>
public Object EvaluateScript(IDocument document, String source)
{
document = document ?? throw new ArgumentNullException(nameof(document));
return GetOrCreateInstance(document).RunScript(source).FromJsValue();
}
#endregion
#region Helpers
internal EngineInstance GetOrCreateInstance(IDocument document)
{
var objectContext = document.DefaultView;
if (!_contexts.TryGetValue(objectContext, out var instance))
{
var libs = GetAssemblies(document.Context).ToArray();
instance = new EngineInstance(objectContext, _external, libs);
_contexts.Add(objectContext, instance);
}
return instance;
}
private static IEnumerable<Assembly> GetAssemblies(IBrowsingContext context) => context
.GetServices<Object>()
.Select(m => m.GetType().GetAssembly())
.Distinct()
.Where(m => m.FullName.StartsWith("AngleSharp"));
#endregion
}
}