• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdlib.h>
6 #include <map>
7 
8 #include "src/v8.h"
9 
10 #include "src/ast/ast.h"
11 #include "src/ast/ast-expression-visitor.h"
12 #include "src/ast/scopes.h"
13 #include "src/parsing/parser.h"
14 #include "src/parsing/rewriter.h"
15 #include "test/cctest/cctest.h"
16 #include "test/cctest/expression-type-collector.h"
17 #include "test/cctest/expression-type-collector-macros.h"
18 
19 using namespace v8::internal;
20 
21 namespace {
22 
23 class NodeTypeCounter : public AstExpressionVisitor {
24  public:
25   typedef std::map<AstNode::NodeType, int> Counters;
26 
NodeTypeCounter(Isolate * isolate,Expression * expr,Counters * counts)27   NodeTypeCounter(Isolate* isolate, Expression* expr, Counters* counts)
28       : AstExpressionVisitor(isolate, expr), counts_(counts) {}
29 
30  protected:
VisitExpression(Expression * expr)31   void VisitExpression(Expression* expr) override {
32     (*counts_)[expr->node_type()]++;
33   }
34 
35  private:
36   Counters* counts_;
37 };
38 
39 }  // namespace
40 
TEST(VisitExpression)41 TEST(VisitExpression) {
42   const char test_function[] =
43       "function GeometricMean(stdlib, foreign, buffer) {\n"
44       "  \"use asm\";\n"
45       "\n"
46       "  var exp = stdlib.Math.exp;\n"
47       "  var log = stdlib.Math.log;\n"
48       "  var values = new stdlib.Float64Array(buffer);\n"
49       "\n"
50       "  function logSum(start, end) {\n"
51       "    start = start|0;\n"
52       "    end = end|0;\n"
53       "\n"
54       "    var sum = 0.0, p = 0, q = 0;\n"
55       "\n"
56       "    // asm.js forces byte addressing of the heap by requiring shifting "
57       "by 3\n"
58       "    for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n"
59       "      sum = sum + +log(values[p>>3]);\n"
60       "    }\n"
61       "\n"
62       "    return +sum;\n"
63       "  }\n"
64       "\n"
65       " function geometricMean(start, end) {\n"
66       "    start = start|0;\n"
67       "    end = end|0;\n"
68       "\n"
69       "    return +exp(+logSum(start, end) / +((end - start)|0));\n"
70       "  }\n"
71       "\n"
72       "  return { geometricMean: geometricMean };\n"
73       "}\n";
74 
75   // Parse + compile test_function, and extract the AST node for it.
76   v8::V8::Initialize();
77   HandleAndZoneScope handles;
78   i::Isolate* isolate = CcTest::i_isolate();
79   i::Handle<i::String> source_code =
80       isolate->factory()
81           ->NewStringFromUtf8(i::CStrVector(test_function))
82           .ToHandleChecked();
83   i::Handle<i::Script> script = isolate->factory()->NewScript(source_code);
84   i::ParseInfo info(handles.main_zone(), script);
85   i::Parser parser(&info);
86   info.set_global();
87   info.set_lazy(false);
88   info.set_allow_lazy_parsing(false);
89   info.set_toplevel(true);
90   CHECK(i::Compiler::ParseAndAnalyze(&info));
91   Expression* test_function_expr =
92       info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun();
93 
94   // Run NodeTypeCounter and sanity check counts for 3 expression types,
95   // and for overall # of types found.
96   NodeTypeCounter::Counters counts;
97   NodeTypeCounter(isolate, test_function_expr, &counts).Run();
98   CHECK_EQ(21, counts[AstNode::kBinaryOperation]);
99   CHECK_EQ(26, counts[AstNode::kLiteral]);
100   CHECK_EQ(3, counts[AstNode::kFunctionLiteral]);
101   CHECK_EQ(10, counts.size());
102 }
103