Skip to content

Commit 0f59e6d

Browse files
committed
In JavaScriptEngineSwitcher.V8 performed a migration to a modern API for pre-compilation of scripts
1 parent cead3af commit 0f59e6d

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

src/JavaScriptEngineSwitcher.V8/JavaScriptEngineSwitcher.V8.csproj

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_V8_Logo128x128.png</PackageIconFullPath>
2323
<Description>JavaScriptEngineSwitcher.V8 contains a `V8JsEngine` adapter (wrapper for the Microsoft ClearScript.V8).</Description>
2424
<PackageTags>$(PackageCommonTags);V8;ClearScript</PackageTags>
25-
<PackageReleaseNotes>1. Microsoft ClearScript.V8 was updated to version 7.5;
26-
2. No longer supports a .NET Framework 4.5;
27-
3. Added support for .NET Framework 4.6.2.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Performed a migration to a modern API for pre-compilation of scripts.</PackageReleaseNotes>
2826
</PropertyGroup>
2927

3028
<ItemGroup>

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

+22-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System.Text.RegularExpressions;
55

66
using OriginalCacheKind = Microsoft.ClearScript.V8.V8CacheKind;
7+
using OriginalCacheResult = Microsoft.ClearScript.V8.V8CacheResult;
8+
using OriginalDocumentFlags = Microsoft.ClearScript.DocumentFlags;
9+
using OriginalDocumentInfo = Microsoft.ClearScript.DocumentInfo;
710
using OriginalEngine = Microsoft.ClearScript.V8.V8ScriptEngine;
811
using OriginalEngineFlags = Microsoft.ClearScript.V8.V8ScriptEngineFlags;
912
using OriginalException = Microsoft.ClearScript.ScriptEngineException;
@@ -364,12 +367,13 @@ protected override IPrecompiledScript InnerPrecompile(string code)
364367

365368
protected override IPrecompiledScript InnerPrecompile(string code, string documentName)
366369
{
370+
var documentInfo = new OriginalDocumentInfo(documentName);
367371
var cacheKind = OriginalCacheKind.Code;
368372
byte[] cachedBytes;
369373

370374
try
371375
{
372-
using (OriginalScript generalScript = _jsEngine.Compile(documentName, code, cacheKind,
376+
using (OriginalScript generalScript = _jsEngine.Compile(documentInfo, code, cacheKind,
373377
out cachedBytes))
374378
{ }
375379
}
@@ -378,7 +382,7 @@ protected override IPrecompiledScript InnerPrecompile(string code, string docume
378382
throw WrapScriptEngineException(e);
379383
}
380384

381-
return new V8PrecompiledScript(code, cacheKind, cachedBytes, documentName);
385+
return new V8PrecompiledScript(code, cacheKind, cachedBytes, documentInfo);
382386
}
383387

384388
protected override object InnerEvaluate(string expression)
@@ -388,11 +392,15 @@ protected override object InnerEvaluate(string expression)
388392

389393
protected override object InnerEvaluate(string expression, string documentName)
390394
{
395+
var documentInfo = new OriginalDocumentInfo(documentName)
396+
{
397+
Flags = OriginalDocumentFlags.None
398+
};
391399
object result;
392400

393401
try
394402
{
395-
result = _jsEngine.Evaluate(documentName, false, expression);
403+
result = _jsEngine.Evaluate(documentInfo, expression);
396404
}
397405
catch (OriginalException e)
398406
{
@@ -427,9 +435,14 @@ protected override void InnerExecute(string code)
427435

428436
protected override void InnerExecute(string code, string documentName)
429437
{
438+
var documentInfo = new OriginalDocumentInfo(documentName)
439+
{
440+
Flags = OriginalDocumentFlags.None
441+
};
442+
430443
try
431444
{
432-
_jsEngine.Execute(documentName, false, code);
445+
_jsEngine.Execute(documentInfo, code);
433446
}
434447
catch (OriginalException e)
435448
{
@@ -453,15 +466,15 @@ protected override void InnerExecute(IPrecompiledScript precompiledScript)
453466
);
454467
}
455468

456-
bool cacheAccepted;
469+
byte[] cachedBytes = v8PrecompiledScript.CachedBytes;
457470

458471
try
459472
{
460-
using (OriginalScript script = _jsEngine.Compile(v8PrecompiledScript.DocumentName,
461-
v8PrecompiledScript.Code, v8PrecompiledScript.CacheKind, v8PrecompiledScript.CachedBytes,
462-
out cacheAccepted))
473+
using (OriginalScript script = _jsEngine.Compile(v8PrecompiledScript.DocumentInfo,
474+
v8PrecompiledScript.Code, v8PrecompiledScript.CacheKind, ref cachedBytes,
475+
out var cacheResult))
463476
{
464-
if (!cacheAccepted)
477+
if (cacheResult != OriginalCacheResult.Accepted && cacheResult != OriginalCacheResult.Verified)
465478
{
466479
throw new WrapperUsageException(Strings.Usage_PrecompiledScriptNotAccepted, Name, Version);
467480
}

src/JavaScriptEngineSwitcher.V8/V8PrecompiledScript.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OriginalCacheKind = Microsoft.ClearScript.V8.V8CacheKind;
2+
using OriginalDocumentInfo = Microsoft.ClearScript.DocumentInfo;
23

34
using JavaScriptEngineSwitcher.Core;
45

@@ -37,9 +38,9 @@ public byte[] CachedBytes
3738
}
3839

3940
/// <summary>
40-
/// Gets a document name
41+
/// Gets a meta-information for the document
4142
/// </summary>
42-
public string DocumentName
43+
public OriginalDocumentInfo DocumentInfo
4344
{
4445
get;
4546
private set;
@@ -52,14 +53,14 @@ public string DocumentName
5253
/// <param name="code">The source code of the script</param>
5354
/// <param name="cacheKind">The kind of cache data to be generated</param>
5455
/// <param name="cachedBytes">Cached data for accelerated recompilation</param>
55-
/// <param name="documentName">Document name</param>
56+
/// <param name="documentInfo">Meta-information for the document</param>
5657
public V8PrecompiledScript(string code, OriginalCacheKind cacheKind, byte[] cachedBytes,
57-
string documentName)
58+
OriginalDocumentInfo documentInfo)
5859
{
5960
Code = code;
6061
CacheKind = cacheKind;
6162
CachedBytes = cachedBytes;
62-
DocumentName = documentName;
63+
DocumentInfo = documentInfo;
6364
}
6465

6566

src/JavaScriptEngineSwitcher.V8/readme.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
=============
3232
RELEASE NOTES
3333
=============
34-
1. Microsoft ClearScript.V8 was updated to version 7.5;
35-
2. No longer supports a .NET Framework 4.5;
36-
3. Added support for .NET Framework 4.6.2.
34+
Performed a migration to a modern API for pre-compilation of scripts.
3735

3836
=============
3937
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/V8/PrecompilationTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ public void MappingRuntimeErrorDuringExecutionOfPrecompiledCode()
102102
Assert.Equal("Runtime error", exception.Category);
103103
Assert.Equal("Cannot read properties of null (reading '5')", exception.Description);
104104
Assert.Equal("TypeError", exception.Type);
105-
Assert.Equal("get-item [2].js", exception.DocumentName);
105+
Assert.Equal("get-item.js", exception.DocumentName);
106106
Assert.Equal(2, exception.LineNumber);
107107
Assert.Equal(18, exception.ColumnNumber);
108108
Assert.Equal(" var item = items[itemIndex];", exception.SourceFragment);
109109
Assert.Equal(
110-
" at getItem (get-item [2].js:2:18)" + Environment.NewLine +
111-
" at get-item [2].js:9:10" + Environment.NewLine +
112-
" at get-item [2].js:13:3",
110+
" at getItem (get-item.js:2:18)" + Environment.NewLine +
111+
" at get-item.js:9:10" + Environment.NewLine +
112+
" at get-item.js:13:3",
113113
exception.CallStack
114114
);
115115
}
@@ -177,10 +177,10 @@ public void GenerationOfRuntimeErrorMessage()
177177
return getFullName(firstName, lastName);
178178
})(getFullName);";
179179
string targetOutput = "ReferenceError: middleName is not defined" + Environment.NewLine +
180-
" at getFullName (get-full-name [2].js:2:35) -> var fullName = firstName + " +
180+
" at getFullName (get-full-name.js:2:35) -> var fullName = firstName + " +
181181
"' ' + middleName + ' ' + lastName;" + Environment.NewLine +
182-
" at get-full-name [2].js:12:9" + Environment.NewLine +
183-
" at get-full-name [2].js:13:3"
182+
" at get-full-name.js:12:9" + Environment.NewLine +
183+
" at get-full-name.js:13:3"
184184
;
185185

186186
JsRuntimeException exception = null;

0 commit comments

Comments
 (0)