Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/parsing/parsing.h"
6 :
7 : #include <memory>
8 :
9 : #include "src/ast/ast.h"
10 : #include "src/objects-inl.h"
11 : #include "src/parsing/parse-info.h"
12 : #include "src/parsing/parser.h"
13 : #include "src/parsing/scanner-character-streams.h"
14 : #include "src/vm-state-inl.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 : namespace parsing {
19 :
20 1710495 : bool ParseProgram(ParseInfo* info, Isolate* isolate) {
21 : DCHECK(info->is_toplevel());
22 : DCHECK_NULL(info->literal());
23 :
24 : VMState<PARSER> state(isolate);
25 :
26 : // Create a character stream for the parser.
27 3420989 : Handle<String> source(String::cast(info->script()->source()), isolate);
28 1710496 : isolate->counters()->total_parse_size()->Increment(source->length());
29 : std::unique_ptr<Utf16CharacterStream> stream(
30 1710497 : ScannerStream::For(isolate, source));
31 3420993 : info->set_character_stream(std::move(stream));
32 :
33 3420993 : Parser parser(info);
34 :
35 : FunctionLiteral* result = nullptr;
36 : // Ok to use Isolate here; this function is only called in the main thread.
37 : DCHECK(parser.parsing_on_main_thread_);
38 :
39 1710491 : result = parser.ParseProgram(isolate, info);
40 : info->set_literal(result);
41 1710490 : if (result == nullptr) {
42 : info->pending_error_handler()->ReportErrors(isolate, info->script(),
43 368124 : info->ast_value_factory());
44 : } else {
45 1342366 : info->set_language_mode(info->literal()->language_mode());
46 1342366 : if (info->is_eval()) {
47 858112 : info->set_allow_eval_cache(parser.allow_eval_cache());
48 : }
49 : }
50 1710490 : parser.UpdateStatistics(isolate, info->script());
51 3420993 : return (result != nullptr);
52 : }
53 :
54 717429 : bool ParseFunction(ParseInfo* info, Handle<SharedFunctionInfo> shared_info,
55 : Isolate* isolate) {
56 : DCHECK(!info->is_toplevel());
57 : DCHECK(!shared_info.is_null());
58 : DCHECK_NULL(info->literal());
59 :
60 : // Create a character stream for the parser.
61 1434869 : Handle<String> source(String::cast(info->script()->source()), isolate);
62 717434 : isolate->counters()->total_parse_size()->Increment(source->length());
63 : std::unique_ptr<Utf16CharacterStream> stream(
64 : ScannerStream::For(isolate, source, shared_info->StartPosition(),
65 1434878 : shared_info->EndPosition()));
66 1434868 : info->set_character_stream(std::move(stream));
67 :
68 : VMState<PARSER> state(isolate);
69 :
70 1434878 : Parser parser(info);
71 :
72 : FunctionLiteral* result = nullptr;
73 : // Ok to use Isolate here; this function is only called in the main thread.
74 : DCHECK(parser.parsing_on_main_thread_);
75 :
76 717423 : result = parser.ParseFunction(isolate, info, shared_info);
77 : info->set_literal(result);
78 717428 : if (result == nullptr) {
79 : info->pending_error_handler()->ReportErrors(isolate, info->script(),
80 5188 : info->ast_value_factory());
81 : } else {
82 712240 : info->ast_value_factory()->Internalize(isolate);
83 712256 : if (info->is_eval()) {
84 54625 : info->set_allow_eval_cache(parser.allow_eval_cache());
85 : }
86 : }
87 717444 : parser.UpdateStatistics(isolate, info->script());
88 1434889 : return (result != nullptr);
89 : }
90 :
91 736230 : bool ParseAny(ParseInfo* info, Handle<SharedFunctionInfo> shared_info,
92 : Isolate* isolate) {
93 : DCHECK(!shared_info.is_null());
94 : return info->is_toplevel() ? ParseProgram(info, isolate)
95 736230 : : ParseFunction(info, shared_info, isolate);
96 : }
97 :
98 : } // namespace parsing
99 : } // namespace internal
100 183867 : } // namespace v8
|