Bug 1708441 - Create js/public/RealmIterators.h and move some functions from jsapi.h in it. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D113792
This commit is contained in:
Nicolas B. Pierron 2021-04-30 16:30:45 +00:00
parent 4c5729b8ae
commit 0697482e20
4 changed files with 85 additions and 57 deletions

View File

@ -0,0 +1,81 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* 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/. */
/*
* Various interfaces to iterate over the Realms given various context such as
* principals, compartments and GC zones.
*/
#ifndef js_RealmIterators_h
#define js_RealmIterators_h
#include "js/GCAPI.h"
#include "js/TypeDecls.h"
struct JSPrincipals;
namespace JS {
class JS_PUBLIC_API AutoRequireNoGC;
using IterateRealmCallback = void (*)(JSContext* cx, void* data, Realm* realm,
const AutoRequireNoGC& nogc);
/**
* This function calls |realmCallback| on every realm. Beware that there is no
* guarantee that the realm will survive after the callback returns. Also,
* barriers are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void IterateRealms(JSContext* cx, void* data,
IterateRealmCallback realmCallback);
/**
* Like IterateRealms, but only call the callback for realms using |principals|.
*/
extern JS_PUBLIC_API void IterateRealmsWithPrincipals(
JSContext* cx, JSPrincipals* principals, void* data,
IterateRealmCallback realmCallback);
/**
* Like IterateRealms, but only iterates realms in |compartment|.
*/
extern JS_PUBLIC_API void IterateRealmsInCompartment(
JSContext* cx, JS::Compartment* compartment, void* data,
IterateRealmCallback realmCallback);
/**
* An enum that JSIterateCompartmentCallback can return to indicate
* whether to keep iterating.
*/
enum class CompartmentIterResult { KeepGoing, Stop };
} // namespace JS
using JSIterateCompartmentCallback =
JS::CompartmentIterResult (*)(JSContext*, void*, JS::Compartment*);
/**
* This function calls |compartmentCallback| on every compartment until either
* all compartments have been iterated or CompartmentIterResult::Stop is
* returned. Beware that there is no guarantee that the compartment will survive
* after the callback returns. Also, barriers are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void JS_IterateCompartments(
JSContext* cx, void* data,
JSIterateCompartmentCallback compartmentCallback);
/**
* This function calls |compartmentCallback| on every compartment in the given
* zone until either all compartments have been iterated or
* CompartmentIterResult::Stop is returned. Beware that there is no guarantee
* that the compartment will survive after the callback returns. Also, barriers
* are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void JS_IterateCompartmentsInZone(
JSContext* cx, JS::Zone* zone, void* data,
JSIterateCompartmentCallback compartmentCallback);
#endif /* js_RealmIterators_h */

View File

@ -34,6 +34,8 @@ struct JS_PUBLIC_API JSRuntime;
class JS_PUBLIC_API JSScript;
class JS_PUBLIC_API JSString;
struct JSPrincipals;
namespace js {
class JS_PUBLIC_API TempAllocPolicy;
}; // namespace js

View File

@ -42,6 +42,7 @@
#include "js/PropertyDescriptor.h"
#include "js/PropertySpec.h"
#include "js/Realm.h"
#include "js/RealmIterators.h"
#include "js/RealmOptions.h"
#include "js/RefCounted.h"
#include "js/RootingAPI.h"
@ -464,65 +465,8 @@ extern JS_PUBLIC_API JS::Realm* EnterRealm(JSContext* cx, JSObject* target);
extern JS_PUBLIC_API void LeaveRealm(JSContext* cx, JS::Realm* oldRealm);
using IterateRealmCallback = void (*)(JSContext* cx, void* data, Realm* realm,
const AutoRequireNoGC& nogc);
/**
* This function calls |realmCallback| on every realm. Beware that there is no
* guarantee that the realm will survive after the callback returns. Also,
* barriers are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void IterateRealms(JSContext* cx, void* data,
IterateRealmCallback realmCallback);
/**
* Like IterateRealms, but only call the callback for realms using |principals|.
*/
extern JS_PUBLIC_API void IterateRealmsWithPrincipals(
JSContext* cx, JSPrincipals* principals, void* data,
IterateRealmCallback realmCallback);
/**
* Like IterateRealms, but only iterates realms in |compartment|.
*/
extern JS_PUBLIC_API void IterateRealmsInCompartment(
JSContext* cx, JS::Compartment* compartment, void* data,
IterateRealmCallback realmCallback);
} // namespace JS
/**
* An enum that JSIterateCompartmentCallback can return to indicate
* whether to keep iterating.
*/
namespace JS {
enum class CompartmentIterResult { KeepGoing, Stop };
} // namespace JS
using JSIterateCompartmentCallback =
JS::CompartmentIterResult (*)(JSContext*, void*, JS::Compartment*);
/**
* This function calls |compartmentCallback| on every compartment until either
* all compartments have been iterated or CompartmentIterResult::Stop is
* returned. Beware that there is no guarantee that the compartment will survive
* after the callback returns. Also, barriers are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void JS_IterateCompartments(
JSContext* cx, void* data,
JSIterateCompartmentCallback compartmentCallback);
/**
* This function calls |compartmentCallback| on every compartment in the given
* zone until either all compartments have been iterated or
* CompartmentIterResult::Stop is returned. Beware that there is no guarantee
* that the compartment will survive after the callback returns. Also, barriers
* are disabled via the TraceSession.
*/
extern JS_PUBLIC_API void JS_IterateCompartmentsInZone(
JSContext* cx, JS::Zone* zone, void* data,
JSIterateCompartmentCallback compartmentCallback);
/**
* Mark a jsid after entering a new compartment. Different zones separately
* mark the ids in a runtime, and this must be used any time an id is obtained

View File

@ -177,6 +177,7 @@ EXPORTS.js += [
"../public/ProtoKey.h",
"../public/Proxy.h",
"../public/Realm.h",
"../public/RealmIterators.h",
"../public/RealmOptions.h",
"../public/RefCounted.h",
"../public/RegExp.h",