Commit 7971f9d6 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Prepare API changes for WebAssembly.instantiateStreaming

This is the first CL to refactor WebAssembly.instantiateStreaming to
make it spec compliant again. The design doc where the whole change is
discussed is available in the tracking bug. The tracking bug also
references prototype implementations of the whole change, which includes
the changes in this CL.

Tests for the new API functions will be added with the second V8 CL
which adds the implementation of the API functions.

R=ulan@chromium.org

Bug: chromium:860637
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: Ia1048b7ca0291c824ef4212ebde2c6054e102069
Reviewed-on: https://chromium-review.googlesource.com/1127667Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54393}
parent 50509a9b
......@@ -154,6 +154,7 @@ class FunctionCallbackArguments;
class GlobalHandles;
namespace wasm {
class CompilationResultResolver;
class StreamingDecoder;
} // namespace wasm
......@@ -4462,6 +4463,52 @@ operator WasmCompiledModule::CallerOwnedBuffer() {
return {start, size};
}
/**
* The V8 interface for WebAssembly streaming compilation. When streaming
* compilation is initiated, V8 passes a {WasmStreaming} object to the embedder
* such that the embedder can pass the input butes for streaming compilation to
* V8.
*/
class V8_EXPORT WasmStreaming final {
public:
class WasmStreamingImpl;
WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
~WasmStreaming();
/**
* Pass a new chunck of bytes to WebAssembly streaming compilation.
* The buffer passed into {OnBytesReceived} is owned by the caller.
*/
void OnBytesReceived(const uint8_t* bytes, size_t size);
/**
* {Finish} should be called after all received bytes where passed to
* {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
* does not have to be called after {Abort} has been called already.
*/
void Finish();
/**
* Abort streaming compilation. If {exception} has a value, then the promise
* associated with streaming compilation is rejected with that value. If
* {exception} does not have value, the promise does not get rejected.
*/
void Abort(MaybeLocal<Value> exception);
/**
* Unpacks a {WasmStreaming} object wrapped in a {Managed} for the embedder.
* Since the embedder is on the other side of the API, it cannot unpack the
* {Managed} itself.
*/
static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
Local<Value> value);
private:
std::unique_ptr<WasmStreamingImpl> impl_;
};
// TODO(mtrofin): when streaming compilation is done, we can rename this
// to simply WasmModuleObjectBuilder
class V8_EXPORT WasmModuleObjectBuilderStreaming final {
......@@ -6678,6 +6725,9 @@ typedef bool (*AllowWasmCodeGenerationCallback)(Local<Context> context,
// by the embedder. Example: WebAssembly.{compile|instantiate}Streaming ---
typedef void (*ApiImplementationCallback)(const FunctionCallbackInfo<Value>&);
// --- Callback for WebAssembly.compileStreaming ---
typedef void (*WasmStreamingCallback)(const FunctionCallbackInfo<Value>&);
// --- Garbage Collection Callbacks ---
/**
......@@ -8190,6 +8240,8 @@ class V8_EXPORT Isolate {
void SetWasmCompileStreamingCallback(ApiImplementationCallback callback);
void SetWasmStreamingCallback(WasmStreamingCallback callback);
/**
* Check if V8 is dead and therefore unusable. This is the case after
* fatal errors such as out-of-memory situations.
......
......@@ -8939,6 +8939,9 @@ CALLBACK_SETTER(WasmInstanceCallback, ExtensionCallback, wasm_instance_callback)
CALLBACK_SETTER(WasmCompileStreamingCallback, ApiImplementationCallback,
wasm_compile_streaming_callback)
CALLBACK_SETTER(WasmStreamingCallback, WasmStreamingCallback,
wasm_streaming_callback)
void Isolate::AddNearHeapLimitCallback(v8::NearHeapLimitCallback callback,
void* data) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
......
......@@ -431,6 +431,7 @@ typedef std::vector<HeapObject*> DebugObjectCache;
V(ExtensionCallback, wasm_module_callback, &NoExtension) \
V(ExtensionCallback, wasm_instance_callback, &NoExtension) \
V(ApiImplementationCallback, wasm_compile_streaming_callback, nullptr) \
V(WasmStreamingCallback, wasm_streaming_callback, nullptr) \
/* State for Relocatable. */ \
V(Relocatable*, relocatable_top, nullptr) \
V(DebugObjectCache*, string_stream_debug_object_cache, nullptr) \
......
......@@ -17,6 +17,7 @@
#include "src/objects/templates.h"
#include "src/parsing/parse-info.h"
#include "src/trap-handler/trap-handler.h"
#include "src/wasm/streaming-decoder.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-memory.h"
......@@ -26,6 +27,41 @@ using v8::internal::wasm::ErrorThrower;
namespace v8 {
class WasmStreaming::WasmStreamingImpl {
public:
void OnBytesReceived(const uint8_t* bytes, size_t size) {}
void Finish() {}
void Abort(MaybeLocal<Value> exception) {}
};
WasmStreaming::WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl)
: impl_(std::move(impl)) {}
// The destructor is defined here because we have a unique_ptr with forward
// declaration.
WasmStreaming::~WasmStreaming() = default;
void WasmStreaming::OnBytesReceived(const uint8_t* bytes, size_t size) {
impl_->OnBytesReceived(bytes, size);
}
void WasmStreaming::Finish() { impl_->Finish(); }
void WasmStreaming::Abort(MaybeLocal<Value> exception) {
impl_->Abort(exception);
}
// static
std::shared_ptr<WasmStreaming> WasmStreaming::Unpack(Isolate* isolate,
Local<Value> value) {
i::HandleScope scope(reinterpret_cast<i::Isolate*>(isolate));
auto managed =
i::Handle<i::Managed<WasmStreaming>>::cast(Utils::OpenHandle(*value));
return managed->get();
}
namespace {
#define ASSIGN(type, var, expr) \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment