V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
module-compiler.h
1 // Copyright 2017 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_WASM_MODULE_COMPILER_H_
6 #define V8_WASM_MODULE_COMPILER_H_
7 
8 #include <atomic>
9 #include <functional>
10 #include <memory>
11 
12 #include "src/cancelable-task.h"
13 #include "src/globals.h"
14 #include "src/wasm/compilation-environment.h"
15 #include "src/wasm/wasm-features.h"
16 #include "src/wasm/wasm-module.h"
17 
18 namespace v8 {
19 namespace internal {
20 
21 class JSArrayBuffer;
22 class JSPromise;
23 class Counters;
24 class WasmModuleObject;
25 class WasmInstanceObject;
26 
27 template <typename T>
28 class Vector;
29 
30 namespace wasm {
31 
32 struct CompilationEnv;
33 class CompilationResultResolver;
34 class ErrorThrower;
35 class ModuleCompiler;
36 class NativeModule;
37 class WasmCode;
38 struct WasmModule;
39 
40 std::unique_ptr<NativeModule> CompileToNativeModule(
41  Isolate* isolate, const WasmFeatures& enabled, ErrorThrower* thrower,
42  std::shared_ptr<const WasmModule> module, const ModuleWireBytes& wire_bytes,
43  Handle<FixedArray>* export_wrappers_out);
44 
45 MaybeHandle<WasmInstanceObject> InstantiateToInstanceObject(
46  Isolate* isolate, ErrorThrower* thrower,
47  Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
48  MaybeHandle<JSArrayBuffer> memory);
49 
50 V8_EXPORT_PRIVATE
51 void CompileJsToWasmWrappers(Isolate* isolate, NativeModule* native_module,
52  Handle<FixedArray> export_wrappers);
53 
54 V8_EXPORT_PRIVATE Handle<Script> CreateWasmScript(
55  Isolate* isolate, const ModuleWireBytes& wire_bytes,
56  const std::string& source_map_url);
57 
58 // Triggered by the WasmCompileLazy builtin.
59 // Returns the instruction start of the compiled code object.
60 Address CompileLazy(Isolate*, NativeModule*, uint32_t func_index);
61 
62 // Encapsulates all the state and steps of an asynchronous compilation.
63 // An asynchronous compile job consists of a number of tasks that are executed
64 // as foreground and background tasks. Any phase that touches the V8 heap or
65 // allocates on the V8 heap (e.g. creating the module object) must be a
66 // foreground task. All other tasks (e.g. decoding and validating, the majority
67 // of the work of compilation) can be background tasks.
68 // TODO(wasm): factor out common parts of this with the synchronous pipeline.
70  public:
71  AsyncCompileJob(Isolate* isolate, const WasmFeatures& enabled_features,
72  std::unique_ptr<byte[]> bytes_copy, size_t length,
73  Handle<Context> context,
74  std::shared_ptr<CompilationResultResolver> resolver);
75  ~AsyncCompileJob();
76 
77  void Start();
78 
79  std::shared_ptr<StreamingDecoder> CreateStreamingDecoder();
80 
81  void Abort();
82  void CancelPendingForegroundTask();
83 
84  Isolate* isolate() const { return isolate_; }
85 
86  private:
87  class CompileTask;
88  class CompileStep;
90 
91  // States of the AsyncCompileJob.
92  class DecodeModule; // Step 1 (async)
93  class DecodeFail; // Step 1b (sync)
94  class PrepareAndStartCompile; // Step 2 (sync)
95  class CompileFailed; // Step 4b (sync)
96  class CompileWrappers; // Step 5 (sync)
97  class FinishModule; // Step 6 (sync)
98 
99  friend class AsyncStreamingProcessor;
100 
101  // Decrements the number of outstanding finishers. The last caller of this
102  // function should finish the asynchronous compilation, see the comment on
103  // {outstanding_finishers_}.
104  V8_WARN_UNUSED_RESULT bool DecrementAndCheckFinisherCount() {
105  return outstanding_finishers_.fetch_sub(1) == 1;
106  }
107 
108  void PrepareRuntimeObjects(std::shared_ptr<const WasmModule>);
109 
110  void FinishCompile(bool compile_wrappers);
111 
112  void AsyncCompileFailed(Handle<Object> error_reason);
113 
114  void AsyncCompileSucceeded(Handle<WasmModuleObject> result);
115 
116  void StartForegroundTask();
117  void ExecuteForegroundTaskImmediately();
118 
119  void StartBackgroundTask();
120 
121  enum UseExistingForegroundTask : bool {
122  kUseExistingForegroundTask = true,
123  kAssertNoExistingForegroundTask = false
124  };
125  // Switches to the compilation step {Step} and starts a foreground task to
126  // execute it. Most of the time we know that there cannot be a running
127  // foreground task. If there might be one, then pass
128  // kUseExistingForegroundTask to avoid spawning a second one.
129  template <typename Step,
130  UseExistingForegroundTask = kAssertNoExistingForegroundTask,
131  typename... Args>
132  void DoSync(Args&&... args);
133 
134  // Switches to the compilation step {Step} and immediately executes that step.
135  template <typename Step, typename... Args>
136  void DoImmediately(Args&&... args);
137 
138  // Switches to the compilation step {Step} and starts a background task to
139  // execute it.
140  template <typename Step, typename... Args>
141  void DoAsync(Args&&... args);
142 
143  // Switches to the compilation step {Step} but does not start a task to
144  // execute it.
145  template <typename Step, typename... Args>
146  void NextStep(Args&&... args);
147 
148  Isolate* const isolate_;
149  const WasmFeatures enabled_features_;
150  // Copy of the module wire bytes, moved into the {native_module_} on its
151  // creation.
152  std::unique_ptr<byte[]> bytes_copy_;
153  // Reference to the wire bytes (hold in {bytes_copy_} or as part of
154  // {native_module_}).
155  ModuleWireBytes wire_bytes_;
156  Handle<Context> native_context_;
157  const std::shared_ptr<CompilationResultResolver> resolver_;
158 
159  std::vector<DeferredHandles*> deferred_handles_;
160  Handle<WasmModuleObject> module_object_;
161  NativeModule* native_module_ = nullptr;
162 
163  std::unique_ptr<CompileStep> step_;
164  CancelableTaskManager background_task_manager_;
165 
166  std::shared_ptr<v8::TaskRunner> foreground_task_runner_;
167 
168  // For async compilation the AsyncCompileJob is the only finisher. For
169  // streaming compilation also the AsyncStreamingProcessor has to finish before
170  // compilation can be finished.
171  std::atomic<int32_t> outstanding_finishers_{1};
172 
173  // A reference to a pending foreground task, or {nullptr} if none is pending.
174  CompileTask* pending_foreground_task_ = nullptr;
175 
176  // The AsyncCompileJob owns the StreamingDecoder because the StreamingDecoder
177  // contains data which is needed by the AsyncCompileJob for streaming
178  // compilation. The AsyncCompileJob does not actively use the
179  // StreamingDecoder.
180  std::shared_ptr<StreamingDecoder> stream_;
181 };
182 
183 } // namespace wasm
184 } // namespace internal
185 } // namespace v8
186 
187 #endif // V8_WASM_MODULE_COMPILER_H_
Definition: libplatform.h:13