Exemple #1
0
        private async Task <IMember> TryEvaluateVithArgumentsAsync(FunctionDefinition fd, IArgumentSet args, CancellationToken cancellationToken = default)
        {
            // Attempt to evaluate with specific arguments but prevent recursion.
            IMember result = UnknownType;

            if (fd != null && !_callEvalStack.Contains(fd))
            {
                using (OpenScope(fd.Parent, out _)) {
                    _callEvalStack.Push(fd);
                    try {
                        // TODO: cache results per function + argument set?
                        var eval = new FunctionCallEvaluator(fd, this, Interpreter);
                        result = await eval.EvaluateCallAsync(args, cancellationToken);
                    } finally {
                        _callEvalStack.Pop();
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        private IMember TryEvaluateWithArguments(IPythonModule module, FunctionDefinition fd, IArgumentSet args)
        {
            // Attempt to evaluate with specific arguments but prevent recursion.
            IMember result = UnknownType;

            if (fd != null && !_callEvalStack.Contains(fd))
            {
                using (OpenScope(module, fd.Parent, out _)) {
                    _callEvalStack.Push(fd);
                    try {
                        // TODO: cache results per function + argument set?
                        var eval = new FunctionCallEvaluator(module, fd, this);
                        result = eval.EvaluateCall(args);
                    } finally {
                        _callEvalStack.Pop();
                    }
                }
            }
            return(result);
        }
        private IMember TryEvaluateWithArguments(IPythonFunctionType fn, IArgumentSet args)
        {
            var name    = fn.DeclaringType != null ? $"{fn.DeclaringModule.Name}.{fn.Name}" : fn.Name;
            var argHash = args
                          .Arguments
                          .Select(a => a.Name.GetHashCode() ^ 397 * (a.Value?.GetHashCode() ?? 0))
                          .Aggregate(0, (current, d) => 31 * current ^ d);
            var key = fn.DeclaringModule.Name.GetHashCode() ^ name.GetHashCode() ^ (397 * argHash);

            if (_argEvalCache.TryGetValue(key, out var result))
            {
                return(result);
            }

            var fd     = fn.FunctionDefinition;
            var module = fn.DeclaringModule;

            // Attempt to evaluate with specific arguments but prevent recursion.
            result = UnknownType;
            if (fd != null && !_callEvalStack.Contains(fd))
            {
                using (OpenScope(module, fd.Parent, out _)) {
                    _callEvalStack.Push(fd);
                    try {
                        // TODO: cache results per function + argument set?
                        var eval = new FunctionCallEvaluator(module, fd, this);
                        result = eval.EvaluateCall(args);
                    } finally {
                        _callEvalStack.Pop();
                    }
                }
            }

            _argEvalCache[key] = result;
            return(result);
        }