v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
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
30/**
31 * An owned byte buffer with associated size.
32 */
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:
45 /**
46 * Serialize the compiled module. The serialized data does not include the
47 * wire bytes.
48 */
50
51 /**
52 * Get the (wasm-encoded) wire bytes that were used to compile this module.
53 */
54 MemorySpan<const uint8_t> GetWireBytesRef();
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
74 /**
75 * Returns underlying ArrayBuffer.
76 */
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
95 /**
96 * Efficiently re-create a WasmModuleObject, without recompiling, from
97 * a CompiledWasmModule.
98 */
100 Isolate* isolate, const CompiledWasmModule&);
101
102 /**
103 * Get the compiled module for this module object. The compiled module can be
104 * shared by several module objects.
105 */
107
108 /**
109 * Compile a Wasm module from the provided uncompiled bytes.
110 */
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
125/**
126 * The V8 interface for WebAssembly streaming compilation. When streaming
127 * compilation is initiated, V8 passes a {WasmStreaming} object to the embedder
128 * such that the embedder can pass the input bytes for streaming compilation to
129 * V8.
130 */
131class V8_EXPORT WasmStreaming final {
132 public:
135 class WasmStreamingImpl;
136
137 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
138
140
141 /**
142 * Pass a new chunk of bytes to WebAssembly streaming compilation.
143 * The buffer passed into {OnBytesReceived} is owned by the caller.
144 */
145 void OnBytesReceived(const uint8_t* bytes, size_t size);
146
147 /**
148 * {Finish} should be called after all received bytes where passed to
149 * {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
150 * must not be called after {Abort} has been called already.
151 * If {can_use_compiled_module} is true and {SetCompiledModuleBytes} was
152 * previously called, the compiled module bytes can be used.
153 * If {can_use_compiled_module} is false, the compiled module bytes previously
154 * set by {SetCompiledModuleBytes} should not be used.
155 */
156 void Finish(bool can_use_compiled_module = true);
157
158 /**
159 * Abort streaming compilation. If {exception} has a value, then the promise
160 * associated with streaming compilation is rejected with that value. If
161 * {exception} does not have value, the promise does not get rejected.
162 * {Abort} must not be called repeatedly, or after {Finish}.
163 */
164 void Abort(MaybeLocal<Value> exception);
165
166 /**
167 * Passes previously compiled module bytes. This must be called before
168 * {OnBytesReceived}, {Finish}, or {Abort}. Returns true if the module bytes
169 * can be used, false otherwise. The buffer passed via {bytes} and {size}
170 * is owned by the caller. If {SetCompiledModuleBytes} returns true, the
171 * buffer must remain valid until either {Finish} or {Abort} completes.
172 * The compiled module bytes should not be used until {Finish(true)} is
173 * called, because they can be invalidated later by {Finish(false)}.
174 */
175 bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size);
176
177 /**
178 * Sets a callback which is called whenever a significant number of new
179 * functions are ready for serialization.
180 */
182 std::function<void(CompiledWasmModule)>);
183
184 /*
185 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
186 * called before {Finish}.
187 */
188 void SetUrl(const char* url, size_t length);
189
190 /**
191 * Unpacks a {WasmStreaming} object wrapped in a {Managed} for the embedder.
192 * Since the embedder is on the other side of the API, it cannot unpack the
193 * {Managed} itself.
194 */
195 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
196 Local<Value> value);
197
198 private:
199 std::unique_ptr<WasmStreamingImpl> impl_;
200};
201
202/**
203 * The V8 interface for a WebAssembly memory map descriptor. This is an
204 * experimental feature that may change and be removed without further
205 * communication.
206 */
208 public:
210
212#ifdef V8_ENABLE_CHECKS
213 CheckCast(value);
214#endif
215 return static_cast<WasmMemoryMapDescriptor*>(value);
216 }
217
218 using WasmFileDescriptor = int32_t;
219
221 WasmFileDescriptor fd);
222
223 private:
224 static void CheckCast(Value* object);
225};
226} // namespace v8
227
228#endif // INCLUDE_V8_WASM_H_
const std::string & source_url() const
Definition v8-wasm.h:56
MemorySpan< const uint8_t > GetWireBytesRef()
OwnedBuffer Serialize()
friend class Local
friend class MaybeLocal
static Local< WasmMemoryMapDescriptor > New(Isolate *isolate, WasmFileDescriptor fd)
static V8_INLINE WasmMemoryMapDescriptor * Cast(Value *value)
Definition v8-wasm.h:211
Local< ArrayBuffer > Buffer()
static V8_INLINE WasmMemoryObject * Cast(Value *value)
Definition v8-wasm.h:79
static V8_INLINE WasmModuleObject * Cast(Value *value)
Definition v8-wasm.h:114
static MaybeLocal< WasmModuleObject > FromCompiledModule(Isolate *isolate, const CompiledWasmModule &)
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, MemorySpan< const uint8_t > wire_bytes)
CompiledWasmModule GetCompiledModule()
static constexpr internal::ExternalPointerTag kManagedTag
Definition v8-wasm.h:133
void Finish(bool can_use_compiled_module=true)
void Abort(MaybeLocal< Value > exception)
void OnBytesReceived(const uint8_t *bytes, size_t size)
void SetUrl(const char *url, size_t length)
bool SetCompiledModuleBytes(const uint8_t *bytes, size_t size)
static std::shared_ptr< WasmStreaming > Unpack(Isolate *isolate, Local< Value > value)
WasmStreaming(std::unique_ptr< WasmStreamingImpl > impl)
void SetMoreFunctionsCanBeSerializedCallback(std::function< void(CompiledWasmModule)>)
size_t size
Definition v8-wasm.h:35
std::unique_ptr< const uint8_t[]> buffer
Definition v8-wasm.h:34
OwnedBuffer()=default
OwnedBuffer(std::unique_ptr< const uint8_t[]> buffer, size_t size)
Definition v8-wasm.h:36
#define V8_EXPORT
Definition v8config.h:860
#define V8_INLINE
Definition v8config.h:513