2016-07-09 07:19:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* 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 js_Realm_h
|
|
|
|
#define js_Realm_h
|
|
|
|
|
2017-06-29 19:57:46 +03:00
|
|
|
#include "jspubtd.h"
|
|
|
|
#include "js/GCPolicyAPI.h"
|
|
|
|
#include "js/TypeDecls.h" // forward-declaration of JS::Realm
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2017-06-29 19:57:46 +03:00
|
|
|
namespace js {
|
|
|
|
namespace gc {
|
2018-11-30 13:46:48 +03:00
|
|
|
JS_PUBLIC_API void TraceRealm(JSTracer* trc, JS::Realm* realm,
|
|
|
|
const char* name);
|
2018-11-19 20:02:47 +03:00
|
|
|
JS_PUBLIC_API bool RealmNeedsSweep(JS::Realm* realm);
|
2018-11-30 13:46:48 +03:00
|
|
|
} // namespace gc
|
|
|
|
} // namespace js
|
2016-07-09 07:19:50 +03:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2017-06-29 19:57:46 +03:00
|
|
|
// Each Realm holds a strong reference to its GlobalObject, and vice versa.
|
|
|
|
template <>
|
2018-11-30 13:46:48 +03:00
|
|
|
struct GCPolicy<Realm*> : public NonGCPointerPolicy<Realm*> {
|
|
|
|
static void trace(JSTracer* trc, Realm** vp, const char* name) {
|
|
|
|
if (*vp) {
|
|
|
|
::js::gc::TraceRealm(trc, *vp, name);
|
2017-06-29 19:57:46 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
static bool needsSweep(Realm** vp) {
|
|
|
|
return *vp && ::js::gc::RealmNeedsSweep(*vp);
|
|
|
|
}
|
2017-06-29 19:57:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get the current realm, if any. The ECMAScript spec calls this "the current
|
|
|
|
// Realm Record".
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API Realm* GetCurrentRealmOrNull(JSContext* cx);
|
2017-06-29 19:57:46 +03:00
|
|
|
|
2018-06-06 15:30:52 +03:00
|
|
|
namespace shadow {
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
class Realm {
|
|
|
|
protected:
|
|
|
|
JS::Compartment* compartment_;
|
2018-06-06 15:30:52 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
explicit Realm(JS::Compartment* comp) : compartment_(comp) {}
|
2018-06-06 15:30:52 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
|
|
|
JS::Compartment* compartment() { return compartment_; }
|
|
|
|
static shadow::Realm* get(JS::Realm* realm) {
|
|
|
|
return reinterpret_cast<shadow::Realm*>(realm);
|
|
|
|
}
|
2018-06-06 15:30:52 +03:00
|
|
|
};
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
}; // namespace shadow
|
2018-06-06 15:30:52 +03:00
|
|
|
|
2017-06-29 19:57:46 +03:00
|
|
|
// Return the compartment that contains a given realm.
|
2018-11-30 13:46:48 +03:00
|
|
|
inline JS::Compartment* GetCompartmentForRealm(Realm* realm) {
|
|
|
|
return shadow::Realm::get(realm)->compartment();
|
2017-06-29 19:57:46 +03:00
|
|
|
}
|
|
|
|
|
2017-06-28 22:31:53 +03:00
|
|
|
// Return an object's realm. All objects except cross-compartment wrappers are
|
|
|
|
// created in a particular realm, which never changes. Returns null if obj is
|
|
|
|
// a cross-compartment wrapper.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API Realm* GetObjectRealmOrNull(JSObject* obj);
|
2017-06-28 22:31:53 +03:00
|
|
|
|
2017-05-22 22:56:02 +03:00
|
|
|
// Get the value of the "private data" internal field of the given Realm.
|
|
|
|
// This field is initially null and is set using SetRealmPrivate.
|
|
|
|
// It's a pointer to embeddding-specific data that SpiderMonkey never uses.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API void* GetRealmPrivate(Realm* realm);
|
2017-05-22 22:56:02 +03:00
|
|
|
|
|
|
|
// Set the "private data" internal field of the given Realm.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API void SetRealmPrivate(Realm* realm, void* data);
|
2017-05-22 22:56:02 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
typedef void (*DestroyRealmCallback)(JSFreeOp* fop, Realm* realm);
|
2017-05-24 00:35:31 +03:00
|
|
|
|
|
|
|
// Set the callback SpiderMonkey calls just before garbage-collecting a realm.
|
|
|
|
// Embeddings can use this callback to free private data associated with the
|
|
|
|
// realm via SetRealmPrivate.
|
|
|
|
//
|
|
|
|
// By the time this is called, the global object for the realm has already been
|
|
|
|
// collected.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API void SetDestroyRealmCallback(
|
|
|
|
JSContext* cx, DestroyRealmCallback callback);
|
2017-05-24 00:35:31 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
typedef void (*RealmNameCallback)(JSContext* cx, Handle<Realm*> realm,
|
|
|
|
char* buf, size_t bufsize);
|
2017-05-24 00:35:31 +03:00
|
|
|
|
|
|
|
// Set the callback SpiderMonkey calls to get the name of a realm, for
|
|
|
|
// diagnostic output.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API void SetRealmNameCallback(JSContext* cx,
|
|
|
|
RealmNameCallback callback);
|
2017-05-24 00:35:31 +03:00
|
|
|
|
2018-05-31 13:53:26 +03:00
|
|
|
// Get the global object for the given realm. This only returns nullptr during
|
|
|
|
// GC, between collecting the global object and destroying the Realm.
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmGlobalOrNull(Handle<Realm*> realm);
|
2017-06-28 22:31:53 +03:00
|
|
|
|
2018-06-13 22:47:47 +03:00
|
|
|
// Initialize standard JS class constructors, prototypes, and any top-level
|
|
|
|
// functions and constants associated with the standard classes (e.g. isNaN
|
|
|
|
// for Number).
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API bool InitRealmStandardClasses(JSContext* cx);
|
2018-06-13 22:47:47 +03:00
|
|
|
|
2018-06-13 22:47:40 +03:00
|
|
|
/*
|
|
|
|
* Ways to get various per-Realm objects. All the getters declared below operate
|
|
|
|
* on the JSContext's current Realm.
|
|
|
|
*/
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmObjectPrototype(JSContext* cx);
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmFunctionPrototype(JSContext* cx);
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmArrayPrototype(JSContext* cx);
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmErrorPrototype(JSContext* cx);
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
extern JS_PUBLIC_API JSObject* GetRealmIteratorPrototype(JSContext* cx);
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
} // namespace JS
|
2016-07-09 07:19:50 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
#endif // js_Realm_h
|