Skip to content

Commit 12efc48

Browse files
spahnkeoliverbock
authored andcommitted
Pass default value if optional parameters are omitted using undefined
1 parent 7e722d8 commit 12efc48

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Source/Noesis.Javascript/JavascriptInterop.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ JavascriptInterop::Invoker(const v8::FunctionCallbackInfo<Value>& iArgs)
719719
arguments = gcnew cli::array<System::Object^>(parametersInfo->Length); // trailing parameters will be null
720720
for (int p = 0; p < suppliedArguments->Length; p++)
721721
{
722-
System::Type^ paramType = parametersInfo[p]->ParameterType;
722+
System::Reflection::ParameterInfo^ parameter = parametersInfo[p];
723+
System::Type^ paramType = parameter->ParameterType;
723724

724725
if (suppliedArguments[p] != nullptr)
725726
{
@@ -740,9 +741,15 @@ JavascriptInterop::Invoker(const v8::FunctionCallbackInfo<Value>& iArgs)
740741
}
741742
}
742743
}
744+
else if (parameter->IsOptional && parameter->HasDefaultValue && iArgs[p]->IsUndefined())
745+
{
746+
// pass default value if parameter is optional and undefined was supplied as an argument
747+
arguments[p] = parameter->DefaultValue;
748+
}
743749
}
744-
for (int p = suppliedArguments->Length; p < arguments->Length; p++) // set default values if there are optional parameters
750+
for (int p = suppliedArguments->Length; p < arguments->Length; p++)
745751
{
752+
// pass default values if there are optional parameters
746753
System::Reflection::ParameterInfo^ parameter = parametersInfo[p];
747754
if (parameter->IsOptional && parameter->HasDefaultValue)
748755
arguments[p] = parameter->DefaultValue;

Tests/Noesis.Javascript.Tests/ConvertFromJavascriptTests.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,16 @@ public void MethodCallWithRequiredAndDefaultParameters_PassingAllActualParameter
272272
}
273273

274274
[TestMethod]
275-
[Ignore]
275+
public void MethodCallWithRequiredAndDefaultParameters_PassingUndefinedForAllOptionalActualParameter()
276+
{
277+
var obj = new TypedPropertiesClass();
278+
_context.SetParameter("obj", obj);
279+
var result = _context.Run("obj.methodWithRequiredAndDefaultParameters(1, undefined, undefined)");
280+
281+
result.Should().Be("i: 1, s: abc, b: True");
282+
}
283+
284+
[TestMethod]
276285
public void MethodCallWithRequiredAndDefaultParameters_PassingOnlyOneOptionalActualParameterLeavingTheMiddleOneUndefined()
277286
{
278287
var obj = new TypedPropertiesClass();

0 commit comments

Comments
 (0)