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

#ifndef V8_WASM_STREAMING_DECODER_H_
#define V8_WASM_STREAMING_DECODER_H_

8
#include <memory>
9
#include <vector>
10 11

#include "src/base/macros.h"
12
#include "src/utils/vector.h"
13
#include "src/wasm/compilation-environment.h"
14
#include "src/wasm/wasm-constants.h"
15
#include "src/wasm/wasm-engine.h"
16
#include "src/wasm/wasm-result.h"
17 18 19 20

namespace v8 {
namespace internal {
namespace wasm {
21
class NativeModule;
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// 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.
  virtual bool ProcessModuleHeader(Vector<const uint8_t> bytes,
                                   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,
                              Vector<const uint8_t> bytes, uint32_t offset) = 0;

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

  // Process a function body. Returns true if the processing finished
  // successfully and the decoding should continue.
  virtual bool ProcessFunctionBody(Vector<const uint8_t> bytes,
                                   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.
54
  virtual void OnFinishedStream(OwnedVector<uint8_t> bytes) = 0;
55
  // Report an error detected in the StreamingDecoder.
56
  virtual void OnError(const WasmError&) = 0;
57 58
  // Report the abortion of the stream.
  virtual void OnAbort() = 0;
59 60 61 62

  // Attempt to deserialize the module. Supports embedder caching.
  virtual bool Deserialize(Vector<const uint8_t> module_bytes,
                           Vector<const uint8_t> wire_bytes) = 0;
63 64
};

65 66 67 68 69
// 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:
70
  virtual ~StreamingDecoder() = default;
71 72

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

75
  virtual void Finish() = 0;
76

77
  virtual void Abort() = 0;
78

79 80
  // Notify the StreamingDecoder that compilation ended and the
  // StreamingProcessor should not be called anymore.
81
  virtual void NotifyCompilationEnded() = 0;
82

83 84
  // Caching support.
  // Sets the callback that is called after the module is fully compiled.
85 86
  using ModuleCompiledCallback =
      std::function<void(const std::shared_ptr<NativeModule>&)>;
87 88 89 90 91

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

92
  // Passes previously compiled module bytes from the embedder's cache.
93 94 95 96
  bool SetCompiledModuleBytes(Vector<const uint8_t> compiled_module_bytes) {
    compiled_module_bytes_ = compiled_module_bytes;
    return true;
  }
97

98 99
  virtual void NotifyNativeModuleCreated(
      const std::shared_ptr<NativeModule>& native_module) = 0;
100

101 102 103 104 105 106
  Vector<const char> url() { return VectorOf(url_); }

  void SetUrl(Vector<const char> url) {
    url_.assign(url.begin(), url.length());
  }

107 108
  static std::unique_ptr<StreamingDecoder> CreateAsyncStreamingDecoder(
      std::unique_ptr<StreamingProcessor> processor);
109 110 111 112 113 114 115 116 117 118 119 120

  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_;
  Vector<const uint8_t> compiled_module_bytes_;
121 122 123 124 125 126 127
};

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

#endif  // V8_WASM_STREAMING_DECODER_H_