File indexing completed on 2026-07-27 09:40:58
0001
0002
0003
0004
0005 #ifndef INCLUDE_V8_WASM_H_
0006 #define INCLUDE_V8_WASM_H_
0007
0008 #include <functional>
0009 #include <memory>
0010 #include <string>
0011 #include <variant>
0012
0013 #include "v8-internal.h" // NOLINT(build/include_directory)
0014 #include "v8-local-handle.h" // NOLINT(build/include_directory)
0015 #include "v8-memory-span.h" // NOLINT(build/include_directory)
0016 #include "v8-object.h" // NOLINT(build/include_directory)
0017 #include "v8config.h" // NOLINT(build/include_directory)
0018
0019 namespace v8 {
0020
0021 class ArrayBuffer;
0022 class Promise;
0023
0024 namespace internal::wasm {
0025 class NativeModule;
0026 }
0027
0028
0029
0030
0031 struct OwnedBuffer {
0032 std::unique_ptr<const uint8_t[]> buffer;
0033 size_t size = 0;
0034 OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
0035 : buffer(std::move(buffer)), size(size) {}
0036 OwnedBuffer() = default;
0037 };
0038
0039
0040
0041
0042
0043 class V8_EXPORT CompiledWasmModule {
0044 public:
0045
0046
0047
0048
0049 OwnedBuffer Serialize();
0050
0051
0052
0053
0054 MemorySpan<const uint8_t> GetWireBytesRef();
0055
0056 const std::string& source_url() const { return source_url_; }
0057
0058 private:
0059 friend class WasmModuleCompilation;
0060 friend class WasmModuleObject;
0061 friend class WasmStreaming;
0062
0063 explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
0064 std::string source_url);
0065
0066 const std::shared_ptr<internal::wasm::NativeModule> native_module_;
0067 const std::string source_url_;
0068 };
0069
0070
0071 class V8_EXPORT WasmMemoryObject : public Object {
0072 public:
0073 WasmMemoryObject() = delete;
0074
0075
0076
0077
0078 Local<ArrayBuffer> Buffer();
0079
0080 V8_INLINE static WasmMemoryObject* Cast(Value* value) {
0081 #ifdef V8_ENABLE_CHECKS
0082 CheckCast(value);
0083 #endif
0084 return static_cast<WasmMemoryObject*>(value);
0085 }
0086
0087 private:
0088 static void CheckCast(Value* object);
0089 };
0090
0091
0092 class V8_EXPORT WasmModuleObject : public Object {
0093 public:
0094 WasmModuleObject() = delete;
0095
0096
0097
0098
0099
0100 static MaybeLocal<WasmModuleObject> FromCompiledModule(
0101 Isolate* isolate, const CompiledWasmModule&);
0102
0103
0104
0105
0106
0107 CompiledWasmModule GetCompiledModule();
0108
0109
0110
0111
0112 static MaybeLocal<WasmModuleObject> Compile(
0113 Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
0114
0115 V8_INLINE static WasmModuleObject* Cast(Value* value) {
0116 #ifdef V8_ENABLE_CHECKS
0117 CheckCast(value);
0118 #endif
0119 return static_cast<WasmModuleObject*>(value);
0120 }
0121
0122 private:
0123 static void CheckCast(Value* obj);
0124 };
0125
0126
0127
0128
0129
0130
0131
0132 class V8_EXPORT WasmStreaming final {
0133 public:
0134 static constexpr internal::ExternalPointerTag kManagedTag =
0135 internal::kWasmWasmStreamingTag;
0136 class WasmStreamingImpl;
0137
0138 class ModuleCachingInterface {
0139 public:
0140
0141 virtual MemorySpan<const uint8_t> GetWireBytes() const = 0;
0142
0143
0144
0145
0146
0147
0148
0149 virtual bool SetCachedCompiledModuleBytes(MemorySpan<const uint8_t>) = 0;
0150 };
0151
0152 using ModuleCachingCallback = std::function<void(ModuleCachingInterface&)>;
0153
0154 explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
0155
0156 ~WasmStreaming();
0157
0158
0159
0160
0161
0162 void OnBytesReceived(const uint8_t* bytes, size_t size);
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 void Finish(const ModuleCachingCallback& caching_callback);
0174
0175
0176
0177
0178
0179
0180
0181 void Abort(MaybeLocal<Value> exception);
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 void SetHasCompiledModuleBytes();
0192
0193
0194
0195
0196
0197 void SetMoreFunctionsCanBeSerializedCallback(
0198 std::function<void(CompiledWasmModule)>);
0199
0200
0201
0202
0203
0204 void SetUrl(const char* url, size_t length);
0205
0206
0207
0208
0209
0210
0211 static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
0212 Local<Value> value);
0213
0214 private:
0215 std::unique_ptr<WasmStreamingImpl> impl_;
0216 };
0217
0218
0219
0220
0221
0222
0223
0224 class V8_EXPORT WasmModuleCompilation final {
0225 public:
0226 using ModuleCachingCallback = WasmStreaming::ModuleCachingCallback;
0227
0228
0229
0230
0231
0232
0233 WasmModuleCompilation();
0234
0235 ~WasmModuleCompilation();
0236
0237 WasmModuleCompilation(const WasmModuleCompilation&) = delete;
0238 WasmModuleCompilation& operator=(const WasmModuleCompilation&) = delete;
0239
0240
0241
0242
0243
0244
0245 void OnBytesReceived(const uint8_t* bytes, size_t size);
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259 void Finish(
0260 Isolate*, const ModuleCachingCallback& caching_callback,
0261 const std::function<void(
0262 std::variant<Local<WasmModuleObject>, Local<Value>> module_or_error)>&
0263 resolution_callback);
0264
0265
0266
0267
0268
0269 void Abort();
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 void SetHasCompiledModuleBytes();
0280
0281
0282
0283
0284
0285 void SetMoreFunctionsCanBeSerializedCallback(
0286 std::function<void(CompiledWasmModule)>);
0287
0288
0289
0290
0291
0292 void SetUrl(const char* url, size_t length);
0293
0294 private:
0295 class Impl;
0296 const std::unique_ptr<Impl> impl_;
0297 };
0298
0299
0300
0301
0302
0303
0304 class V8_EXPORT WasmMemoryMapDescriptor : public Object {
0305 public:
0306 WasmMemoryMapDescriptor() = delete;
0307
0308 V8_INLINE static WasmMemoryMapDescriptor* Cast(Value* value) {
0309 #ifdef V8_ENABLE_CHECKS
0310 CheckCast(value);
0311 #endif
0312 return static_cast<WasmMemoryMapDescriptor*>(value);
0313 }
0314
0315 using WasmFileDescriptor = int32_t;
0316
0317 static Local<WasmMemoryMapDescriptor> New(Isolate* isolate,
0318 WasmFileDescriptor fd);
0319
0320 private:
0321 static void CheckCast(Value* object);
0322 };
0323 }
0324
0325 #endif