summaryrefslogtreecommitdiffstats
path: root/js/src/proxy/BaseProxyHandler.cpp
blob: 1dc4e03664e01b65bdc486a364c61a2a0488ae0a (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* -*- 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/. */

#include "jsapi.h"
#include "NamespaceImports.h"

#include "gc/GC.h"
#include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_*
#include "js/Proxy.h"
#include "proxy/DeadObjectProxy.h"
#include "vm/Interpreter.h"
#include "vm/ProxyObject.h"
#include "vm/WellKnownAtom.h"  // js_*_str
#include "vm/WrapperObject.h"

#include "vm/JSContext-inl.h"
#include "vm/JSObject-inl.h"

using namespace js;

using JS::IsArrayAnswer;

bool BaseProxyHandler::enter(JSContext* cx, HandleObject wrapper, HandleId id,
                             Action act, bool mayThrow, bool* bp) const {
  *bp = true;
  return true;
}

bool BaseProxyHandler::has(JSContext* cx, HandleObject proxy, HandleId id,
                           bool* bp) const {
  assertEnteredPolicy(cx, proxy, id, GET);

  // This method is not covered by any spec, but we follow ES 2016
  // (February 11, 2016) 9.1.7.1 fairly closely.

  // Step 2. (Step 1 is a superfluous assertion.)
  // Non-standard: Use our faster hasOwn trap.
  if (!hasOwn(cx, proxy, id, bp)) {
    return false;
  }

  // Step 3.
  if (*bp) {
    return true;
  }

  // The spec calls this variable "parent", but that word has weird
  // connotations in SpiderMonkey, so let's go with "proto".
  // Step 4.
  RootedObject proto(cx);
  if (!GetPrototype(cx, proxy, &proto)) {
    return false;
  }

  // Step 5.,5.a.
  if (proto) {
    return HasProperty(cx, proto, id, bp);
  }

  // Step 6.
  *bp = false;
  return true;
}

bool BaseProxyHandler::hasOwn(JSContext* cx, HandleObject proxy, HandleId id,
                              bool* bp) const {
  assertEnteredPolicy(cx, proxy, id, GET);
  Rooted<mozilla::Maybe<PropertyDescriptor>> desc(cx);
  if (!getOwnPropertyDescriptor(cx, proxy, id, &desc)) {
    return false;
  }
  *bp = desc.isSome();
  return true;
}

bool BaseProxyHandler::get(JSContext* cx, HandleObject proxy,
                           HandleValue receiver, HandleId id,
                           MutableHandleValue vp) const {
  assertEnteredPolicy(cx, proxy, id, GET);

  // This method is not covered by any spec, but we follow ES 2016
  // (January 21, 2016) 9.1.8 fairly closely.

  // Step 2. (Step 1 is a superfluous assertion.)
  Rooted<mozilla::Maybe<PropertyDescriptor>> desc(cx);
  if (!getOwnPropertyDescriptor(cx, proxy, id, &desc)) {
    return false;
  }
  if (desc.isSome()) {
    desc->assertComplete();
  }

  // Step 3.
  if (desc.isNothing()) {
    // The spec calls this variable "parent", but that word has weird
    // connotations in SpiderMonkey, so let's go with "proto".
    // Step 3.a.
    RootedObject proto(cx);
    if (!GetPrototype(cx, proxy, &proto)) {
      return false;
    }

    // Step 3.b.
    if (!proto) {
      vp.setUndefined();
      return true;
    }

    // Step 3.c.
    return GetProperty(cx, proto, receiver, id, vp);
  }

  // Step 4.
  if (desc->isDataDescriptor()) {
    vp.set(desc->value());
    return true;
  }

  // Step 5.
  MOZ_ASSERT(desc->isAccessorDescriptor());
  RootedObject getter(cx, desc->getter());

  // Step 6.
  if (!getter) {
    vp.setUndefined();
    return true;
  }

  // Step 7.
  RootedValue getterFunc(cx, ObjectValue(*getter));
  return CallGetter(cx, receiver, getterFunc, vp);
}

bool BaseProxyHandler::set(JSContext* cx, HandleObject proxy, HandleId id,
                           HandleValue v, HandleValue receiver,
                           ObjectOpResult& result) const {
  assertEnteredPolicy(cx, proxy, id, SET);

  // This method is not covered by any spec, but we follow ES6 draft rev 28
  // (2014 Oct 14) 9.1.9 fairly closely, adapting it slightly for
  // SpiderMonkey's particular foibles.

  // Steps 2-3.  (Step 1 is a superfluous assertion.)
  Rooted<mozilla::Maybe<PropertyDescriptor>> ownDesc(cx);
  if (!getOwnPropertyDescriptor(cx, proxy, id, &ownDesc)) {
    return false;
  }
  if (ownDesc.isSome()) {
    ownDesc->assertComplete();
  }

  // The rest is factored out into a separate function with a weird name.
  // This algorithm continues just below.
  return SetPropertyIgnoringNamedGetter(cx, proxy, id, v, receiver, ownDesc,
                                        result);
}

bool js::SetPropertyIgnoringNamedGetter(
    JSContext* cx, HandleObject obj, HandleId id, HandleValue v,
    HandleValue receiver, Handle<mozilla::Maybe<PropertyDescriptor>> ownDesc_,
    ObjectOpResult& result) {
  Rooted<PropertyDescriptor> ownDesc(cx);

  // Step 4.
  if (ownDesc_.isNothing()) {
    // The spec calls this variable "parent", but that word has weird
    // connotations in SpiderMonkey, so let's go with "proto".
    RootedObject proto(cx);
    if (!GetPrototype(cx, obj, &proto)) {
      return false;
    }
    if (proto) {
      return SetProperty(cx, proto, id, v, receiver, result);
    }

    // Step 4.d.
    ownDesc.set(PropertyDescriptor::Data(
        UndefinedValue(),
        {JS::PropertyAttribute::Configurable, JS::PropertyAttribute::Enumerable,
         JS::PropertyAttribute::Writable}));
  } else {
    ownDesc.set(*ownDesc_);
  }

  // Step 5.
  if (ownDesc.isDataDescriptor()) {
    // Steps 5.a-b.
    if (!ownDesc.writable()) {
      return result.fail(JSMSG_READ_ONLY);
    }
    if (!receiver.isObject()) {
      return result.fail(JSMSG_SET_NON_OBJECT_RECEIVER);
    }
    RootedObject receiverObj(cx, &receiver.toObject());

    // Steps 5.c-d.
    Rooted<mozilla::Maybe<PropertyDescriptor>> existingDescriptor(cx);
    if (!GetOwnPropertyDescriptor(cx, receiverObj, id, &existingDescriptor)) {
      return false;
    }

    // Step 5.e.
    if (existingDescriptor.isSome()) {
      // Step 5.e.i.
      if (existingDescriptor->isAccessorDescriptor()) {
        return result.fail(JSMSG_OVERWRITING_ACCESSOR);
      }

      // Step 5.e.ii.
      if (!existingDescriptor->writable()) {
        return result.fail(JSMSG_READ_ONLY);
      }
    }

    // Steps 5.e.iii-iv. and 5.f.i.
    Rooted<PropertyDescriptor> desc(cx);
    if (existingDescriptor.isSome()) {
      desc = PropertyDescriptor::Empty();
      desc.setValue(v);
    } else {
      desc = PropertyDescriptor::Data(v, {JS::PropertyAttribute::Configurable,
                                          JS::PropertyAttribute::Enumerable,
                                          JS::PropertyAttribute::Writable});
    }
    return DefineProperty(cx, receiverObj, id, desc, result);
  }

  // Step 6.
  MOZ_ASSERT(ownDesc.isAccessorDescriptor());
  RootedObject setter(cx);
  if (ownDesc.hasSetter()) {
    setter = ownDesc.setter();
  }
  if (!setter) {
    return result.fail(JSMSG_GETTER_ONLY);
  }
  RootedValue setterValue(cx, ObjectValue(*setter));
  if (!CallSetter(cx, receiver, setterValue, v)) {
    return false;
  }
  return result.succeed();
}

bool BaseProxyHandler::getOwnEnumerablePropertyKeys(
    JSContext* cx, HandleObject proxy, MutableHandleIdVector props) const {
  assertEnteredPolicy(cx, proxy, JS::PropertyKey::Void(), ENUMERATE);
  MOZ_ASSERT(props.length() == 0);

  if (!ownPropertyKeys(cx, proxy, props)) {
    return false;
  }

  /* Select only the enumerable properties through in-place iteration. */
  RootedId id(cx);
  size_t i = 0;
  for (size_t j = 0, len = props.length(); j < len; j++) {
    MOZ_ASSERT(i <= j);
    id = props[j];
    if (id.isSymbol()) {
      continue;
    }

    AutoWaivePolicy policy(cx, proxy, id, BaseProxyHandler::GET);
    Rooted<mozilla::Maybe<PropertyDescriptor>> desc(cx);
    if (!getOwnPropertyDescriptor(cx, proxy, id, &desc)) {
      return false;
    }
    if (desc.isSome()) {
      desc->assertComplete();
    }

    if (desc.isSome() && desc->enumerable()) {
      props[i++].set(id);
    }
  }

  MOZ_ASSERT(i <= props.length());
  if (!props.resize(i)) {
    return false;
  }

  return true;
}

bool BaseProxyHandler::enumerate(JSContext* cx, HandleObject proxy,
                                 MutableHandleIdVector props) const {
  assertEnteredPolicy(cx, proxy, JS::PropertyKey::Void(), ENUMERATE);

  // GetPropertyKeys will invoke getOwnEnumerablePropertyKeys along the proto
  // chain for us.
  MOZ_ASSERT(props.empty());
  return GetPropertyKeys(cx, proxy, 0, props);
}

bool BaseProxyHandler::call(JSContext* cx, HandleObject proxy,
                            const CallArgs& args) const {
  MOZ_CRASH("callable proxies should implement call trap");
}

bool BaseProxyHandler::construct(JSContext* cx, HandleObject proxy,
                                 const CallArgs& args) const {
  MOZ_CRASH("callable proxies should implement construct trap");
}

const char* BaseProxyHandler::className(JSContext* cx,
                                        HandleObject proxy) const {
  return proxy->isCallable() ? "Function" : "Object";
}

JSString* BaseProxyHandler::fun_toString(JSContext* cx, HandleObject proxy,
                                         bool isToSource) const {
  if (proxy->isCallable()) {
    return JS_NewStringCopyZ(cx, "function () {\n    [native code]\n}");
  }

  JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                            JSMSG_INCOMPATIBLE_PROTO, js_Function_str,
                            js_toString_str, "object");
  return nullptr;
}

RegExpShared* BaseProxyHandler::regexp_toShared(JSContext* cx,
                                                HandleObject proxy) const {
  MOZ_CRASH("This should have been a wrapped regexp");
}

bool BaseProxyHandler::boxedValue_unbox(JSContext* cx, HandleObject proxy,
                                        MutableHandleValue vp) const {
  vp.setUndefined();
  return true;
}

bool BaseProxyHandler::nativeCall(JSContext* cx, IsAcceptableThis test,
                                  NativeImpl impl, const CallArgs& args) const {
  ReportIncompatible(cx, args);
  return false;
}

bool BaseProxyHandler::getBuiltinClass(JSContext* cx, HandleObject proxy,
                                       ESClass* cls) const {
  *cls = ESClass::Other;
  return true;
}

bool BaseProxyHandler::isArray(JSContext* cx, HandleObject proxy,
                               IsArrayAnswer* answer) const {
  *answer = IsArrayAnswer::NotArray;
  return true;
}

void BaseProxyHandler::trace(JSTracer* trc, JSObject* proxy) const {}

void BaseProxyHandler::finalize(JS::GCContext* gcx, JSObject* proxy) const {}

size_t BaseProxyHandler::objectMoved(JSObject* proxy, JSObject* old) const {
  return 0;
}

bool BaseProxyHandler::getPrototype(JSContext* cx, HandleObject proxy,
                                    MutableHandleObject protop) const {
  MOZ_CRASH("must override getPrototype with dynamic prototype");
}

bool BaseProxyHandler::setPrototype(JSContext* cx, HandleObject proxy,
                                    HandleObject proto,
                                    ObjectOpResult& result) const {
  // Disallow sets of protos on proxies with dynamic prototypes but no hook.
  // This keeps us away from the footgun of having the first proto set opt
  // you out of having dynamic protos altogether.
  JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                            JSMSG_CANT_SET_PROTO_OF, "incompatible Proxy");
  return false;
}

bool BaseProxyHandler::setImmutablePrototype(JSContext* cx, HandleObject proxy,
                                             bool* succeeded) const {
  *succeeded = false;
  return true;
}

bool BaseProxyHandler::getElements(JSContext* cx, HandleObject proxy,
                                   uint32_t begin, uint32_t end,
                                   ElementAdder* adder) const {
  assertEnteredPolicy(cx, proxy, JS::PropertyKey::Void(), GET);

  return js::GetElementsWithAdder(cx, proxy, proxy, begin, end, adder);
}

bool BaseProxyHandler::isCallable(JSObject* obj) const { return false; }

bool BaseProxyHandler::isConstructor(JSObject* obj) const { return false; }

JS_PUBLIC_API void js::NukeNonCCWProxy(JSContext* cx, HandleObject proxy) {
  MOZ_ASSERT(proxy->is<ProxyObject>());
  MOZ_ASSERT(!proxy->is<CrossCompartmentWrapperObject>());

  // (NotifyGCNukeWrapper() only needs to be called on CCWs.)

  // The proxy is about to be replaced, so we need to do any necessary
  // cleanup first.
  proxy->as<ProxyObject>().handler()->finalize(cx->gcContext(), proxy);

  proxy->as<ProxyObject>().nuke();

  MOZ_ASSERT(IsDeadProxyObject(proxy));
}

JS_PUBLIC_API void js::NukeRemovedCrossCompartmentWrapper(JSContext* cx,
                                                          JSObject* wrapper) {
  MOZ_ASSERT(wrapper->is<CrossCompartmentWrapperObject>());

  NotifyGCNukeWrapper(cx, wrapper);

  // We don't need to call finalize here because the CCW finalizer doesn't do
  // anything. Skipping finalize means that |wrapper| doesn't need to be rooted
  // to pass the hazard analysis, which is needed because this method is called
  // from some tricky places inside transplanting where rooting can be
  // difficult.

  wrapper->as<ProxyObject>().nuke();

  MOZ_ASSERT(IsDeadProxyObject(wrapper));
}