/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_InvalidatingFuse_h
#define vm_InvalidatingFuse_h

#include "gc/Barrier.h"
#include "jit/InvalidationScriptSet.h"
#include "js/SweepingAPI.h"

#include "vm/GuardFuse.h"
class JSScript;

namespace js {

// [SMDOC] Invalidating Fuses
//
// An invalidating fuse will invalidate a set of dependent IonScripts when the
// fuse is popped. In this way Ion can choose to ignore fuse guarded
// possibilities when doing compilation.
class InvalidatingFuse : public GuardFuse {
 public:
  // Register a script's IonScript as having a dependency on this fuse.
  virtual bool addFuseDependency(JSContext* cx,
                                 const jit::IonScriptKey& ionScript) = 0;
};

// [SMDOC] Invalidating Runtime Fuses
//
// A specialized sublass for handling runtime wide fuses. This provides a
// version of addFuseDependency which records scripts into sets associated with
// their home zone, and invalidates all sets across all zones linked to this
// specific fuse.
class InvalidatingRuntimeFuse : public InvalidatingFuse {
 public:
  virtual bool addFuseDependency(JSContext* cx,
                                 const jit::IonScriptKey& ionScript) override;
  virtual void popFuse(JSContext* cx) override;
};

// A (weak) set of scripts which are dependent on an associated fuse.
//
// Because it uses JS::WeakCache, GC tracing is taken care of without any need
// for tracing in this class.
class FuseDependentIonScriptSet {
 public:
  FuseDependentIonScriptSet(JSContext* cx, InvalidatingFuse* fuse);

  InvalidatingFuse* associatedFuse;
  bool addScriptForFuse(InvalidatingFuse* fuse,
                        const jit::IonScriptKey& ionScript);
  void invalidateForFuse(JSContext* cx, InvalidatingFuse* fuse);

 private:
  JS::WeakCache<js::jit::DependentIonScriptSet> ionScripts;
};

class DependentIonScriptGroup {
  // A dependent script set pairs a fuse with a set of scripts which depend
  // on said fuse; this is a vector of script sets because the expectation for
  // now is that the number of runtime wide invalidating fuses will be small.
  // This will need to be revisited (convert to HashMap?) should that no
  // longer be the case
  //
  // Note: This isn't  traced through the zone, but rather through the use
  // of JS::WeakCache.
  Vector<FuseDependentIonScriptSet, 1, SystemAllocPolicy> dependencies;

 public:
  FuseDependentIonScriptSet* getOrCreateDependentScriptSet(
      JSContext* cx, InvalidatingFuse* fuse);
  FuseDependentIonScriptSet* begin() { return dependencies.begin(); }
  FuseDependentIonScriptSet* end() { return dependencies.end(); }
};

}  // namespace js

#endif  // vm_InvalidatingFuse_h
