v8 10.2.154 (node 18.16.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 <memory>
9#include <string>
10
11#include "v8-local-handle.h" // NOLINT(build/include_directory)
12#include "v8-memory-span.h" // NOLINT(build/include_directory)
13#include "v8-object.h" // NOLINT(build/include_directory)
14#include "v8config.h" // NOLINT(build/include_directory)
15
16namespace v8 {
17
18class ArrayBuffer;
19class Promise;
20
21namespace internal {
22namespace wasm {
23class NativeModule;
24class StreamingDecoder;
25} // namespace wasm
26} // namespace internal
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
39// Wrapper around a compiled WebAssembly module, which is potentially shared by
40// different WasmModuleObjects.
42 public:
48
53
54 const std::string& source_url() const { return source_url_; }
55
56 private:
57 friend class WasmModuleObject;
58 friend class WasmStreaming;
59
60 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
61 const char* source_url, size_t url_length);
62
63 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
64 const std::string source_url_;
65};
66
67// An instance of WebAssembly.Memory.
69 public:
70 WasmMemoryObject() = delete;
71
76
78#ifdef V8_ENABLE_CHECKS
79 CheckCast(value);
80#endif
81 return static_cast<WasmMemoryObject*>(value);
82 }
83
84 private:
85 static void CheckCast(Value* object);
86};
87
88// An instance of WebAssembly.Module.
90 public:
91 WasmModuleObject() = delete;
92
98 Isolate* isolate, const CompiledWasmModule&);
99
105
110 Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
111
113#ifdef V8_ENABLE_CHECKS
114 CheckCast(value);
115#endif
116 return static_cast<WasmModuleObject*>(value);
117 }
118
119 private:
120 static void CheckCast(Value* obj);
121};
122
130 public:
131 class WasmStreamingImpl;
132
136 class Client {
137 public:
138 virtual ~Client() = default;
143 virtual void OnModuleCompiled(CompiledWasmModule compiled_module) = 0;
144 };
145
146 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
147
149
154 void OnBytesReceived(const uint8_t* bytes, size_t size);
155
165 void Finish(bool can_use_compiled_module = true);
166
172 void Abort(MaybeLocal<Value> exception);
173
183 bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size);
184
189 void SetClient(std::shared_ptr<Client> client);
190
191 /*
192 * Sets the UTF-8 encoded source URL for the {Script} object. This must be
193 * called before {Finish}.
194 */
195 void SetUrl(const char* url, size_t length);
196
202 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
203 Local<Value> value);
204
205 private:
206 std::unique_ptr<WasmStreamingImpl> impl_;
207};
208
209// TODO(mtrofin): when streaming compilation is done, we can rename this
210// to simply WasmModuleObjectBuilder
212 public:
217 void OnBytesReceived(const uint8_t*, size_t size);
218 void Finish();
224 void Abort(MaybeLocal<Value> exception);
226
228
229 private:
231 delete;
233 default;
235 const WasmModuleObjectBuilderStreaming&) = delete;
238 Isolate* isolate_ = nullptr;
239
240#if V8_CC_MSVC
249#else
250 Persistent<Promise> promise_;
251#endif
252 std::shared_ptr<internal::wasm::StreamingDecoder> streaming_decoder_;
253};
254
255} // namespace v8
256
257#endif // INCLUDE_V8_WASM_H_
const std::string & source_url() const
Definition v8-wasm.h:54
MemorySpan< const uint8_t > GetWireBytesRef()
OwnedBuffer Serialize()
Local< ArrayBuffer > Buffer()
static V8_INLINE WasmMemoryObject * Cast(Value *value)
Definition v8-wasm.h:77
void Abort(MaybeLocal< Value > exception)
WasmModuleObjectBuilderStreaming(Isolate *isolate)
void OnBytesReceived(const uint8_t *, size_t size)
static V8_INLINE WasmModuleObject * Cast(Value *value)
Definition v8-wasm.h:112
static MaybeLocal< WasmModuleObject > FromCompiledModule(Isolate *isolate, const CompiledWasmModule &)
static MaybeLocal< WasmModuleObject > Compile(Isolate *isolate, MemorySpan< const uint8_t > wire_bytes)
CompiledWasmModule GetCompiledModule()
virtual void OnModuleCompiled(CompiledWasmModule compiled_module)=0
virtual ~Client()=default
void SetClient(std::shared_ptr< Client > client)
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)
size_t size
Definition v8-wasm.h:33
std::unique_ptr< const uint8_t[]> buffer
Definition v8-wasm.h:32
OwnedBuffer()=default
OwnedBuffer(std::unique_ptr< const uint8_t[]> buffer, size_t size)
Definition v8-wasm.h:34
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425