v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
sync-streaming-decoder.cc
Go to the documentation of this file.
1// Copyright 2020 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
10
11namespace v8 {
12namespace internal {
13namespace wasm {
14
16 public:
18 CompileTimeImports compile_imports,
20 const char* api_method_name_for_errors,
21 std::shared_ptr<CompilationResultResolver> resolver)
22 : isolate_(isolate),
23 enabled_(enabled),
24 compile_imports_(std::move(compile_imports)),
25 context_(indirect_handle(context)),
26 api_method_name_for_errors_(api_method_name_for_errors),
27 resolver_(resolver) {}
28
29 // The buffer passed into OnBytesReceived is owned by the caller.
31 buffer_.emplace_back(bytes.size());
32 CHECK_EQ(buffer_.back().size(), bytes.size());
33 std::memcpy(buffer_.back().data(), bytes.data(), bytes.size());
34 buffer_size_ += bytes.size();
35 }
36
37 void Finish(bool can_use_compiled_module) override {
38 // We copy all received chunks into one byte buffer.
39 auto bytes = base::OwnedVector<uint8_t>::NewForOverwrite(buffer_size_);
40 uint8_t* destination = bytes.begin();
41 for (auto& chunk : buffer_) {
42 std::memcpy(destination, chunk.data(), chunk.size());
43 destination += chunk.size();
44 }
45 CHECK_EQ(destination - bytes.begin(), buffer_size_);
46
47 // Check if we can deserialize the module from cache.
48 if (can_use_compiled_module && deserializing()) {
49 HandleScope scope(isolate_);
50 SaveAndSwitchContext saved_context(isolate_, *context_);
51
53 DeserializeNativeModule(isolate_, compiled_module_bytes_,
54 bytes.as_vector(), compile_imports_,
55 base::VectorOf(url()));
56
57 if (!module_object.is_null()) {
58 DirectHandle<WasmModuleObject> module = module_object.ToHandleChecked();
59 resolver_->OnCompilationSucceeded(module);
60 return;
61 }
62 }
63
64 // Compile the received bytes synchronously.
65 ErrorThrower thrower(isolate_, api_method_name_for_errors_);
68 std::move(compile_imports_), &thrower,
69 std::move(bytes));
70 if (thrower.error()) {
71 resolver_->OnCompilationFailed(thrower.Reify());
72 return;
73 }
74 DirectHandle<WasmModuleObject> module = module_object.ToHandleChecked();
75 resolver_->OnCompilationSucceeded(module);
76 }
77
78 void Abort() override {
79 // Abort is fully handled by the API, we only clear the buffer.
80 buffer_.clear();
81 }
82
83 void NotifyCompilationDiscarded() override { buffer_.clear(); }
84
86 const std::shared_ptr<NativeModule>&) override {
87 // This function is only called from the {AsyncCompileJob}.
89 }
90
91 private:
97 std::shared_ptr<CompilationResultResolver> resolver_;
98
99 std::vector<std::vector<uint8_t>> buffer_;
100 size_t buffer_size_ = 0;
101};
102
103std::unique_ptr<StreamingDecoder> StreamingDecoder::CreateSyncStreamingDecoder(
104 Isolate* isolate, WasmEnabledFeatures enabled,
105 CompileTimeImports compile_imports, DirectHandle<Context> context,
106 const char* api_method_name_for_errors,
107 std::shared_ptr<CompilationResultResolver> resolver) {
108 return std::make_unique<SyncStreamingDecoder>(
109 isolate, enabled, std::move(compile_imports), context,
110 api_method_name_for_errors, std::move(resolver));
111}
112} // namespace wasm
113} // namespace internal
114} // namespace v8
Isolate * isolate_
V8_INLINE DirectHandle< T > ToHandleChecked() const
V8_INLINE bool is_null() const
V8_WARN_UNUSED_RESULT DirectHandle< JSObject > Reify()
static std::unique_ptr< StreamingDecoder > CreateSyncStreamingDecoder(Isolate *isolate, WasmEnabledFeatures enabled, CompileTimeImports compile_imports, DirectHandle< Context > context, const char *api_method_name_for_errors, std::shared_ptr< CompilationResultResolver > resolver)
std::vector< std::vector< uint8_t > > buffer_
void NotifyNativeModuleCreated(const std::shared_ptr< NativeModule > &) override
void Finish(bool can_use_compiled_module) override
void OnBytesReceived(base::Vector< const uint8_t > bytes) override
std::shared_ptr< CompilationResultResolver > resolver_
SyncStreamingDecoder(Isolate *isolate, WasmEnabledFeatures enabled, CompileTimeImports compile_imports, DirectHandle< Context > context, const char *api_method_name_for_errors, std::shared_ptr< CompilationResultResolver > resolver)
MaybeDirectHandle< WasmModuleObject > SyncCompile(Isolate *isolate, WasmEnabledFeatures enabled, CompileTimeImports compile_imports, ErrorThrower *thrower, base::OwnedVector< const uint8_t > bytes)
base::OwnedVector< uint8_t > buffer_
Definition assembler.cc:111
Handle< Context > context_
v8::Global< v8::Promise::Resolver > resolver_
InstructionOperand destination
STL namespace.
MaybeDirectHandle< WasmModuleObject > DeserializeNativeModule(Isolate *isolate, base::Vector< const uint8_t > data, base::Vector< const uint8_t > wire_bytes_vec, const CompileTimeImports &compile_imports, base::Vector< const char > source_url)
WasmEngine * GetWasmEngine()
V8_INLINE IndirectHandle< T > indirect_handle(DirectHandle< T > handle)
Definition handles.h:757
Definition c-api.cc:87
#define UNREACHABLE()
Definition logging.h:67
#define CHECK_EQ(lhs, rhs)
#define V8_EXPORT_PRIVATE
Definition macros.h:460
bool enabled_
Definition string.cc:1013