public void Analyze(Engine engine)
        {
            HasDynamicReturns = false;

            if (!(MapFunc is ScriptFunctionInstance sfi))
            {
                return;
            }

            var theFuncAst = sfi.GetFunctionAst();

            var res = CheckIfSimpleMapExpression(engine, theFuncAst);

            if (res != null)
            {
                MapFunc    = res.Value.Function;
                theFuncAst = res.Value.FunctionAst;
            }
            var loadSearcher = new EsprimaReferencedCollectionVisitor();

            loadSearcher.VisitFunctionExpression(theFuncAst);
            ReferencedCollections.UnionWith(loadSearcher.ReferencedCollection);

            foreach (var returnStatement in JavaScriptIndexUtils.GetReturnStatements(theFuncAst.Body))
            {
                if (returnStatement.Argument == null) // return;
                {
                    continue;
                }

                if (!(returnStatement.Argument is ObjectExpression oe))
                {
                    HasDynamicReturns = true;
                    continue;
                }
                //If we got here we must validate that all return statements have the same structure.
                //Having zero fields means its the first return statements we encounter that has a structure.
                if (Fields.Count == 0)
                {
                    foreach (var prop in oe.Properties)
                    {
                        var fieldName = prop.Key.GetKey();
                        if (fieldName == "_")
                        {
                            HasDynamicReturns = true;
                        }

                        Fields.Add(fieldName);
                    }
                }
                else if (CompareFields(oe) == false)
                {
                    throw new InvalidOperationException($"Index {IndexName} contains different return structure from different code paths," +
                                                        $" expected properties: {string.Join(", ", Fields)} but also got:{string.Join(", ", oe.Properties.Select(x => x.Key.GetKey()))}");
                }
            }
        }
Esempio n. 2
0
        private HashSet <CollectionName> ExecuteCodeAndCollectReferencedCollections(string code)
        {
            var javascriptParser = new JavaScriptParser(code, DefaultParserOptions);
            var program          = javascriptParser.ParseProgram();

            _engine.ExecuteWithReset(program);
            var loadVisitor = new EsprimaReferencedCollectionVisitor();

            loadVisitor.Visit(program);
            return(loadVisitor.ReferencedCollection);
        }
Esempio n. 3
0
        private HashSet <CollectionName> ExecuteCodeAndCollectReferencedCollections(string code, string additionalSources)
        {
            var javascriptParser = new JavaScriptParser(code, DefaultParserOptions);
            var program          = javascriptParser.ParseProgram();

            _engine.ExecuteWithReset(program);
            var loadVisitor = new EsprimaReferencedCollectionVisitor();

            if (string.IsNullOrEmpty(additionalSources) == false)
            {
                loadVisitor.Visit(new JavaScriptParser(additionalSources, DefaultParserOptions).ParseProgram());
            }

            loadVisitor.Visit(program);
            return(loadVisitor.ReferencedCollection);
        }
Esempio n. 4
0
        private MapMetadata ExecuteCodeAndCollectReferencedCollections(string code, string additionalSources)
        {
            var javascriptParser = new JavaScriptParser(code, DefaultParserOptions);
            var program          = javascriptParser.ParseScript();

            _engine.ExecuteWithReset(program);
            var loadVisitor = new EsprimaReferencedCollectionVisitor();

            if (string.IsNullOrEmpty(additionalSources) == false)
            {
                loadVisitor.Visit(new JavaScriptParser(additionalSources, DefaultParserOptions).ParseScript());
            }

            loadVisitor.Visit(program);
            return(new MapMetadata
            {
                ReferencedCollections = loadVisitor.ReferencedCollection,
                HasCompareExchangeReferences = loadVisitor.HasCompareExchangeReferences
            });
        }