Debian Codegraph @

js/src/proxy/ScriptedProxyHandler.cpp

raw
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/. */
6
7 #include "proxy/ScriptedProxyHandler.h"
8
9 #include "mozilla/Maybe.h"
10
11 #include "jsapi.h"
12
13 #include "builtin/Object.h"
14 #include "js/CallAndConstruct.h" // JS::Construct, JS::IsCallable
15 #include "js/CharacterEncoding.h"
16 #include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
17 #include "js/PropertyDescriptor.h" // JS::FromPropertyDescriptor
18 #include "vm/EqualityOperations.h" // js::SameValue
19 #include "vm/Interpreter.h" // js::Call
20 #include "vm/JSFunction.h"
21 #include "vm/JSObject.h"
22 #include "vm/PlainObject.h" // js::PlainObject
23
24 #include "vm/JSObject-inl.h"
25 #include "vm/NativeObject-inl.h"
26
27 using namespace js;
28
29 using JS::IsArrayAnswer;
30
31 using mozilla::Maybe;
32
33 // ES2022 rev 33fe30f9a6b0dc81826f2f217167a89c025779a0
34 // IsCompatiblePropertyDescriptor. BUT that method just calls
35 // ValidateAndApplyPropertyDescriptor with two additional constant arguments.
36 // Therefore step numbering is from the latter method, and resulting dead code
37 // has been removed.
38
39 // If an exception should be thrown, we will set errorDetails.
40 static bool IsCompatiblePropertyDescriptor(
41 JSContext* cx, bool extensible, Handle<PropertyDescriptor> desc,
42 Handle<Maybe<PropertyDescriptor>> current, const char** errorDetails) {
43 // precondition: we won't set details if checks pass, so it must be null
44 // here.
45 MOZ_ASSERT(*errorDetails == nullptr);
46
47 // Step 2.
48 if (current.isNothing()) {
49 // Step 2.a-b,e. As |O| is always undefined, steps 2.c-d fall away.
50 if (!extensible) {
51 static const char DETAILS_NOT_EXTENSIBLE[] =
52 "proxy can't report an extensible object as non-extensible";
53 *errorDetails = DETAILS_NOT_EXTENSIBLE;
54 }
55 return true;
56 }
57
58 current->assertComplete();
59
60 // Step 3.
61 if (!desc.hasValue() && !desc.hasWritable() && !desc.hasGetter() &&
62 !desc.hasSetter() && !desc.hasEnumerable() && !desc.hasConfigurable()) {
63 return true;
64 }
65
66 // Step 4.
67 if (!current->configurable()) {
68 // Step 4.a.
69 if (desc.hasConfigurable() && desc.configurable()) {
70 static const char DETAILS_CANT_REPORT_NC_AS_C[] =
71 "proxy can't report an existing non-configurable property as "
72 "configurable";
73 *errorDetails = DETAILS_CANT_REPORT_NC_AS_C;
74 return true;
75 }
76
77 // Step 4.b.
78 if (desc.hasEnumerable() && desc.enumerable() != current->enumerable()) {
79 static const char DETAILS_ENUM_DIFFERENT[] =
80 "proxy can't report a different 'enumerable' from target when target "
81 "is not configurable";
82 *errorDetails = DETAILS_ENUM_DIFFERENT;
83 return true;
84 }
85 }
86
87 // Step 5.
88 if (desc.isGenericDescriptor()) {
89 return true;
90 }
91
92 // Step 6.
93 if (current->isDataDescriptor() != desc.isDataDescriptor()) {
94 // Steps 6.a., 10. As |O| is always undefined, steps 6.b-c fall away.
95 if (!current->configurable()) {
96 static const char DETAILS_CURRENT_NC_DIFF_TYPE[] =
97 "proxy can't report a different descriptor type when target is not "
98 "configurable";
99 *errorDetails = DETAILS_CURRENT_NC_DIFF_TYPE;
100 }
101 return true;
102 }
103
104 // Step 7.
105 if (current->isDataDescriptor()) {
106 MOZ_ASSERT(desc.isDataDescriptor()); // by step 6
107 // Step 7.a.
108 if (!current->configurable() && !current->writable()) {
109 // Step 7.a.i.
110 if (desc.hasWritable() && desc.writable()) {
111 static const char DETAILS_CANT_REPORT_NW_AS_W[] =
112 "proxy can't report a non-configurable, non-writable property as "
113 "writable";
114 *errorDetails = DETAILS_CANT_REPORT_NW_AS_W;
115 return true;
116 }
117
118 // Step 7.a.ii.
119 if (desc.hasValue()) {
120 RootedValue value(cx, current->value());
121 bool same;
122 if (!SameValue(cx, desc.value(), value, &same)) {
123 return false;
124 }
125 if (!same) {
126 static const char DETAILS_DIFFERENT_VALUE[] =
127 "proxy must report the same value for the non-writable, "
128 "non-configurable property";
129 *errorDetails = DETAILS_DIFFERENT_VALUE;
130 return true;
131 }
132 }
133 }
134
135 // Step 7.a.ii, 10.
136 return true;
137 }
138
139 // Step 8.
140
141 // Step 8.a.
142 MOZ_ASSERT(current->isAccessorDescriptor()); // by step 7
143 MOZ_ASSERT(desc.isAccessorDescriptor()); // by step 6
144
145 // Step 8.b.
146 if (current->configurable()) {
147 return true;
148 }
149 // Steps 8.b.i-ii.
150 if (desc.hasSetter() && desc.setter() != current->setter()) {
151 static const char DETAILS_SETTERS_DIFFERENT[] =
152 "proxy can't report different setters for a currently non-configurable "
153 "property";
154 *errorDetails = DETAILS_SETTERS_DIFFERENT;
155 } else if (desc.hasGetter() && desc.getter() != current->getter()) {
156 static const char DETAILS_GETTERS_DIFFERENT[] =
157 "proxy can't report different getters for a currently non-configurable "
158 "property";
159 *errorDetails = DETAILS_GETTERS_DIFFERENT;
160 }
161
162 // Step 9.
163 // |O| is always undefined.
164
165 // Step 10.
166 return true;
167 }
168
169 // Get the [[ProxyHandler]] of a scripted proxy.
170 /* static */
171 JSObject* ScriptedProxyHandler::handlerObject(const JSObject* proxy) {
172 MOZ_ASSERT(proxy->as<ProxyObject>().handler() ==
173 &ScriptedProxyHandler::singleton);
174 return proxy->as<ProxyObject>()
175 .reservedSlot(ScriptedProxyHandler::HANDLER_EXTRA)
176 .toObjectOrNull();
177 }
178
179 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
180 // 7.3.9 GetMethod, reimplemented for proxy handler trap-getting to produce
181 // better error messages.
182 static bool GetProxyTrap(JSContext* cx, HandleObject handler,
183 Handle<PropertyName*> name, MutableHandleValue func) {
184 // Steps 2, 5.
185 if (!GetProperty(cx, handler, handler, name, func)) {
186 return false;
187 }
188
189 // Step 3.
190 if (func.isUndefined()) {
191 return true;
192 }
193
194 if (func.isNull()) {
195 func.setUndefined();
196 return true;
197 }
198
199 // Step 4.
200 if (!IsCallable(func)) {
201 UniqueChars bytes = EncodeAscii(cx, name);
202 if (!bytes) {
203 return false;
204 }
205
206 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_TRAP,
207 bytes.get());
208 return false;
209 }
210
211 return true;
212 }
213
214 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
215 // 9.5.1 Proxy.[[GetPrototypeOf]].
216 bool ScriptedProxyHandler::getPrototype(JSContext* cx, HandleObject proxy,
217 MutableHandleObject protop) const {
218 // Steps 1-3.
219 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
220 if (!handler) {
221 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
222 JSMSG_PROXY_REVOKED);
223 return false;
224 }
225
226 // Step 4.
227 RootedObject target(cx, proxy->as<ProxyObject>().target());
228 MOZ_ASSERT(target);
229
230 // Step 5.
231 RootedValue trap(cx);
232 if (!GetProxyTrap(cx, handler, cx->names().getPrototypeOf, &trap)) {
233 return false;
234 }
235
236 // Step 6.
237 if (trap.isUndefined()) {
238 return GetPrototype(cx, target, protop);
239 }
240
241 // Step 7.
242 RootedValue handlerProto(cx);
243 {
244 FixedInvokeArgs<1> args(cx);
245
246 args[0].setObject(*target);
247
248 handlerProto.setObject(*handler);
249
250 if (!js::Call(cx, trap, handlerProto, args, &handlerProto)) {
251 return false;
252 }
253 }
254
255 // Step 8.
256 if (!handlerProto.isObjectOrNull()) {
257 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
258 JSMSG_BAD_GETPROTOTYPEOF_TRAP_RETURN);
259 return false;
260 }
261
262 // Step 9.
263 bool extensibleTarget;
264 if (!IsExtensible(cx, target, &extensibleTarget)) {
265 return false;
266 }
267
268 // Step 10.
269 if (extensibleTarget) {
270 protop.set(handlerProto.toObjectOrNull());
271 return true;
272 }
273
274 // Step 11.
275 RootedObject targetProto(cx);
276 if (!GetPrototype(cx, target, &targetProto)) {
277 return false;
278 }
279
280 // Step 12.
281 if (handlerProto.toObjectOrNull() != targetProto) {
282 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
283 JSMSG_INCONSISTENT_GETPROTOTYPEOF_TRAP);
284 return false;
285 }
286
287 // Step 13.
288 protop.set(handlerProto.toObjectOrNull());
289 return true;
290 }
291
292 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
293 // 9.5.2 Proxy.[[SetPrototypeOf]].
294 bool ScriptedProxyHandler::setPrototype(JSContext* cx, HandleObject proxy,
295 HandleObject proto,
296 ObjectOpResult& result) const {
297 // Steps 1-4.
298 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
299 if (!handler) {
300 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
301 JSMSG_PROXY_REVOKED);
302 return false;
303 }
304
305 // Step 5.
306 RootedObject target(cx, proxy->as<ProxyObject>().target());
307 MOZ_ASSERT(target);
308
309 // Step 6.
310 RootedValue trap(cx);
311 if (!GetProxyTrap(cx, handler, cx->names().setPrototypeOf, &trap)) {
312 return false;
313 }
314
315 // Step 7.
316 if (trap.isUndefined()) {
317 return SetPrototype(cx, target, proto, result);
318 }
319
320 // Step 8.
321 bool booleanTrapResult;
322 {
323 FixedInvokeArgs<2> args(cx);
324
325 args[0].setObject(*target);
326 args[1].setObjectOrNull(proto);
327
328 RootedValue hval(cx, ObjectValue(*handler));
329 if (!js::Call(cx, trap, hval, args, &hval)) {
330 return false;
331 }
332
333 booleanTrapResult = ToBoolean(hval);
334 }
335
336 // Step 9.
337 if (!booleanTrapResult) {
338 return result.fail(JSMSG_PROXY_SETPROTOTYPEOF_RETURNED_FALSE);
339 }
340
341 // Step 10.
342 bool extensibleTarget;
343 if (!IsExtensible(cx, target, &extensibleTarget)) {
344 return false;
345 }
346
347 // Step 11.
348 if (extensibleTarget) {
349 return result.succeed();
350 }
351
352 // Step 12.
353 RootedObject targetProto(cx);
354 if (!GetPrototype(cx, target, &targetProto)) {
355 return false;
356 }
357
358 // Step 13.
359 if (proto != targetProto) {
360 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
361 JSMSG_INCONSISTENT_SETPROTOTYPEOF_TRAP);
362 return false;
363 }
364
365 // Step 14.
366 return result.succeed();
367 }
368
369 bool ScriptedProxyHandler::getPrototypeIfOrdinary(
370 JSContext* cx, HandleObject proxy, bool* isOrdinary,
371 MutableHandleObject protop) const {
372 *isOrdinary = false;
373 return true;
374 }
375
376 // Not yet part of ES6, but hopefully to be standards-tracked -- and needed to
377 // handle revoked proxies in any event.
378 bool ScriptedProxyHandler::setImmutablePrototype(JSContext* cx,
379 HandleObject proxy,
380 bool* succeeded) const {
381 RootedObject target(cx, proxy->as<ProxyObject>().target());
382 if (!target) {
383 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
384 JSMSG_PROXY_REVOKED);
385 return false;
386 }
387
388 return SetImmutablePrototype(cx, target, succeeded);
389 }
390
391 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
392 // 9.5.4 Proxy.[[PreventExtensions]]()
393 bool ScriptedProxyHandler::preventExtensions(JSContext* cx, HandleObject proxy,
394 ObjectOpResult& result) const {
395 // Steps 1-3.
396 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
397 if (!handler) {
398 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
399 JSMSG_PROXY_REVOKED);
400 return false;
401 }
402
403 // Step 4.
404 RootedObject target(cx, proxy->as<ProxyObject>().target());
405 MOZ_ASSERT(target);
406
407 // Step 5.
408 RootedValue trap(cx);
409 if (!GetProxyTrap(cx, handler, cx->names().preventExtensions, &trap)) {
410 return false;
411 }
412
413 // Step 6.
414 if (trap.isUndefined()) {
415 return PreventExtensions(cx, target, result);
416 }
417
418 // Step 7.
419 bool booleanTrapResult;
420 {
421 RootedValue arg(cx, ObjectValue(*target));
422 RootedValue trapResult(cx);
423 if (!Call(cx, trap, handler, arg, &trapResult)) {
424 return false;
425 }
426
427 booleanTrapResult = ToBoolean(trapResult);
428 }
429
430 // Step 8.
431 if (booleanTrapResult) {
432 // Step 8a.
433 bool targetIsExtensible;
434 if (!IsExtensible(cx, target, &targetIsExtensible)) {
435 return false;
436 }
437
438 if (targetIsExtensible) {
439 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
440 JSMSG_CANT_REPORT_AS_NON_EXTENSIBLE);
441 return false;
442 }
443
444 // Step 9.
445 return result.succeed();
446 }
447
448 // Also step 9.
449 return result.fail(JSMSG_PROXY_PREVENTEXTENSIONS_RETURNED_FALSE);
450 }
451
452 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
453 // 9.5.3 Proxy.[[IsExtensible]]()
454 bool ScriptedProxyHandler::isExtensible(JSContext* cx, HandleObject proxy,
455 bool* extensible) const {
456 // Steps 1-3.
457 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
458 if (!handler) {
459 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
460 JSMSG_PROXY_REVOKED);
461 return false;
462 }
463
464 // Step 4.
465 RootedObject target(cx, proxy->as<ProxyObject>().target());
466 MOZ_ASSERT(target);
467
468 // Step 5.
469 RootedValue trap(cx);
470 if (!GetProxyTrap(cx, handler, cx->names().isExtensible, &trap)) {
471 return false;
472 }
473
474 // Step 6.
475 if (trap.isUndefined()) {
476 return IsExtensible(cx, target, extensible);
477 }
478
479 // Step 7.
480 bool booleanTrapResult;
481 {
482 RootedValue arg(cx, ObjectValue(*target));
483 RootedValue trapResult(cx);
484 if (!Call(cx, trap, handler, arg, &trapResult)) {
485 return false;
486 }
487
488 booleanTrapResult = ToBoolean(trapResult);
489 }
490
491 // Steps 8.
492 bool targetResult;
493 if (!IsExtensible(cx, target, &targetResult)) {
494 return false;
495 }
496
497 // Step 9.
498 if (targetResult != booleanTrapResult) {
499 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
500 JSMSG_PROXY_EXTENSIBILITY);
501 return false;
502 }
503
504 // Step 10.
505 *extensible = booleanTrapResult;
506 return true;
507 }
508
509 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
510 // 9.5.5 Proxy.[[GetOwnProperty]](P)
511 bool ScriptedProxyHandler::getOwnPropertyDescriptor(
512 JSContext* cx, HandleObject proxy, HandleId id,
513 MutableHandle<mozilla::Maybe<PropertyDescriptor>> desc) const {
514 // Steps 2-4.
515 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
516 if (!handler) {
517 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
518 JSMSG_PROXY_REVOKED);
519 return false;
520 }
521
522 // Step 5.
523 RootedObject target(cx, proxy->as<ProxyObject>().target());
524 MOZ_ASSERT(target);
525
526 // Step 6.
527 RootedValue trap(cx);
528 if (!GetProxyTrap(cx, handler, cx->names().getOwnPropertyDescriptor, &trap)) {
529 return false;
530 }
531
532 // Step 7.
533 if (trap.isUndefined()) {
534 return GetOwnPropertyDescriptor(cx, target, id, desc);
535 }
536
537 // Step 8.
538 RootedValue propKey(cx);
539 if (!IdToStringOrSymbol(cx, id, &propKey)) {
540 return false;
541 }
542
543 RootedValue trapResult(cx);
544 RootedValue targetVal(cx, ObjectValue(*target));
545 if (!Call(cx, trap, handler, targetVal, propKey, &trapResult)) {
546 return false;
547 }
548
549 // Step 9.
550 if (!trapResult.isUndefined() && !trapResult.isObject()) {
551 return js::Throw(cx, id, JSMSG_PROXY_GETOWN_OBJORUNDEF);
552 }
553
554 // Step 10.
555 Rooted<Maybe<PropertyDescriptor>> targetDesc(cx);
556 if (!GetOwnPropertyDescriptor(cx, target, id, &targetDesc)) {
557 return false;
558 }
559
560 // Step 11.
561 if (trapResult.isUndefined()) {
562 // Step 11a.
563 if (targetDesc.isNothing()) {
564 desc.reset();
565 return true;
566 }
567
568 // Step 11b.
569 if (!targetDesc->configurable()) {
570 return js::Throw(cx, id, JSMSG_CANT_REPORT_NC_AS_NE);
571 }
572
573 // Steps 11c-d.
574 bool extensibleTarget;
575 if (!IsExtensible(cx, target, &extensibleTarget)) {
576 return false;
577 }
578
579 // Step 11e.
580 if (!extensibleTarget) {
581 return js::Throw(cx, id, JSMSG_CANT_REPORT_E_AS_NE);
582 }
583
584 // Step 11f.
585 desc.reset();
586 return true;
587 }
588
589 // Step 12.
590 bool extensibleTarget;
591 if (!IsExtensible(cx, target, &extensibleTarget)) {
592 return false;
593 }
594
595 // Step 13.
596 Rooted<PropertyDescriptor> resultDesc(cx);
597 if (!ToPropertyDescriptor(cx, trapResult, true, &resultDesc)) {
598 return false;
599 }
600
601 // Step 14.
602 CompletePropertyDescriptor(&resultDesc);
603
604 // Step 15.
605 const char* errorDetails = nullptr;
606 if (!IsCompatiblePropertyDescriptor(cx, extensibleTarget, resultDesc,
607 targetDesc, &errorDetails))
608 return false;
609
610 // Step 16.
611 if (errorDetails) {
612 return js::Throw(cx, id, JSMSG_CANT_REPORT_INVALID, errorDetails);
613 }
614
615 // Step 17.
616 if (!resultDesc.configurable()) {
617 if (targetDesc.isNothing()) {
618 return js::Throw(cx, id, JSMSG_CANT_REPORT_NE_AS_NC);
619 }
620
621 if (targetDesc->configurable()) {
622 return js::Throw(cx, id, JSMSG_CANT_REPORT_C_AS_NC);
623 }
624
625 if (resultDesc.hasWritable() && !resultDesc.writable()) {
626 if (targetDesc->writable()) {
627 return js::Throw(cx, id, JSMSG_CANT_REPORT_W_AS_NW);
628 }
629 }
630 }
631
632 // Step 18.
633 desc.set(mozilla::Some(resultDesc.get()));
634 return true;
635 }
636
637 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
638 // 9.5.6 Proxy.[[DefineOwnProperty]](P, Desc)
639 bool ScriptedProxyHandler::defineProperty(JSContext* cx, HandleObject proxy,
640 HandleId id,
641 Handle<PropertyDescriptor> desc,
642 ObjectOpResult& result) const {
643 // Steps 2-4.
644 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
645 if (!handler) {
646 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
647 JSMSG_PROXY_REVOKED);
648 return false;
649 }
650
651 // Step 5.
652 RootedObject target(cx, proxy->as<ProxyObject>().target());
653 MOZ_ASSERT(target);
654
655 // Step 6.
656 RootedValue trap(cx);
657 if (!GetProxyTrap(cx, handler, cx->names().defineProperty, &trap)) {
658 return false;
659 }
660
661 // Step 7.
662 if (trap.isUndefined()) {
663 return DefineProperty(cx, target, id, desc, result);
664 }
665
666 // Step 8.
667 RootedValue descObj(cx);
668 if (!FromPropertyDescriptorToObject(cx, desc, &descObj)) {
669 return false;
670 }
671
672 // Step 9.
673 RootedValue propKey(cx);
674 if (!IdToStringOrSymbol(cx, id, &propKey)) {
675 return false;
676 }
677
678 RootedValue trapResult(cx);
679 {
680 FixedInvokeArgs<3> args(cx);
681
682 args[0].setObject(*target);
683 args[1].set(propKey);
684 args[2].set(descObj);
685
686 RootedValue thisv(cx, ObjectValue(*handler));
687 if (!Call(cx, trap, thisv, args, &trapResult)) {
688 return false;
689 }
690 }
691
692 // Step 10.
693 if (!ToBoolean(trapResult)) {
694 return result.fail(JSMSG_PROXY_DEFINE_RETURNED_FALSE);
695 }
696
697 // Step 11.
698 Rooted<Maybe<PropertyDescriptor>> targetDesc(cx);
699 if (!GetOwnPropertyDescriptor(cx, target, id, &targetDesc)) {
700 return false;
701 }
702
703 // Step 12.
704 bool extensibleTarget;
705 if (!IsExtensible(cx, target, &extensibleTarget)) {
706 return false;
707 }
708
709 // Steps 13-14.
710 bool settingConfigFalse = desc.hasConfigurable() && !desc.configurable();
711
712 // Steps 15-16.
713 if (targetDesc.isNothing()) {
714 // Step 15a.
715 if (!extensibleTarget) {
716 return js::Throw(cx, id, JSMSG_CANT_DEFINE_NEW);
717 }
718
719 // Step 15b.
720 if (settingConfigFalse) {
721 return js::Throw(cx, id, JSMSG_CANT_DEFINE_NE_AS_NC);
722 }
723 } else {
724 // Step 16a.
725 const char* errorDetails = nullptr;
726 if (!IsCompatiblePropertyDescriptor(cx, extensibleTarget, desc, targetDesc,
727 &errorDetails))
728 return false;
729
730 if (errorDetails) {
731 return js::Throw(cx, id, JSMSG_CANT_DEFINE_INVALID, errorDetails);
732 }
733
734 // Step 16b.
735 if (settingConfigFalse && targetDesc->configurable()) {
736 static const char DETAILS_CANT_REPORT_C_AS_NC[] =
737 "proxy can't define an existing configurable property as "
738 "non-configurable";
739 return js::Throw(cx, id, JSMSG_CANT_DEFINE_INVALID,
740 DETAILS_CANT_REPORT_C_AS_NC);
741 }
742
743 if (targetDesc->isDataDescriptor() && !targetDesc->configurable() &&
744 targetDesc->writable()) {
745 if (desc.hasWritable() && !desc.writable()) {
746 static const char DETAILS_CANT_DEFINE_NW[] =
747 "proxy can't define an existing non-configurable writable property "
748 "as non-writable";
749 return js::Throw(cx, id, JSMSG_CANT_DEFINE_INVALID,
750 DETAILS_CANT_DEFINE_NW);
751 }
752 }
753 }
754
755 // Step 17.
756 return result.succeed();
757 }
758
759 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
760 // 7.3.17 CreateListFromArrayLike with elementTypes fixed to symbol/string.
761 static bool CreateFilteredListFromArrayLike(JSContext* cx, HandleValue v,
762 MutableHandleIdVector props) {
763 // Step 2.
764 RootedObject obj(cx, RequireObject(cx, JSMSG_OBJECT_REQUIRED_RET_OWNKEYS,
765 JSDVG_IGNORE_STACK, v));
766 if (!obj) {
767 return false;
768 }
769
770 // Step 3.
771 uint64_t len;
772 if (!GetLengthProperty(cx, obj, &len)) {
773 return false;
774 }
775
776 // Steps 4-6.
777 RootedValue next(cx);
778 RootedId id(cx);
779 uint64_t index = 0;
780 while (index < len) {
781 // Steps 6a-b.
782 if (!GetElementLargeIndex(cx, obj, obj, index, &next)) {
783 return false;
784 }
785
786 // Step 6c.
787 if (!next.isString() && !next.isSymbol()) {
788 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
789 JSMSG_OWNKEYS_STR_SYM);
790 return false;
791 }
792
793 if (!PrimitiveValueToId<CanGC>(cx, next, &id)) {
794 return false;
795 }
796
797 // Step 6d.
798 if (!props.append(id)) {
799 return false;
800 }
801
802 // Step 6e.
803 index++;
804 }
805
806 // Step 7.
807 return true;
808 }
809
810 // ES2018 draft rev aab1ea3bd4d03c85d6f4a91503b4169346ab7271
811 // 9.5.11 Proxy.[[OwnPropertyKeys]]()
812 bool ScriptedProxyHandler::ownPropertyKeys(JSContext* cx, HandleObject proxy,
813 MutableHandleIdVector props) const {
814 // Steps 1-3.
815 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
816 if (!handler) {
817 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
818 JSMSG_PROXY_REVOKED);
819 return false;
820 }
821
822 // Step 4.
823 RootedObject target(cx, proxy->as<ProxyObject>().target());
824 MOZ_ASSERT(target);
825
826 // Step 5.
827 RootedValue trap(cx);
828 if (!GetProxyTrap(cx, handler, cx->names().ownKeys, &trap)) {
829 return false;
830 }
831
832 // Step 6.
833 if (trap.isUndefined()) {
834 return GetPropertyKeys(
835 cx, target, JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS, props);
836 }
837
838 // Step 7.
839 RootedValue trapResultArray(cx);
840 RootedValue targetVal(cx, ObjectValue(*target));
841 if (!Call(cx, trap, handler, targetVal, &trapResultArray)) {
842 return false;
843 }
844
845 // Step 8.
846 RootedIdVector trapResult(cx);
847 if (!CreateFilteredListFromArrayLike(cx, trapResultArray, &trapResult)) {
848 return false;
849 }
850
851 // Steps 9, 18.
852 Rooted<GCHashSet<jsid>> uncheckedResultKeys(
853 cx, GCHashSet<jsid>(cx, trapResult.length()));
854
855 for (size_t i = 0, len = trapResult.length(); i < len; i++) {
856 MOZ_ASSERT(!trapResult[i].isVoid());
857
858 auto ptr = uncheckedResultKeys.lookupForAdd(trapResult[i]);
859 if (ptr) {
860 return js::Throw(cx, trapResult[i], JSMSG_OWNKEYS_DUPLICATE);
861 }
862
863 if (!uncheckedResultKeys.add(ptr, trapResult[i])) {
864 return false;
865 }
866 }
867
868 // Step 10.
869 bool extensibleTarget;
870 if (!IsExtensible(cx, target, &extensibleTarget)) {
871 return false;
872 }
873
874 // Steps 11-13.
875 RootedIdVector targetKeys(cx);
876 if (!GetPropertyKeys(cx, target,
877 JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS,
878 &targetKeys)) {
879 return false;
880 }
881
882 // Steps 14-15.
883 RootedIdVector targetConfigurableKeys(cx);
884 RootedIdVector targetNonconfigurableKeys(cx);
885
886 // Step 16.
887 Rooted<Maybe<PropertyDescriptor>> desc(cx);
888 for (size_t i = 0; i < targetKeys.length(); ++i) {
889 // Step 16.a.
890 if (!GetOwnPropertyDescriptor(cx, target, targetKeys[i], &desc)) {
891 return false;
892 }
893
894 // Steps 16.b-c.
895 if (desc.isSome() && !desc->configurable()) {
896 if (!targetNonconfigurableKeys.append(targetKeys[i])) {
897 return false;
898 }
899 } else {
900 if (!targetConfigurableKeys.append(targetKeys[i])) {
901 return false;
902 }
903 }
904 }
905
906 // Step 17.
907 if (extensibleTarget && targetNonconfigurableKeys.empty()) {
908 return props.appendAll(std::move(trapResult));
909 }
910
911 // Step 19.
912 for (size_t i = 0; i < targetNonconfigurableKeys.length(); ++i) {
913 MOZ_ASSERT(!targetNonconfigurableKeys[i].isVoid());
914
915 auto ptr = uncheckedResultKeys.lookup(targetNonconfigurableKeys[i]);
916
917 // Step 19.a.
918 if (!ptr) {
919 return js::Throw(cx, targetNonconfigurableKeys[i], JSMSG_CANT_SKIP_NC);
920 }
921
922 // Step 19.b.
923 uncheckedResultKeys.remove(ptr);
924 }
925
926 // Step 20.
927 if (extensibleTarget) {
928 return props.appendAll(std::move(trapResult));
929 }
930
931 // Step 21.
932 for (size_t i = 0; i < targetConfigurableKeys.length(); ++i) {
933 MOZ_ASSERT(!targetConfigurableKeys[i].isVoid());
934
935 auto ptr = uncheckedResultKeys.lookup(targetConfigurableKeys[i]);
936
937 // Step 21.a.
938 if (!ptr) {
939 return js::Throw(cx, targetConfigurableKeys[i],
940 JSMSG_CANT_REPORT_E_AS_NE);
941 }
942
943 // Step 21.b.
944 uncheckedResultKeys.remove(ptr);
945 }
946
947 // Step 22.
948 if (!uncheckedResultKeys.empty()) {
949 RootedId id(cx, uncheckedResultKeys.all().front());
950 return js::Throw(cx, id, JSMSG_CANT_REPORT_NEW);
951 }
952
953 // Step 23.
954 return props.appendAll(std::move(trapResult));
955 }
956
957 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
958 // 9.5.10 Proxy.[[Delete]](P)
959 bool ScriptedProxyHandler::delete_(JSContext* cx, HandleObject proxy,
960 HandleId id, ObjectOpResult& result) const {
961 // Steps 2-4.
962 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
963 if (!handler) {
964 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
965 JSMSG_PROXY_REVOKED);
966 return false;
967 }
968
969 // Step 5.
970 RootedObject target(cx, proxy->as<ProxyObject>().target());
971 MOZ_ASSERT(target);
972
973 // Step 6.
974 RootedValue trap(cx);
975 if (!GetProxyTrap(cx, handler, cx->names().deleteProperty, &trap)) {
976 return false;
977 }
978
979 // Step 7.
980 if (trap.isUndefined()) {
981 return DeleteProperty(cx, target, id, result);
982 }
983
984 // Step 8.
985 bool booleanTrapResult;
986 {
987 RootedValue value(cx);
988 if (!IdToStringOrSymbol(cx, id, &value)) {
989 return false;
990 }
991
992 RootedValue targetVal(cx, ObjectValue(*target));
993 RootedValue trapResult(cx);
994 if (!Call(cx, trap, handler, targetVal, value, &trapResult)) {
995 return false;
996 }
997
998 booleanTrapResult = ToBoolean(trapResult);
999 }
1000
1001 // Step 9.
1002 if (!booleanTrapResult) {
1003 return result.fail(JSMSG_PROXY_DELETE_RETURNED_FALSE);
1004 }
1005
1006 // Step 10.
1007 Rooted<Maybe<PropertyDescriptor>> desc(cx);
1008 if (!GetOwnPropertyDescriptor(cx, target, id, &desc)) {
1009 return false;
1010 }
1011
1012 // Step 11.
1013 if (desc.isNothing()) {
1014 return result.succeed();
1015 }
1016
1017 // Step 12.
1018 if (!desc->configurable()) {
1019 return Throw(cx, id, JSMSG_CANT_DELETE);
1020 }
1021
1022 bool extensible;
1023 if (!IsExtensible(cx, target, &extensible)) {
1024 return false;
1025 }
1026
1027 if (!extensible) {
1028 return Throw(cx, id, JSMSG_CANT_DELETE_NON_EXTENSIBLE);
1029 }
1030
1031 // Step 13.
1032 return result.succeed();
1033 }
1034
1035 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
1036 // 9.5.7 Proxy.[[HasProperty]](P)
1037 bool ScriptedProxyHandler::has(JSContext* cx, HandleObject proxy, HandleId id,
1038 bool* bp) const {
1039 // Steps 2-4.
1040 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
1041 if (!handler) {
1042 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1043 JSMSG_PROXY_REVOKED);
1044 return false;
1045 }
1046
1047 // Step 5.
1048 RootedObject target(cx, proxy->as<ProxyObject>().target());
1049 MOZ_ASSERT(target);
1050
1051 // Step 6.
1052 RootedValue trap(cx);
1053 if (!GetProxyTrap(cx, handler, cx->names().has, &trap)) {
1054 return false;
1055 }
1056
1057 // Step 7.
1058 if (trap.isUndefined()) {
1059 return HasProperty(cx, target, id, bp);
1060 }
1061
1062 // Step 8.
1063 RootedValue value(cx);
1064 if (!IdToStringOrSymbol(cx, id, &value)) {
1065 return false;
1066 }
1067
1068 RootedValue trapResult(cx);
1069 RootedValue targetVal(cx, ObjectValue(*target));
1070 if (!Call(cx, trap, handler, targetVal, value, &trapResult)) {
1071 return false;
1072 }
1073
1074 bool booleanTrapResult = ToBoolean(trapResult);
1075
1076 // Step 9.
1077 if (!booleanTrapResult) {
1078 // Step 9a.
1079 Rooted<Maybe<PropertyDescriptor>> desc(cx);
1080 if (!GetOwnPropertyDescriptor(cx, target, id, &desc)) {
1081 return false;
1082 }
1083
1084 // Step 9b.
1085 if (desc.isSome()) {
1086 // Step 9b(i).
1087 if (!desc->configurable()) {
1088 return js::Throw(cx, id, JSMSG_CANT_REPORT_NC_AS_NE);
1089 }
1090
1091 // Step 9b(ii).
1092 bool extensible;
1093 if (!IsExtensible(cx, target, &extensible)) {
1094 return false;
1095 }
1096
1097 // Step 9b(iii).
1098 if (!extensible) {
1099 return js::Throw(cx, id, JSMSG_CANT_REPORT_E_AS_NE);
1100 }
1101 }
1102 }
1103
1104 // Step 10.
1105 *bp = booleanTrapResult;
1106 return true;
1107 }
1108
1109 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
1110 // 9.5.8 Proxy.[[GetP]](P, Receiver)
1111 bool ScriptedProxyHandler::get(JSContext* cx, HandleObject proxy,
1112 HandleValue receiver, HandleId id,
1113 MutableHandleValue vp) const {
1114 // Steps 2-4.
1115 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
1116 if (!handler) {
1117 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1118 JSMSG_PROXY_REVOKED);
1119 return false;
1120 }
1121
1122 // Step 5.
1123 RootedObject target(cx, proxy->as<ProxyObject>().target());
1124 MOZ_ASSERT(target);
1125
1126 // Steps 6.
1127 RootedValue trap(cx);
1128 if (!GetProxyTrap(cx, handler, cx->names().get, &trap)) {
1129 return false;
1130 }
1131
1132 // Step 7.
1133 if (trap.isUndefined()) {
1134 return GetProperty(cx, target, receiver, id, vp);
1135 }
1136
1137 // Step 8.
1138 RootedValue value(cx);
1139 if (!IdToStringOrSymbol(cx, id, &value)) {
1140 return false;
1141 }
1142
1143 RootedValue trapResult(cx);
1144 {
1145 FixedInvokeArgs<3> args(cx);
1146
1147 args[0].setObject(*target);
1148 args[1].set(value);
1149 args[2].set(receiver);
1150
1151 RootedValue thisv(cx, ObjectValue(*handler));
1152 if (!Call(cx, trap, thisv, args, &trapResult)) {
1153 return false;
1154 }
1155 }
1156
1157 // Steps 9 and 10.
1158 GetTrapValidationResult validation =
1159 checkGetTrapResult(cx, target, id, trapResult);
1160 if (validation != GetTrapValidationResult::OK) {
1161 reportGetTrapValidationError(cx, id, validation);
1162 return false;
1163 }
1164
1165 // Step 11.
1166 vp.set(trapResult);
1167 return true;
1168 }
1169
1170 void ScriptedProxyHandler::reportGetTrapValidationError(
1171 JSContext* cx, HandleId id, GetTrapValidationResult validation) {
1172 switch (validation) {
1173 case GetTrapValidationResult::MustReportSameValue:
1174 js::Throw(cx, id, JSMSG_MUST_REPORT_SAME_VALUE);
1175 return;
1176 case GetTrapValidationResult::MustReportUndefined:
1177 js::Throw(cx, id, JSMSG_MUST_REPORT_SAME_VALUE);
1178 return;
1179 case GetTrapValidationResult::Exception:
1180 return;
1181 case GetTrapValidationResult::OK:
1182 MOZ_CRASH("unreachable");
1183 }
1184 }
1185
1186 ScriptedProxyHandler::GetTrapValidationResult
1187 ScriptedProxyHandler::checkGetTrapResult(JSContext* cx, HandleObject target,
1188 HandleId id, HandleValue trapResult) {
1189 // Step 9.
1190 Rooted<Maybe<PropertyDescriptor>> desc(cx);
1191 if (!GetOwnPropertyDescriptor(cx, target, id, &desc)) {
1192 return GetTrapValidationResult::Exception;
1193 }
1194
1195 // Step 10.
1196 if (desc.isSome()) {
1197 // Step 10a.
1198 if (desc->isDataDescriptor() && !desc->configurable() &&
1199 !desc->writable()) {
1200 RootedValue value(cx, desc->value());
1201 bool same;
1202 if (!SameValue(cx, trapResult, value, &same)) {
1203 return GetTrapValidationResult::Exception;
1204 }
1205
1206 if (!same) {
1207 return GetTrapValidationResult::MustReportSameValue;
1208 }
1209 }
1210
1211 // Step 10b.
1212 if (desc->isAccessorDescriptor() && !desc->configurable() &&
1213 (desc->getter() == nullptr) && !trapResult.isUndefined()) {
1214 return GetTrapValidationResult::MustReportUndefined;
1215 }
1216 }
1217
1218 return GetTrapValidationResult::OK;
1219 }
1220
1221 // ES8 rev 0c1bd3004329336774cbc90de727cd0cf5f11e93
1222 // 9.5.9 Proxy.[[Set]](P, V, Receiver)
1223 bool ScriptedProxyHandler::set(JSContext* cx, HandleObject proxy, HandleId id,
1224 HandleValue v, HandleValue receiver,
1225 ObjectOpResult& result) const {
1226 // Steps 2-4.
1227 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
1228 if (!handler) {
1229 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1230 JSMSG_PROXY_REVOKED);
1231 return false;
1232 }
1233
1234 // Step 5.
1235 RootedObject target(cx, proxy->as<ProxyObject>().target());
1236 MOZ_ASSERT(target);
1237
1238 // Step 6.
1239 RootedValue trap(cx);
1240 if (!GetProxyTrap(cx, handler, cx->names().set, &trap)) {
1241 return false;
1242 }
1243
1244 // Step 7.
1245 if (trap.isUndefined()) {
1246 return SetProperty(cx, target, id, v, receiver, result);
1247 }
1248
1249 // Step 8.
1250 RootedValue value(cx);
1251 if (!IdToStringOrSymbol(cx, id, &value)) {
1252 return false;
1253 }
1254
1255 RootedValue trapResult(cx);
1256 {
1257 FixedInvokeArgs<4> args(cx);
1258
1259 args[0].setObject(*target);
1260 args[1].set(value);
1261 args[2].set(v);
1262 args[3].set(receiver);
1263
1264 RootedValue thisv(cx, ObjectValue(*handler));
1265 if (!Call(cx, trap, thisv, args, &trapResult)) {
1266 return false;
1267 }
1268 }
1269
1270 // Step 9.
1271 if (!ToBoolean(trapResult)) {
1272 return result.fail(JSMSG_PROXY_SET_RETURNED_FALSE);
1273 }
1274
1275 // Step 10.
1276 Rooted<Maybe<PropertyDescriptor>> desc(cx);
1277 if (!GetOwnPropertyDescriptor(cx, target, id, &desc)) {
1278 return false;
1279 }
1280
1281 // Step 11.
1282 if (desc.isSome()) {
1283 // Step 11a.
1284 if (desc->isDataDescriptor() && !desc->configurable() &&
1285 !desc->writable()) {
1286 RootedValue value(cx, desc->value());
1287 bool same;
1288 if (!SameValue(cx, v, value, &same)) {
1289 return false;
1290 }
1291 if (!same) {
1292 return js::Throw(cx, id, JSMSG_CANT_SET_NW_NC);
1293 }
1294 }
1295
1296 // Step 11b.
1297 if (desc->isAccessorDescriptor() && !desc->configurable() &&
1298 desc->setter() == nullptr) {
1299 return js::Throw(cx, id, JSMSG_CANT_SET_WO_SETTER);
1300 }
1301 }
1302
1303 // Step 12.
1304 return result.succeed();
1305 }
1306
1307 // ES7 0c1bd3004329336774cbc90de727cd0cf5f11e93 9.5.13 Proxy.[[Call]]
1308 bool ScriptedProxyHandler::call(JSContext* cx, HandleObject proxy,
1309 const CallArgs& args) const {
1310 // Steps 1-3.
1311 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
1312 if (!handler) {
1313 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1314 JSMSG_PROXY_REVOKED);
1315 return false;
1316 }
1317
1318 // Step 4.
1319 RootedObject target(cx, proxy->as<ProxyObject>().target());
1320 MOZ_ASSERT(target);
1321 MOZ_ASSERT(target->isCallable());
1322
1323 // Step 5.
1324 RootedValue trap(cx);
1325 if (!GetProxyTrap(cx, handler, cx->names().apply, &trap)) {
1326 return false;
1327 }
1328
1329 // Step 6.
1330 if (trap.isUndefined()) {
1331 InvokeArgs iargs(cx);
1332 if (!FillArgumentsFromArraylike(cx, iargs, args)) {
1333 return false;
1334 }
1335
1336 RootedValue fval(cx, ObjectValue(*target));
1337 return js::Call(cx, fval, args.thisv(), iargs, args.rval());
1338 }
1339
1340 // Step 7.
1341 RootedObject argArray(cx,
1342 NewDenseCopiedArray(cx, args.length(), args.array()));
1343 if (!argArray) {
1344 return false;
1345 }
1346
1347 // Step 8.
1348 FixedInvokeArgs<3> iargs(cx);
1349
1350 iargs[0].setObject(*target);
1351 iargs[1].set(args.thisv());
1352 iargs[2].setObject(*argArray);
1353
1354 RootedValue thisv(cx, ObjectValue(*handler));
1355 return js::Call(cx, trap, thisv, iargs, args.rval());
1356 }
1357
1358 // ES7 0c1bd3004329336774cbc90de727cd0cf5f11e93 9.5.14 Proxy.[[Construct]]
1359 bool ScriptedProxyHandler::construct(JSContext* cx, HandleObject proxy,
1360 const CallArgs& args) const {
1361 // Steps 1-3.
1362 RootedObject handler(cx, ScriptedProxyHandler::handlerObject(proxy));
1363 if (!handler) {
1364 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1365 JSMSG_PROXY_REVOKED);
1366 return false;
1367 }
1368
1369 // Step 4.
1370 RootedObject target(cx, proxy->as<ProxyObject>().target());
1371 MOZ_ASSERT(target);
1372 MOZ_ASSERT(target->isConstructor());
1373
1374 // Step 5.
1375 RootedValue trap(cx);
1376 if (!GetProxyTrap(cx, handler, cx->names().construct, &trap)) {
1377 return false;
1378 }
1379
1380 // Step 6.
1381 if (trap.isUndefined()) {
1382 ConstructArgs cargs(cx);
1383 if (!FillArgumentsFromArraylike(cx, cargs, args)) {
1384 return false;
1385 }
1386
1387 RootedValue targetv(cx, ObjectValue(*target));
1388 RootedObject obj(cx);
1389 if (!Construct(cx, targetv, cargs, args.newTarget(), &obj)) {
1390 return false;
1391 }
1392
1393 args.rval().setObject(*obj);
1394 return true;
1395 }
1396
1397 // Step 7.
1398 RootedObject argArray(cx,
1399 NewDenseCopiedArray(cx, args.length(), args.array()));
1400 if (!argArray) {
1401 return false;
1402 }
1403
1404 // Steps 8, 10.
1405 {
1406 FixedInvokeArgs<3> iargs(cx);
1407
1408 iargs[0].setObject(*target);
1409 iargs[1].setObject(*argArray);
1410 iargs[2].set(args.newTarget());
1411
1412 RootedValue thisv(cx, ObjectValue(*handler));
1413 if (!Call(cx, trap, thisv, iargs, args.rval())) {
1414 return false;
1415 }
1416 }
1417
1418 // Step 9.
1419 if (!args.rval().isObject()) {
1420 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1421 JSMSG_PROXY_CONSTRUCT_OBJECT);
1422 return false;
1423 }
1424
1425 return true;
1426 }
1427
1428 bool ScriptedProxyHandler::nativeCall(JSContext* cx, IsAcceptableThis test,
1429 NativeImpl impl,
1430 const CallArgs& args) const {
1431 ReportIncompatible(cx, args);
1432 return false;
1433 }
1434
1435 bool ScriptedProxyHandler::getBuiltinClass(JSContext* cx, HandleObject proxy,
1436 ESClass* cls) const {
1437 *cls = ESClass::Other;
1438 return true;
1439 }
1440
1441 bool ScriptedProxyHandler::isArray(JSContext* cx, HandleObject proxy,
1442 IsArrayAnswer* answer) const {
1443 RootedObject target(cx, proxy->as<ProxyObject>().target());
1444 if (target) {
1445 return JS::IsArray(cx, target, answer);
1446 }
1447
1448 *answer = IsArrayAnswer::RevokedProxy;
1449 return true;
1450 }
1451
1452 const char* ScriptedProxyHandler::className(JSContext* cx,
1453 HandleObject proxy) const {
1454 // Right now the caller is not prepared to handle failures.
1455 return BaseProxyHandler::className(cx, proxy);
1456 }
1457
1458 JSString* ScriptedProxyHandler::fun_toString(JSContext* cx, HandleObject proxy,
1459 bool isToSource) const {
1460 // The BaseProxyHandler has the desired behavior: Throw for non-callable,
1461 // otherwise return [native code].
1462 return BaseProxyHandler::fun_toString(cx, proxy, isToSource);
1463 }
1464
1465 RegExpShared* ScriptedProxyHandler::regexp_toShared(JSContext* cx,
1466 HandleObject proxy) const {
1467 MOZ_CRASH("Should not end up in ScriptedProxyHandler::regexp_toShared");
1468 }
1469
1470 bool ScriptedProxyHandler::boxedValue_unbox(JSContext* cx, HandleObject proxy,
1471 MutableHandleValue vp) const {
1472 MOZ_CRASH("Should not end up in ScriptedProxyHandler::boxedValue_unbox");
1473 return false;
1474 }
1475
1476 bool ScriptedProxyHandler::isCallable(JSObject* obj) const {
1477 MOZ_ASSERT(obj->as<ProxyObject>().handler() ==
1478 &ScriptedProxyHandler::singleton);
1479 uint32_t callConstruct = obj->as<ProxyObject>()
1480 .reservedSlot(IS_CALLCONSTRUCT_EXTRA)
1481 .toPrivateUint32();
1482 return !!(callConstruct & IS_CALLABLE);
1483 }
1484
1485 bool ScriptedProxyHandler::isConstructor(JSObject* obj) const {
1486 MOZ_ASSERT(obj->as<ProxyObject>().handler() ==
1487 &ScriptedProxyHandler::singleton);
1488 uint32_t callConstruct = obj->as<ProxyObject>()
1489 .reservedSlot(IS_CALLCONSTRUCT_EXTRA)
1490 .toPrivateUint32();
1491 return !!(callConstruct & IS_CONSTRUCTOR);
1492 }
1493
1494 const char ScriptedProxyHandler::family = 0;
1495 const ScriptedProxyHandler ScriptedProxyHandler::singleton;
1496
1497 // ES2021 rev c21b280a2c46e92decf3efeca9e9da35d5b9f622
1498 // Including the changes from: https://github.com/tc39/ecma262/pull/1814
1499 // 9.5.14 ProxyCreate.
1500 static bool ProxyCreate(JSContext* cx, CallArgs& args, const char* callerName) {
1501 if (!args.requireAtLeast(cx, callerName, 2)) {
1502 return false;
1503 }
1504
1505 // Step 1.
1506 RootedObject target(cx,
1507 RequireObjectArg(cx, "`target`", callerName, args[0]));
1508 if (!target) {
1509 return false;
1510 }
1511
1512 // Step 2.
1513 RootedObject handler(cx,
1514 RequireObjectArg(cx, "`handler`", callerName, args[1]));
1515 if (!handler) {
1516 return false;
1517 }
1518
1519 // Steps 3-4, 6.
1520 RootedValue priv(cx, ObjectValue(*target));
1521 JSObject* proxy_ = NewProxyObject(cx, &ScriptedProxyHandler::singleton, priv,
1522 TaggedProto::LazyProto);
1523 if (!proxy_) {
1524 return false;
1525 }
1526
1527 // Step 7 (reordered).
1528 Rooted<ProxyObject*> proxy(cx, &proxy_->as<ProxyObject>());
1529 proxy->setReservedSlot(ScriptedProxyHandler::HANDLER_EXTRA,
1530 ObjectValue(*handler));
1531
1532 // Step 5.
1533 uint32_t callable =
1534 target->isCallable() ? ScriptedProxyHandler::IS_CALLABLE : 0;
1535 uint32_t constructor =
1536 target->isConstructor() ? ScriptedProxyHandler::IS_CONSTRUCTOR : 0;
1537 proxy->setReservedSlot(ScriptedProxyHandler::IS_CALLCONSTRUCT_EXTRA,
1538 PrivateUint32Value(callable | constructor));
1539
1540 // Step 8.
1541 args.rval().setObject(*proxy);
1542 return true;
1543 }
1544
1545 bool js::proxy(JSContext* cx, unsigned argc, Value* vp) {
1546 CallArgs args = CallArgsFromVp(argc, vp);
1547
1548 if (!ThrowIfNotConstructing(cx, args, "Proxy")) {
1549 return false;
1550 }
1551
1552 return ProxyCreate(cx, args, "Proxy");
1553 }
1554
1555 static bool RevokeProxy(JSContext* cx, unsigned argc, Value* vp) {
1556 CallArgs args = CallArgsFromVp(argc, vp);
1557
1558 RootedFunction func(cx, &args.callee().as<JSFunction>());
1559 RootedObject p(cx, func->getExtendedSlot(ScriptedProxyHandler::REVOKE_SLOT)
1560 .toObjectOrNull());
1561
1562 if (p) {
1563 func->setExtendedSlot(ScriptedProxyHandler::REVOKE_SLOT, NullValue());
1564
1565 MOZ_ASSERT(p->is<ProxyObject>());
1566
1567 p->as<ProxyObject>().setSameCompartmentPrivate(NullValue());
1568 p->as<ProxyObject>().setReservedSlot(ScriptedProxyHandler::HANDLER_EXTRA,
1569 NullValue());
1570 }
1571
1572 args.rval().setUndefined();
1573 return true;
1574 }
1575
1576 bool js::proxy_revocable(JSContext* cx, unsigned argc, Value* vp) {
1577 CallArgs args = CallArgsFromVp(argc, vp);
1578
1579 if (!ProxyCreate(cx, args, "Proxy.revocable")) {
1580 return false;
1581 }
1582
1583 RootedValue proxyVal(cx, args.rval());
1584 MOZ_ASSERT(proxyVal.toObject().is<ProxyObject>());
1585
1586 RootedFunction revoker(
1587 cx, NewNativeFunction(cx, RevokeProxy, 0, nullptr,
1588 gc::AllocKind::FUNCTION_EXTENDED, GenericObject));
1589 if (!revoker) {
1590 return false;
1591 }
1592
1593 revoker->initExtendedSlot(ScriptedProxyHandler::REVOKE_SLOT, proxyVal);
1594
1595 Rooted<PlainObject*> result(cx, NewPlainObject(cx));
1596 if (!result) {
1597 return false;
1598 }
1599
1600 RootedValue revokeVal(cx, ObjectValue(*revoker));
1601 if (!DefineDataProperty(cx, result, cx->names().proxy, proxyVal) ||
1602 !DefineDataProperty(cx, result, cx->names().revoke, revokeVal)) {
1603 return false;
1604 }
1605
1606 args.rval().setObject(*result);
1607 return true;
1608 }