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 <string_view>
13#include <variant>
14
15#include "v8-internal.h" // NOLINT(build/include_directory)
16#include "v8-local-handle.h" // NOLINT(build/include_directory)
17#include "v8-object.h" // NOLINT(build/include_directory)
18#include "v8-platform.h" // NOLINT(build/include_directory)
19#include "v8config.h" // NOLINT(build/include_directory)
20
21namespace v8 {
22
23class ArrayBuffer;
24class Promise;
25
26namespace internal::wasm {
27class NativeModule;
28} // namespace internal::wasm
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
46 public:
52
56 std::span<const uint8_t> GetWireBytesRef();
57
58 const std::string& source_url() const { return source_url_; }
59
60 private:
62 friend class WasmModuleObject;
63 friend class WasmStreaming;
64
65 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
66 std::string source_url);
67
68 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
69 const std::string source_url_;
70};
71
72// An instance of WebAssembly.Memory.
74 public:
75 WasmMemoryObject() = delete;
76
81
83#ifdef V8_ENABLE_CHECKS
84 CheckCast(value);
85#endif
86 return static_cast<WasmMemoryObject*>(value);
87 }
88
89 private:
90 static void CheckCast(Value* object);
91};
92
93// An instance of WebAssembly.Module.
95 public:
96 WasmModuleObject() = delete;
97
103 Isolate* isolate, const CompiledWasmModule&);
104
110
117 // Builtin compile-time imports, mirroring the strings accepted in the
118 // `builtins` array of the JS `WebAssembly.Module` constructor options.
119 // Combine values with bitwise-or to enable multiple builtins.
120 struct Builtins {
121 enum {
122 kNone = 0,
123 kJsString = 1 << 0, // "js-string"
124 };
125 };
126 // Bitwise-or of `Builtins` values to enable as compile-time imports.
127 int builtins = Builtins::kNone;
128 // If non-null, enable imported string constants from the named module
129 // (e.g. "wasm:js/string-constants"). The string must be null-terminated and
130 // remain valid for the duration of the compile call.
131 const char* imported_string_constants_module = nullptr;
132 // If non-empty, associated with the module's script as its source URL, for
133 // use in stack traces and developer tooling. If a script already exists in
134 // the isolate for the same module, its existing URL is retained. The
135 // string must remain valid for the duration of the compile call.
136 std::string_view source_url = {};
137 };
138
143 Isolate* isolate, std::span<const uint8_t> wire_bytes);
144
150 Isolate* isolate, std::span<const uint8_t> wire_bytes,
151 const CompileOptions& options);
152
154#ifdef V8_ENABLE_CHECKS
155 CheckCast(value);
156#endif
157 return static_cast<WasmModuleObject*>(value);
158 }
159
160 private:
161 static void CheckCast(Value* obj);
162};
163
171 public:
172 static constexpr internal::ManagedTypeId kTypeID =
173 internal::ManagedTypeId::kWasmStreaming;
174 class WasmStreamingImpl;
175
177 public:
178 // Get the full wire bytes, to check against the cached version.
179 virtual std::span<const uint8_t> GetWireBytes() const = 0;
180 // Pass serialized (cached) compiled module bytes, to be deserialized and
181 // used as the result of this streaming compilation.
182 // The passed bytes will only be accessed inside this callback, i.e.
183 // lifetime can end after the call.
184 // The return value indicates whether V8 could use the passed bytes; {false}
185 // would be returned on e.g. version mismatch.
186 // This method can only be called once.
187 virtual bool SetCachedCompiledModuleBytes(std::span<const uint8_t>) = 0;
188 };
189
190 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
191
192 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
193
195
200 void OnBytesReceived(const uint8_t* bytes, size_t size);
201
211 void Finish(const ModuleCachingCallback& caching_callback);
212
219 void Abort(MaybeLocal<Value> exception);
220
230
236 std::function<void(CompiledWasmModule)>);
237
238 /*
239 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
240 * called before {Finish}.
241 */
242 void SetUrl(const char* url, size_t length);
243
249 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
250 Local<Value> value);
251
252 private:
253 std::unique_ptr<WasmStreamingImpl> impl_;
254};
255
263 public:
266
273 explicit WasmModuleCompilation(const CompileOptions& options = {});
274
276
279
285 void OnBytesReceived(const uint8_t* bytes, size_t size);
286
299 void Finish(
300 Isolate*, const ModuleCachingCallback& caching_callback,
301 const std::function<void(
302 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
303 resolution_callback);
304
309 void Abort();
310
320
326 std::function<void(CompiledWasmModule)>);
327
328 /*
329 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
330 * called before {Finish}.
331 */
332 void SetUrl(const char* url, size_t length);
333
334 private:
335 class Impl;
336 const std::unique_ptr<Impl> impl_;
337};
338
345 public:
347
349
352
353 static bool Unmap(Isolate* isolate, Local<Object> wasm_memory_map_descriptor);
354
355 static size_t Map(Isolate* isolate, Local<Object> wasm_memory_map_descriptor,
356 Local<WasmMemoryObject> memory, size_t offset);
357};
358} // namespace v8
359
360#endif // INCLUDE_V8_WASM_H_
Definition: v8-wasm.h:45
OwnedBuffer Serialize()
const std::string & source_url() const
Definition: v8-wasm.h:58
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:267
int PlatformHandle
Definition: v8-platform.h:491
Definition: v8-value.h:32
Definition: v8-wasm.h:344
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
SharedMemoryHandle::PlatformHandle WasmFileDescriptor
Definition: v8-wasm.h:348
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:73
static WasmMemoryObject * Cast(Value *value)
Definition: v8-wasm.h:82
Local< ArrayBuffer > Buffer()
Definition: v8-wasm.h:262
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
WasmStreaming::ModuleCachingCallback ModuleCachingCallback
Definition: v8-wasm.h:264
WasmModuleCompilation(const WasmModuleCompilation &)=delete
WasmModuleCompilation(const CompileOptions &options={})
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:94
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, std::span< const uint8_t > wire_bytes, const CompileOptions &options)
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:153
virtual std::span< const uint8_t > GetWireBytes() const =0
virtual bool SetCachedCompiledModuleBytes(std::span< const uint8_t >)=0
Definition: v8-wasm.h:170
std::function< void(ModuleCachingInterface &)> ModuleCachingCallback
Definition: v8-wasm.h:190
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)
ManagedTypeId
Definition: v8-internal.h:578
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
Definition: v8-wasm.h:116
#define V8_EXPORT
Definition: v8config.h:867
#define V8_INLINE
Definition: v8config.h:511