Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-nguyen committed Jun 5, 2024
1 parent c9c5a86 commit 826b878
Showing 1 changed file with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.PowerFx.Core.App;
Expand All @@ -11,6 +12,7 @@
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Errors;
using Microsoft.PowerFx.Core.Functions.FunctionArgValidators;
using Microsoft.PowerFx.Core.Glue;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.IR.Nodes;
Expand All @@ -23,6 +25,7 @@
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
using static Microsoft.PowerFx.Core.Localization.TexlStrings;
using CallNode = Microsoft.PowerFx.Syntax.CallNode;

namespace Microsoft.PowerFx.Core.Functions
{
Expand All @@ -31,18 +34,29 @@ namespace Microsoft.PowerFx.Core.Functions
/// This includings the binding (and hence IR for evaluation) -
/// This is conceptually immutable after initialization - if the body or signature changes, you need to create a new instance.
/// </summary>
internal class UserDefinedFunction : TexlFunction
internal class UserDefinedFunction : TexlFunction, IExternalPageableSymbol, IExternalDelegatableSymbol
{
private readonly bool _isImperative;
private readonly IEnumerable<UDFArg> _args;
private TexlBinding _binding;

public override bool IsAsync => _binding?.IsAsync(UdfBody) ?? false;

public bool IsPageable => _binding?.IsPageable(_binding.Top) ?? false;

public bool IsDelegatable => _binding?.IsDelegatable(_binding.Top) ?? false;

public override bool IsServerDelegatable(CallNode callNode, TexlBinding binding)
{
return base.IsServerDelegatable(callNode, binding) || IsDelegatable;
}

public override bool SupportsParamCoercion => true;

public TexlNode UdfBody { get; }

public TexlBinding Binding => _binding;

public override bool IsSelfContained => !_isImperative;

/// <summary>
Expand Down Expand Up @@ -142,7 +156,7 @@ public void CheckTypesOnDeclaration(CheckTypesContext context, DType actualBodyR
}
else
{
binding.ErrorContainer.EnsureError(DocumentErrorSeverity.Severe, UdfBody, TexlStrings.ErrUDF_ReturnTypeDoesNotMatch, ReturnType.GetKindString(), actualBodyReturnType.GetKindString());
binding.ErrorContainer.EnsureError(DocumentErrorSeverity.Severe, UdfBody, TexlStrings.ErrUDF_ReturnTypeDoesNotMatch);
}
}
}
Expand Down Expand Up @@ -292,27 +306,25 @@ private static bool CheckReturnType(IdentToken returnTypeToken, List<TexlError>
errors.Add(new TexlError(returnTypeToken, DocumentErrorSeverity.Severe, TexlStrings.ErrUDF_UnknownType, returnTypeToken.Name));
returnType = DType.Invalid;
return false;
}
}

if (IsRestrictedType(returnTypeFormulaType))
{
errors.Add(new TexlError(returnTypeToken, DocumentErrorSeverity.Severe, TexlStrings.ErrUDF_InvalidReturnType, returnTypeToken.Name));
returnType = DType.Invalid;
return false;
}
}

returnType = returnTypeFormulaType._type;
return true;
return true;
}

// To prevent aggregate types from containing restricted types
internal static bool IsRestrictedType(FormulaType ft)
{
Contracts.AssertValue(ft);

// Datasource types may contain fields that may expand to other datasource types or refernce themselves.
// We can avoid calling this method on these types containing expand info.
if (!ft._type.HasExpandInfo && ft is AggregateType aggType)
if (ft is AggregateType aggType)
{
if (aggType.GetFieldTypes().Any(ct => IsRestrictedType(ct.Type)))
{
Expand Down Expand Up @@ -391,8 +403,8 @@ public bool LookupDataControl(DName name, out NameLookupInfo lookupInfo, out DNa
}

public IEnumerable<TexlFunction> LookupFunctions(DPath theNamespace, string name, bool localeInvariant = false)
{
return _globalNameResolver.LookupFunctions(theNamespace, name, localeInvariant);
{
return _globalNameResolver.LookupFunctions(theNamespace, name, localeInvariant);
}

public IEnumerable<TexlFunction> LookupFunctionsInNamespace(DPath nameSpace)
Expand Down

0 comments on commit 826b878

Please sign in to comment.