v8/test/unittests/heap/js-visitor-unittest.cc
Michael Lippautz 3a50eae048 api: Add JSVisitor and JSMember reference
- Adds JSVisitor that is used for unified heap marking.
- Adds JSMember as supported reference type that also encapsulates a
  write barrier in future. JSMember is a replacement for
  TracedReference which can be deprecated with EmbedderHeapTracer once
  the library is used to handle unified heap collections.

The dispatch for v8::JSMember on cppgc::Visitor is provided through a
specialization of TraceTrait.

Bug: chromium:1056170
Change-Id: I60d976ae66db3e5fa2e690a21627bdcb8c6871af
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2284488
Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68716}
2020-07-07 13:48:31 +00:00

54 lines
1.3 KiB
C++

// Copyright 2020 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 "include/cppgc/type-traits.h"
#include "include/v8-cppgc.h"
#include "include/v8.h"
#include "src/heap/cppgc/visitor.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
namespace {
class TestingVisitor : public JSVisitor {
public:
TestingVisitor() : JSVisitor(cppgc::internal::VisitorFactory::CreateKey()) {}
size_t found() const { return found_; }
void ExpectReference(const void* expected) { expected_ = expected; }
cppgc::Visitor& AsBaseVisitor() { return *this; }
protected:
void Visit(const JSMemberBase& ref) final {
if (&ref == expected_) {
found_++;
}
}
private:
size_t found_ = 0;
const void* expected_ = nullptr;
};
} // namespace
TEST(JSVisitorTest, DispatchJSMember) {
TestingVisitor visitor;
JSMember<v8::Value> js_value;
visitor.ExpectReference(&js_value);
visitor.AsBaseVisitor().Trace(js_value);
EXPECT_EQ(1u, visitor.found());
JSMember<v8::Function> js_function;
visitor.ExpectReference(&js_function);
visitor.AsBaseVisitor().Trace(js_function);
EXPECT_EQ(2u, visitor.found());
}
} // namespace internal
} // namespace v8