Skip to content

Commit f3ee708

Browse files
committed
Was made refactoring
1 parent afdb641 commit f3ee708

File tree

2 files changed

+46
-75
lines changed

2 files changed

+46
-75
lines changed

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ namespace MsieJavaScriptEngine.ActiveScript
1919
/// </summary>
2020
internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
2121
{
22+
/// <summary>
23+
/// Name of resource, which contains a ECMAScript 5 Polyfill
24+
/// </summary>
25+
private const string ES5_POLYFILL_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.ES5.min.js";
26+
27+
/// <summary>
28+
/// Name of resource, which contains a JSON2 library
29+
/// </summary>
30+
private const string JSON2_LIBRARY_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.json2.min.js";
31+
2232
/// <summary>
2333
/// Instance of Active Script wrapper
2434
/// </summary>
@@ -126,6 +136,7 @@ protected ActiveScriptJsEngineBase(JsEngineSettings settings, string clsid,
126136
_activeScriptWrapper.SetScriptSite(CreateScriptSite());
127137
_activeScriptWrapper.InitNew();
128138
_activeScriptWrapper.SetScriptState(ScriptState.Started);
139+
LoadPolyfills();
129140

130141
_dispatch = WrapScriptDispatch(_activeScriptWrapper.GetScriptDispatch());
131142
});
@@ -310,6 +321,29 @@ private void ThrowError()
310321
/// <returns>Error type</returns>
311322
protected abstract string GetErrorTypeByNumber(int errorNumber);
312323

324+
/// <summary>
325+
/// Loads a JS polyfills
326+
/// </summary>
327+
private void LoadPolyfills()
328+
{
329+
Assembly assembly = GetType()
330+
#if !NET40
331+
.GetTypeInfo()
332+
#endif
333+
.Assembly
334+
;
335+
336+
if (_settings.UseEcmaScript5Polyfill)
337+
{
338+
InnerExecuteResource(ES5_POLYFILL_RESOURCE_NAME, assembly);
339+
}
340+
341+
if (_settings.UseJson2Library)
342+
{
343+
InnerExecuteResource(JSON2_LIBRARY_RESOURCE_NAME, assembly);
344+
}
345+
}
346+
313347
/// <summary>
314348
/// Executes a script text
315349
/// </summary>
@@ -345,6 +379,17 @@ private object InnerExecute(string code, string documentName, bool isExpression)
345379
return result;
346380
}
347381

382+
/// <summary>
383+
/// Executes a code from embedded JS resource
384+
/// </summary>
385+
/// <param name="resourceName">The case-sensitive resource name</param>
386+
/// <param name="assembly">The assembly, which contains the embedded resource</param>
387+
private void InnerExecuteResource(string resourceName, Assembly assembly)
388+
{
389+
string code = Utils.GetResourceAsString(resourceName, assembly);
390+
InnerExecute(code, resourceName, false);
391+
}
392+
348393
/// <summary>
349394
/// Try create a debug document
350395
/// </summary>

src/MsieJavaScriptEngine/ActiveScript/ClassicActiveScriptJsEngine.cs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#if !NETSTANDARD
2-
using System;
32
using System.Collections.Generic;
4-
using System.Reflection;
53

64
using MsieJavaScriptEngine.Constants;
7-
#if NET40
8-
using MsieJavaScriptEngine.Polyfills.System;
9-
#endif
10-
using MsieJavaScriptEngine.Resources;
11-
using MsieJavaScriptEngine.Utilities;
125

136
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
147

@@ -19,16 +12,6 @@ namespace MsieJavaScriptEngine.ActiveScript
1912
/// </summary>
2013
internal sealed partial class ClassicActiveScriptJsEngine : ActiveScriptJsEngineBase
2114
{
22-
/// <summary>
23-
/// Name of resource, which contains a ECMAScript 5 Polyfill
24-
/// </summary>
25-
private const string ES5_POLYFILL_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.ES5.min.js";
26-
27-
/// <summary>
28-
/// Name of resource, which contains a JSON2 library
29-
/// </summary>
30-
private const string JSON2_LIBRARY_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.json2.min.js";
31-
3215
/// <summary>
3316
/// Flag indicating whether this JS engine is supported
3417
/// </summary>
@@ -85,9 +68,7 @@ internal sealed partial class ClassicActiveScriptJsEngine : ActiveScriptJsEngine
8568
/// <param name="settings">JS engine settings</param>
8669
public ClassicActiveScriptJsEngine(JsEngineSettings settings)
8770
: base(settings, ClassId.Classic, ScriptLanguageVersion.None, "6", "Microsoft JScript ")
88-
{
89-
LoadResources(_settings.UseEcmaScript5Polyfill, _settings.UseJson2Library);
90-
}
71+
{ }
9172

9273

9374
/// <summary>
@@ -111,61 +92,6 @@ protected override string GetErrorTypeByNumber(int errorNumber)
11192
return ActiveScriptJsErrorHelpers.GetErrorTypeByNumber(errorNumber, _runtimeErrorTypeMap);
11293
}
11394

114-
/// <summary>
115-
/// Loads a resources
116-
/// </summary>
117-
/// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
118-
/// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
119-
private void LoadResources(bool useEcmaScript5Polyfill, bool useJson2Library)
120-
{
121-
Assembly assembly = GetType().GetTypeInfo().Assembly;
122-
123-
if (useEcmaScript5Polyfill)
124-
{
125-
ExecuteResource(ES5_POLYFILL_RESOURCE_NAME, assembly);
126-
}
127-
128-
if (useJson2Library)
129-
{
130-
ExecuteResource(JSON2_LIBRARY_RESOURCE_NAME, assembly);
131-
}
132-
}
133-
134-
/// <summary>
135-
/// Executes a code from embedded JS-resource
136-
/// </summary>
137-
/// <param name="resourceName">The case-sensitive resource name</param>
138-
/// <param name="assembly">The assembly, which contains the embedded resource</param>
139-
private void ExecuteResource(string resourceName, Assembly assembly)
140-
{
141-
if (resourceName == null)
142-
{
143-
throw new ArgumentNullException(
144-
nameof(resourceName),
145-
string.Format(CommonStrings.Common_ArgumentIsNull, nameof(resourceName))
146-
);
147-
}
148-
149-
if (assembly == null)
150-
{
151-
throw new ArgumentNullException(
152-
nameof(assembly),
153-
string.Format(CommonStrings.Common_ArgumentIsNull, nameof(assembly))
154-
);
155-
}
156-
157-
if (string.IsNullOrWhiteSpace(resourceName))
158-
{
159-
throw new ArgumentException(
160-
string.Format(CommonStrings.Common_ArgumentIsEmpty, nameof(resourceName)),
161-
nameof(resourceName)
162-
);
163-
}
164-
165-
string code = Utils.GetResourceAsString(resourceName, assembly);
166-
Execute(code, resourceName);
167-
}
168-
16995
#region ActiveScriptJsEngineBase overrides
17096

17197
/// <summary>

0 commit comments

Comments
 (0)