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
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
18namespace v8 {
19
20class ArrayBuffer;
21class Promise;
22
23namespace internal {
24namespace wasm {
25class NativeModule;
26class StreamingDecoder;
27} // namespace wasm
28} // namespace internal
29
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 : 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.
44 public:
50
55
56 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.
71 public:
72 WasmMemoryObject() = delete;
73
78
80#ifdef V8_ENABLE_CHECKS
81 CheckCast(value);
82#endif
83 return static_cast<WasmMemoryObject*>(value);
84 }
85
86 private:
87 static void CheckCast(Value* object);
88};
89
90// An instance of WebAssembly.Module.
92 public:
93 WasmModuleObject() = delete;
94
100 Isolate* isolate, const CompiledWasmModule&);
101
107
112 Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
113
115#ifdef V8_ENABLE_CHECKS
116 CheckCast(value);
117#endif
118 return static_cast<WasmModuleObject*>(value);
119 }
120
121 private:
122 static void CheckCast(Value* obj);
123};
124
132 public:
133 static constexpr internal::ExternalPointerTag kManagedTag =
134 internal::kWasmWasmStreamingTag;
135 class WasmStreamingImpl;
136
138 public:
139 // Get the full wire bytes, to check against the cached version.
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.
149 };
150
151 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
152
153 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
154
156
161 void OnBytesReceived(const uint8_t* bytes, size_t size);
162
173 "Use the new variant of Finish which takes the caching callback argument")
174 void Finish(bool can_use_compiled_module = true) {
175 ModuleCachingCallback callback;
176 if (can_use_compiled_module && !cached_compiled_module_bytes_.empty()) {
177 callback = [bytes = cached_compiled_module_bytes_](
178 ModuleCachingInterface& caching_interface) {
179 caching_interface.SetCachedCompiledModuleBytes(bytes);
180 };
181 }
182 Finish(callback);
183 }
184
193 void Finish(const ModuleCachingCallback& caching_callback);
194
201 void Abort(MaybeLocal<Value> exception);
202
213 "Use SetHasCompiledModule in combination with the new variant of Finish")
214 bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size) {
215 SetHasCompiledModuleBytes();
216 cached_compiled_module_bytes_ = {bytes, size};
217 // Optimistically return true here, even though we might later find out that
218 // we cannot use the bytes.
219 return true;
220 }
221
231
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
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
266 public:
268
270#ifdef V8_ENABLE_CHECKS
271 CheckCast(value);
272#endif
273 return static_cast<WasmMemoryMapDescriptor*>(value);
274 }
275
276 using WasmFileDescriptor = int32_t;
277
280
281 private:
282 static void CheckCast(Value* object);
283};
284} // namespace v8
285
286#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:234
Definition: v8-value.h:32
Definition: v8-wasm.h:265
int32_t WasmFileDescriptor
Definition: v8-wasm.h:276
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
static WasmMemoryMapDescriptor * Cast(Value *value)
Definition: v8-wasm.h:269
Definition: v8-wasm.h:70
static WasmMemoryObject * Cast(Value *value)
Definition: v8-wasm.h:79
Local< ArrayBuffer > Buffer()
Definition: v8-wasm.h:91
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:114
virtual MemorySpan< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(MemorySpan< const uint8_t >)=0
Definition: v8-wasm.h:131
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:151
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:33
OwnedBuffer()=default
size_t size
Definition: v8-wasm.h:35
std::unique_ptr< const uint8_t[]> buffer
Definition: v8-wasm.h:34
OwnedBuffer(std::unique_ptr< const uint8_t[]> buffer, size_t size)
Definition: v8-wasm.h:36
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:622