Skip to content

Commit 17a3086

Browse files
committed
In IJsEngine interface was added overloaded versions of the Evaluate, Evaluate<T> and Execute methods, which take the document name as second parameter
1 parent d5b51a4 commit 17a3086

File tree

6 files changed

+172
-11
lines changed

6 files changed

+172
-11
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
4242
/// </summary>
4343
private JsContext _jsContext;
4444

45+
/// <summary>
46+
/// JS source context
47+
/// </summary>
48+
JsSourceContext _jsSourceContext = JsSourceContext.FromIntPtr(IntPtr.Zero);
49+
4550
/// <summary>
4651
/// Set of external objects
4752
/// </summary>
@@ -943,10 +948,15 @@ private static JsRuntimeException ConvertJsExceptionToJsRuntimeException(
943948
#region JsEngineBase implementation
944949

945950
protected override object InnerEvaluate(string expression)
951+
{
952+
return InnerEvaluate(expression, string.Empty);
953+
}
954+
955+
protected override object InnerEvaluate(string expression, string documentName)
946956
{
947957
object result = InvokeScript(() =>
948958
{
949-
JsValue resultValue = JsContext.RunScript(expression);
959+
JsValue resultValue = JsContext.RunScript(expression, _jsSourceContext++, documentName);
950960

951961
return MapToHostType(resultValue);
952962
});
@@ -956,14 +966,24 @@ protected override object InnerEvaluate(string expression)
956966

957967
protected override T InnerEvaluate<T>(string expression)
958968
{
959-
object result = InnerEvaluate(expression);
969+
return InnerEvaluate<T>(expression, string.Empty);
970+
}
971+
972+
protected override T InnerEvaluate<T>(string expression, string documentName)
973+
{
974+
object result = InnerEvaluate(expression, documentName);
960975

961976
return TypeConverter.ConvertToType<T>(result);
962977
}
963978

964979
protected override void InnerExecute(string code)
965980
{
966-
InvokeScript(() => JsContext.RunScript(code));
981+
InnerExecute(code, string.Empty);
982+
}
983+
984+
protected override void InnerExecute(string code, string documentName)
985+
{
986+
InvokeScript(() => JsContext.RunScript(code, _jsSourceContext++, documentName));
967987
}
968988

969989
protected override object InnerCallFunction(string functionName, params object[] args)

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static JsValue ParseScript(string script)
208208
/// Requires an active script context.
209209
/// </remarks>
210210
/// <param name="script">The script to run</param>
211-
/// <param name="sourceContext"> The cookie identifying the script that can be used
211+
/// <param name="sourceContext">The cookie identifying the script that can be used
212212
/// by script contexts that have debugging enabled</param>
213213
/// <param name="sourceName">The location the script came from</param>
214214
/// <returns>The result of the script, if any</returns>

src/JavaScriptEngineSwitcher.Core/IJsEngine.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ bool SupportsGarbageCollection
4141
/// <returns>Result of the expression</returns>
4242
object Evaluate(string expression);
4343

44+
/// <summary>
45+
/// Evaluates an expression
46+
/// </summary>
47+
/// <param name="expression">JS-expression</param>
48+
/// <param name="documentName">Document name</param>
49+
/// <returns>Result of the expression</returns>
50+
object Evaluate(string expression, string documentName);
51+
4452
/// <summary>
4553
/// Evaluates an expression
4654
/// </summary>
@@ -49,12 +57,28 @@ bool SupportsGarbageCollection
4957
/// <returns>Result of the expression</returns>
5058
T Evaluate<T>(string expression);
5159

60+
/// <summary>
61+
/// Evaluates an expression
62+
/// </summary>
63+
/// <typeparam name="T">Type of result</typeparam>
64+
/// <param name="expression">JS-expression</param>
65+
/// <param name="documentName">Document name</param>
66+
/// <returns>Result of the expression</returns>
67+
T Evaluate<T>(string expression, string documentName);
68+
5269
/// <summary>
5370
/// Executes a code
5471
/// </summary>
55-
/// <param name="code">Code</param>
72+
/// <param name="code">JS-code</param>
5673
void Execute(string code);
5774

75+
/// <summary>
76+
/// Executes a code
77+
/// </summary>
78+
/// <param name="code">JS-code</param>
79+
/// <param name="documentName">Document name</param>
80+
void Execute(string code, string documentName);
81+
5882
/// <summary>
5983
/// Executes a code from JS-file
6084
/// </summary>

src/JavaScriptEngineSwitcher.Core/JsEngineBase.cs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,25 @@ protected void VerifyNotDisposed()
2929

3030
protected abstract object InnerEvaluate(string expression);
3131

32+
protected virtual object InnerEvaluate(string expression, string documentName)
33+
{
34+
return InnerEvaluate(expression);
35+
}
36+
3237
protected abstract T InnerEvaluate<T>(string expression);
3338

39+
protected virtual T InnerEvaluate<T>(string expression, string documentName)
40+
{
41+
return InnerEvaluate<T>(expression);
42+
}
43+
3444
protected abstract void InnerExecute(string code);
3545

46+
protected virtual void InnerExecute(string code, string documentName)
47+
{
48+
InnerExecute(code);
49+
}
50+
3651
protected abstract object InnerCallFunction(string functionName, params object[] args);
3752

3853
protected abstract T InnerCallFunction<T>(string functionName, params object[] args);
@@ -96,6 +111,19 @@ public virtual object Evaluate(string expression)
96111
return InnerEvaluate(expression);
97112
}
98113

114+
public virtual object Evaluate(string expression, string documentName)
115+
{
116+
VerifyNotDisposed();
117+
118+
if (string.IsNullOrWhiteSpace(expression))
119+
{
120+
throw new ArgumentException(
121+
string.Format(Strings.Common_ArgumentIsEmpty, "expression"), "expression");
122+
}
123+
124+
return InnerEvaluate(expression, documentName);
125+
}
126+
99127
public virtual T Evaluate<T>(string expression)
100128
{
101129
VerifyNotDisposed();
@@ -116,6 +144,26 @@ public virtual T Evaluate<T>(string expression)
116144
return InnerEvaluate<T>(expression);
117145
}
118146

147+
public virtual T Evaluate<T>(string expression, string documentName)
148+
{
149+
VerifyNotDisposed();
150+
151+
if (string.IsNullOrWhiteSpace(expression))
152+
{
153+
throw new ArgumentException(
154+
string.Format(Strings.Common_ArgumentIsEmpty, "expression"), "expression");
155+
}
156+
157+
Type returnValueType = typeof(T);
158+
if (!ValidationHelpers.IsSupportedType(returnValueType))
159+
{
160+
throw new NotSupportedTypeException(
161+
string.Format(Strings.Runtime_ReturnValueTypeNotSupported, returnValueType.FullName));
162+
}
163+
164+
return InnerEvaluate<T>(expression, documentName);
165+
}
166+
119167
public virtual void Execute(string code)
120168
{
121169
VerifyNotDisposed();
@@ -129,6 +177,19 @@ public virtual void Execute(string code)
129177
InnerExecute(code);
130178
}
131179

180+
public virtual void Execute(string code, string documentName)
181+
{
182+
VerifyNotDisposed();
183+
184+
if (string.IsNullOrWhiteSpace(code))
185+
{
186+
throw new ArgumentException(
187+
string.Format(Strings.Common_ArgumentIsEmpty, "code"), "code");
188+
}
189+
190+
InnerExecute(code, documentName);
191+
}
192+
132193
public virtual void ExecuteFile(string path, Encoding encoding = null)
133194
{
134195
VerifyNotDisposed();
@@ -140,7 +201,7 @@ public virtual void ExecuteFile(string path, Encoding encoding = null)
140201
}
141202

142203
string code = Utils.GetFileTextContent(path, encoding);
143-
Execute(code);
204+
Execute(code, path);
144205
}
145206

146207
public virtual void ExecuteResource(string resourceName, Type type)
@@ -166,7 +227,7 @@ public virtual void ExecuteResource(string resourceName, Type type)
166227
}
167228

168229
string code = Utils.GetResourceAsString(resourceName, type);
169-
Execute(code);
230+
Execute(code, resourceName);
170231
}
171232

172233
public virtual void ExecuteResource(string resourceName, Assembly assembly)
@@ -192,7 +253,7 @@ public virtual void ExecuteResource(string resourceName, Assembly assembly)
192253
}
193254

194255
string code = Utils.GetResourceAsString(resourceName, assembly);
195-
Execute(code);
256+
Execute(code, resourceName);
196257
}
197258

198259
public virtual object CallFunction(string functionName, params object[] args)

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,19 @@ private JsRuntimeException ConvertScriptEngineExceptionToJsRuntimeException(
252252
#region JsEngineBase implementation
253253

254254
protected override object InnerEvaluate(string expression)
255+
{
256+
return InnerEvaluate(expression, null);
257+
}
258+
259+
protected override object InnerEvaluate(string expression, string documentName)
255260
{
256261
object result;
257262

258263
lock (_executionSynchronizer)
259264
{
260265
try
261266
{
262-
result = _jsEngine.Evaluate(expression);
267+
result = _jsEngine.Evaluate(documentName, expression);
263268
}
264269
catch (OriginalJsException e)
265270
{
@@ -274,18 +279,28 @@ protected override object InnerEvaluate(string expression)
274279

275280
protected override T InnerEvaluate<T>(string expression)
276281
{
277-
object result = InnerEvaluate(expression);
282+
return InnerEvaluate<T>(expression, null);
283+
}
284+
285+
protected override T InnerEvaluate<T>(string expression, string documentName)
286+
{
287+
object result = InnerEvaluate(expression, documentName);
278288

279289
return TypeConverter.ConvertToType<T>(result);
280290
}
281291

282292
protected override void InnerExecute(string code)
293+
{
294+
InnerExecute(code, null);
295+
}
296+
297+
protected override void InnerExecute(string code, string documentName)
283298
{
284299
lock (_executionSynchronizer)
285300
{
286301
try
287302
{
288-
_jsEngine.Execute(code);
303+
_jsEngine.Execute(documentName, code);
289304
}
290305
catch (OriginalJsException e)
291306
{

src/JavaScriptEngineSwitcher.Vroom/VroomJsEngine.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,39 @@ protected override object InnerEvaluate(string expression)
187187
return result;
188188
}
189189

190+
protected override object InnerEvaluate(string expression, string documentName)
191+
{
192+
object result;
193+
194+
lock (_executionSynchronizer)
195+
{
196+
try
197+
{
198+
result = _jsContext.Execute(expression, documentName);
199+
}
200+
catch (OriginalJsException e)
201+
{
202+
throw ConvertJsExceptionToJsRuntimeException(e);
203+
}
204+
}
205+
206+
return result;
207+
}
208+
190209
protected override T InnerEvaluate<T>(string expression)
191210
{
192211
object result = InnerEvaluate(expression);
193212

194213
return TypeConverter.ConvertToType<T>(result);
195214
}
196215

216+
protected override T InnerEvaluate<T>(string expression, string documentName)
217+
{
218+
object result = InnerEvaluate(expression, documentName);
219+
220+
return TypeConverter.ConvertToType<T>(result);
221+
}
222+
197223
protected override void InnerExecute(string code)
198224
{
199225
lock (_executionSynchronizer)
@@ -209,6 +235,21 @@ protected override void InnerExecute(string code)
209235
}
210236
}
211237

238+
protected override void InnerExecute(string code, string documentName)
239+
{
240+
lock (_executionSynchronizer)
241+
{
242+
try
243+
{
244+
_jsContext.Execute(code, documentName);
245+
}
246+
catch (OriginalJsException e)
247+
{
248+
throw ConvertJsExceptionToJsRuntimeException(e);
249+
}
250+
}
251+
}
252+
212253
protected override object InnerCallFunction(string functionName, params object[] args)
213254
{
214255
string serializedArguments = string.Empty;

0 commit comments

Comments
 (0)