Coverage Report

Created: 2025-12-30 08:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/node/deps/v8/include/v8-wasm.h
Line
Count
Source
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-internal.h"      // NOLINT(build/include_directory)
13
#include "v8-local-handle.h"  // NOLINT(build/include_directory)
14
#include "v8-memory-span.h"   // NOLINT(build/include_directory)
15
#include "v8-object.h"        // NOLINT(build/include_directory)
16
#include "v8config.h"         // NOLINT(build/include_directory)
17
18
namespace v8 {
19
20
class ArrayBuffer;
21
class Promise;
22
23
namespace internal {
24
namespace wasm {
25
class NativeModule;
26
class StreamingDecoder;
27
}  // namespace wasm
28
}  // namespace internal
29
30
/**
31
 * An owned byte buffer with associated size.
32
 */
33
struct OwnedBuffer {
34
  std::unique_ptr<const uint8_t[]> buffer;
35
  size_t size = 0;
36
  OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
37
0
      : buffer(std::move(buffer)), size(size) {}
38
  OwnedBuffer() = default;
39
};
40
41
// Wrapper around a compiled WebAssembly module, which is potentially shared by
42
// different WasmModuleObjects.
43
class V8_EXPORT CompiledWasmModule {
44
 public:
45
  /**
46
   * Serialize the compiled module. The serialized data does not include the
47
   * wire bytes.
48
   */
49
  OwnedBuffer Serialize();
50
51
  /**
52
   * Get the (wasm-encoded) wire bytes that were used to compile this module.
53
   */
54
  MemorySpan<const uint8_t> GetWireBytesRef();
55
56
0
  const std::string& source_url() const { return source_url_; }
57
58
 private:
59
  friend class WasmModuleObject;
60
  friend class WasmStreaming;
61
62
  explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
63
                              const char* source_url, size_t url_length);
64
65
  const std::shared_ptr<internal::wasm::NativeModule> native_module_;
66
  const std::string source_url_;
67
};
68
69
// An instance of WebAssembly.Memory.
70
class V8_EXPORT WasmMemoryObject : public Object {
71
 public:
72
  WasmMemoryObject() = delete;
73
74
  /**
75
   * Returns underlying ArrayBuffer.
76
   */
77
  Local<ArrayBuffer> Buffer();
78
79
0
  V8_INLINE static WasmMemoryObject* Cast(Value* value) {
80
0
#ifdef V8_ENABLE_CHECKS
81
0
    CheckCast(value);
82
0
#endif
83
0
    return static_cast<WasmMemoryObject*>(value);
84
0
  }
85
86
 private:
87
  static void CheckCast(Value* object);
88
};
89
90
// An instance of WebAssembly.Module.
91
class V8_EXPORT WasmModuleObject : public Object {
92
 public:
93
  WasmModuleObject() = delete;
94
95
  /**
96
   * Efficiently re-create a WasmModuleObject, without recompiling, from
97
   * a CompiledWasmModule.
98
   */
99
  static MaybeLocal<WasmModuleObject> FromCompiledModule(
100
      Isolate* isolate, const CompiledWasmModule&);
101
102
  /**
103
   * Get the compiled module for this module object. The compiled module can be
104
   * shared by several module objects.
105
   */
106
  CompiledWasmModule GetCompiledModule();
107
108
  /**
109
   * Compile a Wasm module from the provided uncompiled bytes.
110
   */
111
  static MaybeLocal<WasmModuleObject> Compile(
112
      Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
113
114
0
  V8_INLINE static WasmModuleObject* Cast(Value* value) {
115
0
#ifdef V8_ENABLE_CHECKS
116
0
    CheckCast(value);
117
0
#endif
118
0
    return static_cast<WasmModuleObject*>(value);
119
0
  }
120
121
 private:
122
  static void CheckCast(Value* obj);
123
};
124
125
/**
126
 * The V8 interface for WebAssembly streaming compilation. When streaming
127
 * compilation is initiated, V8 passes a {WasmStreaming} object to the embedder
128
 * such that the embedder can pass the input bytes for streaming compilation to
129
 * V8.
130
 */
131
class V8_EXPORT WasmStreaming final {
132
 public:
133
  static constexpr internal::ExternalPointerTag kManagedTag =
134
      internal::kWasmWasmStreamingTag;
135
  class WasmStreamingImpl;
136
137
  class ModuleCachingInterface {
138
   public:
139
    // Get the full wire bytes, to check against the cached version.
140
    virtual MemorySpan<const uint8_t> GetWireBytes() const = 0;
141
    // Pass serialized (cached) compiled module bytes, to be deserialized and
142
    // used as the result of this streaming compilation.
143
    // The passed bytes will only be accessed inside this callback, i.e.
144
    // lifetime can end after the call.
145
    // The return value indicates whether V8 could use the passed bytes; {false}
146
    // would be returned on e.g. version mismatch.
147
    // This method can only be called once.
148
    virtual bool SetCachedCompiledModuleBytes(MemorySpan<const uint8_t>) = 0;
149
  };
150
151
  using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
152
153
  explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
154
155
  ~WasmStreaming();
156
157
  /**
158
   * Pass a new chunk of bytes to WebAssembly streaming compilation.
159
   * The buffer passed into {OnBytesReceived} is owned by the caller.
160
   */
161
  void OnBytesReceived(const uint8_t* bytes, size_t size);
162
163
  /**
164
   * {Finish} should be called after all received bytes where passed to
165
   * {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
166
   * must not be called after {Abort} has been called already.
167
   * If {can_use_compiled_module} is true and {SetCompiledModuleBytes} was
168
   * previously called, the compiled module bytes can be used.
169
   * If {can_use_compiled_module} is false, the compiled module bytes previously
170
   * set by {SetCompiledModuleBytes} should not be used.
171
   */
172
  V8_DEPRECATE_SOON(
173
      "Use the new variant of Finish which takes the caching callback argument")
174
0
  void Finish(bool can_use_compiled_module = true) {
175
0
    ModuleCachingCallback callback;
176
0
    if (can_use_compiled_module && !cached_compiled_module_bytes_.empty()) {
177
0
      callback = [bytes = cached_compiled_module_bytes_](
178
0
                     ModuleCachingInterface& caching_interface) {
179
0
        caching_interface.SetCachedCompiledModuleBytes(bytes);
180
0
      };
181
0
    }
182
0
    Finish(callback);
183
0
  }
184
185
  /**
186
   * {Finish} should be called after all received bytes where passed to
187
   * {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
188
   * must not be called after {Abort} has been called already.
189
   * If {SetHasCompiledModuleBytes()} was called before, a {caching_callback}
190
   * can be passed which can inspect the full received wire bytes and set cached
191
   * module bytes which will be deserialized then.
192
   */
193
  void Finish(const ModuleCachingCallback& caching_callback);
194
195
  /**
196
   * Abort streaming compilation. If {exception} has a value, then the promise
197
   * associated with streaming compilation is rejected with that value. If
198
   * {exception} does not have value, the promise does not get rejected.
199
   * {Abort} must not be called repeatedly, or after {Finish}.
200
   */
201
  void Abort(MaybeLocal<Value> exception);
202
203
  /**
204
   * Passes previously compiled module bytes. This must be called before
205
   * {OnBytesReceived}, {Finish}, or {Abort}. Returns true if the module bytes
206
   * can be used, false otherwise. The buffer passed via {bytes} and {size}
207
   * is owned by the caller. If {SetCompiledModuleBytes} returns true, the
208
   * buffer must remain valid until either {Finish} or {Abort} completes.
209
   * The compiled module bytes should not be used until {Finish(true)} is
210
   * called, because they can be invalidated later by {Finish(false)}.
211
   */
212
  V8_DEPRECATE_SOON(
213
      "Use SetHasCompiledModule in combination with the new variant of Finish")
214
0
  bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) {
215
0
    SetHasCompiledModuleBytes();
216
0
    cached_compiled_module_bytes_ = {bytes, size};
217
0
    // Optimistically return true here, even though we might later find out that
218
0
    // we cannot use the bytes.
219
0
    return true;
220
0
  }
221
222
  /**
223
   * Mark that the embedder has (potentially) cached compiled module bytes (i.e.
224
   * a serialized {CompiledWasmModule}) that could match this streaming request.
225
   * This will cause V8 to skip streaming compilation.
226
   * The embedder should then pass a callback to the {Finish} method to pass the
227
   * serialized bytes, after potentially checking their validity against the
228
   * full received wire bytes.
229
   */
230
  void SetHasCompiledModuleBytes();
231
232
  /**
233
   * Sets a callback which is called whenever a significant number of new
234
   * functions are ready for serialization.
235
   */
236
  void SetMoreFunctionsCanBeSerializedCallback(
237
      std::function<void(CompiledWasmModule)>);
238
239
  /*
240
   * Sets the UTF-8 encoded source URL for the {Script} object. This must be
241
   * called before {Finish}.
242
   */
243
  void SetUrl(const char* url, size_t length);
244
245
  /**
246
   * Unpacks a {WasmStreaming} object wrapped in a  {Managed} for the embedder.
247
   * Since the embedder is on the other side of the API, it cannot unpack the
248
   * {Managed} itself.
249
   */
250
  static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
251
                                               Local<Value> value);
252
253
 private:
254
  std::unique_ptr<WasmStreamingImpl> impl_;
255
  // Temporarily store the compiled module bytes here until the deprecation (see
256
  // methods above) has gone through.
257
  MemorySpan<const uint8_t> cached_compiled_module_bytes_;
258
};
259
260
/**
261
 * The V8 interface for a WebAssembly memory map descriptor. This is an
262
 * experimental feature that may change and be removed without further
263
 * communication.
264
 */
265
class V8_EXPORT WasmMemoryMapDescriptor : public Object {
266
 public:
267
  WasmMemoryMapDescriptor() = delete;
268
269
0
  V8_INLINE static WasmMemoryMapDescriptor* Cast(Value* value) {
270
0
#ifdef V8_ENABLE_CHECKS
271
0
    CheckCast(value);
272
0
#endif
273
0
    return static_cast<WasmMemoryMapDescriptor*>(value);
274
0
  }
275
276
  using WasmFileDescriptor = int32_t;
277
278
  static Local<WasmMemoryMapDescriptor> New(Isolate* isolate,
279
                                            WasmFileDescriptor fd);
280
281
 private:
282
  static void CheckCast(Value* object);
283
};
284
}  // namespace v8
285
286
#endif  // INCLUDE_V8_WASM_H_