V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
wasm-engine.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_WASM_ENGINE_H_
6 #define V8_WASM_WASM_ENGINE_H_
7 
8 #include <memory>
9 #include <unordered_set>
10 
11 #include "src/wasm/wasm-code-manager.h"
12 #include "src/wasm/wasm-memory.h"
13 #include "src/wasm/wasm-tier.h"
14 #include "src/zone/accounting-allocator.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 class AsmWasmData;
20 class CodeTracer;
21 class CompilationStatistics;
22 class WasmInstanceObject;
23 class WasmModuleObject;
24 
25 namespace wasm {
26 
27 class AsyncCompileJob;
28 class ErrorThrower;
29 struct ModuleWireBytes;
30 struct WasmFeatures;
31 
32 class V8_EXPORT_PRIVATE CompilationResultResolver {
33  public:
34  virtual void OnCompilationSucceeded(Handle<WasmModuleObject> result) = 0;
35  virtual void OnCompilationFailed(Handle<Object> error_reason) = 0;
36  virtual ~CompilationResultResolver() = default;
37 };
38 
39 class V8_EXPORT_PRIVATE InstantiationResultResolver {
40  public:
41  virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
42  virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
43  virtual ~InstantiationResultResolver() = default;
44 };
45 
46 // The central data structure that represents an engine instance capable of
47 // loading, instantiating, and executing WASM code.
48 class V8_EXPORT_PRIVATE WasmEngine {
49  public:
50  WasmEngine();
51  ~WasmEngine();
52 
53  // Synchronously validates the given bytes that represent an encoded WASM
54  // module.
55  bool SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
56  const ModuleWireBytes& bytes);
57 
58  // Synchronously compiles the given bytes that represent a translated
59  // asm.js module.
60  MaybeHandle<AsmWasmData> SyncCompileTranslatedAsmJs(
61  Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
62  Vector<const byte> asm_js_offset_table_bytes,
63  Handle<HeapNumber> uses_bitset);
64  Handle<WasmModuleObject> FinalizeTranslatedAsmJs(
65  Isolate* isolate, Handle<AsmWasmData> asm_wasm_data,
66  Handle<Script> script);
67 
68  // Synchronously compiles the given bytes that represent an encoded WASM
69  // module.
70  MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
71  const WasmFeatures& enabled,
72  ErrorThrower* thrower,
73  const ModuleWireBytes& bytes);
74 
75  // Synchronously instantiate the given WASM module with the given imports.
76  // If the module represents an asm.js module, then the supplied {memory}
77  // should be used as the memory of the instance.
78  MaybeHandle<WasmInstanceObject> SyncInstantiate(
79  Isolate* isolate, ErrorThrower* thrower,
82 
83  // Begin an asynchronous compilation of the given bytes that represent an
84  // encoded WASM module.
85  // The {is_shared} flag indicates if the bytes backing the module could
86  // be shared across threads, i.e. could be concurrently modified.
87  void AsyncCompile(Isolate* isolate, const WasmFeatures& enabled,
88  std::shared_ptr<CompilationResultResolver> resolver,
89  const ModuleWireBytes& bytes, bool is_shared);
90 
91  // Begin an asynchronous instantiation of the given WASM module.
92  void AsyncInstantiate(Isolate* isolate,
93  std::unique_ptr<InstantiationResultResolver> resolver,
94  Handle<WasmModuleObject> module_object,
95  MaybeHandle<JSReceiver> imports);
96 
97  std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
98  Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
99  std::shared_ptr<CompilationResultResolver> resolver);
100 
101  // Compiles the function with the given index at a specific compilation tier
102  // and returns true on success, false otherwise. This is mostly used for
103  // testing to force a function into a specific tier.
104  bool CompileFunction(Isolate* isolate, NativeModule* native_module,
105  uint32_t function_index, ExecutionTier tier);
106 
107  // Exports the sharable parts of the given module object so that they can be
108  // transferred to a different Context/Isolate using the same engine.
109  std::shared_ptr<NativeModule> ExportNativeModule(
110  Handle<WasmModuleObject> module_object);
111 
112  // Imports the shared part of a module from a different Context/Isolate using
113  // the the same engine, recreating a full module object in the given Isolate.
114  Handle<WasmModuleObject> ImportNativeModule(
115  Isolate* isolate, std::shared_ptr<NativeModule> shared_module);
116 
117  WasmCodeManager* code_manager() { return &code_manager_; }
118 
119  WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
120 
121  AccountingAllocator* allocator() { return &allocator_; }
122 
123  // Compilation statistics for TurboFan compilations.
124  CompilationStatistics* GetOrCreateTurboStatistics();
125 
126  // Prints the gathered compilation statistics, then resets them.
127  void DumpAndResetTurboStatistics();
128 
129  // Used to redirect tracing output from {stdout} to a file.
130  CodeTracer* GetCodeTracer();
131 
132  // Remove {job} from the list of active compile jobs.
133  std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
134 
135  // Returns true if at least one AsyncCompileJob that belongs to the given
136  // Isolate is currently running.
137  bool HasRunningCompileJob(Isolate* isolate);
138 
139  // Deletes all AsyncCompileJobs that belong to the given Isolate. All
140  // compilation is aborted, no more callbacks will be triggered. This is used
141  // for tearing down an isolate, or to clean it up to be reused.
142  void DeleteCompileJobsOnIsolate(Isolate* isolate);
143 
144  // Manage the set of Isolates that use this WasmEngine.
145  void AddIsolate(Isolate* isolate);
146  void RemoveIsolate(Isolate* isolate);
147 
148  // Call on process start and exit.
149  static void InitializeOncePerProcess();
150  static void GlobalTearDown();
151 
152  // Constructs a WasmEngine instance. Depending on whether we are sharing
153  // engines this might be a pointer to a new instance or to a shared one.
154  static std::shared_ptr<WasmEngine> GetWasmEngine();
155 
156  private:
157  AsyncCompileJob* CreateAsyncCompileJob(
158  Isolate* isolate, const WasmFeatures& enabled,
159  std::unique_ptr<byte[]> bytes_copy, size_t length,
160  Handle<Context> context,
161  std::shared_ptr<CompilationResultResolver> resolver);
162 
163  WasmMemoryTracker memory_tracker_;
164  WasmCodeManager code_manager_;
165  AccountingAllocator allocator_;
166 
167  // This mutex protects all information which is mutated concurrently or
168  // fields that are initialized lazily on the first access.
169  base::Mutex mutex_;
170 
172  // Protected by {mutex_}:
173 
174  // We use an AsyncCompileJob as the key for itself so that we can delete the
175  // job from the map when it is finished.
176  std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
177 
178  std::unique_ptr<CompilationStatistics> compilation_stats_;
179  std::unique_ptr<CodeTracer> code_tracer_;
180 
181  // Set of isolates which use this WasmEngine. Used for cross-isolate GCs.
182  std::unordered_set<Isolate*> isolates_;
183 
184  // End of fields protected by {mutex_}.
186 
187  DISALLOW_COPY_AND_ASSIGN(WasmEngine);
188 };
189 
190 } // namespace wasm
191 } // namespace internal
192 } // namespace v8
193 
194 #endif // V8_WASM_WASM_ENGINE_H_
Definition: libplatform.h:13