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 
14 namespace v8 {
15 namespace internal {
16 namespace parsing {
17 
ParseProgram(ParseInfo * info,bool internalize)18 bool ParseProgram(ParseInfo* info, bool internalize) {
19   DCHECK(info->is_toplevel());
20   DCHECK_NULL(info->literal());
21 
22   Parser parser(info);
23 
24   FunctionLiteral* result = nullptr;
25   // Ok to use Isolate here; this function is only called in the main thread.
26   DCHECK(parser.parsing_on_main_thread_);
27   Isolate* isolate = info->isolate();
28 
29   parser.SetCachedData(info);
30   result = parser.ParseProgram(isolate, info);
31   info->set_literal(result);
32   if (result == nullptr) {
33     parser.ReportErrors(isolate, info->script());
34   } else {
35     info->set_language_mode(info->literal()->language_mode());
36   }
37   parser.UpdateStatistics(isolate, info->script());
38   if (internalize) {
39     info->ast_value_factory()->Internalize(isolate);
40   }
41   return (result != nullptr);
42 }
43 
ParseFunction(ParseInfo * info,bool internalize)44 bool ParseFunction(ParseInfo* info, bool internalize) {
45   DCHECK(!info->is_toplevel());
46   DCHECK_NULL(info->literal());
47 
48   Parser parser(info);
49 
50   FunctionLiteral* result = nullptr;
51   // Ok to use Isolate here; this function is only called in the main thread.
52   DCHECK(parser.parsing_on_main_thread_);
53   Isolate* isolate = info->isolate();
54 
55   result = parser.ParseFunction(isolate, info);
56   info->set_literal(result);
57   if (result == nullptr) {
58     parser.ReportErrors(isolate, info->script());
59   }
60   parser.UpdateStatistics(isolate, info->script());
61   if (internalize) {
62     info->ast_value_factory()->Internalize(isolate);
63   }
64   return (result != nullptr);
65 }
66 
ParseAny(ParseInfo * info,bool internalize)67 bool ParseAny(ParseInfo* info, bool internalize) {
68   return info->is_toplevel() ? ParseProgram(info, internalize)
69                              : ParseFunction(info, internalize);
70 }
71 
72 }  // namespace parsing
73 }  // namespace internal
74 }  // namespace v8
75