v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
streaming-decoder.h
Go to the documentation of this file.
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_STREAMING_DECODER_H_
6#define V8_WASM_STREAMING_DECODER_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <memory>
13
14#include "src/base/macros.h"
15#include "src/base/vector.h"
20
21namespace v8::internal::wasm {
22
23class NativeModule;
24
25// This class is an interface for the StreamingDecoder to start the processing
26// of the incoming module bytes.
28 public:
29 virtual ~StreamingProcessor() = default;
30 // Process the first 8 bytes of a WebAssembly module. Returns true if the
31 // processing finished successfully and the decoding should continue.
33
34 // Process all sections but the code section. Returns true if the processing
35 // finished successfully and the decoding should continue.
36 virtual bool ProcessSection(SectionCode section_code,
38 uint32_t offset) = 0;
39
40 // Process the start of the code section. Returns true if the processing
41 // finished successfully and the decoding should continue.
42 virtual bool ProcessCodeSectionHeader(int num_functions, uint32_t offset,
43 std::shared_ptr<WireBytesStorage>,
44 int code_section_start,
45 int code_section_length) = 0;
46
47 // Process a function body. Returns true if the processing finished
48 // successfully and the decoding should continue.
50 uint32_t offset) = 0;
51
52 // Report the end of a chunk.
53 virtual void OnFinishedChunk() = 0;
54 // Report the end of the stream. This will be called even after an error has
55 // been detected. In any case, the parameter is the total received bytes.
57 bool after_error) = 0;
58 // Report the abortion of the stream.
59 virtual void OnAbort() = 0;
60
61 // Attempt to deserialize the module. Supports embedder caching.
62 virtual bool Deserialize(base::Vector<const uint8_t> module_bytes,
63 base::Vector<const uint8_t> wire_bytes) = 0;
64};
65
66// The StreamingDecoder takes a sequence of byte arrays, each received by a call
67// of {OnBytesReceived}, and extracts the bytes which belong to section payloads
68// and function bodies.
70 public:
71 virtual ~StreamingDecoder() = default;
72
73 // The buffer passed into OnBytesReceived is owned by the caller.
75
76 virtual void Finish(bool can_use_compiled_module = true) = 0;
77
78 virtual void Abort() = 0;
79
80 // Notify the StreamingDecoder that the job was discarded and the
81 // StreamingProcessor should not be called anymore.
82 virtual void NotifyCompilationDiscarded() = 0;
83
84 // Caching support.
85 // Sets the callback that is called after a new chunk of the module is tiered
86 // up.
88 std::function<void(const std::shared_ptr<NativeModule>&)>;
89
92 more_functions_can_be_serialized_callback_ = std::move(callback);
93 }
94
95 // Passes previously compiled module bytes from the embedder's cache.
96 // The content shouldn't be used until Finish(true) is called.
98 compiled_module_bytes_ = bytes;
99 }
100
102 const std::shared_ptr<NativeModule>& native_module) = 0;
103
104 const std::string& url() const { return *url_; }
105 std::shared_ptr<const std::string> shared_url() const { return url_; }
106
108 url_->assign(url.begin(), url.size());
109 }
110
111 static std::unique_ptr<StreamingDecoder> CreateAsyncStreamingDecoder(
112 std::unique_ptr<StreamingProcessor> processor);
113
114 static std::unique_ptr<StreamingDecoder> CreateSyncStreamingDecoder(
115 Isolate* isolate, WasmEnabledFeatures enabled,
116 CompileTimeImports compile_imports, DirectHandle<Context> context,
117 const char* api_method_name_for_errors,
118 std::shared_ptr<CompilationResultResolver> resolver);
119
120 protected:
121 bool deserializing() const { return !compiled_module_bytes_.empty(); }
122
123 const std::shared_ptr<std::string> url_ = std::make_shared<std::string>();
126 // The content of `compiled_module_bytes_` shouldn't be used until
127 // Finish(true) is called.
129};
130
131} // namespace v8::internal::wasm
132
133#endif // V8_WASM_STREAMING_DECODER_H_
constexpr size_t size() const
Definition vector.h:70
constexpr T * begin() const
Definition vector.h:96
void SetMoreFunctionsCanBeSerializedCallback(MoreFunctionsCanBeSerializedCallback callback)
std::shared_ptr< const std::string > shared_url() const
MoreFunctionsCanBeSerializedCallback more_functions_can_be_serialized_callback_
std::function< void(const std::shared_ptr< NativeModule > &)> MoreFunctionsCanBeSerializedCallback
virtual void Finish(bool can_use_compiled_module=true)=0
virtual void OnBytesReceived(base::Vector< const uint8_t > bytes)=0
void SetUrl(base::Vector< const char > url)
base::Vector< const uint8_t > compiled_module_bytes_
virtual void NotifyNativeModuleCreated(const std::shared_ptr< NativeModule > &native_module)=0
void SetCompiledModuleBytes(base::Vector< const uint8_t > bytes)
virtual void NotifyCompilationDiscarded()=0
virtual bool ProcessModuleHeader(base::Vector< const uint8_t > bytes)=0
virtual bool ProcessFunctionBody(base::Vector< const uint8_t > bytes, uint32_t offset)=0
virtual bool ProcessCodeSectionHeader(int num_functions, uint32_t offset, std::shared_ptr< WireBytesStorage >, int code_section_start, int code_section_length)=0
virtual bool Deserialize(base::Vector< const uint8_t > module_bytes, base::Vector< const uint8_t > wire_bytes)=0
virtual bool ProcessSection(SectionCode section_code, base::Vector< const uint8_t > bytes, uint32_t offset)=0
virtual void OnFinishedStream(base::OwnedVector< const uint8_t > bytes, bool after_error)=0
int32_t offset
TNode< Object > callback
#define V8_EXPORT_PRIVATE
Definition macros.h:460