Skip to content

Commit f6a8260

Browse files
committed
In JavaScriptEngineSwitcher.Jint changed a implementation of the InnerCallFunction method
1 parent 6f34d26 commit f6a8260

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Text;
44

55
using Jint;
6-
using IOriginalCallable = Jint.Native.ICallable;
76
using OriginalEngine = Jint.Engine;
87
using OriginalJavaScriptException = Jint.Runtime.JavaScriptException;
98
using OriginalMemoryLimitExceededException = Jint.Runtime.MemoryLimitExceededException;
@@ -101,10 +100,13 @@ public JintJsEngine(JintSettings settings)
101100
.Strict(jintSettings.StrictMode)
102101
.TimeoutInterval(jintSettings.TimeoutInterval)
103102
;
103+
104104
if (jintSettings.RegexTimeoutInterval.HasValue)
105105
{
106106
options.RegexTimeoutInterval(jintSettings.RegexTimeoutInterval.Value);
107107
}
108+
109+
options.AddObjectConverter(new UndefinedConverter());
108110
});
109111
}
110112
catch (Exception e)
@@ -140,11 +142,6 @@ private static OriginalParserOptions CreateParserOptions(string documentName)
140142
/// <returns>The mapped value</returns>
141143
private OriginalValue MapToScriptType(object value)
142144
{
143-
if (value is Undefined)
144-
{
145-
return OriginalValue.Undefined;
146-
}
147-
148145
return OriginalValue.FromObject(_jsEngine, value);
149146
}
150147

@@ -539,29 +536,11 @@ protected override object InnerCallFunction(string functionName, params object[]
539536
throw WrapJavaScriptException(e);
540537
}
541538

542-
var callable = functionValue.TryCast<IOriginalCallable>();
543-
if (callable == null)
544-
{
545-
throw new WrapperRuntimeException(
546-
string.Format(CoreStrings.Runtime_FunctionNotExist, functionName));
547-
}
548-
549-
int argumentCount = args.Length;
550-
var processedArgs = new OriginalValue[argumentCount];
551-
552-
if (argumentCount > 0)
553-
{
554-
for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
555-
{
556-
processedArgs[argumentIndex] = MapToScriptType(args[argumentIndex]);
557-
}
558-
}
559-
560539
OriginalValue resultValue;
561540

562541
try
563542
{
564-
resultValue = callable.Call(functionValue, processedArgs);
543+
resultValue = _jsEngine.Invoke(functionValue, args);
565544
}
566545
catch (OriginalJavaScriptException e)
567546
{
@@ -583,6 +562,11 @@ protected override object InnerCallFunction(string functionName, params object[]
583562
{
584563
throw WrapTimeoutException(e);
585564
}
565+
catch (ArgumentException e) when (e.Message == "Can only invoke functions")
566+
{
567+
throw new WrapperRuntimeException(
568+
string.Format(CoreStrings.Runtime_FunctionNotExist, functionName));
569+
}
586570

587571
result = MapToHostType(resultValue);
588572
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using IOriginalObjectConverter = Jint.Runtime.Interop.IObjectConverter;
2+
using OriginalEngine = Jint.Engine;
3+
using OriginalValue = Jint.Native.JsValue;
4+
5+
using JavaScriptEngineSwitcher.Core;
6+
7+
namespace JavaScriptEngineSwitcher.Jint
8+
{
9+
/// <summary>
10+
/// Converts a <see cref="Undefined"/> instance to a <see cref="OriginalValue"/> instance
11+
/// </summary>
12+
internal sealed class UndefinedConverter : IOriginalObjectConverter
13+
{
14+
public bool TryConvert(OriginalEngine engine, object value, out OriginalValue result)
15+
{
16+
if (value is Undefined)
17+
{
18+
result = OriginalValue.Undefined;
19+
return true;
20+
}
21+
22+
result = OriginalValue.Null;
23+
return false;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)