Skip to content

Commit 4cb448a

Browse files
committed
1. In IJsEngine interface was added SupportsScriptPrecompilation property and three new methods: Precompile, PrecompileFile and PrecompileResource;
2. In JavaScriptEngineSwitcher.V8, JavaScriptEngineSwitcher.Jurassic, JavaScriptEngineSwitcher.Jint and JavaScriptEngineSwitcher.ChakraCore added a ability to pre-compile scripts.
1 parent ccbadcb commit 4cb448a

Some content is hidden

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

48 files changed

+2797
-146
lines changed

JavaScriptEngineSwitcher.NoSamples.sln

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{53B43213-2
3838
EndProject
3939
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SharedFiles", "SharedFiles", "{958A803E-3A62-4E29-9866-9DCE451943BC}"
4040
ProjectSection(SolutionItems) = preProject
41+
test\SharedFiles\declinationOfMinutes.js = test\SharedFiles\declinationOfMinutes.js
4142
test\SharedFiles\link.txt = test\SharedFiles\link.txt
4243
test\SharedFiles\square.js = test\SharedFiles\square.js
4344
EndProjectSection
@@ -148,14 +149,14 @@ Global
148149
{238D7E69-7052-4DFC-83EF-79D3D124C12B}.Debug|Any CPU.Build.0 = Debug|Any CPU
149150
{238D7E69-7052-4DFC-83EF-79D3D124C12B}.Release|Any CPU.ActiveCfg = Release|Any CPU
150151
{238D7E69-7052-4DFC-83EF-79D3D124C12B}.Release|Any CPU.Build.0 = Release|Any CPU
151-
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
152-
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
153-
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
154-
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Release|Any CPU.Build.0 = Release|Any CPU
155152
{4BD3D648-A33B-4D4D-9C10-7FAA9C3FDFAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
156153
{4BD3D648-A33B-4D4D-9C10-7FAA9C3FDFAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
157154
{4BD3D648-A33B-4D4D-9C10-7FAA9C3FDFAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
158155
{4BD3D648-A33B-4D4D-9C10-7FAA9C3FDFAD}.Release|Any CPU.Build.0 = Release|Any CPU
156+
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
157+
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
158+
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
159+
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF}.Release|Any CPU.Build.0 = Release|Any CPU
159160
EndGlobalSection
160161
GlobalSection(SolutionProperties) = preSolution
161162
HideSolutionNode = FALSE
@@ -178,8 +179,8 @@ Global
178179
{893590AC-3D31-4DC8-A1B9-822BCC983A46} = {0C281F46-F1D2-4A1C-8560-375EDA65D680}
179180
{E14C4086-9877-4658-AE39-6313039A9076} = {0C281F46-F1D2-4A1C-8560-375EDA65D680}
180181
{238D7E69-7052-4DFC-83EF-79D3D124C12B} = {0C281F46-F1D2-4A1C-8560-375EDA65D680}
181-
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF} = {53B43213-2E66-42C2-8476-600A2FD2DA75}
182182
{4BD3D648-A33B-4D4D-9C10-7FAA9C3FDFAD} = {0C281F46-F1D2-4A1C-8560-375EDA65D680}
183+
{E95FDEF6-18A0-4E26-8FDF-B4B590E6EDAF} = {53B43213-2E66-42C2-8476-600A2FD2DA75}
183184
EndGlobalSection
184185
GlobalSection(ExtensibilityGlobals) = postSolution
185186
SolutionGuid = {8184BE59-ACBC-4CD1-9419-D59A0FAC6131}

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.1.105"
3+
"version": "2.1.200"
44
}
55
}

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,15 @@ private void ProjectMethods(JsValue target, Type type, bool instance)
936936
}
937937
}
938938

939-
private static WrapperException WrapJsException(OriginalException originalException)
939+
private static WrapperException WrapJsException(OriginalException originalException,
940+
string defaultDocumentName = null)
940941
{
941942
WrapperException wrapperException;
942943
JsErrorCode errorCode = originalException.ErrorCode;
943944
string description = originalException.Message;
944945
string message = description;
945946
string type = string.Empty;
946-
string documentName = string.Empty;
947+
string documentName = defaultDocumentName ?? string.Empty;
947948
int lineNumber = 0;
948949
int columnNumber = 0;
949950
string callStack = string.Empty;
@@ -973,7 +974,11 @@ private static WrapperException WrapJsException(OriginalException originalExcept
973974
if (metadataValue.HasProperty(urlPropertyId))
974975
{
975976
JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId);
976-
documentName = urlPropertyValue.ConvertToString().ToString();
977+
string url = urlPropertyValue.ConvertToString().ToString();
978+
if (url != "undefined")
979+
{
980+
documentName = url;
981+
}
977982
}
978983

979984
JsPropertyId linePropertyId = JsPropertyId.FromString("line");
@@ -1259,6 +1264,35 @@ private static WrapperEngineLoadException WrapDllNotFoundException(
12591264

12601265
#region JsEngineBase overrides
12611266

1267+
protected override IPrecompiledScript InnerPrecompile(string code)
1268+
{
1269+
return InnerPrecompile(code, null);
1270+
}
1271+
1272+
protected override IPrecompiledScript InnerPrecompile(string code, string documentName)
1273+
{
1274+
string uniqueDocumentName = _documentNameManager.GetUniqueName(documentName);
1275+
1276+
IPrecompiledScript precompiledScript = _dispatcher.Invoke(() =>
1277+
{
1278+
using (CreateJsScope())
1279+
{
1280+
try
1281+
{
1282+
byte[] cachedBytes = JsContext.SerializeScript(code);
1283+
1284+
return new ChakraCorePrecompiledScript(code, cachedBytes, uniqueDocumentName);
1285+
}
1286+
catch (OriginalException e)
1287+
{
1288+
throw WrapJsException(e, uniqueDocumentName);
1289+
}
1290+
}
1291+
});
1292+
1293+
return precompiledScript;
1294+
}
1295+
12621296
protected override object InnerEvaluate(string expression)
12631297
{
12641298
return InnerEvaluate(expression, null);
@@ -1326,6 +1360,36 @@ protected override void InnerExecute(string code, string documentName)
13261360
});
13271361
}
13281362

1363+
protected override void InnerExecute(IPrecompiledScript precompiledScript)
1364+
{
1365+
var chakraCorePrecompiledScript = precompiledScript as ChakraCorePrecompiledScript;
1366+
if (chakraCorePrecompiledScript == null)
1367+
{
1368+
throw new WrapperUsageException(
1369+
string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType,
1370+
typeof(ChakraCorePrecompiledScript).FullName),
1371+
Name, Version
1372+
);
1373+
}
1374+
1375+
_dispatcher.Invoke(() =>
1376+
{
1377+
using (CreateJsScope())
1378+
{
1379+
try
1380+
{
1381+
JsContext.RunSerializedScript(chakraCorePrecompiledScript.Code,
1382+
chakraCorePrecompiledScript.CachedBytes, _jsSourceContext++,
1383+
chakraCorePrecompiledScript.DocumentName);
1384+
}
1385+
catch (OriginalException e)
1386+
{
1387+
throw WrapJsException(e);
1388+
}
1389+
}
1390+
});
1391+
}
1392+
13291393
protected override object InnerCallFunction(string functionName, params object[] args)
13301394
{
13311395
object result = _dispatcher.Invoke(() =>
@@ -1576,6 +1640,14 @@ public override string Version
15761640
get { return EngineVersion; }
15771641
}
15781642

1643+
/// <summary>
1644+
/// Gets a value that indicates if the JS engine supports script pre-compilation
1645+
/// </summary>
1646+
public override bool SupportsScriptPrecompilation
1647+
{
1648+
get { return true; }
1649+
}
1650+
15791651
/// <summary>
15801652
/// Gets a value that indicates if the JS engine supports script interruption
15811653
/// </summary>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using JavaScriptEngineSwitcher.Core;
2+
3+
namespace JavaScriptEngineSwitcher.ChakraCore
4+
{
5+
/// <summary>
6+
/// Represents a pre-compiled script that can be executed by different instances of the ChakraCore JS engine
7+
/// </summary>
8+
internal sealed class ChakraCorePrecompiledScript : IPrecompiledScript
9+
{
10+
/// <summary>
11+
/// Gets a source code of the script
12+
/// </summary>
13+
public string Code
14+
{
15+
get;
16+
private set;
17+
}
18+
19+
/// <summary>
20+
/// Gets a cached data for accelerated recompilation
21+
/// </summary>
22+
public byte[] CachedBytes
23+
{
24+
get;
25+
private set;
26+
}
27+
28+
/// <summary>
29+
/// Gets a document name
30+
/// </summary>
31+
public string DocumentName
32+
{
33+
get;
34+
private set;
35+
}
36+
37+
38+
/// <summary>
39+
/// Constructs an instance of pre-compiled script
40+
/// </summary>
41+
/// <param name="code">The source code of the script</param>
42+
/// <param name="cachedBytes">Cached data for accelerated recompilation</param>
43+
/// <param name="documentName">Document name</param>
44+
public ChakraCorePrecompiledScript(string code, byte[] cachedBytes, string documentName)
45+
{
46+
Code = code;
47+
CachedBytes = cachedBytes;
48+
DocumentName = documentName;
49+
}
50+
51+
52+
#region IPrecompiledScript implementation
53+
54+
/// <summary>
55+
/// Gets a name of JS engine for which the pre-compiled script was created
56+
/// </summary>
57+
public string EngineName
58+
{
59+
get { return ChakraCoreJsEngine.EngineName; }
60+
}
61+
62+
#endregion
63+
}
64+
}

src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>ChakraCore was updated to version 1.8.3.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Added a ability to pre-compile scripts.</PackageReleaseNotes>
2626
</PropertyGroup>
2727

2828
<Import Project="../../build/common.props" />

0 commit comments

Comments
 (0)