Coverage Report

Created: 2025-07-04 09:33

/src/node/deps/v8/include/v8-wasm.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2021 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 INCLUDE_V8_WASM_H_
6
#define INCLUDE_V8_WASM_H_
7
8
#include <functional>
9
#include <memory>
10
#include <string>
11
12
#include "v8-local-handle.h"  // NOLINT(build/include_directory)
13
#include "v8-memory-span.h"   // NOLINT(build/include_directory)
14
#include "v8-object.h"        // NOLINT(build/include_directory)
15
#include "v8config.h"         // NOLINT(build/include_directory)
16
17
namespace v8 {
18
19
class ArrayBuffer;
20
class Promise;
21
22
namespace internal {
23
namespace wasm {
24
class NativeModule;
25
class StreamingDecoder;
26
}  // namespace wasm
27
}  // namespace internal
28
29
/**
30
 * An owned byte buffer with associated size.
31
 */
32
struct OwnedBuffer {
33
  std::unique_ptr<const uint8_t[]> buffer;
34
  size_t size = 0;
35
  OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
36
0
      : buffer(std::move(buffer)), size(size) {}
37
  OwnedBuffer() = default;
38
};
39
40
// Wrapper around a compiled WebAssembly module, which is potentially shared by
41
// different WasmModuleObjects.
42
class V8_EXPORT CompiledWasmModule {
43
 public:
44
  /**
45
   * Serialize the compiled module. The serialized data does not include the
46
   * wire bytes.
47
   */
48
  OwnedBuffer Serialize();
49
50
  /**
51
   * Get the (wasm-encoded) wire bytes that were used to compile this module.
52
   */
53
  MemorySpan<const uint8_t> GetWireBytesRef();
54
55
0
  const std::string& source_url() const { return source_url_; }
56
57
 private:
58
  friend class WasmModuleObject;
59
  friend class WasmStreaming;
60
61
  explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
62
                              const char* source_url, size_t url_length);
63
64
  const std::shared_ptr<internal::wasm::NativeModule> native_module_;
65
  const std::string source_url_;
66
};
67
68
// An instance of WebAssembly.Memory.
69
class V8_EXPORT WasmMemoryObject : public Object {
70
 public:
71
  WasmMemoryObject() = delete;
72
73
  /**
74
   * Returns underlying ArrayBuffer.
75
   */
76
  Local<ArrayBuffer> Buffer();
77
78
0
  V8_INLINE static WasmMemoryObject* Cast(Value* value) {
79
0
#ifdef V8_ENABLE_CHECKS
80
0
    CheckCast(value);
81
0
#endif
82
0
    return static_cast<WasmMemoryObject*>(value);
83
0
  }
84
85
 private:
86
  static void CheckCast(Value* object);
87
};
88
89
// An instance of WebAssembly.Module.
90
class V8_EXPORT WasmModuleObject : public Object {
91
 public:
92
  WasmModuleObject() = delete;
93
94
  /**
95
   * Efficiently re-create a WasmModuleObject, without recompiling, from
96
   * a CompiledWasmModule.
97
   */
98
  static MaybeLocal<WasmModuleObject> FromCompiledModule(
99
      Isolate* isolate, const CompiledWasmModule&);
100
101
  /**
102
   * Get the compiled module for this module object. The compiled module can be
103
   * shared by several module objects.
104
   */
105
  CompiledWasmModule GetCompiledModule();
106
107
  /**
108
   * Compile a Wasm module from the provided uncompiled bytes.
109
   */
110
  static MaybeLocal<WasmModuleObject> Compile(
111
      Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
112
113
0
  V8_INLINE static WasmModuleObject* Cast(Value* value) {
114
0
#ifdef V8_ENABLE_CHECKS
115
0
    CheckCast(value);
116
0
#endif
117
0
    return static_cast<WasmModuleObject*>(value);
118
0
  }
119
120
 private:
121
  static void CheckCast(Value* obj);
122
};
123
124
/**
125
 * The V8 interface for WebAssembly streaming compilation. When streaming
126
 * compilation is initiated, V8 passes a {WasmStreaming} object to the embedder
127
 * such that the embedder can pass the input bytes for streaming compilation to
128
 * V8.
129
 */
130
class V8_EXPORT WasmStreaming final {
131
 public:
132
  class WasmStreamingImpl;
133
134
  explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
135
136
  ~WasmStreaming();
137
138
  /**
139
   * Pass a new chunk of bytes to WebAssembly streaming compilation.
140
   * The buffer passed into {OnBytesReceived} is owned by the caller.
141
   */
142
  void OnBytesReceived(const uint8_t* bytes, size_t size);
143
144
  /**
145
   * {Finish} should be called after all received bytes where passed to
146
   * {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
147
   * must not be called after {Abort} has been called already.
148
   * If {can_use_compiled_module} is true and {SetCompiledModuleBytes} was
149
   * previously called, the compiled module bytes can be used.
150
   * If {can_use_compiled_module} is false, the compiled module bytes previously
151
   * set by {SetCompiledModuleBytes} should not be used.
152
   */
153
  void Finish(bool can_use_compiled_module = true);
154
155
  /**
156
   * Abort streaming compilation. If {exception} has a value, then the promise
157
   * associated with streaming compilation is rejected with that value. If
158
   * {exception} does not have value, the promise does not get rejected.
159
   * {Abort} must not be called repeatedly, or after {Finish}.
160
   */
161
  void Abort(MaybeLocal<Value> exception);
162
163
  /**
164
   * Passes previously compiled module bytes. This must be called before
165
   * {OnBytesReceived}, {Finish}, or {Abort}. Returns true if the module bytes
166
   * can be used, false otherwise. The buffer passed via {bytes} and {size}
167
   * is owned by the caller. If {SetCompiledModuleBytes} returns true, the
168
   * buffer must remain valid until either {Finish} or {Abort} completes.
169
   * The compiled module bytes should not be used until {Finish(true)} is
170
   * called, because they can be invalidated later by {Finish(false)}.
171
   */
172
  bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size);
173
174
  /**
175
   * Sets a callback which is called whenever a significant number of new
176
   * functions are ready for serialization.
177
   */
178
  void SetMoreFunctionsCanBeSerializedCallback(
179
      std::function<void(CompiledWasmModule)>);
180
181
  /*
182
   * Sets the UTF-8 encoded source URL for the {Script} object. This must be
183
   * called before {Finish}.
184
   */
185
  void SetUrl(const char* url, size_t length);
186
187
  /**
188
   * Unpacks a {WasmStreaming} object wrapped in a  {Managed} for the embedder.
189
   * Since the embedder is on the other side of the API, it cannot unpack the
190
   * {Managed} itself.
191
   */
192
  static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
193
                                               Local<Value> value);
194
195
 private:
196
  std::unique_ptr<WasmStreamingImpl> impl_;
197
};
198
199
}  // namespace v8
200
201
#endif  // INCLUDE_V8_WASM_H_