|
8 | 8 | #include <functional> |
9 | 9 | #include <memory> |
10 | 10 | #include <string> |
| 11 | +#include <string_view> |
11 | 12 | #include <variant> |
12 | 13 |
|
13 | 14 | #include "v8-internal.h" // NOLINT(build/include_directory) |
@@ -106,12 +107,48 @@ class V8_EXPORT WasmModuleObject : public Object { |
106 | 107 | */ |
107 | 108 | CompiledWasmModule GetCompiledModule(); |
108 | 109 |
|
| 110 | + /** |
| 111 | + * Options that influence how a Wasm module is compiled. The compile-time |
| 112 | + * import options mirror those accepted by the JS `WebAssembly.Module` |
| 113 | + * constructor (`{ builtins, importedStringConstants }`). |
| 114 | + */ |
| 115 | + struct CompileOptions { |
| 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 the compile call. |
| 130 | + const char* imported_string_constants_module = nullptr; |
| 131 | + // If non-empty, associated with the module's script as its source URL, for |
| 132 | + // use in stack traces and developer tooling. If a script already exists in |
| 133 | + // the isolate for the same module, its existing URL is retained. The |
| 134 | + // string must remain valid for the duration of the compile call. |
| 135 | + std::string_view source_url = {}; |
| 136 | + }; |
| 137 | + |
109 | 138 | /** |
110 | 139 | * Compile a Wasm module from the provided uncompiled bytes. |
111 | 140 | */ |
112 | 141 | static MaybeLocal<WasmModuleObject> Compile( |
113 | 142 | Isolate* isolate, MemorySpan<const uint8_t> wire_bytes); |
114 | 143 |
|
| 144 | + /** |
| 145 | + * Compile a Wasm module from the provided uncompiled bytes, applying the |
| 146 | + * given compile options. |
| 147 | + */ |
| 148 | + static MaybeLocal<WasmModuleObject> Compile( |
| 149 | + Isolate* isolate, MemorySpan<const uint8_t> wire_bytes, |
| 150 | + const CompileOptions& options); |
| 151 | + |
115 | 152 | V8_INLINE static WasmModuleObject* Cast(Value* value) { |
116 | 153 | #ifdef V8_ENABLE_CHECKS |
117 | 154 | CheckCast(value); |
@@ -224,13 +261,15 @@ class V8_EXPORT WasmStreaming final { |
224 | 261 | class V8_EXPORT WasmModuleCompilation final { |
225 | 262 | public: |
226 | 263 | using ModuleCachingCallback = WasmStreaming::ModuleCachingCallback; |
| 264 | + using CompileOptions = WasmModuleObject::CompileOptions; |
227 | 265 |
|
228 | 266 | /** |
229 | | - * Start an asynchronous module compilation. This can be called on any thread. |
| 267 | + * Start an asynchronous module compilation, applying the given compile |
| 268 | + * options. This can be called on any thread. Providing |
| 269 | + * {CompileOptions::source_url} is equivalent to calling {SetUrl}. |
230 | 270 | * TODO(clemensb): Add some way to pass enabled features. |
231 | | - * TODO(clemensb): Add some way to pass compile time imports. |
232 | 271 | */ |
233 | | - WasmModuleCompilation(); |
| 272 | + explicit WasmModuleCompilation(const CompileOptions& options = {}); |
234 | 273 |
|
235 | 274 | ~WasmModuleCompilation(); |
236 | 275 |
|
|
0 commit comments