Loading...
Searching...
No Matches
v8-wasm.h
Go to the documentation of this file.
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 <span>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <variant>
15
16#include "v8-internal.h" // NOLINT(build/include_directory)
17#include "v8-local-handle.h" // NOLINT(build/include_directory)
18#include "v8-object.h" // NOLINT(build/include_directory)
19#include "v8-platform.h" // NOLINT(build/include_directory)
20#include "v8config.h" // NOLINT(build/include_directory)
21
22namespace v8 {
23
24class ArrayBuffer;
25class ArrayBufferBase;
26class Promise;
27
28namespace internal::wasm {
29class NativeModule;
30} // namespace internal::wasm
31
36 V8_DEPRECATE_SOON("Use as_span() instead")
37 std::unique_ptr<const uint8_t[]> buffer;
38 V8_DEPRECATE_SOON("Use as_span() instead")
39 size_t size = 0;
40
42 OwnedBuffer() = default;
43 ~OwnedBuffer() = default;
44
46 "Use as_span() instead; external construction is deprecated")
47 OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
48 : buffer(std::move(buffer)), size(size) {}
49
50 OwnedBuffer(const OwnedBuffer&) = delete;
52
53 OwnedBuffer(OwnedBuffer&& other) noexcept
54 : buffer(std::move(other.buffer)), size(std::exchange(other.size, 0)) {}
55
56 OwnedBuffer& operator=(OwnedBuffer&& other) noexcept {
57 if (this != &other) {
58 buffer = std::move(other.buffer);
59 size = std::exchange(other.size, 0);
60 }
61 return *this;
62 }
63
64 constexpr bool empty() const { return size == 0; }
65
66 constexpr std::span<const uint8_t> as_span() const {
67 return {buffer.get(), size};
68 }
70};
71
77 public:
83
87 std::span<const uint8_t> GetWireBytesRef();
88
89 const std::string& source_url() const { return source_url_; }
90
91 private:
93 friend class WasmModuleObject;
94 friend class WasmStreaming;
95
96 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
97 std::string source_url);
98
99 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
100 const std::string source_url_;
101};
102
103// An instance of WebAssembly.Memory.
105 public:
107
111 V8_DEPRECATE_SOON("Use GetBuffer instead")
116 Local<ArrayBufferBase> GetBuffer();
117
118 V8_INLINE static WasmMemoryObject* Cast(Value* value) {
119#ifdef V8_ENABLE_CHECKS
120 CheckCast(value);
121#endif
122 return static_cast<WasmMemoryObject*>(value);
123 }
124
125 private:
126 static void CheckCast(Value* object);
127};
128
129// An instance of WebAssembly.Module.
131 public:
133
139 Isolate* isolate, const CompiledWasmModule&);
140
146
153 // Builtin compile-time imports, mirroring the strings accepted in the
154 // `builtins` array of the JS `WebAssembly.Module` constructor options.
155 // Combine values with bitwise-or to enable multiple builtins.
156 struct Builtins {
157 enum {
158 kNone = 0,
159 kJsString = 1 << 0, // "js-string"
160 };
161 };
162 // Bitwise-or of `Builtins` values to enable as compile-time imports.
163 int builtins = Builtins::kNone;
164 // If non-null, enable imported string constants from the named module
165 // (e.g. "wasm:js/string-constants"). The string must be null-terminated and
166 // remain valid for the duration of the compile call.
167 const char* imported_string_constants_module = nullptr;
168 // If non-empty, associated with the module's script as its source URL, for
169 // use in stack traces and developer tooling. The string must remain valid
170 // for the duration of the compile call.
171 std::string_view source_url = {};
172 };
173
178 Isolate* isolate, std::span<const uint8_t> wire_bytes);
179
185 Isolate* isolate, std::span<const uint8_t> wire_bytes,
186 const CompileOptions& options);
187
189#ifdef V8_ENABLE_CHECKS
190 CheckCast(value);
191#endif
192 return static_cast<WasmModuleObject*>(value);
193 }
194
195 private:
196 static void CheckCast(Value* obj);
197};
198
206 public:
207 static constexpr internal::ManagedTypeId kTypeID =
208 internal::ManagedTypeId::kWasmStreaming;
209 class WasmStreamingImpl;
210
212 public:
213 // Get the full wire bytes, to check against the cached version.
214 virtual std::span<const uint8_t> GetWireBytes() const = 0;
215 // Pass serialized (cached) compiled module bytes, to be deserialized and
216 // used as the result of this streaming compilation.
217 // The passed bytes will only be accessed inside this callback, i.e.
218 // lifetime can end after the call.
219 // The return value indicates whether V8 could use the passed bytes; {false}
220 // would be returned on e.g. version mismatch.
221 // This method can only be called once.
222 virtual bool SetCachedCompiledModuleBytes(std::span<const uint8_t>) = 0;
223 };
224
225 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
226
227 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
228
230
235 void OnBytesReceived(const uint8_t* bytes, size_t size);
236
246 void Finish(const ModuleCachingCallback& caching_callback);
247
254 void Abort(MaybeLocal<Value> exception);
255
265
271 std::function<void(CompiledWasmModule)>);
272
273 /*
274 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
275 * called before {Finish}.
276 */
277 void SetUrl(const char* url, size_t length);
278
284 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
285 Local<Data> data);
286
287 private:
288 std::unique_ptr<WasmStreamingImpl> impl_;
289};
290
298 public:
301
308 explicit WasmModuleCompilation(const CompileOptions& options = {});
309
311
314
320 void OnBytesReceived(const uint8_t* bytes, size_t size);
321
334 void Finish(
335 Isolate*, const ModuleCachingCallback& caching_callback,
336 const std::function<void(
337 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
338 resolution_callback);
339
344 void Abort();
345
355
361 std::function<void(CompiledWasmModule)>);
362
363 /*
364 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
365 * called before {Finish}.
366 */
367 void SetUrl(const char* url, size_t length);
368
369 private:
370 class Impl;
371 const std::unique_ptr<Impl> impl_;
372};
373
380 public:
382
384
387
388 static bool Unmap(Isolate* isolate, Local<Object> wasm_memory_map_descriptor);
389
390 static size_t Map(Isolate* isolate, Local<Object> wasm_memory_map_descriptor,
391 Local<WasmMemoryObject> memory, size_t offset);
392};
393} // namespace v8
394
395#endif // INCLUDE_V8_WASM_H_
Definition: v8-array-buffer.h:143
Definition: v8-array-buffer.h:244
Definition: v8-wasm.h:76
OwnedBuffer Serialize()
const std::string & source_url() const
Definition: v8-wasm.h:89
std::span< const uint8_t > GetWireBytesRef()
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:359
Definition: v8-local-handle.h:730
Definition: v8-object.h:267
int PlatformHandle
Definition: v8-platform.h:500
Definition: v8-value.h:32
Definition: v8-wasm.h:379
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
SharedMemoryHandle::PlatformHandle WasmFileDescriptor
Definition: v8-wasm.h:383
static bool Unmap(Isolate *isolate, Local< Object > wasm_memory_map_descriptor)
static size_t Map(Isolate *isolate, Local< Object > wasm_memory_map_descriptor, Local< WasmMemoryObject > memory, size_t offset)
Definition: v8-wasm.h:104
Definition: v8-wasm.h:297
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:299
WasmModuleCompilation(const WasmModuleCompilation &)=delete
WasmModuleCompilation(const CompileOptions &options={})
void Finish(Isolate *, const ModuleCachingCallback &caching_callback, const std::function< void(std::variant< Local< WasmModuleObject >, Local< Value > > module_or_error)> &resolution_callback)
void SetUrl(const char *url, size_t length)
WasmModuleCompilation & operator=(const WasmModuleCompilation &)=delete
Definition: v8-wasm.h:130
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, std::span< const uint8_t > wire_bytes, const CompileOptions &options)
static MaybeLocal< WasmModuleObject > FromCompiledModule(Isolate *isolate, const CompiledWasmModule &)
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, std::span< const uint8_t > wire_bytes)
CompiledWasmModule GetCompiledModule()
static WasmModuleObject * Cast(Value *value)
Definition: v8-wasm.h:188
virtual std::span< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(std::span< const uint8_t >)=0
Definition: v8-wasm.h:205
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:225
WasmStreaming(std::unique_ptr< WasmStreamingImpl > impl)
void Finish(const ModuleCachingCallback &caching_callback)
static std::shared_ptr< WasmStreaming > Unpack(Isolate *isolate, Local< Data > data)
void SetUrl(const char *url, size_t length)
void SetHasCompiledModuleBytes()
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
void Abort(MaybeLocal< Value > exception)
void OnBytesReceived(const uint8_t *bytes, size_t size)
ManagedTypeId
Definition: v8-internal.h:597
Definition: libplatform.h:15
Definition: v8-wasm.h:35
constexpr bool empty() const
Definition: v8-wasm.h:64
size_t size
Definition: v8-wasm.h:39
OwnedBuffer & operator=(OwnedBuffer &&other) noexcept
Definition: v8-wasm.h:56
std::unique_ptr< const uint8_t[]> buffer
Definition: v8-wasm.h:37
OwnedBuffer(const OwnedBuffer &)=delete
OwnedBuffer(OwnedBuffer &&other) noexcept
Definition: v8-wasm.h:53
OwnedBuffer & operator=(const OwnedBuffer &)=delete
constexpr std::span< const uint8_t > as_span() const
Definition: v8-wasm.h:66
Definition: v8-wasm.h:152
#define V8_EXPORT
Definition: v8config.h:874
#define V8_INLINE
Definition: v8config.h:518
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:631
#define END_ALLOW_USE_DEPRECATED()
Definition: v8config.h:651
#define START_ALLOW_USE_DEPRECATED()
Definition: v8config.h:650