Support for passing Script reference to functions. useful when options are required for function
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using Upsilon.Evaluator;
|
||||
|
||||
namespace Upsilon.BaseTypes.ScriptFunction
|
||||
{
|
||||
internal abstract class ScriptFunction : ScriptType
|
||||
@@ -13,6 +15,6 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract ScriptType Run(Diagnostics diagnostics, ScriptType[] variables);
|
||||
public abstract ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Upsilon.BaseTypes.UserData;
|
||||
using Upsilon.Evaluator;
|
||||
|
||||
namespace Upsilon.BaseTypes.ScriptFunction
|
||||
{
|
||||
internal class ScriptMethodInfoFunction : ScriptFunction
|
||||
{
|
||||
public ScriptMethodInfoFunction(UserDataMethod method, object o, bool directTypeManipulation)
|
||||
public ScriptMethodInfoFunction(UserDataMethod method, object o, bool directTypeManipulation, bool passScriptReference = false)
|
||||
{
|
||||
_method = method;
|
||||
_object = o;
|
||||
_directTypeManipulation = directTypeManipulation;
|
||||
_passScriptReference = passScriptReference;
|
||||
ReturnType = _method.ReturnType;
|
||||
}
|
||||
|
||||
private readonly UserDataMethod _method;
|
||||
private readonly object _object;
|
||||
private readonly bool _directTypeManipulation;
|
||||
private readonly bool _passScriptReference;
|
||||
public System.Type ReturnType { get; }
|
||||
|
||||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables)
|
||||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script)
|
||||
{
|
||||
var types = _directTypeManipulation
|
||||
? variables.Select(x => x.GetType()).ToArray()
|
||||
@@ -32,9 +36,10 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
||||
$"No valid function found on type '{_object.GetType()}' with name '{_method.Name}' " +
|
||||
$"and parameter types: {string.Join(", ", types.Select(x => $"'{x.Name}'"))}");
|
||||
}
|
||||
var objects = _directTypeManipulation
|
||||
var objects = new List<object>();
|
||||
objects.AddRange(_directTypeManipulation
|
||||
? variables.Select(x => (object) x).ToList()
|
||||
: variables.Select(x => x.ToCSharpObject()).ToList();
|
||||
: variables.Select(x => x.ToCSharpObject()).ToList());
|
||||
var pars = method.GetParameters();
|
||||
if (pars.Length != objects.Count)
|
||||
{
|
||||
|
||||
@@ -19,9 +19,9 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
||||
EvaluationScope = evaluationScope;
|
||||
}
|
||||
|
||||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables)
|
||||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script)
|
||||
{
|
||||
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope);
|
||||
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope, script);
|
||||
for (var i = 0; i < Parameters.Length; i++)
|
||||
{
|
||||
var parameterVariable = Parameters[i];
|
||||
|
||||
Reference in New Issue
Block a user