Skip to content

Commit 5d34842

Browse files
Version 7.3.7: Added one-way CommonJS-ES6 module interop; fixed an intermittent ClearScriptV8 crash (GitHub Issue ClearFoundry#478); added IScriptEngine, IScriptObject, IJavaScriptObject, JavaScriptObjectKind, and JavaScriptObjectFlags; added DocumentAccessFlags.AllowCategoryMismatch; fixed host collection iteration with script access disabled by default at the engine level (GitHub Issue ClearFoundry#471); changed SunSpider benchmark to download from GitHub and print simple results to the console; added .NET 7 targets to test projects; updated API documentation. Tested with V8 10.9.194.10.
1 parent fd148c8 commit 5d34842

File tree

884 files changed

+6906
-2674
lines changed

Some content is hidden

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

884 files changed

+6906
-2674
lines changed

ClearScript/CanonicalRefTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private abstract class CanonicalRefMapBase : ICanonicalRefMap
7070
protected const int CompactionThreshold = 256 * 1024;
7171
protected static readonly TimeSpan CompactionInterval = TimeSpan.FromMinutes(2);
7272

73-
#region ICanonicalRefMap implementation (abstract)
73+
#region ICanonicalRefMap implementation
7474

7575
public abstract object GetRef(object obj);
7676

ClearScript/DefaultDocumentLoader.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,13 @@ private async Task<Document> LoadDocumentAsync(DocumentSettings settings, Uri ur
292292
var callback = settings.LoadCallback;
293293
callback?.Invoke(ref documentInfo);
294294

295-
return CacheDocument(new StringDocument(documentInfo, contents), false);
295+
var document = CacheDocument(new StringDocument(documentInfo, contents), false);
296+
if (!settings.AccessFlags.HasFlag(DocumentAccessFlags.AllowCategoryMismatch) && (documentInfo.Category != (category ?? DocumentCategory.Script)))
297+
{
298+
throw new FileLoadException("Document category mismatch", uri.IsFile ? uri.LocalPath : uri.AbsoluteUri);
299+
}
300+
301+
return document;
296302
}
297303

298304
#region DocumentLoader overrides

ClearScript/DocumentAccessFlags.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public enum DocumentAccessFlags
3636
/// considered a relative path. By default, any path that is not explicitly a top-level
3737
/// or root path is eligible.
3838
/// </summary>
39-
EnforceRelativePrefix = 0x00000004
39+
EnforceRelativePrefix = 0x00000004,
40+
41+
/// <summary>
42+
/// Relaxes the requirement that a loaded document must be of the requested category.
43+
/// </summary>
44+
AllowCategoryMismatch = 0x00000008
4045
}
4146
}

ClearScript/DocumentSettings.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,36 +155,12 @@ public void AddSystemDocument(string identifier, Document document)
155155

156156
internal Document LoadDocument(DocumentInfo? sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback)
157157
{
158-
var document = FindSystemDocument(specifier, category);
159-
if (document != null)
160-
{
161-
return document;
162-
}
163-
164-
document = Loader.LoadDocument(this, sourceInfo, specifier, category, contextCallback);
165-
if (document.Info.Category != (category ?? DocumentCategory.Script))
166-
{
167-
throw new FileLoadException("Loaded document category mismatch", "specifier");
168-
}
169-
170-
return document;
158+
return FindSystemDocument(specifier, category) ?? Loader.LoadDocument(this, sourceInfo, specifier, category, contextCallback);
171159
}
172160

173161
internal async Task<Document> LoadDocumentAsync(DocumentInfo? sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback)
174162
{
175-
var document = FindSystemDocument(specifier, category);
176-
if (document != null)
177-
{
178-
return document;
179-
}
180-
181-
document = await Loader.LoadDocumentAsync(this, sourceInfo, specifier, category, contextCallback).ConfigureAwait(false);
182-
if (document.Info.Category != (category ?? DocumentCategory.Script))
183-
{
184-
throw new FileLoadException("Loaded document category mismatch", "specifier");
185-
}
186-
187-
return document;
163+
return FindSystemDocument(specifier, category) ?? await Loader.LoadDocumentAsync(this, sourceInfo, specifier, category, contextCallback).ConfigureAwait(false);
188164
}
189165

190166
private Document FindSystemDocument(string identifier, DocumentCategory category)

ClearScript/Exports/VersionSymbols.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#pragma once
77

8-
#define CLEARSCRIPT_VERSION_STRING "7.3.6"
9-
#define CLEARSCRIPT_VERSION_COMMA_SEPARATED 7,3,6
10-
#define CLEARSCRIPT_VERSION_STRING_INFORMATIONAL "7.3.6"
8+
#define CLEARSCRIPT_VERSION_STRING "7.3.7"
9+
#define CLEARSCRIPT_VERSION_COMMA_SEPARATED 7,3,7
10+
#define CLEARSCRIPT_VERSION_STRING_INFORMATIONAL "7.3.7"
1111
#define CLEARSCRIPT_FILE_FLAGS 0L

ClearScript/IScriptEngine.cs

Lines changed: 1227 additions & 0 deletions
Large diffs are not rendered by default.

ClearScript/IScriptObject.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.ClearScript
8+
{
9+
/// <summary>
10+
/// Represents a script object.
11+
/// </summary>
12+
public interface IScriptObject : IDisposable
13+
{
14+
/// <summary>
15+
/// Gets the script engine that owns the object.
16+
/// </summary>
17+
ScriptEngine Engine { get; }
18+
19+
/// <summary>
20+
/// Gets the value of a named script object property.
21+
/// </summary>
22+
/// <param name="name">The name of the property to get.</param>
23+
/// <param name="args">Optional arguments for property retrieval.</param>
24+
/// <returns>The value of the specified property.</returns>
25+
object GetProperty(string name, params object[] args);
26+
27+
/// <summary>
28+
/// Sets the value of a named script object property.
29+
/// </summary>
30+
/// <param name="name">The name of the property to set.</param>
31+
/// <param name="args">An array containing optional arguments and the new property value.</param>
32+
/// <remarks>
33+
/// The <paramref name="args"></paramref> array must contain at least one element. The new
34+
/// property value must be the last element of the array.
35+
/// </remarks>
36+
void SetProperty(string name, params object[] args);
37+
38+
/// <summary>
39+
/// Removes a named script object property.
40+
/// </summary>
41+
/// <param name="name">The name of the property to remove.</param>
42+
/// <returns><c>True</c> if the property was removed successfully, <c>false</c> otherwise.</returns>
43+
bool DeleteProperty(string name);
44+
45+
/// <summary>
46+
/// Enumerates the script object's property names.
47+
/// </summary>
48+
IEnumerable<string> PropertyNames { get; }
49+
50+
/// <summary>
51+
/// Gets or sets the value of a named script object property.
52+
/// </summary>
53+
/// <param name="name">The name of the property to get or set.</param>
54+
/// <param name="args">Optional arguments for property access.</param>
55+
/// <returns>The value of the specified property.</returns>
56+
object this[string name, params object[] args] { get; set; }
57+
58+
/// <summary>
59+
/// Gets the value of an indexed script object property.
60+
/// </summary>
61+
/// <param name="index">The index of the property to get.</param>
62+
/// <returns>The value of the specified property.</returns>
63+
object GetProperty(int index);
64+
65+
/// <summary>
66+
/// Sets the value of an indexed script object property.
67+
/// </summary>
68+
/// <param name="index">The index of the property to set.</param>
69+
/// <param name="value">The new property value.</param>
70+
void SetProperty(int index, object value);
71+
72+
/// <summary>
73+
/// Removes an indexed script object property.
74+
/// </summary>
75+
/// <param name="index">The index of the property to remove.</param>
76+
/// <returns><c>True</c> if the property was removed successfully, <c>false</c> otherwise.</returns>
77+
bool DeleteProperty(int index);
78+
79+
/// <summary>
80+
/// Enumerates the script object's property indices.
81+
/// </summary>
82+
IEnumerable<int> PropertyIndices { get; }
83+
84+
/// <summary>
85+
/// Gets or sets the value of an indexed script object property.
86+
/// </summary>
87+
/// <param name="index">The index of the property to get or set.</param>
88+
/// <returns>The value of the specified property.</returns>
89+
object this[int index] { get; set; }
90+
91+
/// <summary>
92+
/// Invokes the script object.
93+
/// </summary>
94+
/// <param name="asConstructor"><c>True</c> to invoke the object as a constructor, <c>false</c> otherwise.</param>
95+
/// <param name="args">Optional arguments for object invocation.</param>
96+
/// <returns>The invocation result value.</returns>
97+
object Invoke(bool asConstructor, params object[] args);
98+
99+
/// <summary>
100+
/// Invokes a script object method.
101+
/// </summary>
102+
/// <param name="name">The name of the method to invoke.</param>
103+
/// <param name="args">Optional arguments for method invocation.</param>
104+
/// <returns>The invocation result value.</returns>
105+
object InvokeMethod(string name, params object[] args);
106+
107+
/// <summary>
108+
/// Invokes the script object as a function.
109+
/// </summary>
110+
/// <param name="args">Optional arguments for object invocation.</param>
111+
/// <returns>The invocation result value.</returns>
112+
object InvokeAsFunction(params object[] args);
113+
}
114+
}

ClearScript/JavaScript/CommonJSLegacyModule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Microsoft.ClearScript.JavaScript
77
{
88
/// <exclude/>
9+
[BypassCustomAttributeLoader]
10+
[DefaultScriptUsage(ScriptAccess.Full)]
911
public sealed class CommonJSLegacyModule
1012
{
1113
private readonly ScriptObject context;

ClearScript/JavaScript/CommonJSManager.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
78
using Microsoft.ClearScript.Util;
89

@@ -158,7 +159,9 @@ public static string GetAugmentedCode(string code)
158159
return codePrefix + code + codeSuffix;
159160
}
160161

161-
public object Process()
162+
public object Process() => Process(out _);
163+
164+
public object Process(out object marshaledExports)
162165
{
163166
if (module == null)
164167
{
@@ -175,15 +178,20 @@ public object Process()
175178
function = (ScriptObject)engine.MarshalToHost(engine.ScriptInvoke(() => Evaluator()), false);
176179
}
177180

178-
if (!invoked)
181+
object result;
182+
if (invoked)
183+
{
184+
result = Undefined.Value;
185+
}
186+
else
179187
{
180188
invoked = true;
181-
var result = function.Invoke(false, module, exports, require);
189+
result = function.Invoke(false, module, exports, require);
182190
exports = (module is CommonJSLegacyModule legacyModule) ? legacyModule.exports : ((ScriptObject)module).GetProperty("exports");
183-
return result;
184191
}
185192

186-
return Undefined.Value;
193+
marshaledExports = engine.MarshalToScript(exports);
194+
return result;
187195
}
188196

189197
private void Initialize(object scriptExports, object scriptRequire)
@@ -194,8 +202,12 @@ private void Initialize(object scriptExports, object scriptRequire)
194202

195203
private object Require(string specifier)
196204
{
197-
var settings = engine.DocumentSettings;
198-
var document = settings.LoadDocument(DocumentInfo.Info, specifier, ModuleCategory.CommonJS, null);
205+
var document = engine.DocumentSettings.LoadDocument(DocumentInfo.Info, specifier, ModuleCategory.CommonJS, null);
206+
if (document.Info.Category != ModuleCategory.CommonJS)
207+
{
208+
var uri = document.Info.Uri;
209+
throw new FileLoadException("Document category mismatch", uri.IsFile ? uri.LocalPath : uri.AbsoluteUri);
210+
}
199211

200212
var code = document.GetTextContents();
201213
if (engine.FormatCode)

ClearScript/JavaScript/IArrayBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.ClearScript.JavaScript
99
/// Represents a JavaScript
1010
/// <c><see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</see></c>.
1111
/// </summary>
12-
public interface IArrayBuffer
12+
public interface IArrayBuffer : IJavaScriptObject
1313
{
1414
/// <summary>
1515
/// Gets the size of the <c>ArrayBuffer</c> in bytes.

0 commit comments

Comments
 (0)