streaming-decoder.h 4.91 KB
Newer Older
1 2 3 4
// 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.

5 6 7 8
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

9 10 11
#ifndef V8_WASM_STREAMING_DECODER_H_
#define V8_WASM_STREAMING_DECODER_H_

12
#include <memory>
13
#include <vector>
14 15

#include "src/base/macros.h"
16
#include "src/base/vector.h"
17
#include "src/wasm/compilation-environment.h"
18
#include "src/wasm/wasm-constants.h"
19
#include "src/wasm/wasm-engine.h"
20
#include "src/wasm/wasm-result.h"
21 22 23 24

namespace v8 {
namespace internal {
namespace wasm {
25
class NativeModule;
26

27 28 29 30 31 32 33
// This class is an interface for the StreamingDecoder to start the processing
// of the incoming module bytes.
class V8_EXPORT_PRIVATE StreamingProcessor {
 public:
  virtual ~StreamingProcessor() = default;
  // Process the first 8 bytes of a WebAssembly module. Returns true if the
  // processing finished successfully and the decoding should continue.
34
  virtual bool ProcessModuleHeader(base::Vector<const uint8_t> bytes,
35 36 37 38 39
                                   uint32_t offset) = 0;

  // Process all sections but the code section. Returns true if the processing
  // finished successfully and the decoding should continue.
  virtual bool ProcessSection(SectionCode section_code,
40 41
                              base::Vector<const uint8_t> bytes,
                              uint32_t offset) = 0;
42 43 44

  // Process the start of the code section. Returns true if the processing
  // finished successfully and the decoding should continue.
45
  virtual bool ProcessCodeSectionHeader(int num_functions, uint32_t offset,
46
                                        std::shared_ptr<WireBytesStorage>,
47
                                        int code_section_start,
48
                                        int code_section_length) = 0;
49 50 51

  // Process a function body. Returns true if the processing finished
  // successfully and the decoding should continue.
52
  virtual bool ProcessFunctionBody(base::Vector<const uint8_t> bytes,
53 54 55 56 57 58 59
                                   uint32_t offset) = 0;

  // Report the end of a chunk.
  virtual void OnFinishedChunk() = 0;
  // Report the end of the stream. If the stream was successful, all
  // received bytes are passed by parameter. If there has been an error, an
  // empty array is passed.
60
  virtual void OnFinishedStream(base::OwnedVector<uint8_t> bytes) = 0;
61
  // Report an error detected in the StreamingDecoder.
62
  virtual void OnError(const WasmError&) = 0;
63 64
  // Report the abortion of the stream.
  virtual void OnAbort() = 0;
65 66

  // Attempt to deserialize the module. Supports embedder caching.
67 68
  virtual bool Deserialize(base::Vector<const uint8_t> module_bytes,
                           base::Vector<const uint8_t> wire_bytes) = 0;
69 70
};

71 72 73 74 75
// The StreamingDecoder takes a sequence of byte arrays, each received by a call
// of {OnBytesReceived}, and extracts the bytes which belong to section payloads
// and function bodies.
class V8_EXPORT_PRIVATE StreamingDecoder {
 public:
76
  virtual ~StreamingDecoder() = default;
77 78

  // The buffer passed into OnBytesReceived is owned by the caller.
79
  virtual void OnBytesReceived(base::Vector<const uint8_t> bytes) = 0;
80

81
  virtual void Finish() = 0;
82

83
  virtual void Abort() = 0;
84

85 86
  // Notify the StreamingDecoder that compilation ended and the
  // StreamingProcessor should not be called anymore.
87
  virtual void NotifyCompilationEnded() = 0;
88

89 90
  // Caching support.
  // Sets the callback that is called after the module is fully compiled.
91 92
  using ModuleCompiledCallback =
      std::function<void(const std::shared_ptr<NativeModule>&)>;
93 94 95 96 97

  void SetModuleCompiledCallback(ModuleCompiledCallback callback) {
    module_compiled_callback_ = callback;
  }

98
  // Passes previously compiled module bytes from the embedder's cache.
99 100
  bool SetCompiledModuleBytes(
      base::Vector<const uint8_t> compiled_module_bytes) {
101 102 103
    compiled_module_bytes_ = compiled_module_bytes;
    return true;
  }
104

105 106
  virtual void NotifyNativeModuleCreated(
      const std::shared_ptr<NativeModule>& native_module) = 0;
107

108
  base::Vector<const char> url() { return base::VectorOf(url_); }
109

110
  void SetUrl(base::Vector<const char> url) {
111 112 113
    url_.assign(url.begin(), url.length());
  }

114 115
  static std::unique_ptr<StreamingDecoder> CreateAsyncStreamingDecoder(
      std::unique_ptr<StreamingProcessor> processor);
116 117 118 119 120 121 122 123 124 125 126

  static std::unique_ptr<StreamingDecoder> CreateSyncStreamingDecoder(
      Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
      const char* api_method_name_for_errors,
      std::shared_ptr<CompilationResultResolver> resolver);

 protected:
  bool deserializing() const { return !compiled_module_bytes_.empty(); }

  std::string url_;
  ModuleCompiledCallback module_compiled_callback_;
127
  base::Vector<const uint8_t> compiled_module_bytes_;
128 129 130 131 132 133 134
};

}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_STREAMING_DECODER_H_