11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
61cb0ef41Sopenharmony_ci#define V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "src/heap/cppgc-js/cpp-heap.h"
111cb0ef41Sopenharmony_ci#include "src/heap/cppgc/marking-state.h"
121cb0ef41Sopenharmony_ci#include "src/heap/cppgc/marking-worklists.h"
131cb0ef41Sopenharmony_ci#include "src/objects/embedder-data-slot.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciclass JSObject;
191cb0ef41Sopenharmony_ciclass EmbedderDataSlot;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciclass CppMarkingState {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  using EmbedderDataSnapshot =
241cb0ef41Sopenharmony_ci      std::pair<EmbedderDataSlot::EmbedderDataSlotSnapshot,
251cb0ef41Sopenharmony_ci                EmbedderDataSlot::EmbedderDataSlotSnapshot>;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
281cb0ef41Sopenharmony_ci                  cppgc::internal::MarkingStateBase& main_thread_marking_state)
291cb0ef41Sopenharmony_ci      : isolate_(isolate),
301cb0ef41Sopenharmony_ci        wrapper_descriptor_(wrapper_descriptor),
311cb0ef41Sopenharmony_ci        owned_marking_state_(nullptr),
321cb0ef41Sopenharmony_ci        marking_state_(main_thread_marking_state) {}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
351cb0ef41Sopenharmony_ci                  std::unique_ptr<cppgc::internal::MarkingStateBase>
361cb0ef41Sopenharmony_ci                      concurrent_marking_state)
371cb0ef41Sopenharmony_ci      : isolate_(isolate),
381cb0ef41Sopenharmony_ci        wrapper_descriptor_(wrapper_descriptor),
391cb0ef41Sopenharmony_ci        owned_marking_state_(std::move(concurrent_marking_state)),
401cb0ef41Sopenharmony_ci        marking_state_(*owned_marking_state_) {}
411cb0ef41Sopenharmony_ci  CppMarkingState(const CppMarkingState&) = delete;
421cb0ef41Sopenharmony_ci  CppMarkingState& operator=(const CppMarkingState&) = delete;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  void Publish() { marking_state_.Publish(); }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  inline bool ExtractEmbedderDataSnapshot(Map, JSObject, EmbedderDataSnapshot&);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  inline void MarkAndPush(const EmbedderDataSnapshot&);
491cb0ef41Sopenharmony_ci  inline void MarkAndPush(const EmbedderDataSlot type_slot,
501cb0ef41Sopenharmony_ci                          const EmbedderDataSlot instance_slot);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  bool IsLocalEmpty() {
531cb0ef41Sopenharmony_ci    return marking_state_.marking_worklist().IsLocalEmpty();
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci private:
571cb0ef41Sopenharmony_ci  Isolate* const isolate_;
581cb0ef41Sopenharmony_ci  const WrapperDescriptor& wrapper_descriptor_;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  std::unique_ptr<cppgc::internal::MarkingStateBase> owned_marking_state_;
611cb0ef41Sopenharmony_ci  cppgc::internal::MarkingStateBase& marking_state_;
621cb0ef41Sopenharmony_ci};
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci}  // namespace internal
651cb0ef41Sopenharmony_ci}  // namespace v8
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#endif  // V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
68