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 <variant>
13
14#include "v8-internal.h" // NOLINT(build/include_directory)
15#include "v8-local-handle.h" // NOLINT(build/include_directory)
16#include "v8-object.h" // NOLINT(build/include_directory)
17#include "v8-platform.h" // NOLINT(build/include_directory)
18#include "v8config.h" // NOLINT(build/include_directory)
19
20namespace v8 {
21
22class ArrayBuffer;
23class Promise;
24
25namespace internal::wasm {
26class NativeModule;
27} // namespace internal::wasm
28
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 : buffer(std::move(buffer)), size(size) {}
37 OwnedBuffer() = default;
38};
39
45 public:
51
55 std::span<const uint8_t> GetWireBytesRef();
56
57 const std::string& source_url() const { return source_url_; }
58
59 private:
61 friend class WasmModuleObject;
62 friend class WasmStreaming;
63
64 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
65 std::string source_url);
66
67 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
68 const std::string source_url_;
69};
70
71// An instance of WebAssembly.Memory.
73 public:
74 WasmMemoryObject() = delete;
75
80
82#ifdef V8_ENABLE_CHECKS
83 CheckCast(value);
84#endif
85 return static_cast<WasmMemoryObject*>(value);
86 }
87
88 private:
89 static void CheckCast(Value* object);
90};
91
92// An instance of WebAssembly.Module.
94 public:
95 WasmModuleObject() = delete;
96
102 Isolate* isolate, const CompiledWasmModule&);
103
109
114 Isolate* isolate, std::span<const uint8_t> wire_bytes);
115
117#ifdef V8_ENABLE_CHECKS
118 CheckCast(value);
119#endif
120 return static_cast<WasmModuleObject*>(value);
121 }
122
123 private:
124 static void CheckCast(Value* obj);
125};
126
134 public:
135 static constexpr internal::ExternalPointerTag kManagedTag =
136 internal::kWasmWasmStreamingTag;
137 class WasmStreamingImpl;
138
140 public:
141 // Get the full wire bytes, to check against the cached version.
142 virtual std::span<const uint8_t> GetWireBytes() const = 0;
143 // Pass serialized (cached) compiled module bytes, to be deserialized and
144 // used as the result of this streaming compilation.
145 // The passed bytes will only be accessed inside this callback, i.e.
146 // lifetime can end after the call.
147 // The return value indicates whether V8 could use the passed bytes; {false}
148 // would be returned on e.g. version mismatch.
149 // This method can only be called once.
150 virtual bool SetCachedCompiledModuleBytes(std::span<const uint8_t>) = 0;
151 };
152
153 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
154
155 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
156
158
163 void OnBytesReceived(const uint8_t* bytes, size_t size);
164
174 void Finish(const ModuleCachingCallback& caching_callback);
175
182 void Abort(MaybeLocal<Value> exception);
183
193
199 std::function<void(CompiledWasmModule)>);
200
201 /*
202 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
203 * called before {Finish}.
204 */
205 void SetUrl(const char* url, size_t length);
206
212 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
213 Local<Value> value);
214
215 private:
216 std::unique_ptr<WasmStreamingImpl> impl_;
217};
218
226 public:
228
235
237
240
246 void OnBytesReceived(const uint8_t* bytes, size_t size);
247
260 void Finish(
261 Isolate*, const ModuleCachingCallback& caching_callback,
262 const std::function<void(
263 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
264 resolution_callback);
265
270 void Abort();
271
281
287 std::function<void(CompiledWasmModule)>);
288
289 /*
290 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
291 * called before {Finish}.
292 */
293 void SetUrl(const char* url, size_t length);
294
295 private:
296 class Impl;
297 const std::unique_ptr<Impl> impl_;
298};
299
306 public:
308
310
313
314 static bool Unmap(Isolate* isolate, Local<Object> wasm_memory_map_descriptor);
315
316 static size_t Map(Isolate* isolate, Local<Object> wasm_memory_map_descriptor,
317 Local<WasmMemoryObject> memory, size_t offset);
318};
319} // namespace v8
320
321#endif // INCLUDE_V8_WASM_H_
Definition: v8-wasm.h:44
OwnedBuffer Serialize()
const std::string & source_url() const
Definition: v8-wasm.h:57
std::span< const uint8_t > GetWireBytesRef()
Definition: v8-isolate.h:291
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-object.h:266
int PlatformHandle
Definition: v8-platform.h:491
Definition: v8-value.h:32
Definition: v8-wasm.h:305
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
SharedMemoryHandle::PlatformHandle WasmFileDescriptor
Definition: v8-wasm.h:309
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:72
static WasmMemoryObject * Cast(Value *value)
Definition: v8-wasm.h:81
Local< ArrayBuffer > Buffer()
Definition: v8-wasm.h:225
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:227
WasmModuleCompilation(const WasmModuleCompilation &)=delete
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:93
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:116
virtual std::span< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(std::span< const uint8_t >)=0
Definition: v8-wasm.h:133
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:153
WasmStreaming(std::unique_ptr< WasmStreamingImpl > impl)
void Finish(const ModuleCachingCallback &caching_callback)
static std::shared_ptr< WasmStreaming > Unpack(Isolate *isolate, Local< Value > value)
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)
ExternalPointerTag
Definition: v8-internal.h:662
Definition: libplatform.h:15
Definition: v8-wasm.h:32
OwnedBuffer()=default
size_t size
Definition: v8-wasm.h:34
std::unique_ptr< const uint8_t[]> buffer
Definition: v8-wasm.h:33
OwnedBuffer(std::unique_ptr< const uint8_t[]> buffer, size_t size)
Definition: v8-wasm.h:35
#define V8_EXPORT
Definition: v8config.h:867
#define V8_INLINE
Definition: v8config.h:511