blob: 5df8253afa756df4e89899b1f2195031776fe744 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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>
Ben Murdochc5610432016-08-08 18:44:38 +01006#include <map>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007
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
19using namespace v8::internal;
20
21namespace {
22
Ben Murdochc5610432016-08-08 18:44:38 +010023class NodeTypeCounter : public AstExpressionVisitor {
24 public:
25 typedef std::map<AstNode::NodeType, int> Counters;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026
Ben Murdochc5610432016-08-08 18:44:38 +010027 NodeTypeCounter(Isolate* isolate, Expression* expr, Counters* counts)
28 : AstExpressionVisitor(isolate, expr), counts_(counts) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029
Ben Murdochc5610432016-08-08 18:44:38 +010030 protected:
31 void VisitExpression(Expression* expr) override {
32 (*counts_)[expr->node_type()]++;
33 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034
Ben Murdochc5610432016-08-08 18:44:38 +010035 private:
36 Counters* counts_;
37};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038
39} // namespace
40
Ben Murdochc5610432016-08-08 18:44:38 +010041TEST(VisitExpression) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 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
Ben Murdochc5610432016-08-08 18:44:38 +010075 // Parse + compile test_function, and extract the AST node for it.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 v8::V8::Initialize();
77 HandleAndZoneScope handles;
Ben Murdochc5610432016-08-08 18:44:38 +010078 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();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093
Ben Murdochc5610432016-08-08 18:44:38 +010094 // 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());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102}