Skip to content

Commit a6c3eb1

Browse files
committed
Fixed a error #23 “GetResourceAsString does not work as expected”
1 parent f228412 commit a6c3eb1

File tree

9 files changed

+172
-25
lines changed

9 files changed

+172
-25
lines changed

NuGet/JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (MSIE JavaScript Engine for .Net, Microsoft ClearScript.V8, Jurassic, Jint and ChakraCore). This library allows you to quickly and easily switch to using of another JavaScript engine.</description>
1414
<summary>JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (MSIE JavaScript Engine for .Net, Microsoft ClearScript.V8, Jurassic, Jint and ChakraCore).</summary>
15-
<releaseNotes>Fixed a error #22 “Make Exception serializable”.</releaseNotes>
15+
<releaseNotes>Fixed a error #23 “GetResourceAsString does not work as expected”.</releaseNotes>
1616
<copyright>Copyright (c) 2013-2016 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1717
<language>en-US</language>
1818
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript</tags>

NuGet/JavaScriptEngineSwitcher.Core/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
=============
2020
RELEASE NOTES
2121
=============
22-
Fixed a error #22 “Make Exception serializable”.
22+
Fixed a error #23 “GetResourceAsString does not work as expected”.
2323

2424
=============
2525
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public sealed class ChakraCoreSettings
77
{
88
/// <summary>
9-
/// Gets or sets a flag for whether to disable any work (such as garbage collection)
9+
/// Gets or sets a flag for whether to disable any background work (such as garbage collection)
1010
/// on background threads
1111
/// </summary>
1212
public bool DisableBackgroundWork

src/JavaScriptEngineSwitcher.Core/JsEngineBase.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ public virtual void ExecuteResource(string resourceName, Type type)
147147
{
148148
VerifyNotDisposed();
149149

150-
if (string.IsNullOrWhiteSpace(resourceName))
150+
if (resourceName == null)
151151
{
152-
throw new ArgumentException(
153-
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
152+
throw new ArgumentNullException(
153+
"resourceName", string.Format(Strings.Common_ArgumentIsNull, "resourceName"));
154154
}
155155

156156
if (type == null)
@@ -159,6 +159,12 @@ public virtual void ExecuteResource(string resourceName, Type type)
159159
"type", string.Format(Strings.Common_ArgumentIsNull, "type"));
160160
}
161161

162+
if (string.IsNullOrWhiteSpace(resourceName))
163+
{
164+
throw new ArgumentException(
165+
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
166+
}
167+
162168
string code = Utils.GetResourceAsString(resourceName, type);
163169
Execute(code);
164170
}
@@ -167,10 +173,10 @@ public virtual void ExecuteResource(string resourceName, Assembly assembly)
167173
{
168174
VerifyNotDisposed();
169175

170-
if (string.IsNullOrWhiteSpace(resourceName))
176+
if (resourceName == null)
171177
{
172-
throw new ArgumentException(
173-
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
178+
throw new ArgumentNullException(
179+
"resourceName", string.Format(Strings.Common_ArgumentIsNull, "resourceName"));
174180
}
175181

176182
if (assembly == null)
@@ -179,6 +185,12 @@ public virtual void ExecuteResource(string resourceName, Assembly assembly)
179185
"assembly", string.Format(Strings.Common_ArgumentIsNull, "assembly"));
180186
}
181187

188+
if (string.IsNullOrWhiteSpace(resourceName))
189+
{
190+
throw new ArgumentException(
191+
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
192+
}
193+
182194
string code = Utils.GetResourceAsString(resourceName, assembly);
183195
Execute(code);
184196
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#if NETSTANDARD1_3
2+
using System;
3+
using System.IO;
4+
using System.Reflection;
5+
6+
using JavaScriptEngineSwitcher.Core.Resources;
7+
8+
namespace JavaScriptEngineSwitcher.Core.Utilities
9+
{
10+
/// <summary>
11+
/// Assembly extensions
12+
/// </summary>
13+
public static class AssemblyExtensions
14+
{
15+
/// <summary>
16+
/// Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly.
17+
/// </summary>
18+
/// <param name="source">This assembly.</param>
19+
/// <param name="type">The type whose namespace is used to scope the manifest resource name.</param>
20+
/// <param name="name">The case-sensitive name of the manifest resource being requested.</param>
21+
/// <returns>The manifest resource; or null if no resources were specified during compilation
22+
/// or if the resource is not visible to the caller.</returns>
23+
/// <exception cref="ArgumentNullException">The name parameter is null.</exception>
24+
/// <exception cref="ArgumentException">The name parameter is an empty string ("").</exception>
25+
/// <exception cref="FileLoadException">A file that was found could not be loaded.</exception>
26+
/// <exception cref="FileNotFoundException">name was not found.</exception>
27+
/// <exception cref="BadImageFormatException">name is not a valid assembly.</exception>
28+
/// <exception cref="NotImplementedException">Resource length is greater than System.Int64.MaxValue.</exception>
29+
public static Stream GetManifestResourceStream(this Assembly source, Type type, string name)
30+
{
31+
if (source == null)
32+
{
33+
throw new ArgumentNullException("source");
34+
}
35+
36+
if (type == null)
37+
{
38+
throw new ArgumentNullException("type");
39+
}
40+
41+
if (name == null)
42+
{
43+
throw new ArgumentNullException("name");
44+
}
45+
46+
if (string.IsNullOrWhiteSpace(name))
47+
{
48+
throw new ArgumentException(
49+
string.Format(Strings.Common_ArgumentIsEmpty, "name"), "name");
50+
}
51+
52+
Assembly assembly = type.GetTypeInfo().Assembly;
53+
string nameSpace = type.Namespace;
54+
string fullName = nameSpace != null ? nameSpace + "." + name : name;
55+
56+
return assembly.GetManifestResourceStream(fullName);
57+
}
58+
}
59+
}
60+
#endif

src/JavaScriptEngineSwitcher.Core/Utilities/Utils.cs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,39 @@ public static bool Is64BitProcess()
6969
/// <returns>Сontent of the embedded resource as string</returns>
7070
public static string GetResourceAsString(string resourceName, Type type)
7171
{
72+
if (resourceName == null)
73+
{
74+
throw new ArgumentNullException(
75+
"resourceName", string.Format(Strings.Common_ArgumentIsNull, "resourceName"));
76+
}
77+
78+
if (type == null)
79+
{
80+
throw new ArgumentNullException(
81+
"type", string.Format(Strings.Common_ArgumentIsNull, "type"));
82+
}
83+
84+
if (string.IsNullOrWhiteSpace(resourceName))
85+
{
86+
throw new ArgumentException(
87+
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
88+
}
89+
7290
Assembly assembly = type.GetTypeInfo().Assembly;
91+
string nameSpace = type.Namespace;
7392

74-
return GetResourceAsString(resourceName, assembly);
93+
if (nameSpace != null && resourceName.StartsWith(nameSpace, StringComparison.Ordinal))
94+
{
95+
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
96+
{
97+
return ReadResourceAsString(resourceName, stream);
98+
}
99+
}
100+
101+
using (Stream stream = assembly.GetManifestResourceStream(type, resourceName))
102+
{
103+
return ReadResourceAsString(resourceName, stream);
104+
}
75105
}
76106

77107
/// <summary>
@@ -82,18 +112,41 @@ public static string GetResourceAsString(string resourceName, Type type)
82112
/// <returns>Сontent of the embedded resource as string</returns>
83113
public static string GetResourceAsString(string resourceName, Assembly assembly)
84114
{
115+
if (resourceName == null)
116+
{
117+
throw new ArgumentNullException(
118+
"resourceName", string.Format(Strings.Common_ArgumentIsNull, "resourceName"));
119+
}
120+
121+
if (assembly == null)
122+
{
123+
throw new ArgumentNullException(
124+
"assembly", string.Format(Strings.Common_ArgumentIsNull, "assembly"));
125+
}
126+
127+
if (string.IsNullOrWhiteSpace(resourceName))
128+
{
129+
throw new ArgumentException(
130+
string.Format(Strings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
131+
}
132+
85133
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
86134
{
87-
if (stream == null)
88-
{
89-
throw new NullReferenceException(
90-
string.Format(Strings.Resources_ResourceIsNull, resourceName));
91-
}
135+
return ReadResourceAsString(resourceName, stream);
136+
}
137+
}
92138

93-
using (var reader = new StreamReader(stream))
94-
{
95-
return reader.ReadToEnd();
96-
}
139+
private static string ReadResourceAsString(string resourceName, Stream stream)
140+
{
141+
if (stream == null)
142+
{
143+
throw new NullReferenceException(
144+
string.Format(Strings.Resources_ResourceIsNull, resourceName));
145+
}
146+
147+
using (var reader = new StreamReader(stream))
148+
{
149+
return reader.ReadToEnd();
97150
}
98151
}
99152

src/JavaScriptEngineSwitcher.Core/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"buildOptions": {
3434
"compile": {
3535
"exclude": [
36+
"Utilities/AssemblyExtensions.cs",
3637
"Utilities/TypeInfoExtensions.cs"
3738
]
3839
}

src/JavaScriptEngineSwitcher.Vroom/VroomSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public sealed class VroomSettings
77
{
88
/// <summary>
9-
/// Gets or sets a maximum size of the young object heap
9+
/// Gets or sets a maximum size of the young object heap in bytes
1010
/// </summary>
1111
public int MaxYoungSpaceSize
1212
{
@@ -15,7 +15,7 @@ public int MaxYoungSpaceSize
1515
}
1616

1717
/// <summary>
18-
/// Gets or sets a maximum size of the old object heap
18+
/// Gets or sets a maximum size of the old object heap in bytes
1919
/// </summary>
2020
public int MaxOldSpaceSize
2121
{

test/JavaScriptEngineSwitcher.Tests/CommonTestsBase.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,28 @@ public virtual void ExecutionOfFileIsCorrect()
200200
}
201201

202202
[Fact]
203-
public virtual void ExecutionOfResourceByTypeIsCorrect()
203+
public virtual void ExecutionOfResourceByNameAndTypeIsCorrect()
204+
{
205+
// Arrange
206+
const string resourceName = "Resources.cube.js";
207+
const string input = "cube(5);";
208+
const int targetOutput = 125;
209+
210+
// Act
211+
int output;
212+
213+
using (var jsEngine = CreateJsEngine())
214+
{
215+
jsEngine.ExecuteResource(resourceName, typeof(CommonTestsBase));
216+
output = jsEngine.Evaluate<int>(input);
217+
}
218+
219+
// Assert
220+
Assert.Equal(targetOutput, output);
221+
}
222+
223+
[Fact]
224+
public virtual void ExecutionOfResourceByFullNameAndTypeIsCorrect()
204225
{
205226
// Arrange
206227
const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.cube.js";
@@ -212,7 +233,7 @@ public virtual void ExecutionOfResourceByTypeIsCorrect()
212233

213234
using (var jsEngine = CreateJsEngine())
214235
{
215-
jsEngine.ExecuteResource(resourceName, GetType());
236+
jsEngine.ExecuteResource(resourceName, typeof(CommonTestsBase));
216237
output = jsEngine.Evaluate<int>(input);
217238
}
218239

@@ -221,7 +242,7 @@ public virtual void ExecutionOfResourceByTypeIsCorrect()
221242
}
222243

223244
[Fact]
224-
public virtual void ExecutionOfResourceByAssemblyIsCorrect()
245+
public virtual void ExecutionOfResourceByFullNameAndAssemblyIsCorrect()
225246
{
226247
// Arrange
227248
const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.power.js";
@@ -233,7 +254,7 @@ public virtual void ExecutionOfResourceByAssemblyIsCorrect()
233254

234255
using (var jsEngine = CreateJsEngine())
235256
{
236-
jsEngine.ExecuteResource(resourceName, GetType().GetTypeInfo().Assembly);
257+
jsEngine.ExecuteResource(resourceName, typeof(CommonTestsBase).GetTypeInfo().Assembly);
237258
output = jsEngine.Evaluate<int>(input);
238259
}
239260

0 commit comments

Comments
 (0)