v8/test/cctest/test-api-wasm.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

221 lines
7.6 KiB
C++
Raw Normal View History

// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
Reland "[include] Split out v8.h" This is a reland of d1b27019d3bf86360ea838c317f8505fac6d3a7e Fixes include: Adding missing file to bazel build Forward-declaring classing before friend-classing them to fix win/gcc Add missing v8-isolate.h include for vtune builds Original change's description: > [include] Split out v8.h > > This moves every single class/function out of include/v8.h into a > separate header in include/, which v8.h then includes so that > externally nothing appears to have changed. > > Every include of v8.h from inside v8 has been changed to a more > fine-grained include. > > Previously inline functions defined at the bottom of v8.h would call > private non-inline functions in the V8 class. Since that class is now > in v8-initialization.h and is rarely included (as that would create > dependency cycles), this is not possible and so those methods have been > moved out of the V8 class into the namespace v8::api_internal. > > None of the previous files in include/ now #include v8.h, which means > if embedders were relying on this transitive dependency then it will > give compile failures. > > v8-inspector.h does depend on v8-scripts.h for the time being to ensure > that Chrome continue to compile but that change will be reverted once > those transitive #includes in chrome are changed to include it directly. > > Full design: > https://docs.google.com/document/d/1rTD--I8hCAr-Rho1WTumZzFKaDpEp0IJ8ejZtk4nJdA/edit?usp=sharing > > Bug: v8:11965 > Change-Id: I53b84b29581632710edc80eb11f819c2097a2877 > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3097448 > Reviewed-by: Yang Guo <yangguo@chromium.org> > Reviewed-by: Camillo Bruni <cbruni@chromium.org> > Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> > Reviewed-by: Leszek Swirski <leszeks@chromium.org> > Reviewed-by: Michael Lippautz <mlippautz@chromium.org> > Commit-Queue: Dan Elphick <delphick@chromium.org> > Cr-Commit-Position: refs/heads/main@{#76424} Cq-Include-Trybots: luci.v8.try:v8_linux_vtunejit Bug: v8:11965 Change-Id: I99f5d3a73bf8fe25b650adfaf9567dc4e44a09e6 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3113629 Reviewed-by: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Camillo Bruni <cbruni@chromium.org> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org> Commit-Queue: Dan Elphick <delphick@chromium.org> Cr-Commit-Position: refs/heads/main@{#76460}
2021-08-23 13:01:06 +00:00
#include "include/v8-context.h"
#include "include/v8-function-callback.h"
#include "include/v8-local-handle.h"
#include "include/v8-object.h"
#include "include/v8-persistent-handle.h"
#include "include/v8-promise.h"
#include "include/v8-wasm.h"
#include "src/api/api-inl.h"
#include "src/handles/global-handles.h"
#include "test/cctest/cctest.h"
namespace {
bool wasm_streaming_callback_got_called = false;
bool wasm_streaming_data_got_collected = false;
// The bytes of a minimal WebAssembly module.
const uint8_t kMinimalWasmModuleBytes[]{0x00, 0x61, 0x73, 0x6d,
0x01, 0x00, 0x00, 0x00};
void WasmStreamingTestFinalizer(const v8::WeakCallbackInfo<void>& data) {
CHECK(!wasm_streaming_data_got_collected);
wasm_streaming_data_got_collected = true;
i::GlobalHandles::Destroy(reinterpret_cast<i::Address*>(data.GetParameter()));
}
void WasmStreamingCallbackTestCallbackIsCalled(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK(!wasm_streaming_callback_got_called);
wasm_streaming_callback_got_called = true;
i::Handle<i::Object> global_handle =
reinterpret_cast<i::Isolate*>(args.GetIsolate())
->global_handles()
->Create(*v8::Utils::OpenHandle(*args.Data()));
i::GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(),
WasmStreamingTestFinalizer,
v8::WeakCallbackType::kParameter);
}
void WasmStreamingCallbackTestOnBytesReceived(
const v8::FunctionCallbackInfo<v8::Value>& args) {
std::shared_ptr<v8::WasmStreaming> streaming =
v8::WasmStreaming::Unpack(args.GetIsolate(), args.Data());
// The first bytes of the WebAssembly magic word.
const uint8_t bytes[]{0x00, 0x61, 0x73};
streaming->OnBytesReceived(bytes, arraysize(bytes));
}
void WasmStreamingCallbackTestFinishWithSuccess(
const v8::FunctionCallbackInfo<v8::Value>& args) {
std::shared_ptr<v8::WasmStreaming> streaming =
v8::WasmStreaming::Unpack(args.GetIsolate(), args.Data());
streaming->OnBytesReceived(kMinimalWasmModuleBytes,
arraysize(kMinimalWasmModuleBytes));
streaming->Finish();
}
void WasmStreamingCallbackTestFinishWithFailure(
const v8::FunctionCallbackInfo<v8::Value>& args) {
std::shared_ptr<v8::WasmStreaming> streaming =
v8::WasmStreaming::Unpack(args.GetIsolate(), args.Data());
streaming->Finish();
}
void WasmStreamingCallbackTestAbortWithReject(
const v8::FunctionCallbackInfo<v8::Value>& args) {
std::shared_ptr<v8::WasmStreaming> streaming =
v8::WasmStreaming::Unpack(args.GetIsolate(), args.Data());
streaming->Abort(v8::Object::New(args.GetIsolate()));
}
void WasmStreamingCallbackTestAbortNoReject(
const v8::FunctionCallbackInfo<v8::Value>& args) {
std::shared_ptr<v8::WasmStreaming> streaming =
v8::WasmStreaming::Unpack(args.GetIsolate(), args.Data());
streaming->Abort({});
}
void TestWasmStreaming(v8::WasmStreamingCallback callback,
v8::Promise::PromiseState expected_state) {
CcTest::isolate()->SetWasmStreamingCallback(callback);
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
// Call {WebAssembly.compileStreaming} with {null} as parameter. The parameter
// is only really processed by the embedder, so for this test the value is
// irrelevant.
v8::Local<v8::Promise> promise = v8::Local<v8::Promise>::Cast(
CompileRun("WebAssembly.compileStreaming(null)"));
EmptyMessageQueues(isolate);
CHECK_EQ(expected_state, promise->State());
}
} // namespace
TEST(WasmStreamingCallback) {
TestWasmStreaming(WasmStreamingCallbackTestCallbackIsCalled,
v8::Promise::kPending);
CHECK(wasm_streaming_callback_got_called);
CcTest::CollectAllAvailableGarbage();
CHECK(wasm_streaming_data_got_collected);
}
TEST(WasmStreamingOnBytesReceived) {
TestWasmStreaming(WasmStreamingCallbackTestOnBytesReceived,
v8::Promise::kPending);
}
TEST(WasmStreamingFinishWithSuccess) {
TestWasmStreaming(WasmStreamingCallbackTestFinishWithSuccess,
v8::Promise::kFulfilled);
}
TEST(WasmStreamingFinishWithFailure) {
TestWasmStreaming(WasmStreamingCallbackTestFinishWithFailure,
v8::Promise::kRejected);
}
TEST(WasmStreamingAbortWithReject) {
TestWasmStreaming(WasmStreamingCallbackTestAbortWithReject,
v8::Promise::kRejected);
}
TEST(WasmStreamingAbortWithoutReject) {
TestWasmStreaming(WasmStreamingCallbackTestAbortNoReject,
v8::Promise::kPending);
}
TEST(WasmCompileToWasmModuleObject) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
auto maybe_module = v8::WasmModuleObject::Compile(
CcTest::isolate(),
{kMinimalWasmModuleBytes, arraysize(kMinimalWasmModuleBytes)});
CHECK(!maybe_module.IsEmpty());
}
namespace {
bool wasm_simd_enabled_value = false;
bool wasm_exceptions_enabled_value = false;
bool MockWasmSimdEnabledCallback(v8::Local<v8::Context>) {
return wasm_simd_enabled_value;
}
bool MockWasmExceptionsEnabledCallback(v8::Local<v8::Context>) {
return wasm_exceptions_enabled_value;
}
} // namespace
TEST(TestSetWasmSimdEnabledCallback) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
v8::HandleScope scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate());
i::Handle<i::Context> i_context = v8::Utils::OpenHandle(*context);
// {Isolate::IsWasmSimdEnabled} calls the callback set by the embedder if
// such a callback exists. Otherwise it returns
// {FLAG_experimental_wasm_simd}. First we test that the flag is returned
// correctly if no callback is set. Then we test that the flag is ignored if
// the callback is set.
i::FLAG_experimental_wasm_simd = false;
CHECK(!i_isolate->IsWasmSimdEnabled(i_context));
i::FLAG_experimental_wasm_simd = true;
CHECK(i_isolate->IsWasmSimdEnabled(i_context));
isolate->SetWasmSimdEnabledCallback(MockWasmSimdEnabledCallback);
wasm_simd_enabled_value = false;
CHECK(!i_isolate->IsWasmSimdEnabled(i_context));
wasm_simd_enabled_value = true;
i::FLAG_experimental_wasm_simd = false;
CHECK(i_isolate->IsWasmSimdEnabled(i_context));
}
TEST(TestSetWasmExceptionsEnabledCallback) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
v8::HandleScope scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate());
i::Handle<i::Context> i_context = v8::Utils::OpenHandle(*context);
// {Isolate::AreWasmExceptionsEnabled} calls the callback set by the embedder
// if such a callback exists. Otherwise it returns
// {FLAG_experimental_wasm_eh}. First we test that the flag is returned
// correctly if no callback is set. Then we test that the flag is ignored if
// the callback is set.
i::FLAG_experimental_wasm_eh = false;
CHECK(!i_isolate->AreWasmExceptionsEnabled(i_context));
i::FLAG_experimental_wasm_eh = true;
CHECK(i_isolate->AreWasmExceptionsEnabled(i_context));
isolate->SetWasmExceptionsEnabledCallback(MockWasmExceptionsEnabledCallback);
wasm_exceptions_enabled_value = false;
CHECK(!i_isolate->AreWasmExceptionsEnabled(i_context));
wasm_exceptions_enabled_value = true;
i::FLAG_experimental_wasm_eh = false;
CHECK(i_isolate->AreWasmExceptionsEnabled(i_context));
}