Skip to content

Commit

Permalink
Support local methods with params modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Mar 9, 2022
1 parent 72f80e8 commit 38d532a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ Also, we can see that named arguments are also working with `params` arguments.

If a regular argument (not optional) is missing, the runtime will complain about the missing argument giving precise source location of the error.

When last parameter is of type `object[]` it is automatically treated as if it was declared with `params` modifier.

#### Accessing nested `ScriptObject`

A nested ScriptObject can be accessed indirectly through another `ScriptObject`:
Expand Down
24 changes: 24 additions & 0 deletions src/Scriban.Tests/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,33 @@

namespace Scriban.Tests
{
delegate string Args(object[] args);

[TestFixture]
public class TestRuntime
{
[Test]
public void TestPars()
{
string Dump(params object[] args)
{
return "hello";
}

ScriptObject model = new ScriptObject();
ScriptObject debug = new ScriptObject();
Args dump = Dump;

debug.Import("dump", dump);
model["debug"] = debug;

var input = "{{debug.dump(10, \"hello\", [0, 1, 2])}}";
var template = Template.Parse(input);
var result = template.Render(model);

Assert.AreEqual("hello", result);
}

[Test]
public void TestUlong()
{
Expand Down
13 changes: 11 additions & 2 deletions src/Scriban/Runtime/DynamicCustomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ protected DynamicCustomFunction(MethodInfo method)
var lastParam = Parameters[Parameters.Length - 1];
if (lastParam.ParameterType.IsArray)
{
foreach (var param in lastParam.GetCustomAttributes(typeof(ParamArrayAttribute), false))
if (lastParam.ParameterType == typeof(object[]))
{
_varParamKind = ScriptVarParamKind.LastParameter;
_paramsElementType = lastParam.ParameterType.GetElementType();
_paramsIndex = Parameters.Length - 1;
break;
}
else
{
var paramAttrs = lastParam.GetCustomAttributes(typeof(ParamArrayAttribute), false);
if (paramAttrs.Length > 0)
{
_varParamKind = ScriptVarParamKind.LastParameter;
_paramsElementType = lastParam.ParameterType.GetElementType();
_paramsIndex = Parameters.Length - 1;
}
}
}
}
Expand Down

0 comments on commit 38d532a

Please sign in to comment.