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
173 void Finish(const ModuleCachingCallback& caching_callback);
174
181 void Abort(MaybeLocal<Value> exception);
182
192
198 std::function<void(CompiledWasmModule)>);
199
200 /*
201 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
202 * called before {Finish}.
203 */
204 void SetUrl(const char* url, size_t length);
205
211 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
212 Local<Value> value);
213
214 private:
215 std::unique_ptr<WasmStreamingImpl> impl_;
216};
217
225 public:
227
234
236
239
245 void OnBytesReceived(const uint8_t* bytes, size_t size);
246
259 void Finish(
260 Isolate*, const ModuleCachingCallback& caching_callback,
261 const std::function<void(
262 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
263 resolution_callback);
264
269 void Abort();
270
280
286 std::function<void(CompiledWasmModule)>);
287
288 /*
289 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
290 * called before {Finish}.
291 */
292 void SetUrl(const char* url, size_t length);
293
294 private:
295 class Impl;
296 const std::unique_ptr<Impl> impl_;
297};
298
305 public:
307
309#ifdef V8_ENABLE_CHECKS
310 CheckCast(value);
311#endif
312 return static_cast<WasmMemoryMapDescriptor*>(value);
313 }
314
315 using WasmFileDescriptor = int32_t;
316
319
320 private:
321 static void CheckCast(Value* object);
322};
323} // namespace v8
324
325#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:304
int32_t WasmFileDescriptor
Definition: v8-wasm.h:315
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
static WasmMemoryMapDescriptor * Cast(Value *value)
Definition: v8-wasm.h:308
Definition: v8-wasm.h:71
static WasmMemoryObject * Cast(Value *value)
Definition: v8-wasm.h:80
Local< ArrayBuffer > Buffer()
Definition: v8-wasm.h:224
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:226
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:589
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