[SM91] Update to Spidermonkey 91.1.3 APIs
[0ad.git] / libraries / source / spidermonkey / include-win32-debug / js / Wrapper.h
blob807e9e4d69a466e3c228e1aa73125e6c924dfb74
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_Wrapper_h
8 #define js_Wrapper_h
10 #include "mozilla/Attributes.h"
12 #include "js/Proxy.h"
14 namespace js {
15 struct CompartmentFilter;
18 * Helper for Wrapper::New default options.
20 * Callers of Wrapper::New() who wish to specify a prototype for the created
21 * Wrapper, *MUST* construct a WrapperOptions with a JSContext.
23 class MOZ_STACK_CLASS WrapperOptions : public ProxyOptions {
24 public:
25 WrapperOptions() : ProxyOptions(false), proto_() {}
27 explicit WrapperOptions(JSContext* cx) : ProxyOptions(false), proto_() {
28 proto_.emplace(cx);
31 inline JSObject* proto() const;
32 WrapperOptions& setProto(JSObject* protoArg) {
33 MOZ_ASSERT(proto_);
34 *proto_ = protoArg;
35 return *this;
38 private:
39 mozilla::Maybe<JS::RootedObject> proto_;
42 // Base class for proxy handlers that want to forward all operations to an
43 // object stored in the proxy's private slot.
44 class JS_PUBLIC_API ForwardingProxyHandler : public BaseProxyHandler {
45 public:
46 using BaseProxyHandler::BaseProxyHandler;
48 /* Standard internal methods. */
49 virtual bool getOwnPropertyDescriptor(
50 JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
51 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)
52 const override;
53 virtual bool defineProperty(JSContext* cx, JS::HandleObject proxy,
54 JS::HandleId id,
55 JS::Handle<JS::PropertyDescriptor> desc,
56 JS::ObjectOpResult& result) const override;
57 virtual bool ownPropertyKeys(JSContext* cx, JS::HandleObject proxy,
58 JS::MutableHandleIdVector props) const override;
59 virtual bool delete_(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
60 JS::ObjectOpResult& result) const override;
61 virtual bool enumerate(JSContext* cx, JS::HandleObject proxy,
62 JS::MutableHandleIdVector props) const override;
63 virtual bool getPrototype(JSContext* cx, JS::HandleObject proxy,
64 JS::MutableHandleObject protop) const override;
65 virtual bool setPrototype(JSContext* cx, JS::HandleObject proxy,
66 JS::HandleObject proto,
67 JS::ObjectOpResult& result) const override;
68 virtual bool getPrototypeIfOrdinary(
69 JSContext* cx, JS::HandleObject proxy, bool* isOrdinary,
70 JS::MutableHandleObject protop) const override;
71 virtual bool setImmutablePrototype(JSContext* cx, JS::HandleObject proxy,
72 bool* succeeded) const override;
73 virtual bool preventExtensions(JSContext* cx, JS::HandleObject proxy,
74 JS::ObjectOpResult& result) const override;
75 virtual bool isExtensible(JSContext* cx, JS::HandleObject proxy,
76 bool* extensible) const override;
77 virtual bool has(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
78 bool* bp) const override;
79 virtual bool get(JSContext* cx, JS::HandleObject proxy,
80 JS::HandleValue receiver, JS::HandleId id,
81 JS::MutableHandleValue vp) const override;
82 virtual bool set(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
83 JS::HandleValue v, JS::HandleValue receiver,
84 JS::ObjectOpResult& result) const override;
85 virtual bool call(JSContext* cx, JS::HandleObject proxy,
86 const JS::CallArgs& args) const override;
87 virtual bool construct(JSContext* cx, JS::HandleObject proxy,
88 const JS::CallArgs& args) const override;
90 /* SpiderMonkey extensions. */
91 virtual bool hasOwn(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
92 bool* bp) const override;
93 virtual bool getOwnEnumerablePropertyKeys(
94 JSContext* cx, JS::HandleObject proxy,
95 JS::MutableHandleIdVector props) const override;
96 virtual bool nativeCall(JSContext* cx, JS::IsAcceptableThis test,
97 JS::NativeImpl impl,
98 const JS::CallArgs& args) const override;
99 virtual bool hasInstance(JSContext* cx, JS::HandleObject proxy,
100 JS::MutableHandleValue v, bool* bp) const override;
101 virtual bool getBuiltinClass(JSContext* cx, JS::HandleObject proxy,
102 ESClass* cls) const override;
103 virtual bool isArray(JSContext* cx, JS::HandleObject proxy,
104 JS::IsArrayAnswer* answer) const override;
105 virtual const char* className(JSContext* cx,
106 JS::HandleObject proxy) const override;
107 virtual JSString* fun_toString(JSContext* cx, JS::HandleObject proxy,
108 bool isToSource) const override;
109 virtual RegExpShared* regexp_toShared(JSContext* cx,
110 JS::HandleObject proxy) const override;
111 virtual bool boxedValue_unbox(JSContext* cx, JS::HandleObject proxy,
112 JS::MutableHandleValue vp) const override;
113 virtual bool isCallable(JSObject* obj) const override;
114 virtual bool isConstructor(JSObject* obj) const override;
116 // Use the target object for private fields.
117 virtual bool useProxyExpandoObjectForPrivateFields() const override {
118 return false;
123 * A wrapper is a proxy with a target object to which it generally forwards
124 * operations, but may restrict access to certain operations or augment those
125 * operations in various ways.
127 * A wrapper can be "unwrapped" in C++, exposing the underlying object.
128 * Callers should be careful to avoid unwrapping security wrappers in the wrong
129 * context.
131 * Important: If you add a method implementation here, you probably also need
132 * to add an override in CrossCompartmentWrapper. If you don't, you risk
133 * compartment mismatches. See bug 945826 comment 0.
135 class JS_PUBLIC_API Wrapper : public ForwardingProxyHandler {
136 unsigned mFlags;
138 public:
139 explicit constexpr Wrapper(unsigned aFlags, bool aHasPrototype = false,
140 bool aHasSecurityPolicy = false)
141 : ForwardingProxyHandler(&family, aHasPrototype, aHasSecurityPolicy),
142 mFlags(aFlags) {}
144 virtual bool finalizeInBackground(const JS::Value& priv) const override;
147 * A hook subclasses can override to implement CheckedUnwrapDynamic
148 * behavior. The JSContext represents the "who is trying to unwrap?" Realm.
149 * The JSObject is the wrapper that the caller is trying to unwrap.
151 virtual bool dynamicCheckedUnwrapAllowed(JS::HandleObject obj,
152 JSContext* cx) const {
153 MOZ_ASSERT(hasSecurityPolicy(), "Why are you asking?");
154 return false;
157 using BaseProxyHandler::Action;
159 enum Flags { CROSS_COMPARTMENT = 1 << 0, LAST_USED_FLAG = CROSS_COMPARTMENT };
161 static JSObject* New(JSContext* cx, JSObject* obj, const Wrapper* handler,
162 const WrapperOptions& options = WrapperOptions());
164 static JSObject* Renew(JSObject* existing, JSObject* obj,
165 const Wrapper* handler);
167 static inline const Wrapper* wrapperHandler(const JSObject* wrapper);
169 static JSObject* wrappedObject(JSObject* wrapper);
171 unsigned flags() const { return mFlags; }
173 bool isCrossCompartmentWrapper() const {
174 return !!(mFlags & CROSS_COMPARTMENT);
177 static const char family;
178 static const Wrapper singleton;
179 static const Wrapper singletonWithPrototype;
181 static JSObject* const defaultProto;
184 inline JSObject* WrapperOptions::proto() const {
185 return proto_ ? *proto_ : Wrapper::defaultProto;
188 /* Base class for all cross compartment wrapper handlers. */
189 class JS_PUBLIC_API CrossCompartmentWrapper : public Wrapper {
190 public:
191 explicit constexpr CrossCompartmentWrapper(unsigned aFlags,
192 bool aHasPrototype = false,
193 bool aHasSecurityPolicy = false)
194 : Wrapper(CROSS_COMPARTMENT | aFlags, aHasPrototype, aHasSecurityPolicy) {
197 /* Standard internal methods. */
198 virtual bool getOwnPropertyDescriptor(
199 JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
200 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)
201 const override;
202 virtual bool defineProperty(JSContext* cx, JS::HandleObject wrapper,
203 JS::HandleId id,
204 JS::Handle<JS::PropertyDescriptor> desc,
205 JS::ObjectOpResult& result) const override;
206 virtual bool ownPropertyKeys(JSContext* cx, JS::HandleObject wrapper,
207 JS::MutableHandleIdVector props) const override;
208 virtual bool delete_(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
209 JS::ObjectOpResult& result) const override;
210 virtual bool enumerate(JSContext* cx, JS::HandleObject proxy,
211 JS::MutableHandleIdVector props) const override;
212 virtual bool getPrototype(JSContext* cx, JS::HandleObject proxy,
213 JS::MutableHandleObject protop) const override;
214 virtual bool setPrototype(JSContext* cx, JS::HandleObject proxy,
215 JS::HandleObject proto,
216 JS::ObjectOpResult& result) const override;
218 virtual bool getPrototypeIfOrdinary(
219 JSContext* cx, JS::HandleObject proxy, bool* isOrdinary,
220 JS::MutableHandleObject protop) const override;
221 virtual bool setImmutablePrototype(JSContext* cx, JS::HandleObject proxy,
222 bool* succeeded) const override;
223 virtual bool preventExtensions(JSContext* cx, JS::HandleObject wrapper,
224 JS::ObjectOpResult& result) const override;
225 virtual bool isExtensible(JSContext* cx, JS::HandleObject wrapper,
226 bool* extensible) const override;
227 virtual bool has(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
228 bool* bp) const override;
229 virtual bool get(JSContext* cx, JS::HandleObject wrapper,
230 JS::HandleValue receiver, JS::HandleId id,
231 JS::MutableHandleValue vp) const override;
232 virtual bool set(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
233 JS::HandleValue v, JS::HandleValue receiver,
234 JS::ObjectOpResult& result) const override;
235 virtual bool call(JSContext* cx, JS::HandleObject wrapper,
236 const JS::CallArgs& args) const override;
237 virtual bool construct(JSContext* cx, JS::HandleObject wrapper,
238 const JS::CallArgs& args) const override;
240 /* SpiderMonkey extensions. */
241 virtual bool hasOwn(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
242 bool* bp) const override;
243 virtual bool getOwnEnumerablePropertyKeys(
244 JSContext* cx, JS::HandleObject wrapper,
245 JS::MutableHandleIdVector props) const override;
246 virtual bool nativeCall(JSContext* cx, JS::IsAcceptableThis test,
247 JS::NativeImpl impl,
248 const JS::CallArgs& args) const override;
249 virtual bool hasInstance(JSContext* cx, JS::HandleObject wrapper,
250 JS::MutableHandleValue v, bool* bp) const override;
251 virtual const char* className(JSContext* cx,
252 JS::HandleObject proxy) const override;
253 virtual JSString* fun_toString(JSContext* cx, JS::HandleObject wrapper,
254 bool isToSource) const override;
255 virtual RegExpShared* regexp_toShared(JSContext* cx,
256 JS::HandleObject proxy) const override;
257 virtual bool boxedValue_unbox(JSContext* cx, JS::HandleObject proxy,
258 JS::MutableHandleValue vp) const override;
260 // Allocate CrossCompartmentWrappers in the nursery.
261 virtual bool canNurseryAllocate() const override { return true; }
263 static const CrossCompartmentWrapper singleton;
264 static const CrossCompartmentWrapper singletonWithPrototype;
267 class JS_PUBLIC_API OpaqueCrossCompartmentWrapper
268 : public CrossCompartmentWrapper {
269 public:
270 explicit constexpr OpaqueCrossCompartmentWrapper()
271 : CrossCompartmentWrapper(0) {}
273 /* Standard internal methods. */
274 virtual bool getOwnPropertyDescriptor(
275 JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
276 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)
277 const override;
278 virtual bool defineProperty(JSContext* cx, JS::HandleObject wrapper,
279 JS::HandleId id,
280 JS::Handle<JS::PropertyDescriptor> desc,
281 JS::ObjectOpResult& result) const override;
282 virtual bool ownPropertyKeys(JSContext* cx, JS::HandleObject wrapper,
283 JS::MutableHandleIdVector props) const override;
284 virtual bool delete_(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
285 JS::ObjectOpResult& result) const override;
286 virtual bool enumerate(JSContext* cx, JS::HandleObject proxy,
287 JS::MutableHandleIdVector props) const override;
288 virtual bool getPrototype(JSContext* cx, JS::HandleObject wrapper,
289 JS::MutableHandleObject protop) const override;
290 virtual bool setPrototype(JSContext* cx, JS::HandleObject wrapper,
291 JS::HandleObject proto,
292 JS::ObjectOpResult& result) const override;
293 virtual bool getPrototypeIfOrdinary(
294 JSContext* cx, JS::HandleObject wrapper, bool* isOrdinary,
295 JS::MutableHandleObject protop) const override;
296 virtual bool setImmutablePrototype(JSContext* cx, JS::HandleObject wrapper,
297 bool* succeeded) const override;
298 virtual bool preventExtensions(JSContext* cx, JS::HandleObject wrapper,
299 JS::ObjectOpResult& result) const override;
300 virtual bool isExtensible(JSContext* cx, JS::HandleObject wrapper,
301 bool* extensible) const override;
302 virtual bool has(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
303 bool* bp) const override;
304 virtual bool get(JSContext* cx, JS::HandleObject wrapper,
305 JS::HandleValue receiver, JS::HandleId id,
306 JS::MutableHandleValue vp) const override;
307 virtual bool set(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
308 JS::HandleValue v, JS::HandleValue receiver,
309 JS::ObjectOpResult& result) const override;
310 virtual bool call(JSContext* cx, JS::HandleObject wrapper,
311 const JS::CallArgs& args) const override;
312 virtual bool construct(JSContext* cx, JS::HandleObject wrapper,
313 const JS::CallArgs& args) const override;
315 /* SpiderMonkey extensions. */
316 virtual bool hasOwn(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
317 bool* bp) const override;
318 virtual bool getOwnEnumerablePropertyKeys(
319 JSContext* cx, JS::HandleObject wrapper,
320 JS::MutableHandleIdVector props) const override;
321 virtual bool getBuiltinClass(JSContext* cx, JS::HandleObject wrapper,
322 ESClass* cls) const override;
323 virtual bool isArray(JSContext* cx, JS::HandleObject obj,
324 JS::IsArrayAnswer* answer) const override;
325 virtual bool hasInstance(JSContext* cx, JS::HandleObject wrapper,
326 JS::MutableHandleValue v, bool* bp) const override;
327 virtual const char* className(JSContext* cx,
328 JS::HandleObject wrapper) const override;
329 virtual JSString* fun_toString(JSContext* cx, JS::HandleObject proxy,
330 bool isToSource) const override;
332 static const OpaqueCrossCompartmentWrapper singleton;
336 * Base class for security wrappers. A security wrapper is potentially hiding
337 * all or part of some wrapped object thus SecurityWrapper defaults to denying
338 * access to the wrappee. This is the opposite of Wrapper which tries to be
339 * completely transparent.
341 * NB: Currently, only a few ProxyHandler operations are overridden to deny
342 * access, relying on derived SecurityWrapper to block access when necessary.
344 template <class Base>
345 class JS_PUBLIC_API SecurityWrapper : public Base {
346 public:
347 explicit constexpr SecurityWrapper(unsigned flags, bool hasPrototype = false)
348 : Base(flags, hasPrototype, /* hasSecurityPolicy = */ true) {}
350 virtual bool enter(JSContext* cx, JS::HandleObject wrapper, JS::HandleId id,
351 Wrapper::Action act, bool mayThrow,
352 bool* bp) const override;
354 virtual bool defineProperty(JSContext* cx, JS::HandleObject wrapper,
355 JS::HandleId id,
356 JS::Handle<JS::PropertyDescriptor> desc,
357 JS::ObjectOpResult& result) const override;
358 virtual bool isExtensible(JSContext* cx, JS::HandleObject wrapper,
359 bool* extensible) const override;
360 virtual bool preventExtensions(JSContext* cx, JS::HandleObject wrapper,
361 JS::ObjectOpResult& result) const override;
362 virtual bool setPrototype(JSContext* cx, JS::HandleObject proxy,
363 JS::HandleObject proto,
364 JS::ObjectOpResult& result) const override;
365 virtual bool setImmutablePrototype(JSContext* cx, JS::HandleObject proxy,
366 bool* succeeded) const override;
368 virtual bool nativeCall(JSContext* cx, JS::IsAcceptableThis test,
369 JS::NativeImpl impl,
370 const JS::CallArgs& args) const override;
371 virtual bool getBuiltinClass(JSContext* cx, JS::HandleObject wrapper,
372 ESClass* cls) const override;
373 virtual bool isArray(JSContext* cx, JS::HandleObject wrapper,
374 JS::IsArrayAnswer* answer) const override;
375 virtual RegExpShared* regexp_toShared(JSContext* cx,
376 JS::HandleObject proxy) const override;
377 virtual bool boxedValue_unbox(JSContext* cx, JS::HandleObject proxy,
378 JS::MutableHandleValue vp) const override;
380 // Allow isCallable and isConstructor. They used to be class-level, and so
381 // could not be guarded against.
384 * Allow our subclasses to select the superclass behavior they want without
385 * needing to specify an exact superclass.
387 typedef Base Permissive;
388 typedef SecurityWrapper<Base> Restrictive;
391 typedef SecurityWrapper<CrossCompartmentWrapper>
392 CrossCompartmentSecurityWrapper;
394 extern JSObject* TransparentObjectWrapper(JSContext* cx,
395 JS::HandleObject existing,
396 JS::HandleObject obj);
398 inline bool IsWrapper(const JSObject* obj) {
399 return IsProxy(obj) && GetProxyHandler(obj)->family() == &Wrapper::family;
402 inline bool IsCrossCompartmentWrapper(const JSObject* obj) {
403 return IsWrapper(obj) &&
404 (Wrapper::wrapperHandler(obj)->flags() & Wrapper::CROSS_COMPARTMENT);
407 /* static */ inline const Wrapper* Wrapper::wrapperHandler(
408 const JSObject* wrapper) {
409 MOZ_ASSERT(IsWrapper(wrapper));
410 return static_cast<const Wrapper*>(GetProxyHandler(wrapper));
413 // Given a JSObject, returns that object stripped of wrappers. If
414 // stopAtWindowProxy is true, then this returns the WindowProxy if it was
415 // previously wrapped. Otherwise, this returns the first object for which
416 // JSObject::isWrapper returns false.
418 // ExposeToActiveJS is called on wrapper targets to allow gray marking
419 // assertions to work while an incremental GC is in progress, but this means
420 // that this cannot be called from the GC or off the main thread.
421 JS_PUBLIC_API JSObject* UncheckedUnwrap(JSObject* obj,
422 bool stopAtWindowProxy = true,
423 unsigned* flagsp = nullptr);
425 // Given a JSObject, returns that object stripped of wrappers, except
426 // WindowProxy wrappers. At each stage, the wrapper has the opportunity to veto
427 // the unwrap. Null is returned if there are security wrappers that can't be
428 // unwrapped.
430 // This does a static-only unwrap check: it basically checks whether _all_
431 // globals in the wrapper's source compartment should be able to access the
432 // wrapper target. This won't necessarily return the right thing for the HTML
433 // spec's cross-origin objects (WindowProxy and Location), but is fine to use
434 // when failure to unwrap one of those objects wouldn't be a problem. For
435 // example, if you want to test whether your target object is a specific class
436 // that's not WindowProxy or Location, you can use this.
438 // ExposeToActiveJS is called on wrapper targets to allow gray marking
439 // assertions to work while an incremental GC is in progress, but this means
440 // that this cannot be called from the GC or off the main thread.
441 JS_PUBLIC_API JSObject* CheckedUnwrapStatic(JSObject* obj);
443 // Unwrap only the outermost security wrapper, with the same semantics as
444 // above. This is the checked version of Wrapper::wrappedObject.
445 JS_PUBLIC_API JSObject* UnwrapOneCheckedStatic(JSObject* obj);
447 // Given a JSObject, returns that object stripped of wrappers. At each stage,
448 // the security wrapper has the opportunity to veto the unwrap. If
449 // stopAtWindowProxy is true, then this returns the WindowProxy if it was
450 // previously wrapped. Null is returned if there are security wrappers that
451 // can't be unwrapped.
453 // ExposeToActiveJS is called on wrapper targets to allow gray marking
454 // assertions to work while an incremental GC is in progress, but this means
455 // that this cannot be called from the GC or off the main thread.
457 // The JSContext argument will be used for dynamic checks (needed by WindowProxy
458 // and Location) and should represent the Realm doing the unwrapping. It is not
459 // used to throw exceptions; this function never throws.
461 // This function may be able to GC (and the static analysis definitely thinks it
462 // can), but it still takes a JSObject* argument, because some of its callers
463 // would actually have a bit of a hard time producing a Rooted. And it ends up
464 // having to root internally anyway, because it wants to use the value in a loop
465 // and you can't assign to a HandleObject. What this means is that callers who
466 // plan to use the argument object after they have called this function will
467 // need to root it to avoid hazard failures, even though this function doesn't
468 // require a Handle.
469 JS_PUBLIC_API JSObject* CheckedUnwrapDynamic(JSObject* obj, JSContext* cx,
470 bool stopAtWindowProxy = true);
472 // Unwrap only the outermost security wrapper, with the same semantics as
473 // above. This is the checked version of Wrapper::wrappedObject.
474 JS_PUBLIC_API JSObject* UnwrapOneCheckedDynamic(JS::HandleObject obj,
475 JSContext* cx,
476 bool stopAtWindowProxy = true);
478 // Given a JSObject, returns that object stripped of wrappers. This returns the
479 // WindowProxy if it was previously wrapped.
481 // ExposeToActiveJS is not called on wrapper targets so this can be called from
482 // the GC or off the main thread.
483 JS_PUBLIC_API JSObject* UncheckedUnwrapWithoutExpose(JSObject* obj);
485 void ReportAccessDenied(JSContext* cx);
487 JS_PUBLIC_API void NukeCrossCompartmentWrapper(JSContext* cx,
488 JSObject* wrapper);
490 // If a cross-compartment wrapper source => target exists, nuke it.
491 JS_PUBLIC_API void NukeCrossCompartmentWrapperIfExists(JSContext* cx,
492 JS::Compartment* source,
493 JSObject* target);
495 void RemapWrapper(JSContext* cx, JSObject* wobj, JSObject* newTarget);
496 void RemapDeadWrapper(JSContext* cx, JS::HandleObject wobj,
497 JS::HandleObject newTarget);
499 JS_PUBLIC_API bool RemapAllWrappersForObject(JSContext* cx,
500 JS::HandleObject oldTarget,
501 JS::HandleObject newTarget);
503 // API to recompute all cross-compartment wrappers whose source and target
504 // match the given filters.
505 JS_PUBLIC_API bool RecomputeWrappers(JSContext* cx,
506 const CompartmentFilter& sourceFilter,
507 const CompartmentFilter& targetFilter);
509 } /* namespace js */
511 #endif /* js_Wrapper_h */