1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/* -*- 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/. */
/*
* Window and WindowProxy.
*
* For silly obscure reasons embedders are better off not knowing, the web wants
* every global object to exist as two linked components: a Window component
* that stores global variables and appears in environment chains but can't be
* directly referred to by any script, and a WindowProxy component that
* intermediates access to its Window that *can* be directly referred to by
* script. (Thus the global |window| and |globalThis| properties, |this| in
* global code, the value of |(function() { return this; })()| in non-strict
* mode code, and similar values are WindowProxy objects, not Windows.)
*
* Maintaining an invariant of never exposing a Window to script requires
* substituting in its WindowProxy in a variety of apparently arbitrary (but
* actually *very* carefully and nervously selected) places throughout the
* engine and indeed the universe.
*
* This header defines functions that let embeddings convert from a WindowProxy
* to its Window and vice versa.
*
* If you're not embedding SpiderMonkey in a web browser, you can almost
* certainly ignore this header.
*/
#ifndef js_friend_WindowProxy_h
#define js_friend_WindowProxy_h
#include "jstypes.h" // JS_PUBLIC_API
#include "js/Class.h" // JSCLASS_IS_GLOBAL
#include "js/Object.h" // JS::GetClass
#include "js/RootingAPI.h" // JS::Handle
struct JS_PUBLIC_API JSContext;
class JS_PUBLIC_API JSObject;
namespace js {
/**
* Tell the JS engine which Class is used for WindowProxy objects. Used by the
* functions below.
*/
extern JS_PUBLIC_API void SetWindowProxyClass(JSContext* cx,
const JSClass* clasp);
/**
* Associates a WindowProxy with a Window (global object). `windowProxy` must
* have the Class set by SetWindowProxyClass.
*/
extern JS_PUBLIC_API void SetWindowProxy(JSContext* cx,
JS::Handle<JSObject*> global,
JS::Handle<JSObject*> windowProxy);
namespace detail {
extern JS_PUBLIC_API bool IsWindowSlow(JSObject* obj);
extern JS_PUBLIC_API JSObject* ToWindowProxyIfWindowSlow(JSObject* obj);
} // namespace detail
/**
* Returns true iff `obj` is a global object with an associated WindowProxy,
* see SetWindowProxy.
*/
inline bool IsWindow(JSObject* obj) {
if (JS::GetClass(obj)->flags & JSCLASS_IS_GLOBAL) {
return detail::IsWindowSlow(obj);
}
return false;
}
/**
* Returns true iff `obj` has the WindowProxy Class (see SetWindowProxyClass).
*/
extern JS_PUBLIC_API bool IsWindowProxy(JSObject* obj);
/**
* If `obj` is a Window, get its associated WindowProxy (or a CCW or dead
* wrapper if the page was navigated away from), else return `obj`. This
* function is infallible and never returns nullptr.
*/
MOZ_ALWAYS_INLINE JSObject* ToWindowProxyIfWindow(JSObject* obj) {
if (JS::GetClass(obj)->flags & JSCLASS_IS_GLOBAL) {
return detail::ToWindowProxyIfWindowSlow(obj);
}
return obj;
}
/**
* If `obj` is a WindowProxy, get its associated Window (the compartment's
* global), else return `obj`. This function is infallible and never returns
* nullptr.
*/
extern JS_PUBLIC_API JSObject* ToWindowIfWindowProxy(JSObject* obj);
} // namespace js
#endif // js_friend_WindowProxy_h
|