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
116 // Builtin compile-time imports, mirroring the strings accepted in the
117 // `builtins` array of the JS `WebAssembly.Module` constructor options.
118 // Combine values with bitwise-or to enable multiple builtins.
119 struct Builtins {
120 enum {
121 kNone = 0,
122 kJsString = 1 << 0, // "js-string"
123 };
124 };
125 // Bitwise-or of `Builtins` values to enable as compile-time imports.
126 int builtins = Builtins::kNone;
127 // If non-null, enable imported string constants from the named module
128 // (e.g. "wasm:js/string-constants"). The string must be null-terminated and
129 // remain valid for the duration of this call.
130 const char* imported_string_constants_module = nullptr;
131 };
132
137 Isolate* isolate, std::span<const uint8_t> wire_bytes);
138
144 Isolate* isolate, std::span<const uint8_t> wire_bytes,
145 const CompileTimeImports& compile_imports);
146
148#ifdef V8_ENABLE_CHECKS
149 CheckCast(value);
150#endif
151 return static_cast<WasmModuleObject*>(value);
152 }
153
154 private:
155 static void CheckCast(Value* obj);
156};
157
165 public:
166 static constexpr internal::ExternalPointerTag kManagedTag =
167 internal::kWasmWasmStreamingTag;
168 class WasmStreamingImpl;
169
171 public:
172 // Get the full wire bytes, to check against the cached version.
173 virtual std::span<const uint8_t> GetWireBytes() const = 0;
174 // Pass serialized (cached) compiled module bytes, to be deserialized and
175 // used as the result of this streaming compilation.
176 // The passed bytes will only be accessed inside this callback, i.e.
177 // lifetime can end after the call.
178 // The return value indicates whether V8 could use the passed bytes; {false}
179 // would be returned on e.g. version mismatch.
180 // This method can only be called once.
181 virtual bool SetCachedCompiledModuleBytes(std::span<const uint8_t>) = 0;
182 };
183
184 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
185
186 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
187
189
194 void OnBytesReceived(const uint8_t* bytes, size_t size);
195
205 void Finish(const ModuleCachingCallback& caching_callback);
206
213 void Abort(MaybeLocal<Value> exception);
214
224
230 std::function<void(CompiledWasmModule)>);
231
232 /*
233 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
234 * called before {Finish}.
235 */
236 void SetUrl(const char* url, size_t length);
237
243 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
244 Local<Value> value);
245
246 private:
247 std::unique_ptr<WasmStreamingImpl> impl_;
248};
249
257 public:
259
266
268
271
277 void OnBytesReceived(const uint8_t* bytes, size_t size);
278
291 void Finish(
292 Isolate*, const ModuleCachingCallback& caching_callback,
293 const std::function<void(
294 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
295 resolution_callback);
296
301 void Abort();
302
312
318 std::function<void(CompiledWasmModule)>);
319
320 /*
321 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
322 * called before {Finish}.
323 */
324 void SetUrl(const char* url, size_t length);
325
326 private:
327 class Impl;
328 const std::unique_ptr<Impl> impl_;
329};
330
337 public:
339
341
344
345 static bool Unmap(Isolate* isolate, Local<Object> wasm_memory_map_descriptor);
346
347 static size_t Map(Isolate* isolate, Local<Object> wasm_memory_map_descriptor,
348 Local<WasmMemoryObject> memory, size_t offset);
349};
350} // namespace v8
351
352#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:292
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:336
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
SharedMemoryHandle::PlatformHandle WasmFileDescriptor
Definition: v8-wasm.h:340
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:256
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:258
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, const CompileTimeImports &compile_imports)
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, std::span< const uint8_t > wire_bytes)
CompiledWasmModule GetCompiledModule()
static WasmModuleObject * Cast(Value *value)
Definition: v8-wasm.h:147
virtual std::span< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(std::span< const uint8_t >)=0
Definition: v8-wasm.h:164
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:184
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:665
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