Skip to content

Commit 7219a43

Browse files
committed
Downgrade Jint
1 parent 7baaea1 commit 7219a43

9 files changed

Lines changed: 86 additions & 118 deletions

File tree

src/AngleSharp.Js.Tests/Helpers.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,35 @@ public static async Task<String> EvalScriptAsync(this String source)
1414
{
1515
var console = new ConsoleLogger();
1616
var cfg = Configuration.Default.WithJs().WithConsoleLogger(context => console);
17-
var html = "<!doctype html><script>console.log(" + source + ")</script>";
17+
var html = $"<!doctype html><script>console.log({source})</script>";
1818
await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html));
1919
return console.Content.ToString().Trim();
2020
}
2121

22-
internal static IConfiguration GetCssConfig()
23-
{
24-
return Configuration.Default.WithJs().WithCss();
25-
}
22+
internal static IConfiguration GetCssConfig() =>
23+
Configuration.Default
24+
.WithJs()
25+
.WithCss()
26+
.WithRenderDevice();
2627

2728
public static async Task<String> EvalScriptsAsync(this IEnumerable<String> sources)
2829
{
2930
var cfg = GetCssConfig().WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true });
30-
var scripts = "<script>" + String.Join("</script><script>", sources) + "</script>";
31-
var html = "<!doctype html><div id=result></div>" + scripts;
31+
var content = String.Join("</script><script>", sources);
32+
var html = $"<!doctype html><div id=result></div><script>{content}</script>";
3233
var document = await BrowsingContext.New(cfg).OpenAsync(m => m.Content(html));
3334
return document.GetElementById("result").InnerHtml;
3435
}
3536

3637
public static Boolean IsNetworkAvailable()
3738
{
38-
if (NetworkInterface.GetIsNetworkAvailable())
39+
if (!NetworkInterface.GetIsNetworkAvailable())
3940
{
40-
return true;
41+
Assert.Inconclusive("No network has been detected. Test skipped.");
42+
return false;
4143
}
4244

43-
Assert.Inconclusive("No network has been detected. Test skipped.");
44-
return false;
45+
return true;
4546
}
4647
}
4748
}

src/AngleSharp.Js/AngleSharp.Js.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="AngleSharp" Version="0.12.0" />
15-
<PackageReference Include="Jint" Version="3.0.0-beta-1525" />
15+
<PackageReference Include="Jint" Version="2.10.4" />
1616
</ItemGroup>
1717

1818
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">

src/AngleSharp.Js/ConsoleInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ConsoleInstance(Engine engine, IConsoleLogger logger)
1414
: base(engine)
1515
{
1616
_logger = logger;
17-
FastAddProperty("log", new ClrFunctionInstance(engine, "log", Log), false, false, false);
17+
FastAddProperty("log", new ClrFunctionInstance(engine, Log), false, false, false);
1818
}
1919

2020
private JsValue Log(JsValue ctx, JsValue[] args)

src/AngleSharp.Js/DomConstructorInstance.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ sealed class DomConstructorInstance : FunctionInstance, IConstructor
1515
private readonly ObjectInstance _objectPrototype;
1616

1717
public DomConstructorInstance(EngineInstance engine, Type type)
18-
: base(engine.Jint, type.Name, null, null, false)
18+
: base(engine.Jint, null, null, false)
1919
{
20-
var toString = new ClrFunctionInstance(Engine, "toString", ToString);
20+
var toString = new ClrFunctionInstance(Engine, ToString);
2121
_objectPrototype = engine.GetDomPrototype(type);
2222
_instance = engine;
2323
FastAddProperty("toString", toString, true, false, true);
@@ -34,15 +34,19 @@ public DomConstructorInstance(EngineInstance engine, ConstructorInfo constructor
3434
public override JsValue Call(JsValue thisObject, JsValue[] arguments)
3535
{
3636
if (_constructor != null)
37+
{
3738
throw new JavaScriptException("Only call the constructor with the new keyword.");
39+
}
3840

3941
return ((IConstructor)this).Construct(arguments);
4042
}
4143

4244
ObjectInstance IConstructor.Construct(JsValue[] arguments)
4345
{
4446
if (_constructor == null)
47+
{
4548
throw new JavaScriptException("Illegal constructor.");
49+
}
4650

4751
try
4852
{
@@ -56,9 +60,7 @@ ObjectInstance IConstructor.Construct(JsValue[] arguments)
5660
}
5761
}
5862

59-
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
60-
{
61-
return $"function {_objectPrototype.Class}() {{ [native code] }}";
62-
}
63+
private JsValue ToString(JsValue thisObj, JsValue[] arguments) =>
64+
$"function {_objectPrototype.Class}() {{ [native code] }}";
6365
}
6466
}

src/AngleSharp.Js/DomEventInstance.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace AngleSharp.Js
44
using Jint.Native;
55
using Jint.Native.Function;
66
using Jint.Runtime.Interop;
7-
using System;
87
using System.Reflection;
98

109
sealed class DomEventInstance
@@ -18,8 +17,8 @@ public DomEventInstance(EngineInstance engine, EventInfo eventInfo = null)
1817
{
1918
_engine = engine;
2019
_eventInfo = eventInfo;
21-
Getter = new ClrFunctionInstance(engine.Jint, String.Empty, GetEventHandler);
22-
Setter = new ClrFunctionInstance(engine.Jint, String.Empty, SetEventHandler);
20+
Getter = new ClrFunctionInstance(engine.Jint, GetEventHandler);
21+
Setter = new ClrFunctionInstance(engine.Jint, SetEventHandler);
2322
}
2423

2524
public ClrFunctionInstance Getter { get; }

src/AngleSharp.Js/DomFunctionInstance.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ sealed class DomFunctionInstance : FunctionInstance
1212
private readonly MethodInfo _method;
1313

1414
public DomFunctionInstance(EngineInstance engine, MethodInfo method)
15-
: base(engine.Jint, method.Name, method.GetParameterNames(), null, false)
15+
: base(engine.Jint, method.GetParameterNames(), null, false)
1616
{
17-
var toString = new ClrFunctionInstance(Engine, "toString", ToString);
17+
var toString = new ClrFunctionInstance(Engine, ToString);
1818
_instance = engine;
1919
_method = method;
2020
FastAddProperty("toString", toString, true, false, true);
@@ -24,9 +24,7 @@ public override JsValue Call(JsValue thisObject, JsValue[] arguments)
2424
{
2525
if (_method != null && thisObject.Type == Types.Object)
2626
{
27-
var node = thisObject.AsObject() as DomNodeInstance;
28-
29-
if (node != null)
27+
if (thisObject.AsObject() is DomNodeInstance node)
3028
{
3129
try
3230
{
@@ -45,12 +43,8 @@ public override JsValue Call(JsValue thisObject, JsValue[] arguments)
4543

4644
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
4745
{
48-
var func = thisObj.TryCast<FunctionInstance>();
49-
50-
if (func == null)
51-
{
46+
var func = thisObj.TryCast<FunctionInstance>() ??
5247
throw new JavaScriptException(Engine.TypeError, "Function object expected.");
53-
}
5448

5549
var officialName = _method.GetOfficialName();
5650
return $"function {officialName}() {{ [native code] }}";

src/AngleSharp.Js/DomNodeInstance.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ public DomNodeInstance(EngineInstance engine, Object value)
1919
Prototype = engine.GetDomPrototype(value.GetType());
2020
}
2121

22-
public Object Value
23-
{
24-
get { return _value; }
25-
}
22+
public Object Value => _value;
23+
24+
public override String Class => Prototype.Class;
2625

2726
public override PropertyDescriptor GetOwnProperty(String propertyName)
2827
{

src/AngleSharp.Js/DomPrototypeInstance.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace AngleSharp.Js
55
using Jint.Native;
66
using Jint.Native.Object;
77
using Jint.Runtime.Descriptors;
8-
using Jint.Runtime.Descriptors.Specialized;
98
using System;
109
using System.Collections.Generic;
1110
using System.Linq;
@@ -36,13 +35,14 @@ public DomPrototypeInstance(EngineInstance engine, Type type)
3635
Prototype = engine.GetDomPrototype(baseType);
3736
}
3837

38+
public override String Class => _name;
39+
3940
public Boolean TryGetFromIndex(Object value, String index, out PropertyDescriptor result)
4041
{
4142
// If we have a numeric indexer and the property is numeric
42-
var numericIndex = default(Int32);
43-
result = default(PropertyDescriptor);
43+
result = default;
4444

45-
if (_numericIndexer != null && Int32.TryParse(index, out numericIndex))
45+
if (_numericIndexer != null && Int32.TryParse(index, out var numericIndex))
4646
{
4747
var args = new Object[] { numericIndex };
4848

@@ -72,7 +72,7 @@ public Boolean TryGetFromIndex(Object value, String index, out PropertyDescripto
7272
// Eg. object.callMethod1() vs object['callMethod1'] is not necessarily the same if the object has a string indexer?? (I'm not an ECMA expert!)
7373
// node.attributes is one such object - has both a string and numeric indexer
7474
// This GetOwnProperty override might need an additional parameter to let us know this was called via an indexer
75-
if (_stringIndexer != null && !HasProperty(index))
75+
if (_stringIndexer != null && !Properties.ContainsKey(index))
7676
{
7777
var args = new Object[] { index };
7878
var prop = _stringIndexer.GetMethod.Invoke(value, args).ToJsValue(_instance);
@@ -118,7 +118,7 @@ private void SetEvents(IEnumerable<EventInfo> eventInfos)
118118
foreach (var name in names.Select(m => m.OfficialName))
119119
{
120120
var eventInstance = new DomEventInstance(_instance, eventInfo);
121-
FastSetProperty(name, new GetSetPropertyDescriptor(eventInstance.Getter, eventInstance.Setter, false, false));
121+
FastSetProperty(name, new PropertyDescriptor(eventInstance.Getter, eventInstance.Setter, false, false));
122122
}
123123
}
124124
}
@@ -150,7 +150,7 @@ private void SetProperties(IEnumerable<PropertyInfo> properties)
150150

151151
foreach (var name in names.Select(m => m.OfficialName))
152152
{
153-
FastSetProperty(name, new GetSetPropertyDescriptor(
153+
FastSetProperty(name, new PropertyDescriptor(
154154
new DomFunctionInstance(_instance, property.GetMethod),
155155
new DomFunctionInstance(_instance, property.SetMethod), false, false));
156156
}
@@ -169,7 +169,7 @@ private void SetMethods(IEnumerable<MethodInfo> methods)
169169
// If it already has a property with the given name (usually another method),
170170
// then convert that method to a two-layer method, which decides which one
171171
// to pick depending on the number (and probably types) of arguments.
172-
if (!HasProperty(name))
172+
if (!Properties.ContainsKey(name))
173173
{
174174
var func = new DomFunctionInstance(_instance, method);
175175
FastAddProperty(name, func, false, false, false);
@@ -187,23 +187,23 @@ private void SetPseudoProperties()
187187
var unloadEventInstance = new DomEventInstance(_instance);
188188
var contextMenuEventInstance = new DomEventInstance(_instance);
189189

190-
FastSetProperty("scrollLeft", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
191-
FastSetProperty("scrollTop", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
192-
FastSetProperty("scrollWidth", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
193-
FastSetProperty("scrollHeight", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
194-
FastSetProperty("clientLeft", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
195-
FastSetProperty("clientTop", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
196-
FastSetProperty("clientWidth", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
197-
FastSetProperty("clientHeight", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
198-
FastSetProperty("offsetLeft", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
199-
FastSetProperty("offsetTop", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
200-
FastSetProperty("offsetWidth", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
201-
FastSetProperty("offsetHeight", new PropertyDescriptor(new JsNumber(0.0), false, false, false));
202-
203-
FastSetProperty("focusin", new GetSetPropertyDescriptor(focusInEventInstance.Getter, focusInEventInstance.Setter, false, false));
204-
FastSetProperty("focusout", new GetSetPropertyDescriptor(focusOutEventInstance.Getter, focusOutEventInstance.Setter, false, false));
205-
FastSetProperty("unload", new GetSetPropertyDescriptor(unloadEventInstance.Getter, unloadEventInstance.Setter, false, false));
206-
FastSetProperty("contextmenu", new GetSetPropertyDescriptor(contextMenuEventInstance.Getter, contextMenuEventInstance.Setter, false, false));
190+
FastSetProperty("scrollLeft", new PropertyDescriptor(new JsValue(0.0), false, false, false));
191+
FastSetProperty("scrollTop", new PropertyDescriptor(new JsValue(0.0), false, false, false));
192+
FastSetProperty("scrollWidth", new PropertyDescriptor(new JsValue(0.0), false, false, false));
193+
FastSetProperty("scrollHeight", new PropertyDescriptor(new JsValue(0.0), false, false, false));
194+
FastSetProperty("clientLeft", new PropertyDescriptor(new JsValue(0.0), false, false, false));
195+
FastSetProperty("clientTop", new PropertyDescriptor(new JsValue(0.0), false, false, false));
196+
FastSetProperty("clientWidth", new PropertyDescriptor(new JsValue(0.0), false, false, false));
197+
FastSetProperty("clientHeight", new PropertyDescriptor(new JsValue(0.0), false, false, false));
198+
FastSetProperty("offsetLeft", new PropertyDescriptor(new JsValue(0.0), false, false, false));
199+
FastSetProperty("offsetTop", new PropertyDescriptor(new JsValue(0.0), false, false, false));
200+
FastSetProperty("offsetWidth", new PropertyDescriptor(new JsValue(0.0), false, false, false));
201+
FastSetProperty("offsetHeight", new PropertyDescriptor(new JsValue(0.0), false, false, false));
202+
203+
FastSetProperty("focusin", new PropertyDescriptor(focusInEventInstance.Getter, focusInEventInstance.Setter, false, false));
204+
FastSetProperty("focusout", new PropertyDescriptor(focusOutEventInstance.Getter, focusOutEventInstance.Setter, false, false));
205+
FastSetProperty("unload", new PropertyDescriptor(unloadEventInstance.Getter, unloadEventInstance.Setter, false, false));
206+
FastSetProperty("contextmenu", new PropertyDescriptor(contextMenuEventInstance.Getter, contextMenuEventInstance.Setter, false, false));
207207
}
208208
}
209209
}

0 commit comments

Comments
 (0)