|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
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/. */ |
|
6 |
|
7 #include "vm/ProxyObject.h" |
|
8 |
|
9 #include "jscompartment.h" |
|
10 #include "jsgcinlines.h" |
|
11 #include "jsinferinlines.h" |
|
12 #include "jsobjinlines.h" |
|
13 |
|
14 using namespace js; |
|
15 |
|
16 /* static */ ProxyObject * |
|
17 ProxyObject::New(JSContext *cx, BaseProxyHandler *handler, HandleValue priv, TaggedProto proto_, |
|
18 JSObject *parent_, const ProxyOptions &options) |
|
19 { |
|
20 Rooted<TaggedProto> proto(cx, proto_); |
|
21 RootedObject parent(cx, parent_); |
|
22 |
|
23 const Class *clasp = options.clasp(); |
|
24 |
|
25 JS_ASSERT(isValidProxyClass(clasp)); |
|
26 JS_ASSERT_IF(proto.isObject(), cx->compartment() == proto.toObject()->compartment()); |
|
27 JS_ASSERT_IF(parent, cx->compartment() == parent->compartment()); |
|
28 |
|
29 /* |
|
30 * Eagerly mark properties unknown for proxies, so we don't try to track |
|
31 * their properties and so that we don't need to walk the compartment if |
|
32 * their prototype changes later. But don't do this for DOM proxies, |
|
33 * because we want to be able to keep track of them in typesets in useful |
|
34 * ways. |
|
35 */ |
|
36 if (proto.isObject() && !options.singleton() && !clasp->isDOMClass()) { |
|
37 RootedObject protoObj(cx, proto.toObject()); |
|
38 if (!JSObject::setNewTypeUnknown(cx, clasp, protoObj)) |
|
39 return nullptr; |
|
40 } |
|
41 |
|
42 NewObjectKind newKind = options.singleton() ? SingletonObject : GenericObject; |
|
43 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp); |
|
44 if (handler->finalizeInBackground(priv)) |
|
45 allocKind = GetBackgroundAllocKind(allocKind); |
|
46 RootedObject obj(cx, NewObjectWithGivenProto(cx, clasp, proto, parent, allocKind, newKind)); |
|
47 if (!obj) |
|
48 return nullptr; |
|
49 |
|
50 Rooted<ProxyObject*> proxy(cx, &obj->as<ProxyObject>()); |
|
51 proxy->initHandler(handler); |
|
52 proxy->initCrossCompartmentPrivate(priv); |
|
53 |
|
54 /* Don't track types of properties of non-DOM and non-singleton proxies. */ |
|
55 if (newKind != SingletonObject && !clasp->isDOMClass()) |
|
56 MarkTypeObjectUnknownProperties(cx, proxy->type()); |
|
57 |
|
58 return proxy; |
|
59 } |
|
60 |
|
61 void |
|
62 ProxyObject::initCrossCompartmentPrivate(HandleValue priv) |
|
63 { |
|
64 initCrossCompartmentSlot(PRIVATE_SLOT, priv); |
|
65 } |
|
66 |
|
67 void |
|
68 ProxyObject::initHandler(BaseProxyHandler *handler) |
|
69 { |
|
70 initSlot(HANDLER_SLOT, PrivateValue(handler)); |
|
71 } |
|
72 |
|
73 static void |
|
74 NukeSlot(ProxyObject *proxy, uint32_t slot) |
|
75 { |
|
76 Value old = proxy->getSlot(slot); |
|
77 if (old.isMarkable()) { |
|
78 Zone *zone = ZoneOfValue(old); |
|
79 AutoMarkInDeadZone amd(zone); |
|
80 proxy->setReservedSlot(slot, NullValue()); |
|
81 } else { |
|
82 proxy->setReservedSlot(slot, NullValue()); |
|
83 } |
|
84 } |
|
85 |
|
86 void |
|
87 ProxyObject::nuke(BaseProxyHandler *handler) |
|
88 { |
|
89 /* Allow people to add their own number of reserved slots beyond the expected 4 */ |
|
90 unsigned numSlots = JSCLASS_RESERVED_SLOTS(getClass()); |
|
91 for (unsigned i = 0; i < numSlots; i++) |
|
92 NukeSlot(this, i); |
|
93 /* Restore the handler as requested after nuking. */ |
|
94 setHandler(handler); |
|
95 } |