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#ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
6#define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
7
8#include <memory>
9
10#include "src/base/macros.h"
11#include "src/globals.h"
12#include "src/handles.h"
13#include "testing/gtest/include/gtest/gtest_prod.h"
14
15namespace v8 {
16namespace internal {
17
18class CompilerDispatcherTracer;
19class CompilationInfo;
20class CompilationJob;
21class Isolate;
22class ParseInfo;
23class Parser;
24class SharedFunctionInfo;
25class String;
26class UnicodeCache;
27class Utf16CharacterStream;
28class Zone;
29
30enum class CompileJobStatus {
31  kInitial,
32  kReadyToParse,
33  kParsed,
34  kReadyToAnalyse,
35  kReadyToCompile,
36  kCompiled,
37  kFailed,
38  kDone,
39};
40
41class V8_EXPORT_PRIVATE CompilerDispatcherJob {
42 public:
43  CompilerDispatcherJob(Isolate* isolate, Handle<SharedFunctionInfo> shared,
44                        size_t max_stack_size);
45  ~CompilerDispatcherJob();
46
47  CompileJobStatus status() const { return status_; }
48  bool can_parse_on_background_thread() const {
49    return can_parse_on_background_thread_;
50  }
51  // Should only be called after kReadyToCompile.
52  bool can_compile_on_background_thread() const {
53    DCHECK(compile_job_.get());
54    return can_compile_on_background_thread_;
55  }
56
57  // Transition from kInitial to kReadyToParse.
58  void PrepareToParseOnMainThread();
59
60  // Transition from kReadyToParse to kParsed.
61  void Parse();
62
63  // Transition from kParsed to kReadyToAnalyse (or kFailed). Returns false
64  // when transitioning to kFailed. In that case, an exception is pending.
65  bool FinalizeParsingOnMainThread();
66
67  // Transition from kReadyToAnalyse to kReadyToCompile (or kFailed). Returns
68  // false when transitioning to kFailed. In that case, an exception is pending.
69  bool PrepareToCompileOnMainThread();
70
71  // Transition from kReadyToCompile to kCompiled.
72  void Compile();
73
74  // Transition from kCompiled to kDone (or kFailed). Returns false when
75  // transitioning to kFailed. In that case, an exception is pending.
76  bool FinalizeCompilingOnMainThread();
77
78  // Transition from any state to kInitial and free all resources.
79  void ResetOnMainThread();
80
81 private:
82  FRIEND_TEST(CompilerDispatcherJobTest, ScopeChain);
83
84  CompileJobStatus status_ = CompileJobStatus::kInitial;
85  Isolate* isolate_;
86  CompilerDispatcherTracer* tracer_;
87  Handle<SharedFunctionInfo> shared_;  // Global handle.
88  Handle<String> source_;        // Global handle.
89  size_t max_stack_size_;
90
91  // Members required for parsing.
92  std::unique_ptr<UnicodeCache> unicode_cache_;
93  std::unique_ptr<Zone> zone_;
94  std::unique_ptr<Utf16CharacterStream> character_stream_;
95  std::unique_ptr<ParseInfo> parse_info_;
96  std::unique_ptr<Parser> parser_;
97  std::unique_ptr<DeferredHandles> handles_from_parsing_;
98
99  // Members required for compiling.
100  std::unique_ptr<CompilationInfo> compile_info_;
101  std::unique_ptr<CompilationJob> compile_job_;
102
103  bool can_parse_on_background_thread_;
104  bool can_compile_on_background_thread_;
105
106  DISALLOW_COPY_AND_ASSIGN(CompilerDispatcherJob);
107};
108
109}  // namespace internal
110}  // namespace v8
111
112#endif  // V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_JOB_H_
113