2017-12-05 00:28:35 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2018-02-02 12:05:19 +00:00
|
|
|
#ifndef V8_WASM_WASM_ENGINE_H_
|
|
|
|
#define V8_WASM_WASM_ENGINE_H_
|
2017-12-05 00:28:35 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2017-12-05 01:47:59 +00:00
|
|
|
#include "src/wasm/wasm-code-manager.h"
|
2018-01-06 01:33:31 +00:00
|
|
|
#include "src/wasm/wasm-memory.h"
|
2017-12-05 00:28:35 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
class WasmModuleObject;
|
|
|
|
class WasmInstanceObject;
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
namespace wasm {
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
class ErrorThrower;
|
|
|
|
struct ModuleWireBytes;
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
// The central data structure that represents an engine instance capable of
|
|
|
|
// loading, instantiating, and executing WASM code.
|
2018-01-17 14:46:27 +00:00
|
|
|
class V8_EXPORT_PRIVATE WasmEngine {
|
2017-12-05 00:28:35 +00:00
|
|
|
public:
|
|
|
|
explicit WasmEngine(std::unique_ptr<WasmCodeManager> code_manager)
|
|
|
|
: code_manager_(std::move(code_manager)) {}
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
// Synchronously validates the given bytes that represent an encoded WASM
|
|
|
|
// module.
|
2018-01-17 14:46:27 +00:00
|
|
|
bool SyncValidate(Isolate* isolate, const ModuleWireBytes& bytes);
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
// Synchronously compiles the given bytes that represent a translated
|
|
|
|
// asm.js module.
|
|
|
|
MaybeHandle<WasmModuleObject> SyncCompileTranslatedAsmJs(
|
|
|
|
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
|
|
|
|
Handle<Script> asm_js_script,
|
|
|
|
Vector<const byte> asm_js_offset_table_bytes);
|
|
|
|
|
|
|
|
// Synchronously compiles the given bytes that represent an encoded WASM
|
|
|
|
// module.
|
|
|
|
MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
|
|
|
|
ErrorThrower* thrower,
|
|
|
|
const ModuleWireBytes& bytes);
|
|
|
|
|
|
|
|
// Synchronously instantiate the given WASM module with the given imports.
|
|
|
|
// If the module represents an asm.js module, then the supplied {memory}
|
|
|
|
// should be used as the memory of the instance.
|
|
|
|
MaybeHandle<WasmInstanceObject> SyncInstantiate(
|
|
|
|
Isolate* isolate, ErrorThrower* thrower,
|
|
|
|
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
|
|
|
|
MaybeHandle<JSArrayBuffer> memory);
|
|
|
|
|
|
|
|
// Begin an asynchronous compilation of the given bytes that represent an
|
|
|
|
// encoded WASM module, placing the result in the supplied {promise}.
|
|
|
|
// The {is_shared} flag indicates if the bytes backing the module could
|
|
|
|
// be shared across threads, i.e. could be concurrently modified.
|
|
|
|
void AsyncCompile(Isolate* isolate, Handle<JSPromise> promise,
|
|
|
|
const ModuleWireBytes& bytes, bool is_shared);
|
|
|
|
|
|
|
|
// Begin an asynchronous instantiation of the given WASM module, placing the
|
|
|
|
// result in the supplied {promise}.
|
|
|
|
void AsyncInstantiate(Isolate* isolate, Handle<JSPromise> promise,
|
|
|
|
Handle<WasmModuleObject> module_object,
|
|
|
|
MaybeHandle<JSReceiver> imports);
|
|
|
|
|
2018-05-09 13:48:47 +00:00
|
|
|
std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
|
|
|
|
Isolate* isolate, Handle<Context> context, Handle<JSPromise> promise);
|
2017-12-05 00:28:35 +00:00
|
|
|
|
|
|
|
WasmCodeManager* code_manager() const { return code_manager_.get(); }
|
|
|
|
|
2018-03-20 17:23:01 +00:00
|
|
|
WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2018-03-19 09:22:23 +00:00
|
|
|
// We register and unregister CancelableTaskManagers that run
|
|
|
|
// isolate-dependent tasks. These tasks need to be shutdown if the isolate is
|
|
|
|
// shut down.
|
|
|
|
void Register(CancelableTaskManager* task_manager);
|
|
|
|
void Unregister(CancelableTaskManager* task_manager);
|
|
|
|
|
2018-05-09 13:48:47 +00:00
|
|
|
// Remove {job} from the list of active compile jobs.
|
|
|
|
std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
|
|
|
|
|
|
|
|
// Returns true if at lease one AsyncCompileJob is currently running.
|
|
|
|
bool HasRunningCompileJob() const { return !jobs_.empty(); }
|
|
|
|
|
|
|
|
// Cancel all AsyncCompileJobs so that they are not processed any further,
|
|
|
|
// but delay the deletion of their state until all tasks accessing the
|
|
|
|
// AsyncCompileJob finish their execution. This is used to clean-up the
|
|
|
|
// isolate to be reused.
|
|
|
|
void AbortAllCompileJobs();
|
|
|
|
|
2018-03-19 09:22:23 +00:00
|
|
|
void TearDown();
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
private:
|
2018-05-09 13:48:47 +00:00
|
|
|
AsyncCompileJob* CreateAsyncCompileJob(Isolate* isolate,
|
|
|
|
std::unique_ptr<byte[]> bytes_copy,
|
|
|
|
size_t length, Handle<Context> context,
|
|
|
|
Handle<JSPromise> promise);
|
|
|
|
|
|
|
|
// We use an AsyncCompileJob as the key for itself so that we can delete the
|
|
|
|
// job from the map when it is finished.
|
|
|
|
std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
|
2017-12-05 00:28:35 +00:00
|
|
|
std::unique_ptr<WasmCodeManager> code_manager_;
|
2018-03-20 17:23:01 +00:00
|
|
|
WasmMemoryTracker memory_tracker_;
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2018-03-19 09:22:23 +00:00
|
|
|
// Contains all CancelableTaskManagers that run tasks that are dependent
|
|
|
|
// on the isolate.
|
|
|
|
std::list<CancelableTaskManager*> task_managers_;
|
|
|
|
|
2018-01-06 01:33:31 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WasmEngine);
|
2017-12-05 00:28:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2018-02-02 12:05:19 +00:00
|
|
|
#endif // V8_WASM_WASM_ENGINE_H_
|