Skip to content

Commit 6d14525

Browse files
committed
Support for .NET Standard 1.3 and improved loop
1 parent b681e34 commit 6d14525

11 files changed

+70
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# 0.13.0
22

3-
(tbd)
3+
Released on Friday, August 23 2019.
44

55
- Added thread-based event loop implementation `JsEventLoop`
66
- Included new `WithEventLoop` configuration extensions
77
- Added constructors to `window` (#12)
88
- Fixed `btoa` and `atob` missing (#55)
99
- Included `javascript:` URL handler (#47)
10+
- Included support for .NET Standard 1.3 (#58)
1011

1112
# 0.12.1
1213

build.cake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var solutionName = "AngleSharp.Js";
44
var frameworks = new Dictionary<String, String>
55
{
66
{ "net46", "net46" },
7+
{ "netstandard1.3", "netstandard1.3" },
78
{ "netstandard2.0", "netstandard2.0" },
89
};
910

src/AngleSharp.Js.nuspec

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@
1414
<copyright>Copyright 2017-2019, AngleSharp</copyright>
1515
<tags>html html5 css css3 dom javascript scripting library js scripts runtime jint anglesharp angle</tags>
1616
<dependencies>
17-
<dependency id="AngleSharp" version="0.12.1" />
18-
<dependency id="Jint" version="2.10.4" />
17+
<group targetFramework="netstandard1.3">
18+
<dependency id="AngleSharp" version="0.13.0" />
19+
<dependency id="Jint" version="2.10.4" />
20+
<dependency id="System.Threading.Thread" version="4.0.0" />
21+
</group>
22+
<group targetFramework="netstandard2.0">
23+
<dependency id="AngleSharp" version="0.13.0" />
24+
<dependency id="Jint" version="2.10.4" />
25+
</group>
26+
<group targetFramework="net46">
27+
<dependency id="AngleSharp" version="0.13.0" />
28+
<dependency id="Jint" version="2.10.4" />
29+
</group>
1930
</dependencies>
2031
</metadata>
2132
</package>

src/AngleSharp.Js/AngleSharp.Js.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<PropertyGroup>
33
<AssemblyName>AngleSharp.Js</AssemblyName>
44
<RootNamespace>AngleSharp.Js</RootNamespace>
5-
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard2.0;net46</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.3;netstandard2.0</TargetFrameworks>
6+
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.3;netstandard2.0;net46</TargetFrameworks>
77
<SignAssembly>true</SignAssembly>
88
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -15,6 +15,10 @@
1515
<PackageReference Include="Jint" Version="2.10.4" />
1616
</ItemGroup>
1717

18+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
19+
<PackageReference Include="System.Threading.Thread" Version="4.0.0" />
20+
</ItemGroup>
21+
1822
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
1923
<DelaySign>false</DelaySign>
2024
</PropertyGroup>

src/AngleSharp.Js/Dom/EventAttributeObserver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public EventAttributeObserver(JsScriptingService service)
9191
private void RegisterEventCallback<TElement>(String eventName)
9292
where TElement : IElement
9393
{
94-
_observers.Add("on" + eventName, (element, value) =>
94+
_observers.Add("on" + eventName, (sender, value) =>
9595
{
96-
if (element is TElement)
96+
if (sender is TElement element)
9797
{
9898
var document = element.Owner;
9999
var engine = _service.GetOrCreateInstance(document);

src/AngleSharp.Js/Dom/Navigator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void WaitForStorageUpdates()
3838
{
3939
}
4040

41+
#if NETSTANDARD1_3
42+
public Boolean IsOnline => true;
43+
#else
4144
public Boolean IsOnline => NetworkInterface.GetIsNetworkAvailable();
45+
#endif
4246
}
4347
}

src/AngleSharp.Js/Dom/XmlHttpRequest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AngleSharp.Js.Dom
22
{
33
using AngleSharp.Attributes;
4+
using AngleSharp.Browser;
45
using AngleSharp.Dom;
56
using AngleSharp.Dom.Events;
67
using AngleSharp.Io;
@@ -370,6 +371,9 @@ private async Task Receive(IDocumentLoader loader, DocumentRequest request)
370371
}
371372
}
372373

374+
private IEventLoop GetEventLoop() =>
375+
_window?.Document?.Context.GetService<IEventLoop>();
376+
373377
private IDocumentLoader GetLoader() =>
374378
_window?.Document?.Context.GetService<IDocumentLoader>();
375379

@@ -387,7 +391,7 @@ private static Stream Serialize(Object body)
387391
}
388392

389393
private void Fire(String eventName) =>
390-
Dispatch(new Event(eventName));
394+
GetEventLoop().Enqueue(() => Dispatch(new Event(eventName)));
391395

392396
#endregion
393397
}

src/AngleSharp.Js/Extensions/EngineExtensions.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,33 +225,30 @@ public static JsValue RunScript(this EngineInstance engine, String source, INode
225225

226226
public static JsValue Call(this EngineInstance instance, MethodInfo method, JsValue thisObject, JsValue[] arguments)
227227
{
228-
if (method != null && thisObject.Type == Types.Object)
228+
if (method != null && thisObject.Type == Types.Object && thisObject.AsObject() is DomNodeInstance node)
229229
{
230-
if (thisObject.AsObject() is DomNodeInstance node)
230+
try
231231
{
232-
try
232+
if (method.IsStatic)
233233
{
234-
if (method.IsStatic)
234+
var newArgs = new List<JsValue>
235235
{
236-
var newArgs = new List<JsValue>
237-
{
238-
thisObject,
239-
};
240-
newArgs.AddRange(arguments);
241-
var parameters = instance.BuildArgs(method, newArgs.ToArray());
242-
return method.Invoke(null, parameters).ToJsValue(instance);
243-
}
244-
else
245-
{
246-
var parameters = instance.BuildArgs(method, arguments);
247-
return method.Invoke(node.Value, parameters).ToJsValue(instance);
248-
}
236+
thisObject,
237+
};
238+
newArgs.AddRange(arguments);
239+
var parameters = instance.BuildArgs(method, newArgs.ToArray());
240+
return method.Invoke(null, parameters).ToJsValue(instance);
249241
}
250-
catch (TargetInvocationException)
242+
else
251243
{
252-
throw new JavaScriptException(instance.Jint.Error);
244+
var parameters = instance.BuildArgs(method, arguments);
245+
return method.Invoke(node.Value, parameters).ToJsValue(instance);
253246
}
254247
}
248+
catch (TargetInvocationException)
249+
{
250+
throw new JavaScriptException(instance.Jint.Error);
251+
}
255252
}
256253

257254
return JsValue.Undefined;

src/AngleSharp.Js/Extensions/ReflectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static Object GetDefaultValue(this Type type) =>
2020

2121
public static IEnumerable<Type> GetExtensionTypes(this IEnumerable<Assembly> libs, String name) => libs
2222
.SelectMany(m => m.ExportedTypes)
23-
.Where(m => m.GetCustomAttributes<DomExposedAttribute>().Any(n => n.Target.Is(name)))
23+
.Where(m => m.GetTypeInfo().GetCustomAttributes<DomExposedAttribute>().Any(n => n.Target.Is(name)))
2424
.ToArray();
2525

2626
public static IEnumerable<Type> GetTypeTree(this Type root)
@@ -89,7 +89,7 @@ public static String GetOfficialName(this Type currentType, Type baseType)
8989

9090
public static PropertyInfo GetInheritedProperty(this Type type, String propertyName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance)
9191
{
92-
if (type.IsInterface)
92+
if (type.GetTypeInfo().IsInterface)
9393
{
9494
return type.GetInterfaces()
9595
.Union(new[] { type })
@@ -103,7 +103,7 @@ public static PropertyInfo GetInheritedProperty(this Type type, String propertyN
103103

104104
public static IEnumerable<PropertyInfo> GetInheritedProperties(this Type type, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance)
105105
{
106-
if (type.IsInterface)
106+
if (type.GetTypeInfo().IsInterface)
107107
{
108108
return type.GetInterfaces()
109109
.Union(new[] { type })

src/AngleSharp.Js/JsEventLoop.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ public sealed class JsEventLoop : IEventLoop, IDisposable
1515
private readonly Object _lockObj = new Object();
1616
private CancellationTokenSource _cts;
1717

18+
/// <summary>
19+
/// Creates a new event loop thread.
20+
/// </summary>
21+
public JsEventLoop()
22+
{
23+
var thread = new Thread(Runner)
24+
{
25+
IsBackground = true,
26+
Name = "AngleSharpEventLoop",
27+
#if !NETSTANDARD1_3
28+
Priority = ThreadPriority.Highest,
29+
#endif
30+
};
31+
_cts = new CancellationTokenSource();
32+
thread.Start(_cts.Token);
33+
}
34+
1835
ICancellable IEventLoop.Enqueue(Action<CancellationToken> action, TaskPriority priority)
1936
{
2037
var entry = new LoopEntry(action);
@@ -61,17 +78,6 @@ LoopEntry Dequeue(TaskPriority priority)
6178

6279
void IEventLoop.Spin()
6380
{
64-
if (_cts == null)
65-
{
66-
var thread = new Thread(Runner)
67-
{
68-
IsBackground = true,
69-
Name = "AngleSharpEventLoop",
70-
Priority = ThreadPriority.Highest,
71-
};
72-
_cts = new CancellationTokenSource();
73-
thread.Start(_cts.Token);
74-
}
7581
}
7682

7783
void IEventLoop.CancelAll()

0 commit comments

Comments
 (0)