OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Test specific cases of the lazy/eager-parse decision. | 5 // Test specific cases of the lazy/eager-parse decision. |
6 // | 6 // |
7 // Note that presently most unit tests for parsing are found in | 7 // Note that presently most unit tests for parsing are found in |
8 // cctest/test-parsing.cc. | 8 // cctest/test-parsing.cc. |
9 | 9 |
10 #include <unordered_map> | 10 #include <unordered_map> |
11 | 11 |
12 #include "include/v8.h" | 12 #include "include/v8.h" |
13 #include "src/api.h" | 13 #include "src/api.h" |
14 #include "src/handles-inl.h" | 14 #include "src/handles-inl.h" |
15 #include "src/isolate.h" | 15 #include "src/isolate.h" |
16 #include "src/utils.h" | 16 #include "src/utils.h" |
17 | 17 |
18 #include "test/cctest/cctest.h" | 18 #include "test/cctest/cctest.h" |
19 | 19 |
20 using namespace v8::internal; | 20 using namespace v8::internal; |
21 | 21 |
| 22 namespace { |
| 23 |
| 24 // Record the 'compiled' state of all top level functions. |
| 25 void GetTopLevelFunctionInfo( |
| 26 v8::Local<v8::Script> script, |
| 27 std::unordered_map<std::string, bool>* is_compiled) { |
| 28 // Get the v8::internal::Script object from the API v8::Script. |
| 29 // The API object 'wraps' the compiled top-level function, not the i::Script. |
| 30 Handle<JSFunction> toplevel_fn = v8::Utils::OpenHandle(*script); |
| 31 Handle<Script> i_script = |
| 32 handle(Script::cast(toplevel_fn->shared()->script())); |
| 33 |
| 34 WeakFixedArray::Iterator iter(i_script->shared_function_infos()); |
| 35 while (SharedFunctionInfo* shared = iter.Next<SharedFunctionInfo>()) { |
| 36 std::unique_ptr<char[]> name = String::cast(shared->name())->ToCString(); |
| 37 is_compiled->insert(std::make_pair(name.get(), shared->is_compiled())); |
| 38 } |
| 39 } |
| 40 |
| 41 } // anonymous namespace |
| 42 |
| 43 TEST(GetTopLevelFunctionInfo) { |
| 44 if (!FLAG_lazy) return; |
| 45 |
| 46 Isolate* isolate = CcTest::i_isolate(); |
| 47 HandleScope scope(isolate); |
| 48 LocalContext env; |
| 49 |
| 50 const char src[] = "function foo() { var a; }\n"; |
| 51 std::unordered_map<std::string, bool> is_compiled; |
| 52 GetTopLevelFunctionInfo(v8_compile(src), &is_compiled); |
| 53 |
| 54 // Test that our helper function GetTopLevelFunctionInfo does what it claims: |
| 55 DCHECK(is_compiled.find("foo") != is_compiled.end()); |
| 56 DCHECK(is_compiled.find("bar") == is_compiled.end()); |
| 57 } |
| 58 |
22 TEST(EagerlyCompileImmediateUseFunctions) { | 59 TEST(EagerlyCompileImmediateUseFunctions) { |
23 if (!FLAG_lazy) return; | 60 if (!FLAG_lazy) return; |
24 | 61 |
| 62 Isolate* isolate = CcTest::i_isolate(); |
| 63 HandleScope scope(isolate); |
| 64 LocalContext env; |
| 65 |
25 // Test parenthesized, exclaimed, and regular functions. Make sure these | 66 // Test parenthesized, exclaimed, and regular functions. Make sure these |
26 // occur both intermixed and after each other, to make sure the 'reset' | 67 // occur both intermixed and after each other, to make sure the 'reset' |
27 // mechanism works. | 68 // mechanism works. |
28 const char src[] = | 69 const char src[] = |
29 "function normal() { var a; }\n" // Normal: Should lazy parse. | 70 "function normal() { var a; }\n" // Normal: Should lazy parse. |
30 "(function parenthesized() { var b; })()\n" // Parenthesized: Pre-parse. | 71 "(function parenthesized() { var b; })()\n" // Parenthesized: Pre-parse. |
31 "!function exclaimed() { var c; }() \n" // Exclaimed: Pre-parse. | 72 "!function exclaimed() { var c; }() \n" // Exclaimed: Pre-parse. |
32 "function normal2() { var d; }\n" | 73 "function normal2() { var d; }\n" |
33 "(function parenthesized2() { var e; })()\n" | 74 "(function parenthesized2() { var e; })()\n" |
34 "function normal3() { var f; }\n" | 75 "function normal3() { var f; }\n" |
35 "!function exclaimed2() { var g; }() \n" | 76 "!function exclaimed2() { var g; }() \n" |
36 "function normal4() { var h; }\n"; | 77 "function normal4() { var h; }\n"; |
37 | 78 |
38 Isolate* isolate = CcTest::i_isolate(); | |
39 HandleScope scope(isolate); | |
40 LocalContext env; | |
41 | |
42 // Compile src & record the 'compiled' state of all top level functions in | |
43 // is_compiled. | |
44 std::unordered_map<std::string, bool> is_compiled; | 79 std::unordered_map<std::string, bool> is_compiled; |
45 { | 80 GetTopLevelFunctionInfo(v8_compile(src), &is_compiled); |
46 v8::Local<v8::Script> api_script = v8_compile(src); | |
47 Handle<JSFunction> toplevel_fn = v8::Utils::OpenHandle(*api_script); | |
48 Handle<Script> script = | |
49 handle(Script::cast(toplevel_fn->shared()->script())); | |
50 | |
51 WeakFixedArray::Iterator iter(script->shared_function_infos()); | |
52 while (SharedFunctionInfo* shared = iter.Next<SharedFunctionInfo>()) { | |
53 std::unique_ptr<char[]> name = String::cast(shared->name())->ToCString(); | |
54 is_compiled[name.get()] = shared->is_compiled(); | |
55 } | |
56 } | |
57 | |
58 DCHECK(is_compiled.find("normal") != is_compiled.end()); | |
59 | 81 |
60 DCHECK(is_compiled["parenthesized"]); | 82 DCHECK(is_compiled["parenthesized"]); |
61 DCHECK(is_compiled["parenthesized2"]); | 83 DCHECK(is_compiled["parenthesized2"]); |
62 DCHECK(is_compiled["exclaimed"]); | 84 DCHECK(is_compiled["exclaimed"]); |
63 DCHECK(is_compiled["exclaimed2"]); | 85 DCHECK(is_compiled["exclaimed2"]); |
64 DCHECK(!is_compiled["normal"]); | 86 DCHECK(!is_compiled["normal"]); |
65 DCHECK(!is_compiled["normal2"]); | 87 DCHECK(!is_compiled["normal2"]); |
66 DCHECK(!is_compiled["normal3"]); | 88 DCHECK(!is_compiled["normal3"]); |
67 DCHECK(!is_compiled["normal4"]); | 89 DCHECK(!is_compiled["normal4"]); |
68 } | 90 } |
| 91 |
| 92 TEST(CommaFunctionSequence) { |
| 93 if (!FLAG_lazy) return; |
| 94 |
| 95 Isolate* isolate = CcTest::i_isolate(); |
| 96 HandleScope scope(isolate); |
| 97 LocalContext env; |
| 98 |
| 99 const char src[] = "!function a(){}(),function b(){}(),function c(){}();"; |
| 100 std::unordered_map<std::string, bool> is_compiled; |
| 101 GetTopLevelFunctionInfo(v8_compile(src), &is_compiled); |
| 102 |
| 103 DCHECK(is_compiled["a"]); |
| 104 DCHECK(is_compiled["b"]); |
| 105 DCHECK(is_compiled["c"]); |
| 106 } |
OLD | NEW |