Skip to content

Commit 79564f1

Browse files
committed
In JavaScriptEngineSwitcher.Msie:
1. Added support of MSIE JavaScript Engine version 3.0.0 Beta 2; 2. Added a ability to pre-compile scripts.
1 parent d835349 commit 79564f1

File tree

5 files changed

+436
-8
lines changed

5 files changed

+436
-8
lines changed

src/JavaScriptEngineSwitcher.Msie/JavaScriptEngineSwitcher.Msie.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
<Description>JavaScriptEngineSwitcher.Msie contains adapter `MsieJsEngine` (wrapper for the MSIE JavaScript Engine for .Net (http://github.com/Taritsyn/MsieJavaScriptEngine)). For correct working of the MSIE JavaScript Engine it is recommended to install Internet Explorer 9+ or Microsoft Edge on the machine.</Description>
1414
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_Msie_Logo128x128.png</PackageIconUrl>
1515
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;MSIE;IE;Chakra</PackageTags>
16-
<PackageReleaseNotes>Added support of MSIE JavaScript Engine version 3.0.0 Beta 1.</PackageReleaseNotes>
16+
<PackageReleaseNotes>1. Added support of MSIE JavaScript Engine version 3.0.0 Beta 2;
17+
2. Added a ability to pre-compile scripts.</PackageReleaseNotes>
1718
</PropertyGroup>
1819

1920
<Import Project="../../build/common.props" />
@@ -22,7 +23,7 @@
2223

2324
<ItemGroup>
2425
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.1.0" PrivateAssets="All" />
25-
<PackageReference Include="MsieJavaScriptEngine" Version="3.0.0-beta1" />
26+
<PackageReference Include="MsieJavaScriptEngine" Version="3.0.0-beta2" />
2627

2728
<ProjectReference Include="../JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.csproj" />
2829
</ItemGroup>

src/JavaScriptEngineSwitcher.Msie/MsieJsEngine.cs

Lines changed: 187 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using OriginalException = MsieJavaScriptEngine.JsException;
1313
using OriginalFatalException = MsieJavaScriptEngine.JsFatalException;
1414
using OriginalInterruptedException = MsieJavaScriptEngine.JsInterruptedException;
15+
using OriginalPrecompiledScript = MsieJavaScriptEngine.PrecompiledScript;
1516
using OriginalRuntimeException = MsieJavaScriptEngine.JsRuntimeException;
1617
using OriginalScriptException = MsieJavaScriptEngine.JsScriptException;
1718
using OriginalTypeConverter = MsieJavaScriptEngine.Utilities.TypeConverter;
@@ -215,12 +216,23 @@ private WrapperException WrapJsException(OriginalException originalException)
215216

216217
protected override IPrecompiledScript InnerPrecompile(string code)
217218
{
218-
throw new NotSupportedException();
219+
return InnerPrecompile(code, null);
219220
}
220221

221222
protected override IPrecompiledScript InnerPrecompile(string code, string documentName)
222223
{
223-
throw new NotSupportedException();
224+
OriginalPrecompiledScript precompiledScript;
225+
226+
try
227+
{
228+
precompiledScript = _jsEngine.Precompile(code, documentName);
229+
}
230+
catch (OriginalException e)
231+
{
232+
throw WrapJsException(e);
233+
}
234+
235+
return new MsiePrecompiledScript(precompiledScript);
224236
}
225237

226238
protected override object InnerEvaluate(string expression)
@@ -277,7 +289,24 @@ protected override void InnerExecute(string code, string documentName)
277289

278290
protected override void InnerExecute(IPrecompiledScript precompiledScript)
279291
{
280-
throw new NotSupportedException();
292+
var msiePrecompiledScript = precompiledScript as MsiePrecompiledScript;
293+
if (msiePrecompiledScript == null)
294+
{
295+
throw new WrapperUsageException(
296+
string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType,
297+
typeof(MsiePrecompiledScript).FullName),
298+
Name, Version
299+
);
300+
}
301+
302+
try
303+
{
304+
_jsEngine.Execute(msiePrecompiledScript.PrecompiledScript);
305+
}
306+
catch (OriginalException e)
307+
{
308+
throw WrapJsException(e);
309+
}
281310
}
282311

283312
protected override object InnerCallFunction(string functionName, params object[] args)
@@ -441,7 +470,7 @@ public override string Version
441470
/// </summary>
442471
public override bool SupportsScriptPrecompilation
443472
{
444-
get { return false; }
473+
get { return _jsEngine.SupportsScriptPrecompilation; }
445474
}
446475

447476
/// <summary>
@@ -461,6 +490,160 @@ public override bool SupportsGarbageCollection
461490
}
462491

463492

493+
public override IPrecompiledScript PrecompileFile(string path, Encoding encoding = null)
494+
{
495+
VerifyNotDisposed();
496+
497+
if (path == null)
498+
{
499+
throw new ArgumentNullException(
500+
nameof(path),
501+
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
502+
);
503+
}
504+
505+
if (string.IsNullOrWhiteSpace(path))
506+
{
507+
throw new ArgumentException(
508+
string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)),
509+
nameof(path)
510+
);
511+
}
512+
513+
if (!ValidationHelpers.CheckDocumentNameFormat(path))
514+
{
515+
throw new ArgumentException(
516+
string.Format(CoreStrings.Usage_InvalidFileNameFormat, path),
517+
nameof(path)
518+
);
519+
}
520+
521+
OriginalPrecompiledScript precompiledScript;
522+
523+
try
524+
{
525+
precompiledScript = _jsEngine.PrecompileFile(path, encoding);
526+
}
527+
catch (OriginalException e)
528+
{
529+
throw WrapJsException(e);
530+
}
531+
catch (FileNotFoundException)
532+
{
533+
throw;
534+
}
535+
536+
return new MsiePrecompiledScript(precompiledScript);
537+
}
538+
539+
public override IPrecompiledScript PrecompileResource(string resourceName, Type type)
540+
{
541+
VerifyNotDisposed();
542+
543+
if (resourceName == null)
544+
{
545+
throw new ArgumentNullException(
546+
nameof(resourceName),
547+
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName))
548+
);
549+
}
550+
551+
if (type == null)
552+
{
553+
throw new ArgumentNullException(
554+
nameof(type),
555+
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(type))
556+
);
557+
}
558+
559+
if (string.IsNullOrWhiteSpace(resourceName))
560+
{
561+
throw new ArgumentException(
562+
string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)),
563+
nameof(resourceName)
564+
);
565+
}
566+
567+
if (!ValidationHelpers.CheckDocumentNameFormat(resourceName))
568+
{
569+
throw new ArgumentException(
570+
string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName),
571+
nameof(resourceName)
572+
);
573+
}
574+
575+
OriginalPrecompiledScript precompiledScript;
576+
577+
try
578+
{
579+
precompiledScript = _jsEngine.PrecompileResource(resourceName, type);
580+
}
581+
catch (OriginalException e)
582+
{
583+
throw WrapJsException(e);
584+
}
585+
catch (NullReferenceException)
586+
{
587+
throw;
588+
}
589+
590+
return new MsiePrecompiledScript(precompiledScript);
591+
}
592+
593+
public override IPrecompiledScript PrecompileResource(string resourceName, Assembly assembly)
594+
{
595+
VerifyNotDisposed();
596+
597+
if (resourceName == null)
598+
{
599+
throw new ArgumentNullException(
600+
nameof(resourceName),
601+
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName))
602+
);
603+
}
604+
605+
if (assembly == null)
606+
{
607+
throw new ArgumentNullException(
608+
nameof(assembly),
609+
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly))
610+
);
611+
}
612+
613+
if (string.IsNullOrWhiteSpace(resourceName))
614+
{
615+
throw new ArgumentException(
616+
string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)),
617+
nameof(resourceName)
618+
);
619+
}
620+
621+
if (!ValidationHelpers.CheckDocumentNameFormat(resourceName))
622+
{
623+
throw new ArgumentException(
624+
string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName),
625+
nameof(resourceName)
626+
);
627+
}
628+
629+
OriginalPrecompiledScript precompiledScript;
630+
631+
try
632+
{
633+
precompiledScript = _jsEngine.PrecompileResource(resourceName, assembly);
634+
}
635+
catch (OriginalException e)
636+
{
637+
throw WrapJsException(e);
638+
}
639+
catch (NullReferenceException)
640+
{
641+
throw;
642+
}
643+
644+
return new MsiePrecompiledScript(precompiledScript);
645+
}
646+
464647
public override void ExecuteFile(string path, Encoding encoding = null)
465648
{
466649
VerifyNotDisposed();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using OriginalPrecompiledScript = MsieJavaScriptEngine.PrecompiledScript;
2+
3+
using JavaScriptEngineSwitcher.Core;
4+
5+
namespace JavaScriptEngineSwitcher.Msie
6+
{
7+
/// <summary>
8+
/// Represents a pre-compiled script that can be executed by different instances of the MSIE JS engine
9+
/// </summary>
10+
internal sealed class MsiePrecompiledScript : IPrecompiledScript
11+
{
12+
/// <summary>
13+
/// Gets a original pre-compiled script
14+
/// </summary>
15+
public OriginalPrecompiledScript PrecompiledScript
16+
{
17+
get;
18+
private set;
19+
}
20+
21+
22+
/// <summary>
23+
/// Constructs an instance of pre-compiled script
24+
/// </summary>
25+
/// <param name="precompiledScript">The original pre-compiled script</param>
26+
public MsiePrecompiledScript(OriginalPrecompiledScript precompiledScript)
27+
{
28+
PrecompiledScript = precompiledScript;
29+
}
30+
31+
32+
#region IPrecompiledScript implementation
33+
34+
/// <summary>
35+
/// Gets a name of JS engine for which the pre-compiled script was created
36+
/// </summary>
37+
public string EngineName
38+
{
39+
get { return MsieJsEngine.EngineName; }
40+
}
41+
42+
#endregion
43+
}
44+
}

src/JavaScriptEngineSwitcher.Msie/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
=============
2020
RELEASE NOTES
2121
=============
22-
Added support of MSIE JavaScript Engine version 3.0.0 Beta 1.
22+
1. Added support of MSIE JavaScript Engine version 3.0.0 Beta 2;
23+
2. Added a ability to pre-compile scripts.
2324

2425
=============
2526
DOCUMENTATION

0 commit comments

Comments
 (0)