Skip to content

Commit b115280

Browse files
committed
Improved implementation of the CallFunction method for Chakra JsRT modes
1 parent 95d040b commit b115280

File tree

4 files changed

+28
-222
lines changed

4 files changed

+28
-222
lines changed

src/MsieJavaScriptEngine/JsRt/Edge/ChakraEdgeJsRtJsEngine.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Globalization;
55
using System.Linq;
6-
using System.Text;
76

87
using Constants;
98
using Helpers;
@@ -412,34 +411,26 @@ public void Execute(string code)
412411

413412
public object CallFunction(string functionName, params object[] args)
414413
{
415-
string serializedArguments = string.Empty;
416-
int argumentCount = args.Length;
417-
418-
if (argumentCount == 1)
419-
{
420-
object value = args[0];
421-
serializedArguments = SimplisticJsSerializer.Serialize(value);
422-
}
423-
else if (argumentCount > 1)
414+
object result = InvokeScript(() =>
424415
{
425-
var serializedArgumentsBuilder = new StringBuilder();
416+
EdgeJsValue globalObj = EdgeJsValue.GlobalObject;
417+
EdgeJsPropertyId functionId = EdgeJsPropertyId.FromString(functionName);
426418

427-
for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
419+
bool functionExist = globalObj.HasProperty(functionId);
420+
if (!functionExist)
428421
{
429-
object value = args[argumentIndex];
430-
string serializedValue = SimplisticJsSerializer.Serialize(value);
431-
432-
if (argumentIndex > 0)
433-
{
434-
serializedArgumentsBuilder.Append(", ");
435-
}
436-
serializedArgumentsBuilder.Append(serializedValue);
422+
throw new JsRuntimeException(
423+
string.Format(Strings.Runtime_FunctionNotExist, functionName));
437424
}
438425

439-
serializedArguments = serializedArgumentsBuilder.ToString();
440-
}
426+
var processedArgs = MapToScriptType(args);
427+
var allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
441428

442-
object result = Evaluate(string.Format("{0}({1});", functionName, serializedArguments));
429+
EdgeJsValue functionValue = globalObj.GetProperty(functionId);
430+
EdgeJsValue resultValue = functionValue.CallFunction(allProcessedArgs);
431+
432+
return MapToHostType(resultValue);
433+
});
443434

444435
return result;
445436
}

src/MsieJavaScriptEngine/JsRt/Ie/ChakraIeJsRtJsEngine.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Globalization;
55
using System.Linq;
6-
using System.Text;
76

87
using Constants;
98
using Helpers;
@@ -433,34 +432,26 @@ public void Execute(string code)
433432

434433
public object CallFunction(string functionName, params object[] args)
435434
{
436-
string serializedArguments = string.Empty;
437-
int argumentCount = args.Length;
438-
439-
if (argumentCount == 1)
440-
{
441-
object value = args[0];
442-
serializedArguments = SimplisticJsSerializer.Serialize(value);
443-
}
444-
else if (argumentCount > 1)
435+
object result = InvokeScript(() =>
445436
{
446-
var serializedArgumentsBuilder = new StringBuilder();
437+
IeJsValue globalObj = IeJsValue.GlobalObject;
438+
IeJsPropertyId functionId = IeJsPropertyId.FromString(functionName);
447439

448-
for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
440+
bool functionExist = globalObj.HasProperty(functionId);
441+
if (!functionExist)
449442
{
450-
object value = args[argumentIndex];
451-
string serializedValue = SimplisticJsSerializer.Serialize(value);
452-
453-
if (argumentIndex > 0)
454-
{
455-
serializedArgumentsBuilder.Append(", ");
456-
}
457-
serializedArgumentsBuilder.Append(serializedValue);
443+
throw new JsRuntimeException(
444+
string.Format(Strings.Runtime_FunctionNotExist, functionName));
458445
}
459446

460-
serializedArguments = serializedArgumentsBuilder.ToString();
461-
}
447+
var processedArgs = MapToScriptType(args);
448+
var allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
462449

463-
object result = Evaluate(string.Format("{0}({1});", functionName, serializedArguments));
450+
IeJsValue functionValue = globalObj.GetProperty(functionId);
451+
IeJsValue resultValue = functionValue.CallFunction(allProcessedArgs);
452+
453+
return MapToHostType(resultValue);
454+
});
464455

465456
return result;
466457
}

src/MsieJavaScriptEngine/MsieJavaScriptEngine.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
<Compile Include="JsRt\Ie\ProcessDebugManager.cs" />
100100
<Compile Include="JsRt\ProfilerEventMask.cs" />
101101
<Compile Include="JsRt\ProfilerScriptType.cs" />
102-
<Compile Include="Utilities\SimplisticJsSerializer.cs" />
103102
<Compile Include="JsException.cs" />
104103
<Compile Include="JsRuntimeException.cs" />
105104
<Compile Include="JsEngineLoadException.cs" />

src/MsieJavaScriptEngine/Utilities/SimplisticJsSerializer.cs

Lines changed: 0 additions & 175 deletions
This file was deleted.

0 commit comments

Comments
 (0)