OLD | NEW |
(Empty) | |
| 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 |
| 7 #include "src/v8.h" |
| 8 |
| 9 #include "src/ast.h" |
| 10 #include "src/ast-expression-visitor.h" |
| 11 #include "src/parser.h" |
| 12 #include "src/rewriter.h" |
| 13 #include "src/scopes.h" |
| 14 #include "test/cctest/cctest.h" |
| 15 #include "test/cctest/expression-type-collector.h" |
| 16 |
| 17 using namespace v8::internal; |
| 18 |
| 19 namespace { |
| 20 |
| 21 static std::string CollectTypes(const char* source) { |
| 22 v8::V8::Initialize(); |
| 23 HandleAndZoneScope handles; |
| 24 |
| 25 i::Isolate* isolate = CcTest::i_isolate(); |
| 26 i::Factory* factory = isolate->factory(); |
| 27 |
| 28 i::Handle<i::String> source_code = |
| 29 factory->NewStringFromUtf8(i::CStrVector(source)).ToHandleChecked(); |
| 30 |
| 31 i::Handle<i::Script> script = factory->NewScript(source_code); |
| 32 |
| 33 i::ParseInfo info(handles.main_zone(), script); |
| 34 i::Parser parser(&info); |
| 35 parser.set_allow_harmony_arrow_functions(true); |
| 36 parser.set_allow_harmony_sloppy(true); |
| 37 info.set_global(); |
| 38 info.set_lazy(false); |
| 39 info.set_allow_lazy_parsing(false); |
| 40 info.set_toplevel(true); |
| 41 |
| 42 i::CompilationInfo compilation_info(&info); |
| 43 CHECK(i::Compiler::ParseAndAnalyze(&info)); |
| 44 info.set_literal( |
| 45 info.scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); |
| 46 |
| 47 return ExpressionTypeCollector::Run(&compilation_info); |
| 48 } |
| 49 } |
| 50 |
| 51 |
| 52 TEST(VisitExpressions) { |
| 53 const char test_function[] = |
| 54 "function GeometricMean(stdlib, foreign, buffer) {\n" |
| 55 " \"use asm\";\n" |
| 56 "\n" |
| 57 " var exp = stdlib.Math.exp;\n" |
| 58 " var log = stdlib.Math.log;\n" |
| 59 " var values = new stdlib.Float64Array(buffer);\n" |
| 60 "\n" |
| 61 " function logSum(start, end) {\n" |
| 62 " start = start|0;\n" |
| 63 " end = end|0;\n" |
| 64 "\n" |
| 65 " var sum = 0.0, p = 0, q = 0;\n" |
| 66 "\n" |
| 67 " // asm.js forces byte addressing of the heap by requiring shifting " |
| 68 "by 3\n" |
| 69 " for (p = start << 3, q = end << 3; (p|0) < (q|0); p = (p + 8)|0) {\n" |
| 70 " sum = sum + +log(values[p>>3]);\n" |
| 71 " }\n" |
| 72 "\n" |
| 73 " return +sum;\n" |
| 74 " }\n" |
| 75 "\n" |
| 76 " function geometricMean(start, end) {\n" |
| 77 " start = start|0;\n" |
| 78 " end = end|0;\n" |
| 79 "\n" |
| 80 " return +exp(+logSum(start, end) / +((end - start)|0));\n" |
| 81 " }\n" |
| 82 "\n" |
| 83 " return { geometricMean: geometricMean };\n" |
| 84 "}\n"; |
| 85 const char test_expected[] = |
| 86 "FunctionLiteral: Other\n" |
| 87 " FunctionLiteral: Other\n" |
| 88 " Assignment: Other\n" |
| 89 " start: VariableProxy: Other\n" |
| 90 " BinaryOperation: Other\n" |
| 91 " start: VariableProxy: Other\n" |
| 92 " Literal: Other\n" |
| 93 " Assignment: Other\n" |
| 94 " end: VariableProxy: Other\n" |
| 95 " BinaryOperation: Other\n" |
| 96 " end: VariableProxy: Other\n" |
| 97 " Literal: Other\n" |
| 98 " Assignment: Other\n" |
| 99 " sum: VariableProxy: Other\n" |
| 100 " Literal: Other\n" |
| 101 " Assignment: Other\n" |
| 102 " p: VariableProxy: Other\n" |
| 103 " Literal: Other\n" |
| 104 " Assignment: Other\n" |
| 105 " q: VariableProxy: Other\n" |
| 106 " Literal: Other\n" |
| 107 " BinaryOperation: Other\n" |
| 108 " Assignment: Other\n" |
| 109 " p: VariableProxy: Other\n" |
| 110 " BinaryOperation: Other\n" |
| 111 " start: VariableProxy: Other\n" |
| 112 " Literal: Other\n" |
| 113 " Assignment: Other\n" |
| 114 " q: VariableProxy: Other\n" |
| 115 " BinaryOperation: Other\n" |
| 116 " end: VariableProxy: Other\n" |
| 117 " Literal: Other\n" |
| 118 " CompareOperation: Other\n" |
| 119 " BinaryOperation: Other\n" |
| 120 " p: VariableProxy: Other\n" |
| 121 " Literal: Other\n" |
| 122 " BinaryOperation: Other\n" |
| 123 " q: VariableProxy: Other\n" |
| 124 " Literal: Other\n" |
| 125 " Assignment: Other\n" |
| 126 " p: VariableProxy: Other\n" |
| 127 " BinaryOperation: Other\n" |
| 128 " BinaryOperation: Other\n" |
| 129 " p: VariableProxy: Other\n" |
| 130 " Literal: Other\n" |
| 131 " Literal: Other\n" |
| 132 " Assignment: Other\n" |
| 133 " sum: VariableProxy: Other\n" |
| 134 " BinaryOperation: Other\n" |
| 135 " sum: VariableProxy: Other\n" |
| 136 " BinaryOperation: Other\n" |
| 137 " Call: Other\n" |
| 138 " log: VariableProxy: Other\n" |
| 139 " values: VariableProxy: Other\n" |
| 140 " BinaryOperation: Other\n" |
| 141 " p: VariableProxy: Other\n" |
| 142 " Literal: Other\n" |
| 143 " Literal: Other\n" |
| 144 " BinaryOperation: Other\n" |
| 145 " sum: VariableProxy: Other\n" |
| 146 " Literal: Other\n" |
| 147 " FunctionLiteral: Other\n" |
| 148 " Assignment: Other\n" |
| 149 " start: VariableProxy: Other\n" |
| 150 " BinaryOperation: Other\n" |
| 151 " start: VariableProxy: Other\n" |
| 152 " Literal: Other\n" |
| 153 " Assignment: Other\n" |
| 154 " end: VariableProxy: Other\n" |
| 155 " BinaryOperation: Other\n" |
| 156 " end: VariableProxy: Other\n" |
| 157 " Literal: Other\n" |
| 158 " BinaryOperation: Other\n" |
| 159 " Call: Other\n" |
| 160 " exp: VariableProxy: Other\n" |
| 161 " BinaryOperation: Other\n" |
| 162 " BinaryOperation: Other\n" |
| 163 " Call: Other\n" |
| 164 " logSum: VariableProxy: Other\n" |
| 165 " start: VariableProxy: Other\n" |
| 166 " end: VariableProxy: Other\n" |
| 167 " Literal: Other\n" |
| 168 " BinaryOperation: Other\n" |
| 169 " BinaryOperation: Other\n" |
| 170 " BinaryOperation: Other\n" |
| 171 " end: VariableProxy: Other\n" |
| 172 " start: VariableProxy: Other\n" |
| 173 " Literal: Other\n" |
| 174 " Literal: Other\n" |
| 175 " Literal: Other\n" |
| 176 " Literal: Other\n" |
| 177 " Assignment: Other\n" |
| 178 " exp: VariableProxy: Other\n" |
| 179 " stdlib: VariableProxy: Other\n" |
| 180 " Literal: Other\n" |
| 181 " Literal: Other\n" |
| 182 " Assignment: Other\n" |
| 183 " log: VariableProxy: Other\n" |
| 184 " stdlib: VariableProxy: Other\n" |
| 185 " Literal: Other\n" |
| 186 " Literal: Other\n" |
| 187 " Assignment: Other\n" |
| 188 " values: VariableProxy: Other\n" |
| 189 " CallNew: Other\n" |
| 190 " stdlib: VariableProxy: Other\n" |
| 191 " Literal: Other\n" |
| 192 " buffer: VariableProxy: Other\n" |
| 193 " ObjectLiteral: Other\n" |
| 194 " geometricMean: VariableProxy: Other\n"; |
| 195 CHECK_EQ(test_expected, CollectTypes(test_function)); |
| 196 } |
OLD | NEW |