5 #ifndef V8_WASM_STREAMING_DECODER_H_ 6 #define V8_WASM_STREAMING_DECODER_H_ 11 #include "src/base/macros.h" 12 #include "src/vector.h" 13 #include "src/wasm/compilation-environment.h" 14 #include "src/wasm/wasm-constants.h" 15 #include "src/wasm/wasm-result.h" 22 class WasmModuleObject;
38 virtual bool ProcessSection(SectionCode section_code,
43 virtual bool ProcessCodeSectionHeader(
size_t num_functions,
uint32_t offset,
44 std::shared_ptr<WireBytesStorage>) = 0;
52 virtual void OnFinishedChunk() = 0;
60 virtual void OnAbort() = 0;
83 void NotifyCompilationEnded() { Fail(); }
87 using ModuleCompiledCallback = std::function<void(Handle<WasmModuleObject>)>;
88 void SetModuleCompiledCallback(ModuleCompiledCallback callback);
93 ModuleCompiledCallback module_compiled_callback()
const {
94 return module_compiled_callback_;
109 SectionBuffer(
uint32_t module_offset, uint8_t
id,
size_t payload_length,
112 module_offset_(module_offset),
115 payload_offset_(1 + length_bytes.length()) {
116 bytes_.start()[0] = id;
117 memcpy(bytes_.start() + 1, &length_bytes.first(), length_bytes.length());
120 SectionCode section_code()
const {
121 return static_cast<SectionCode
>(bytes_.start()[0]);
125 DCHECK_LE(module_offset_, ref.offset());
126 uint32_t offset_in_code_buffer = ref.offset() - module_offset_;
127 return bytes().SubVector(offset_in_code_buffer,
128 offset_in_code_buffer + ref.length());
131 uint32_t module_offset()
const {
return module_offset_; }
134 size_t length()
const {
return bytes_.size(); }
135 size_t payload_offset()
const {
return payload_offset_; }
140 const size_t payload_offset_;
169 class DecodingState {
171 virtual ~DecodingState() =
default;
179 virtual std::unique_ptr<DecodingState> Next(
184 size_t offset()
const {
return offset_; }
185 void set_offset(
size_t value) { offset_ = value; }
188 virtual bool is_finishing_allowed()
const {
return false; }
206 SectionBuffer* CreateNewBuffer(
uint32_t module_offset, uint8_t section_id,
210 std::unique_ptr<DecodingState> Error(
VoidResult result) {
211 if (ok()) processor_->OnError(std::move(result));
213 return std::unique_ptr<DecodingState>(
nullptr);
216 std::unique_ptr<DecodingState> Error(std::string message) {
217 return Error(VoidResult::Error(module_offset_ - 1, std::move(message)));
220 void ProcessModuleHeader() {
222 if (!processor_->ProcessModuleHeader(state_->buffer(), 0)) Fail();
225 void ProcessSection(SectionBuffer* buffer) {
227 if (!processor_->ProcessSection(
228 buffer->section_code(), buffer->payload(),
229 buffer->module_offset() +
230 static_cast<uint32_t>(buffer->payload_offset()))) {
235 void StartCodeSection(
size_t num_functions,
236 std::shared_ptr<WireBytesStorage> wire_bytes_storage) {
240 if (!processor_->ProcessCodeSectionHeader(num_functions,
242 std::move(wire_bytes_storage))) {
250 if (!processor_->ProcessFunctionBody(bytes, module_offset)) Fail();
260 bool ok()
const {
return processor_ !=
nullptr; }
262 uint32_t module_offset()
const {
return module_offset_; }
264 bool deserializing()
const {
return !compiled_module_bytes_.is_empty(); }
266 std::unique_ptr<StreamingProcessor> processor_;
267 std::unique_ptr<DecodingState> state_;
268 std::vector<std::shared_ptr<SectionBuffer>> section_buffers_;
270 size_t total_size_ = 0;
271 uint8_t next_section_id_ = kFirstSectionInModule;
274 ModuleCompiledCallback module_compiled_callback_ =
nullptr;
276 std::vector<uint8_t> wire_bytes_for_deserializing_;
286 #endif // V8_WASM_STREAMING_DECODER_H_