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 <string>
11#include <variant>
12
13#include "v8-internal.h" // NOLINT(build/include_directory)
14#include "v8-local-handle.h" // NOLINT(build/include_directory)
15#include "v8-memory-span.h" // NOLINT(build/include_directory)
16#include "v8-object.h" // NOLINT(build/include_directory)
17#include "v8config.h" // NOLINT(build/include_directory)
18
19namespace v8 {
20
21class ArrayBuffer;
22class Promise;
23
24namespace internal::wasm {
25class NativeModule;
26} // namespace internal::wasm
27
32 std::unique_ptr<const uint8_t[]> buffer;
33 size_t size = 0;
34 OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
35 : buffer(std::move(buffer)), size(size) {}
36 OwnedBuffer() = default;
37};
38
44 public:
50
55
56 const std::string& source_url() const { return source_url_; }
57
58 private:
60 friend class WasmModuleObject;
61 friend class WasmStreaming;
62
63 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
64 std::string source_url);
65
66 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
67 const std::string source_url_;
68};
69
70// An instance of WebAssembly.Memory.
72 public:
73 WasmMemoryObject() = delete;
74
79
81#ifdef V8_ENABLE_CHECKS
82 CheckCast(value);
83#endif
84 return static_cast<WasmMemoryObject*>(value);
85 }
86
87 private:
88 static void CheckCast(Value* object);
89};
90
91// An instance of WebAssembly.Module.
93 public:
94 WasmModuleObject() = delete;
95
101 Isolate* isolate, const CompiledWasmModule&);
102
108
113 Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
114
116#ifdef V8_ENABLE_CHECKS
117 CheckCast(value);
118#endif
119 return static_cast<WasmModuleObject*>(value);
120 }
121
122 private:
123 static void CheckCast(Value* obj);
124};
125
133 public:
134 static constexpr internal::ExternalPointerTag kManagedTag =
135 internal::kWasmWasmStreamingTag;
136 class WasmStreamingImpl;
137
139 public:
140 // Get the full wire bytes, to check against the cached version.
142 // Pass serialized (cached) compiled module bytes, to be deserialized and
143 // used as the result of this streaming compilation.
144 // The passed bytes will only be accessed inside this callback, i.e.
145 // lifetime can end after the call.
146 // The return value indicates whether V8 could use the passed bytes; {false}
147 // would be returned on e.g. version mismatch.
148 // This method can only be called once.
150 };
151
152 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
153
154 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
155
157
162 void OnBytesReceived(const uint8_t* bytes, size_t size);
163
174 "Use the new variant of Finish which takes the caching callback argument")
175 void Finish(bool can_use_compiled_module = true) {
176 ModuleCachingCallback callback;
177 if (can_use_compiled_module && !cached_compiled_module_bytes_.empty()) {
178 callback = [bytes = cached_compiled_module_bytes_](
179 ModuleCachingInterface& caching_interface) {
180 caching_interface.SetCachedCompiledModuleBytes(bytes);
181 };
182 }
183 Finish(callback);
184 }
185
195 void Finish(const ModuleCachingCallback& caching_callback);
196
203 void Abort(MaybeLocal<Value> exception);
204
215 "Use SetHasCompiledModule in combination with the new variant of Finish")
216 bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) {
217 SetHasCompiledModuleBytes();
218 cached_compiled_module_bytes_ = {bytes, size};
219 // Optimistically return true here, even though we might later find out that
220 // we cannot use the bytes.
221 return true;
222 }
223
233
239 std::function<void(CompiledWasmModule)>);
240
241 /*
242 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
243 * called before {Finish}.
244 */
245 void SetUrl(const char* url, size_t length);
246
252 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
253 Local<Value> value);
254
255 private:
256 std::unique_ptr<WasmStreamingImpl> impl_;
257 // Temporarily store the compiled module bytes here until the deprecation (see
258 // methods above) has gone through.
259 MemorySpan<const uint8_t> cached_compiled_module_bytes_;
260};
261
269 public:
271
278
280
283
289 void OnBytesReceived(const uint8_t* bytes, size_t size);
290
303 void Finish(
304 Isolate*, const ModuleCachingCallback& caching_callback,
305 const std::function<void(
306 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
307 resolution_callback);
308
313 void Abort();
314
324
330 std::function<void(CompiledWasmModule)>);
331
332 /*
333 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
334 * called before {Finish}.
335 */
336 void SetUrl(const char* url, size_t length);
337
338 private:
339 class Impl;
340 const std::unique_ptr<Impl> impl_;
341};
342
349 public:
351
353#ifdef V8_ENABLE_CHECKS
354 CheckCast(value);
355#endif
356 return static_cast<WasmMemoryMapDescriptor*>(value);
357 }
358
359 using WasmFileDescriptor = int32_t;
360
363
364 private:
365 static void CheckCast(Value* object);
366};
367} // namespace v8
368
369#endif // INCLUDE_V8_WASM_H_
Definition: v8-wasm.h:43
OwnedBuffer Serialize()
MemorySpan< const uint8_t > GetWireBytesRef()
const std::string & source_url() const
Definition: v8-wasm.h:56
Definition: v8-isolate.h:291
Definition: v8-local-handle.h:366
Definition: v8-local-handle.h:734
Definition: v8-memory-span.h:48
Definition: v8-object.h:262
Definition: v8-value.h:32
Definition: v8-wasm.h:348
int32_t WasmFileDescriptor
Definition: v8-wasm.h:359
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
static WasmMemoryMapDescriptor * Cast(Value *value)
Definition: v8-wasm.h:352
Definition: v8-wasm.h:71
static WasmMemoryObject * Cast(Value *value)
Definition: v8-wasm.h:80
Local< ArrayBuffer > Buffer()
Definition: v8-wasm.h:268
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:270
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:92
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, MemorySpan< const uint8_t > wire_bytes)
static MaybeLocal< WasmModuleObject > FromCompiledModule(Isolate *isolate, const CompiledWasmModule &)
CompiledWasmModule GetCompiledModule()
static WasmModuleObject * Cast(Value *value)
Definition: v8-wasm.h:115
virtual MemorySpan< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(MemorySpan< const uint8_t >)=0
Definition: v8-wasm.h:132
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:152
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:548
Definition: libplatform.h:15
Definition: v8-wasm.h:31
OwnedBuffer()=default
size_t size
Definition: v8-wasm.h:33
std::unique_ptr< const uint8_t[]> buffer
Definition: v8-wasm.h:32
OwnedBuffer(std::unique_ptr< const uint8_t[]> buffer, size_t size)
Definition: v8-wasm.h:34
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:622