Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unifying IAsyncTexlFunction interfaces. #2748

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Types;

namespace Microsoft.PowerFx.Core.Functions
{
// Texl function interface with IServiceProvider
internal interface IAsyncTexlFunction5
{
internal interface IAsyncTexlFunctionService
{
Task<FormulaValue> InvokeAsync(IServiceProvider runtimeServiceProvider, FormulaType irContext, FormulaValue[] args, CancellationToken cancellationToken);
Task<FormulaValue> InvokeAsync(IServiceProvider runtimeServiceProvider, IRContext irContext, FormulaValue[] args, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public SymbolTable SymbolTable
set => _symbolTable = value;
}

internal readonly Dictionary<TexlFunction, IAsyncTexlFunction> AdditionalFunctions = new ();

[Obsolete("Use Config.EnumStore or symboltable directly")]
internal EnumStoreBuilder EnumStoreBuilder => InternalConfigSymbols.EnumStoreBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public class SymbolTable : ReadOnlySymbolTable, IGlobalSymbolNameResolver
/// True if we have AddVariables, but not needed if we just have constants or functions.
/// </summary>
public bool NeedsValues => !_slots.IsEmpty;

public SymbolTable()
{
EnumStoreBuilder = new EnumStoreBuilder();
}

private DName ValidateName(string name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.PowerFx.Core.Texl.Builtins
{
// IsMatch(text:s, format:s)
// Checks if the input text is of the correct format.
internal sealed class IsMatchFunction : BuiltinFunction
internal class IsMatchFunction : BuiltinFunction
{
public override bool UseParentScopeForArgumentSuggestions => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.PowerFx.Core.Texl.Builtins
{
// Match(text:s, regular_expression:s, [options:s])
internal class MatchFunction : BaseMatchFunction
{
{
public MatchFunction(RegexTypeCache regexCache)
: base("Match", TexlStrings.AboutMatch, DType.EmptyRecord, regexCache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.Texl.Builtins;
using Microsoft.PowerFx.Functions;
using Microsoft.PowerFx.Interpreter;
using Microsoft.PowerFx.Types;
using static Microsoft.PowerFx.Functions.Library;

namespace Microsoft.PowerFx
{
Expand Down Expand Up @@ -68,16 +67,28 @@ public static void EnableRegExFunctions(this PowerFxConfig config, TimeSpan regE
{
RegexTypeCache regexTypeCache = new (regexCacheSize);

foreach (KeyValuePair<TexlFunction, IAsyncTexlFunction> func in Library.RegexFunctions(regExTimeout, regexTypeCache))
var duplicated = config.ComposedConfigSymbols.Functions.AnyWithName("IsMatch") ||
config.ComposedConfigSymbols.Functions.AnyWithName("Match") ||
config.ComposedConfigSymbols.Functions.AnyWithName("MatchAll");

if (duplicated)
{
if (config.ComposedConfigSymbols.Functions.AnyWithName(func.Key.Name))
{
throw new InvalidOperationException("Cannot add RegEx functions more than once.");
}
throw new InvalidOperationException("Cannot add RegEx functions more than once.");
}

config.InternalConfigSymbols.AddFunction(func.Key);
config.AdditionalFunctions.Add(func.Key, func.Value);
if (regExTimeout == TimeSpan.Zero)
{
regExTimeout = new TimeSpan(0, 0, 1);
}

if (regExTimeout.TotalMilliseconds < 0)
{
throw new ArgumentOutOfRangeException(nameof(regExTimeout), "Timeout duration for regular expression execution must be positive.");
}

config.SymbolTable.AddFunction(new IsMatchImplementation(regExTimeout));
config.SymbolTable.AddFunction(new MatchImplementation(regExTimeout, regexTypeCache));
config.SymbolTable.AddFunction(new MatchAllImplementation(regExTimeout, regexTypeCache));
}

[Obsolete("OptionSetInfo function is deprecated. Use the Value function on an option set backed by a number and the Boolean function on an option set backed by a Boolean instead. A new ChoiceInfo function is in the works for access to logical names.")]
Expand Down
25 changes: 5 additions & 20 deletions src/libraries/Microsoft.PowerFx.Interpreter/EvalVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,14 @@ public override async ValueTask<FormulaValue> Visit(CallNode node, EvalVisitorCo
var childContext = context.SymbolContext.WithScope(node.Scope);

FormulaValue result;
IReadOnlyDictionary<TexlFunction, IAsyncTexlFunction> extraFunctions = _services.GetService<IReadOnlyDictionary<TexlFunction, IAsyncTexlFunction>>();


try
{
if (func is IAsyncTexlFunction asyncFunc || extraFunctions?.TryGetValue(func, out asyncFunc) == true)
{
result = await asyncFunc.InvokeAsync(args, _cancellationToken).ConfigureAwait(false);
}
#pragma warning disable CS0618 // Type or member is obsolete
else if (func is IAsyncTexlFunction2 asyncFunc2)
#pragma warning restore CS0618 // Type or member is obsolete
{
result = await asyncFunc2.InvokeAsync(this.GetFormattingInfo(), args, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction3 asyncFunc3)
{
result = await asyncFunc3.InvokeAsync(node.IRContext.ResultType, args, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction4 asyncFunc4)
if (func is IAsyncTexlFunction asyncFunc)
{
result = await asyncFunc4.InvokeAsync(TimeZoneInfo, node.IRContext.ResultType, args, _cancellationToken).ConfigureAwait(false);
result = await asyncFunc.InvokeAsync(this, context.IncrementStackDepthCounter(childContext), node.IRContext, args).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction5 asyncFunc5)
else if (func is IAsyncTexlFunctionService asyncFuncService)
{
BasicServiceProvider services2 = new BasicServiceProvider(_services);

Expand All @@ -344,7 +329,7 @@ public override async ValueTask<FormulaValue> Visit(CallNode node, EvalVisitorCo
services2.AddService(new Canceller(CheckCancel));
}

result = await asyncFunc5.InvokeAsync(services2, node.IRContext.ResultType, args, _cancellationToken).ConfigureAwait(false);
result = await asyncFuncService.InvokeAsync(services2, node.IRContext, args, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncConnectorTexlFunction asyncConnectorTexlFunction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Texl.Builtins;
using Microsoft.PowerFx.Interpreter;
using Microsoft.PowerFx.Types;

namespace Microsoft.PowerFx
{
internal class FileInfoFunctionImpl : FileInfoFunction, IAsyncTexlFunction3
internal class FileInfoFunctionImpl : FileInfoFunction, IAsyncTexlFunction
{
public async Task<FormulaValue> InvokeAsync(FormulaType irContext, FormulaValue[] args, CancellationToken cancellationToken)
public async Task<FormulaValue> InvokeAsync(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args)
{
var arg0 = args[0];
if (arg0 is BlankValue || arg0 is ErrorValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Types;

namespace Microsoft.PowerFx.Core.Functions
{
// A Texl function capable of async invokes.
internal interface IAsyncTexlFunction
{
Task<FormulaValue> InvokeAsync(FormulaValue[] args, CancellationToken cancellationToken);
Task<FormulaValue> InvokeAsync(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, FormulaValue[] args);
}
}

This file was deleted.

Loading