Skip to content

Commit ee1a5e3

Browse files
Version 6.0.2: Fixed error handling in JavaScriptExtensions.ToPromise; added JavaScriptExtensions.ToTask (GitHub Issue #182); added DocumentLoader.MaxCacheSize and DocumentCategory.MaxCacheSize; added code to break event connections on engine disposal (GitHub Issue #183); improved ES6 module support, fixing cycle crash (GitHub Issue #181); added DynamicHostObject (GitHub Issue #180); added BigInt / BigInteger support for V8 (GitHub Issue #176); hardened Assembly.Load call in V8Proxy.cs (GitHub Issue #175); improved V8Update environment isolation to fix some V8 build issues (GitHub Issue #185); updated API documentation. Tested with V8 8.3.110.9.
1 parent 18478cb commit ee1a5e3

File tree

642 files changed

+2916
-1057
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

642 files changed

+2916
-1057
lines changed

ClearScript.NoV8.sln.DotSettings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>
32
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Ecpp/@EntryIndexedValue">True</s:Boolean>
43
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Eh/@EntryIndexedValue">True</s:Boolean>
54
<s:Boolean x:Key="/Default/CodeInspection/Highlighting/CalculateUnusedTypeMembers/@EntryValue">False</s:Boolean>
@@ -17,7 +16,6 @@
1716
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseObjectOrCollectionInitializer/@EntryIndexedValue">HINT</s:String>
1817
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VBStringEndsWithIsCultureSpecific/@EntryIndexedValue">WARNING</s:String>
1918
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VBStringStartsWithIsCultureSpecific/@EntryIndexedValue">WARNING</s:String>
20-
<s:String x:Key="/Default/CodeInspection/JsInspections/LanguageLevel/@EntryValue">Experimental</s:String>
2119
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
2220
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>
2321
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_PREPROCESSOR_IF/@EntryValue">DO_NOT_CHANGE</s:String>
@@ -136,6 +134,7 @@
136134
<s:Boolean x:Key="/Default/UserDictionary/Words/=HRESULT/@EntryIndexedValue">True</s:Boolean>
137135
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
138136
<s:Boolean x:Key="/Default/UserDictionary/Words/=ijwhost/@EntryIndexedValue">True</s:Boolean>
137+
<s:Boolean x:Key="/Default/UserDictionary/Words/=instanceof/@EntryIndexedValue">True</s:Boolean>
139138
<s:Boolean x:Key="/Default/UserDictionary/Words/=INVALIDARG/@EntryIndexedValue">True</s:Boolean>
140139
<s:Boolean x:Key="/Default/UserDictionary/Words/=Invocability/@EntryIndexedValue">True</s:Boolean>
141140
<s:Boolean x:Key="/Default/UserDictionary/Words/=invocable/@EntryIndexedValue">True</s:Boolean>
@@ -146,6 +145,7 @@
146145
<s:Boolean x:Key="/Default/UserDictionary/Words/=memid/@EntryIndexedValue">True</s:Boolean>
147146
<s:Boolean x:Key="/Default/UserDictionary/Words/=NETFRAMEWORK/@EntryIndexedValue">True</s:Boolean>
148147
<s:Boolean x:Key="/Default/UserDictionary/Words/=NOINTERFACE/@EntryIndexedValue">True</s:Boolean>
148+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Numerics/@EntryIndexedValue">True</s:Boolean>
149149
<s:Boolean x:Key="/Default/UserDictionary/Words/=nupkg/@EntryIndexedValue">True</s:Boolean>
150150
<s:Boolean x:Key="/Default/UserDictionary/Words/=oleaut/@EntryIndexedValue">True</s:Boolean>
151151
<s:Boolean x:Key="/Default/UserDictionary/Words/=Plex/@EntryIndexedValue">True</s:Boolean>

ClearScript.sln.DotSettings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>
32
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Ecpp/@EntryIndexedValue">True</s:Boolean>
43
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Eh/@EntryIndexedValue">True</s:Boolean>
54
<s:Boolean x:Key="/Default/CodeInspection/Highlighting/CalculateUnusedTypeMembers/@EntryValue">False</s:Boolean>
@@ -17,7 +16,6 @@
1716
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseObjectOrCollectionInitializer/@EntryIndexedValue">HINT</s:String>
1817
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VBStringEndsWithIsCultureSpecific/@EntryIndexedValue">WARNING</s:String>
1918
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VBStringStartsWithIsCultureSpecific/@EntryIndexedValue">WARNING</s:String>
20-
<s:String x:Key="/Default/CodeInspection/JsInspections/LanguageLevel/@EntryValue">Experimental</s:String>
2119
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">NEXT_LINE</s:String>
2220
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">NEXT_LINE</s:String>
2321
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_PREPROCESSOR_IF/@EntryValue">DO_NOT_CHANGE</s:String>
@@ -136,6 +134,7 @@
136134
<s:Boolean x:Key="/Default/UserDictionary/Words/=HRESULT/@EntryIndexedValue">True</s:Boolean>
137135
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
138136
<s:Boolean x:Key="/Default/UserDictionary/Words/=ijwhost/@EntryIndexedValue">True</s:Boolean>
137+
<s:Boolean x:Key="/Default/UserDictionary/Words/=instanceof/@EntryIndexedValue">True</s:Boolean>
139138
<s:Boolean x:Key="/Default/UserDictionary/Words/=INVALIDARG/@EntryIndexedValue">True</s:Boolean>
140139
<s:Boolean x:Key="/Default/UserDictionary/Words/=Invocability/@EntryIndexedValue">True</s:Boolean>
141140
<s:Boolean x:Key="/Default/UserDictionary/Words/=invocable/@EntryIndexedValue">True</s:Boolean>
@@ -146,6 +145,7 @@
146145
<s:Boolean x:Key="/Default/UserDictionary/Words/=memid/@EntryIndexedValue">True</s:Boolean>
147146
<s:Boolean x:Key="/Default/UserDictionary/Words/=NETFRAMEWORK/@EntryIndexedValue">True</s:Boolean>
148147
<s:Boolean x:Key="/Default/UserDictionary/Words/=NOINTERFACE/@EntryIndexedValue">True</s:Boolean>
148+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Numerics/@EntryIndexedValue">True</s:Boolean>
149149
<s:Boolean x:Key="/Default/UserDictionary/Words/=nupkg/@EntryIndexedValue">True</s:Boolean>
150150
<s:Boolean x:Key="/Default/UserDictionary/Words/=oleaut/@EntryIndexedValue">True</s:Boolean>
151151
<s:Boolean x:Key="/Default/UserDictionary/Words/=Plex/@EntryIndexedValue">True</s:Boolean>

ClearScript/CanonicalRefTable.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Microsoft.ClearScript
1010
{
1111
internal static class CanonicalRefTable
1212
{
13-
private static readonly object tableLock = new object();
1413
private static readonly Dictionary<Type, ICanonicalRefMap> table = new Dictionary<Type, ICanonicalRefMap>();
1514

1615
public static object GetCanonicalRef(object obj)
@@ -30,7 +29,7 @@ public static object GetCanonicalRef(object obj)
3029
private static ICanonicalRefMap GetMap(object obj)
3130
{
3231
var type = obj.GetType();
33-
lock (tableLock)
32+
lock (table)
3433
{
3534
ICanonicalRefMap map;
3635
if (!table.TryGetValue(type, out map))
@@ -81,7 +80,6 @@ private abstract class CanonicalRefMapBase : ICanonicalRefMap
8180

8281
private sealed class CanonicalRefMap<T> : CanonicalRefMapBase
8382
{
84-
private readonly object mapLock = new object();
8583
private readonly Dictionary<T, WeakReference> map = new Dictionary<T, WeakReference>();
8684
private DateTime lastCompactionTime = DateTime.MinValue;
8785

@@ -126,7 +124,7 @@ private void CompactIfNecessary()
126124

127125
public override object GetRef(object obj)
128126
{
129-
lock (mapLock)
127+
lock (map)
130128
{
131129
var result = GetRefInternal(obj);
132130
CompactIfNecessary();

ClearScript/ClearScript.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3333
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3434
<Prefer32Bit>false</Prefer32Bit>
35-
<LangVersion>5</LangVersion>
3635
</PropertyGroup>
3736
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
3837
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -47,7 +46,6 @@
4746
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
4847
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4948
<Prefer32Bit>false</Prefer32Bit>
50-
<LangVersion>5</LangVersion>
5149
</PropertyGroup>
5250
<PropertyGroup Condition="Exists('$(SolutionDir)ClearScript.snk')">
5351
<SignAssembly>true</SignAssembly>
@@ -64,10 +62,12 @@
6462
<Reference Include="System.Core" />
6563
<Reference Include="Microsoft.CSharp" />
6664
<Reference Include="System.Net.Http" />
65+
<Reference Include="System.Numerics" />
6766
<Reference Include="WindowsBase" />
6867
</ItemGroup>
6968
<ItemGroup>
7069
<Compile Include="CanonicalRefTable.cs" />
70+
<Compile Include="DynamicHostMetaObject.cs" />
7171
<Compile Include="HostItem.NetFramework.cs" />
7272
<Compile Include="HostSettings.cs" />
7373
<Compile Include="Document.cs" />
@@ -107,6 +107,7 @@
107107
<DesignTime>True</DesignTime>
108108
<DependentUpon>AssemblyInfo.tt</DependentUpon>
109109
</Compile>
110+
<Compile Include="DynamicHostObject.cs" />
110111
<Compile Include="ScriptAccess.cs" />
111112
<Compile Include="ScriptEngineException.cs" />
112113
<Compile Include="ScriptInterruptedException.cs" />

ClearScript/DelegateFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ private static Delegate CreateSimpleDelegate(ScriptEngine engine, object target,
8282
}
8383
else
8484
{
85-
var typeArgs = paramTypes.Concat(new[] { method.ReturnType, delegateType }).ToArray();
85+
// ReSharper disable once RedundantExplicitArrayCreation
86+
var typeArgs = paramTypes.Concat(new Type[] { method.ReturnType, delegateType }).ToArray();
8687
shimType = funcShimTemplates[paramTypes.Length].MakeSpecificType(typeArgs);
8788
}
8889

ClearScript/DocumentCategory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@ public abstract class DocumentCategory
1010
{
1111
internal DocumentCategory()
1212
{
13+
MaxCacheSize = 1024;
1314
}
1415

16+
/// <summary>
17+
/// Gets or sets the maximum cache size for the document category.
18+
/// </summary>
19+
/// <remarks>
20+
/// <para>
21+
/// This property specifies the maximum number of prepared or compiled documents of the
22+
/// current category to be cached by script engines. Its initial value is 1024.
23+
/// </para>
24+
/// <para>
25+
/// Each script engine or runtime maintains private caches for supported document
26+
/// categories. These are distinct from the caches used by document loaders.
27+
/// </para>
28+
/// </remarks>
29+
/// <seealso cref="DocumentLoader.MaxCacheSize"/>
30+
public uint MaxCacheSize { get; set; }
31+
1532
/// <summary>
1633
/// Gets the document category for normal scripts.
1734
/// </summary>

ClearScript/DocumentLoader.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public static DocumentLoader Default
3838
get { return DefaultImpl.Instance; }
3939
}
4040

41+
/// <summary>
42+
/// Gets or sets the maximum size of the document loader's cache.
43+
/// </summary>
44+
/// <remarks>
45+
/// This property specifies the maximum number of documents to be cached by the document
46+
/// loader. For the default document loader, its initial value is 1024.
47+
/// </remarks>
48+
/// <seealso cref="Default"/>
49+
public virtual uint MaxCacheSize
50+
{
51+
get { return 0; }
52+
set { throw new NotSupportedException("Loader does not support caching"); }
53+
}
54+
4155
/// <summary>
4256
/// Loads a document.
4357
/// </summary>
@@ -99,7 +113,7 @@ public virtual void DiscardCachedDocuments()
99113

100114
#region Nested type: DefaultImpl
101115

102-
private class DefaultImpl : DocumentLoader
116+
private sealed class DefaultImpl : DocumentLoader
103117
{
104118
public static readonly DefaultImpl Instance = new DefaultImpl();
105119

@@ -111,12 +125,11 @@ private class DefaultImpl : DocumentLoader
111125
".." + Path.AltDirectorySeparatorChar,
112126
};
113127

114-
private readonly object cacheLock = new object();
115128
private readonly List<Document> cache = new List<Document>();
116-
private const int maxCacheSize = 1024;
117129

118130
private DefaultImpl()
119131
{
132+
MaxCacheSize = 1024;
120133
}
121134

122135
private static async Task<List<Uri>> GetCandidateUrisAsync(DocumentSettings settings, DocumentInfo? sourceInfo, Uri uri)
@@ -310,7 +323,7 @@ private async Task<Document> LoadDocumentAsync(DocumentSettings settings, Uri ur
310323
{
311324
if (!flags.HasFlag(DocumentAccessFlags.EnableFileLoading))
312325
{
313-
throw new UnauthorizedAccessException("This script engine is not configured for loading documents from the file system");
326+
throw new UnauthorizedAccessException("The script engine is not configured for loading documents from the file system");
314327
}
315328

316329
using (var reader = new StreamReader(uri.LocalPath))
@@ -322,7 +335,7 @@ private async Task<Document> LoadDocumentAsync(DocumentSettings settings, Uri ur
322335
{
323336
if (!flags.HasFlag(DocumentAccessFlags.EnableWebLoading))
324337
{
325-
throw new UnauthorizedAccessException("This script engine is not configured for downloading documents from the Web");
338+
throw new UnauthorizedAccessException("The script engine is not configured for downloading documents from the Web");
326339
}
327340

328341
using (var client = new WebClient())
@@ -344,7 +357,7 @@ private async Task<Document> LoadDocumentAsync(DocumentSettings settings, Uri ur
344357

345358
private Document GetCachedDocument(Uri uri)
346359
{
347-
lock (cacheLock)
360+
lock (cache)
348361
{
349362
for (var index = 0; index < cache.Count; index++)
350363
{
@@ -363,7 +376,7 @@ private Document GetCachedDocument(Uri uri)
363376

364377
private Document CacheDocument(Document document)
365378
{
366-
lock (cacheLock)
379+
lock (cache)
367380
{
368381
var cachedDocument = cache.FirstOrDefault(testDocument => testDocument.Info.Uri == document.Info.Uri);
369382
if (cachedDocument != null)
@@ -372,6 +385,7 @@ private Document CacheDocument(Document document)
372385
return cachedDocument;
373386
}
374387

388+
var maxCacheSize = Math.Max(16, Convert.ToInt32(Math.Min(MaxCacheSize, int.MaxValue)));
375389
while (cache.Count >= maxCacheSize)
376390
{
377391
cache.RemoveAt(cache.Count - 1);
@@ -384,14 +398,16 @@ private Document CacheDocument(Document document)
384398

385399
#region DocumentLoader overrides
386400

401+
public override uint MaxCacheSize { get; set; }
402+
387403
public override async Task<Document> LoadDocumentAsync(DocumentSettings settings, DocumentInfo? sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback)
388404
{
389405
MiscHelpers.VerifyNonNullArgument(settings, "settings");
390406
MiscHelpers.VerifyNonBlankArgument(specifier, "specifier", "Invalid document specifier");
391407

392408
if ((settings.AccessFlags & DocumentAccessFlags.EnableAllLoading) == DocumentAccessFlags.None)
393409
{
394-
throw new UnauthorizedAccessException("This script engine is not configured for loading documents");
410+
throw new UnauthorizedAccessException("The script engine is not configured for loading documents");
395411
}
396412

397413
if (category == null)
@@ -461,7 +477,7 @@ public override async Task<Document> LoadDocumentAsync(DocumentSettings settings
461477

462478
public override void DiscardCachedDocuments()
463479
{
464-
lock (cacheLock)
480+
lock (cache)
465481
{
466482
cache.Clear();
467483
}

ClearScript/DynamicHostMetaObject.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Dynamic;
6+
7+
namespace Microsoft.ClearScript
8+
{
9+
internal sealed class DynamicHostMetaObject : DynamicMetaObject
10+
{
11+
private readonly IDynamicMetaObjectProvider metaObjectProvider;
12+
private readonly DynamicMetaObject metaObject;
13+
14+
public DynamicHostMetaObject(IDynamicMetaObjectProvider metaObjectProvider, DynamicMetaObject metaObject)
15+
: base(metaObject.Expression, metaObject.Restrictions, metaObject.Value)
16+
{
17+
this.metaObjectProvider = metaObjectProvider;
18+
this.metaObject = metaObject;
19+
}
20+
21+
public bool HasMember(string name, bool ignoreCase)
22+
{
23+
return DynamicHostObject.HasMember(metaObjectProvider, metaObject, name, ignoreCase);
24+
}
25+
26+
#region DynamicMetaObject overrides
27+
28+
public override IEnumerable<string> GetDynamicMemberNames()
29+
{
30+
return metaObject.GetDynamicMemberNames();
31+
}
32+
33+
public override DynamicMetaObject BindBinaryOperation(BinaryOperationBinder binder, DynamicMetaObject arg)
34+
{
35+
return metaObject.BindBinaryOperation(binder, arg);
36+
}
37+
38+
public override DynamicMetaObject BindConvert(ConvertBinder binder)
39+
{
40+
return metaObject.BindConvert(binder);
41+
}
42+
43+
public override DynamicMetaObject BindCreateInstance(CreateInstanceBinder binder, DynamicMetaObject[] args)
44+
{
45+
return metaObject.BindCreateInstance(binder, args);
46+
}
47+
48+
public override DynamicMetaObject BindDeleteIndex(DeleteIndexBinder binder, DynamicMetaObject[] indexes)
49+
{
50+
return metaObject.BindDeleteIndex(binder, indexes);
51+
}
52+
53+
public override DynamicMetaObject BindDeleteMember(DeleteMemberBinder binder)
54+
{
55+
return metaObject.BindDeleteMember(binder);
56+
}
57+
58+
public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes)
59+
{
60+
return metaObject.BindGetIndex(binder, indexes);
61+
}
62+
63+
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
64+
{
65+
return metaObject.BindGetMember(binder);
66+
}
67+
68+
public override DynamicMetaObject BindInvoke(InvokeBinder binder, DynamicMetaObject[] args)
69+
{
70+
return metaObject.BindInvoke(binder, args);
71+
}
72+
73+
public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
74+
{
75+
return metaObject.BindInvokeMember(binder, args);
76+
}
77+
78+
public override DynamicMetaObject BindSetIndex(SetIndexBinder binder, DynamicMetaObject[] indexes, DynamicMetaObject value)
79+
{
80+
return metaObject.BindSetIndex(binder, indexes, value);
81+
}
82+
83+
public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
84+
{
85+
return metaObject.BindSetMember(binder, value);
86+
}
87+
88+
public override DynamicMetaObject BindUnaryOperation(UnaryOperationBinder binder)
89+
{
90+
return metaObject.BindUnaryOperation(binder);
91+
}
92+
93+
#endregion
94+
}
95+
}

0 commit comments

Comments
 (0)