forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSProxy.cpp
More file actions
1552 lines (1492 loc) · 56.9 KB
/
JSProxy.cpp
File metadata and controls
1552 lines (1492 loc) · 56.9 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/JSProxy.h"
#include "hermes/VM/ArrayLike.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/JSArray.h"
#include "hermes/VM/JSCallableProxy.h"
#include "hermes/VM/OrderedHashMap.h"
#include "llvh/ADT/SmallSet.h"
namespace hermes {
namespace vm {
namespace detail {
ProxySlots &slots(JSObject *self) {
if (auto *proxy = dyn_vmcast<JSProxy>(self)) {
return proxy->slots_;
} else {
auto *cproxy = dyn_vmcast<JSCallableProxy>(self);
assert(
cproxy && "JSProxy methods must be passed JSProxy or JSCallableProxy");
return cproxy->slots_;
}
}
CallResult<Handle<Callable>>
findTrap(Handle<JSObject> selfHandle, Runtime *runtime, Predefined::Str name) {
// 2. Let handler be O.[[ProxyHandler]].
// 3. If handler is null, throw a TypeError exception.
JSObject *handlerPtr = detail::slots(*selfHandle).handler.get(runtime);
if (!handlerPtr) {
return runtime->raiseTypeError("Proxy handler is null");
}
// Calls to look up the trap are effectively recursion, and so
// require their own scope. They also need a
// ScopedNativeDepthTracker, as it's possible to use up arbitrary
// native stack depth with nested proxies.
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(Runtime::StackOverflowKind::NativeStack);
}
// 4. Assert: Type(handler) is Object.
// 5. Let target be O.[[ProxyTarget]].
// 6. Let trap be ? GetMethod(handler, « name »).
Handle<JSObject> handler = runtime->makeHandle(handlerPtr);
CallResult<PseudoHandle<>> trapVal =
JSObject::getNamed_RJS(handler, runtime, Predefined::getSymbolID(name));
if (trapVal == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
if ((*trapVal)->isUndefined() || (*trapVal)->isNull()) {
return Runtime::makeNullHandle<Callable>();
}
if (!vmisa<Callable>(trapVal->get())) {
return runtime->raiseTypeErrorForValue(
runtime->makeHandle(std::move(*trapVal)),
" is not a Proxy trap function");
}
return runtime->makeHandleInParentScope<Callable>(std::move(trapVal->get()));
}
} // namespace detail
//===----------------------------------------------------------------------===//
// class JSProxy
const ObjectVTable JSProxy::vt{
VTable(CellKind::ProxyKind, cellSize<JSProxy>()),
JSProxy::_getOwnIndexedRangeImpl,
JSProxy::_haveOwnIndexedImpl,
JSProxy::_getOwnIndexedPropertyFlagsImpl,
JSProxy::_getOwnIndexedImpl,
JSProxy::_setOwnIndexedImpl,
JSProxy::_deleteOwnIndexedImpl,
JSProxy::_checkAllOwnIndexedImpl,
};
void ProxyBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(JSObject::numOverlapSlots<JSProxy>());
ObjectBuildMeta(cell, mb);
const auto *self = static_cast<const JSProxy *>(cell);
mb.addField("@target", &self->slots_.target);
mb.addField("@handler", &self->slots_.handler);
}
#ifdef HERMESVM_SERIALIZE
JSProxy::JSProxy(Deserializer &d) : JSObject(d, &vt.base) {
d.readRelocation(&slots_.target, RelocationKind::GCPointer);
d.readRelocation(&slots_.handler, RelocationKind::GCPointer);
}
void ProxySerialize(Serializer &s, const GCCell *cell) {
JSObject::serializeObjectImpl(s, cell, JSObject::numOverlapSlots<JSProxy>());
auto *self = vmcast<const JSProxy>(cell);
s.writeRelocation(self->slots_.target.get(s.getRuntime()));
s.writeRelocation(self->slots_.handler.get(s.getRuntime()));
s.endObject(cell);
}
void ProxyDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::ProxyKind && "Expected Proxy");
void *mem = d.getRuntime()->alloc(cellSize<JSProxy>());
auto *cell = new (mem) JSProxy(d);
d.endObject(cell);
}
#endif
PseudoHandle<JSProxy> JSProxy::create(Runtime *runtime) {
JSObjectAlloc<JSProxy> mem{runtime};
JSProxy *proxy = new (mem) JSProxy(
runtime,
runtime->objectPrototypeRawPtr,
runtime->getHiddenClassForPrototypeRaw(
runtime->objectPrototypeRawPtr,
JSObject::numOverlapSlots<JSProxy>() + ANONYMOUS_PROPERTY_SLOTS));
proxy->flags_.proxyObject = true;
return mem.initToPseudoHandle(proxy);
}
void JSProxy::setTargetAndHandler(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<JSObject> target,
Handle<JSObject> handler) {
auto &slots = detail::slots(*selfHandle);
slots.target.set(runtime, target.get(), &runtime->getHeap());
slots.handler.set(runtime, handler.get(), &runtime->getHeap());
}
namespace {
void completePropertyDescriptor(DefinePropertyFlags &desc) {
if ((desc.setValue || desc.setWritable) ||
(!desc.setGetter && !desc.setSetter)) {
if (!desc.setWritable) {
desc.writable = false;
}
}
if (!desc.setEnumerable) {
desc.enumerable = false;
}
if (!desc.setConfigurable) {
desc.configurable = false;
}
}
// ES9 9.1.6.2 IsCompatiblePropertyDescriptor
// prereq: step 2 is already done externally.
// The abstract definition returns a boolean; this returns EXCEPTION
// (and sets an exception) or RETURNED instead of false or true, so
// the exception messages can be more specific.
ExecutionStatus isCompatiblePropertyDescriptor(
Runtime *runtime,
const DefinePropertyFlags &desc,
Handle<> descValueOrAccessor,
const ComputedPropertyDescriptor ¤t,
Handle<> currentValueOrAccessor) {
// 4. If current.[[Configurable]] is false, then
if (!current.flags.configurable) {
// a. If Desc.[[Configurable]] is present and its value is true, return
// false.
if (desc.setConfigurable && desc.configurable) {
return runtime->raiseTypeError(
"trap result is configurable but target property is non-configurable");
}
// b. If Desc.[[Enumerable]] is present and the [[Enumerable]]
// fields of current and Desc are the Boolean negation of each
// other, return false.
if (desc.setEnumerable && desc.enumerable != current.flags.enumerable) {
return runtime->raiseTypeError(
TwineChar16("trap result is ") + (desc.enumerable ? "" : "not ") +
"enumerable but target property is " +
(current.flags.enumerable ? "" : "not ") + "enumerable");
}
}
// 5. If IsGenericDescriptor(Desc) is true, no further validation is required.
bool descIsAccessor = desc.setSetter || desc.setGetter;
bool descIsData = desc.setValue || desc.setWritable;
assert(
(!descIsData || !descIsAccessor) &&
"descriptor cannot be both Data and Accessor");
if (!descIsData && !descIsAccessor) {
return ExecutionStatus::RETURNED;
}
// 6. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc)
// have different results, then
bool currentIsAccessor = current.flags.accessor;
bool currentIsData = !currentIsAccessor;
if (currentIsData != descIsData) {
// a. If current.[[Configurable]] is false, return false.
if (!current.flags.configurable) {
return runtime->raiseTypeError(
TwineChar16("trap result is ") +
(currentIsData ? "data " : "accessor ") + "but target property is " +
(descIsData ? "data " : "accessor ") + "and non-configurable");
}
}
// 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both
// true, then
// a. If current.[[Configurable]] is false and current.[[Writable]] is
// false, then
if (currentIsData && descIsData && !current.flags.configurable &&
!current.flags.writable) {
// i. If Desc.[[Writable]] is present and Desc.[[Writable]] is true,
// return false.
if (desc.setWritable && desc.writable) {
return runtime->raiseTypeError(
"trap result is writable but "
"target property is non-configurable and non-writable");
}
// ii. If Desc.[[Value]] is present and SameValue(Desc.[[Value]],
// current.[[Value]]) is false, return false.
if (desc.setValue &&
!isSameValue(
descValueOrAccessor.getHermesValue(),
currentValueOrAccessor.getHermesValue())) {
return runtime->raiseTypeError(
"trap result has different value than target property but "
"target property is non-configurable and non-writable");
}
// iii. Return true.
return ExecutionStatus::RETURNED;
}
// 8. Else IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) are
// both true,
// a. If current.[[Configurable]] is false, then
if (currentIsAccessor && descIsAccessor && !current.flags.configurable) {
PropertyAccessor *descAccessor =
vmcast<PropertyAccessor>(descValueOrAccessor.get());
PropertyAccessor *currentAccessor =
vmcast<PropertyAccessor>(currentValueOrAccessor.get());
// i. If Desc.[[Set]] is present and SameValue(Desc.[[Set]],
// current.[[Set]]) is false, return false.
if (descAccessor->setter &&
descAccessor->setter != currentAccessor->setter) {
return runtime->raiseTypeError(
"trap result has different setter than target property but "
"target property is non-configurable");
}
// ii. If Desc.[[Get]] is present and SameValue(Desc.[[Get]],
// current.[[Get]]) is false, return false.
if (descAccessor->getter &&
descAccessor->getter != currentAccessor->getter) {
return runtime->raiseTypeError(
"trap result has different getter than target property but "
"target property is non-configurable");
}
// iii. Return true.
}
return ExecutionStatus::RETURNED;
}
} // namespace
CallResult<PseudoHandle<JSObject>> JSProxy::getPrototypeOf(
Handle<JSObject> selfHandle,
Runtime *runtime) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::getPrototypeOf);
if (LLVM_UNLIKELY(trapRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 6. If trap is undefined, then
if (!*trapRes) {
// a. Return ? target.[[GetPrototypeOf]](P).
// All calls to the target in the no-trap case are effectively
// recursion, and so require their own scope. They also need a
// ScopedNativeDepthTracker, as it's possible to use up arbitrary
// native stack depth with nested proxies.
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
return JSObject::getPrototypeOf(target, runtime);
}
// 7. Let handlerProto be ? Call(trap, handler, « target »).
CallResult<PseudoHandle<>> handlerProtoRes = Callable::executeCall1(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue());
if (handlerProtoRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 8. If Type(handlerProto) is neither Object nor Null, throw a TypeError
// exception.
if (!(*handlerProtoRes)->isObject() && !(*handlerProtoRes)->isNull()) {
return runtime->raiseTypeError(
"getPrototypeOf trap result is neither Object nor Null");
}
Handle<JSObject> handlerProto =
runtime->makeHandle(dyn_vmcast<JSObject>(handlerProtoRes->get()));
// 9. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 10. If extensibleTarget is true, return handlerProto.
if (*extensibleRes) {
return createPseudoHandle(*handlerProto);
}
// 11. Let targetProto be ? target.[[GetPrototypeOf]]().
CallResult<PseudoHandle<JSObject>> targetProtoRes =
JSObject::getPrototypeOf(target, runtime);
if (targetProtoRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError
// exception.
if (handlerProto.get() != targetProtoRes->get()) {
return runtime->raiseTypeError(
"getPrototypeOf trap result is not the same as non-extensible target getPrototypeOf");
}
// 13. Return handlerProto.
return std::move(*targetProtoRes);
}
CallResult<bool> JSProxy::setPrototypeOf(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<JSObject> parent) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::setPrototypeOf);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[SetPrototypeOf]](V).
return JSObject::setParent(*target, runtime, *parent);
}
// 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, V
// »)).
CallResult<PseudoHandle<>> booleanTrapRes = Callable::executeCall2(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue(),
*parent ? parent.getHermesValue() : HermesValue::encodeNullValue());
if (booleanTrapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 9. If booleanTrapResult is false, return false.
if (!toBoolean(booleanTrapRes->get())) {
return false;
}
// 10. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 11. If extensibleTarget is true, return true.
if (*extensibleRes) {
return true;
}
// 12. Let targetProto be ? target.[[GetPrototypeOf]]().
CallResult<PseudoHandle<JSObject>> targetProtoRes =
JSObject::getPrototypeOf(target, runtime);
if (targetProtoRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
if (parent.get() != targetProtoRes->get()) {
return runtime->raiseTypeError(
"setPrototypeOf trap changed prototype on non-extensible target");
}
// 14. Return true.
return true;
}
CallResult<bool> JSProxy::isExtensible(
Handle<JSObject> selfHandle,
Runtime *runtime) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::isExtensible);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 6. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[IsExtensible]]().
return JSObject::isExtensible(target, runtime);
}
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target »)).
CallResult<PseudoHandle<>> res = Callable::executeCall1(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue());
if (res == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 8. Let targetResult be ? target.[[IsExtensible]]().
CallResult<bool> targetRes = JSObject::isExtensible(target, runtime);
if (targetRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 9. If SameValue(booleanTrapResult, targetResult) is false, throw
// a TypeError exception.
bool booleanTrapResult = toBoolean(res->get());
if (booleanTrapResult != *targetRes) {
return runtime->raiseTypeError(
"isExtensible trap returned different value than target");
}
// 10. Return booleanTrapResult.
return booleanTrapResult;
}
CallResult<bool> JSProxy::preventExtensions(
Handle<JSObject> selfHandle,
Runtime *runtime,
PropOpFlags opFlags) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::preventExtensions);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 6. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[PreventExtensions]]().
// We pass in opFlags here. If getThrowOnError, then this will cause
// the underlying exception to bubble up. If !getThrowOnError, then
// we don't get a chance to raise a particular exception anyway. So in
// either case, just return the CallResult.
return JSObject::preventExtensions(target, runtime, opFlags);
}
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target »)).
CallResult<PseudoHandle<>> res = Callable::executeCall1(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue());
if (res == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
bool booleanTrapResult = toBoolean(res->get());
if (booleanTrapResult) {
// a. Let targetIsExtensible be ? target.[[IsExtensible]]().
CallResult<bool> targetRes = JSObject::isExtensible(target, runtime);
if (targetRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// b. If targetIsExtensible is true, throw a TypeError exception.
if (*targetRes) {
return runtime->raiseTypeError(
"preventExtensions trap returned true for extensible target");
}
}
// 10. Return booleanTrapResult.
if (!booleanTrapResult && opFlags.getThrowOnError()) {
return runtime->raiseTypeError("preventExtensions trap returned false");
}
return booleanTrapResult;
}
CallResult<bool> JSProxy::getOwnProperty(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<> nameValHandle,
ComputedPropertyDescriptor &desc,
MutableHandle<> *valueOrAccessor) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes = detail::findTrap(
selfHandle, runtime, Predefined::getOwnPropertyDescriptor);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[GetOwnProperty]](P).
return valueOrAccessor
? JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, desc, *valueOrAccessor)
: JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, desc);
}
// 8. Let trapResultObj be ? Call(trap, handler, « target, P »).
// 9. If Type(trapResultObj) is neither Object nor Undefined, throw a
// TypeError exception.
CallResult<PseudoHandle<>> trapResultRes = Callable::executeCall2(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue(),
nameValHandle.getHermesValue());
if (trapResultRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<> trapResultObj = runtime->makeHandle(std::move(*trapResultRes));
// 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
ComputedPropertyDescriptor targetDesc;
MutableHandle<> targetValueOrAccessor{runtime};
CallResult<bool> targetDescRes = JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, targetDesc, targetValueOrAccessor);
if (targetDescRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 11. If trapResultObj is undefined, then
if (trapResultObj->isUndefined()) {
// a. If targetDesc is undefined, return undefined.
if (!*targetDescRes) {
return false;
}
// b. If targetDesc.[[Configurable]] is false, throw a TypeError
// exception.
if (!targetDesc.flags.configurable) {
return runtime->raiseTypeError(
"getOwnPropertyDescriptor trap result is not configurable");
}
// c. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// d. Assert: Type(extensibleTarget) is Boolean.
// e. If extensibleTarget is false, throw a TypeError exception.
if (!*extensibleRes) {
return runtime->raiseTypeErrorForValue(
runtime->makeHandle(detail::slots(*selfHandle).target),
" is not extensible (getOwnPropertyDescriptor target)");
}
// f. Return undefined.
return false;
} else if (!trapResultObj->isObject()) {
// 9. If Type(trapResultObj) is neither Object nor Undefined, throw a
// TypeError exception.
return runtime->raiseTypeErrorForValue(
trapResultObj,
" is not undefined or Object (Proxy getOwnPropertyDescriptor)");
}
// 12. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 13. Let resultDesc be ? ToPropertyDescriptor(trapResultObj).
// 14. Call CompletePropertyDescriptor(resultDesc).
DefinePropertyFlags resultDesc;
MutableHandle<> resultValueOrAccessor{runtime};
Handle<JSObject> trapResult = runtime->makeHandle<JSObject>(*trapResultObj);
if (LLVM_UNLIKELY(
toPropertyDescriptor(
trapResult, runtime, resultDesc, resultValueOrAccessor) ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
completePropertyDescriptor(resultDesc);
// 15. Let valid be IsCompatiblePropertyDescriptor(extensibleTarget,
// resultDesc, targetDesc).
// 16. If valid is false, throw a TypeError exception.
// ES9 9.1.6.3 ValidateAndApplyPropertyDescriptor step 2 [O is undefined]
if (!*targetDescRes) {
// a. If extensible is false, return false.
if (!*extensibleRes) {
return runtime->raiseTypeErrorForValue(
"getOwnPropertyDescriptor target is not extensible and has no property ",
nameValHandle,
"");
}
// e. return true
// this concludes steps 15 and 16.
} else {
if (LLVM_UNLIKELY(
isCompatiblePropertyDescriptor(
runtime,
resultDesc,
resultValueOrAccessor,
targetDesc,
targetValueOrAccessor) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
}
// 17. If resultDesc.[[Configurable]] is false, then
// a. If targetDesc is undefined or targetDesc.[[Configurable]] is true,
// then
// i. Throw a TypeError exception.
if (!resultDesc.configurable &&
(!*targetDescRes || targetDesc.flags.configurable)) {
return runtime->raiseTypeErrorForValue(
"getOwnPropertyDescriptor trap result is not configurable but "
"target property ",
nameValHandle,
" is configurable or non-existent");
}
// 18. Return resultDesc.
desc.flags.enumerable = resultDesc.enumerable;
desc.flags.configurable = resultDesc.configurable;
desc.flags.writable = resultDesc.writable;
if (resultDesc.setGetter || resultDesc.setSetter) {
desc.flags.accessor = true;
}
if (valueOrAccessor) {
*valueOrAccessor = std::move(resultValueOrAccessor);
}
return true;
}
CallResult<bool> JSProxy::defineOwnProperty(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<> nameValHandle,
DefinePropertyFlags dpFlags,
Handle<> valueOrAccessor,
PropOpFlags opFlags) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::defineProperty);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[GetOwnProperty]](P).
return JSObject::defineOwnComputedPrimitive(
target, runtime, nameValHandle, dpFlags, valueOrAccessor, opFlags);
}
// 8. Let descObj be FromPropertyDescriptor(Desc).
ComputedPropertyDescriptor desc;
desc.flags.accessor = dpFlags.setGetter || dpFlags.setSetter;
desc.flags.writable = dpFlags.setWritable && dpFlags.writable;
desc.flags.enumerable = dpFlags.setEnumerable && dpFlags.enumerable;
desc.flags.configurable = dpFlags.setConfigurable && dpFlags.configurable;
CallResult<HermesValue> descObjRes =
objectFromPropertyDescriptor(runtime, desc, valueOrAccessor);
if (descObjRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 9. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P,
// descObj »)).
CallResult<PseudoHandle<>> trapResultRes = Callable::executeCall3(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue(),
nameValHandle.getHermesValue(),
*descObjRes);
if (trapResultRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
bool trapResult = toBoolean(trapResultRes->get());
// 10. If booleanTrapResult is false, return false.
if (!trapResult) {
if (opFlags.getThrowOnError()) {
return runtime->raiseTypeError(
"defineProperty proxy trap returned false");
} else {
return false;
}
}
// 11. Let targetDesc be ? target.[[GetOwnProperty]](P).
ComputedPropertyDescriptor targetDesc;
MutableHandle<> targetDescValueOrAccessor{runtime};
CallResult<bool> targetDescRes = JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, targetDesc, targetDescValueOrAccessor);
if (targetDescRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 12. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 13. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] is
// false, then
// a. Let settingConfigFalse be true.
// 14. Else, let settingConfigFalse be false.
bool settingConfigFalse = dpFlags.setConfigurable && !dpFlags.configurable;
// 15. If targetDesc is undefined, then
if (!*targetDescRes) {
// a. If extensibleTarget is false, throw a TypeError exception.
if (!*extensibleRes) {
return runtime->raiseTypeError(
"defineProperty trap called for non-existent property on non-extensible target");
}
// b. If settingConfigFalse is true, throw a TypeError exception.
if (settingConfigFalse) {
return runtime->raiseTypeError(
"defineProperty trap attempted to define non-configurable property for non-existent property in the target");
}
} else {
// 16. Else targetDesc is not undefined,
// a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc,
// targetDesc) is false, throw a TypeError exception.
if (LLVM_UNLIKELY(
isCompatiblePropertyDescriptor(
runtime,
dpFlags,
valueOrAccessor,
targetDesc,
targetDescValueOrAccessor) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
// b. If settingConfigFalse is true and targetDesc.[[Configurable]] is
// true, throw a TypeError exception.
if (settingConfigFalse && targetDesc.flags.configurable) {
return runtime->raiseTypeError(
"defineProperty trap attempted to define non-configurable property for configurable property in the target");
}
}
// 17. Return true.
return true;
}
namespace {
/// Common parts of hasNamed/hasComputed
CallResult<bool> hasWithTrap(
Runtime *runtime,
Handle<> nameValHandle,
Handle<Callable> trap,
Handle<JSObject> handler,
Handle<JSObject> target) {
// 1. Assert: IsPropertyKey(P) is true.
assert(isPropertyKey(nameValHandle) && "key is not a String or Symbol");
// 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P
// »)).
CallResult<PseudoHandle<>> trapResultRes = Callable::executeCall2(
trap,
runtime,
handler,
target.getHermesValue(),
nameValHandle.getHermesValue());
if (trapResultRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
bool trapResult = toBoolean(trapResultRes->get());
// 9. If booleanTrapResult is false, then
if (!trapResult) {
// a. Let targetDesc be ? target.[[GetOwnProperty]](P).
ComputedPropertyDescriptor targetDesc;
CallResult<bool> targetDescRes = JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, targetDesc);
if (targetDescRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// b. If targetDesc is not undefined, then
if (*targetDescRes) {
// i. If targetDesc.[[Configurable]] is false, throw a TypeError
// exception.
if (!targetDesc.flags.configurable) {
return runtime->raiseTypeError(
"HasProperty trap result is not configurable");
}
// ii. Let extensibleTarget be ? IsExtensible(target).
CallResult<bool> extensibleRes = JSObject::isExtensible(target, runtime);
if (extensibleRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// iii. If extensibleTarget is false, throw a TypeError exception.
if (!*extensibleRes) {
return runtime->raiseTypeError(
"HasProperty proxy target is not extensible");
}
}
}
// 11. Return trapResult.
return trapResult;
}
} // namespace
CallResult<bool> JSProxy::hasNamed(
Handle<JSObject> selfHandle,
Runtime *runtime,
SymbolID name) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::has);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[HasProperty]](P, Receiver).
return JSObject::hasNamed(target, runtime, name);
}
return hasWithTrap(
runtime,
runtime->makeHandle(HermesValue::encodeStringValue(
runtime->getStringPrimFromSymbolID(name))),
*trapRes,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target);
}
CallResult<bool> JSProxy::hasComputed(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<> nameValHandle) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::has);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[HasProperty]](P, Receiver).
return JSObject::hasComputed(target, runtime, nameValHandle);
}
return hasWithTrap(
runtime,
nameValHandle,
*trapRes,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target);
}
namespace {
/// Common parts of getNamed/getComputed
CallResult<PseudoHandle<>> getWithTrap(
Runtime *runtime,
Handle<> nameValHandle,
Handle<Callable> trap,
Handle<JSObject> handler,
Handle<JSObject> target,
Handle<> receiver) {
// 1. Assert: IsPropertyKey(P) is true.
assert(isPropertyKey(nameValHandle) && "key is not a String or Symbol");
// 8. Let trapResult be ? Call(trap, handler, « target, P, Receiver »).
CallResult<PseudoHandle<>> trapResultRes = Callable::executeCall3(
trap,
runtime,
handler,
target.getHermesValue(),
nameValHandle.getHermesValue(),
receiver.getHermesValue());
if (trapResultRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<> trapResult = runtime->makeHandle(std::move(*trapResultRes));
// 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
ComputedPropertyDescriptor targetDesc;
MutableHandle<> targetValueOrAccessor{runtime};
CallResult<bool> targetDescRes = JSObject::getOwnComputedDescriptor(
target, runtime, nameValHandle, targetDesc, targetValueOrAccessor);
if (targetDescRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 10. If targetDesc is not undefined and targetDesc.[[Configurable]] is
// false, then
if (*targetDescRes && !targetDesc.flags.configurable) {
// a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]]
// is false, then
if (!targetDesc.flags.accessor && !targetDesc.flags.writable) {
// i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a
// TypeError exception.
if (!isSameValue(*trapResult, targetValueOrAccessor.getHermesValue())) {
return runtime->raiseTypeError(
"target property is non-configurable and non-writable, and get trap result differs from target property value");
}
}
// b. If IsAccessorDescriptor(targetDesc) is true and targetDesc.[[Get]]
// is undefined, then
// i. If trapResult is not undefined, throw a TypeError exception.
if (targetDesc.flags.accessor &&
!vmcast<PropertyAccessor>(*targetValueOrAccessor)->getter &&
!trapResult->isUndefined()) {
return runtime->raiseTypeError(
"target property is non-configurable accessor with no getter, but get trap returned not undefined");
}
}
// 11. Return trapResult.
return {trapResult};
}
} // namespace
CallResult<PseudoHandle<>> JSProxy::getNamed(
Handle<JSObject> selfHandle,
Runtime *runtime,
SymbolID name,
Handle<> receiver) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::get);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);
ScopedNativeDepthTracker depthTracker(runtime);
if (LLVM_UNLIKELY(depthTracker.overflowed())) {
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
}
// a. Return ? target.[[Get]](P, Receiver).
return JSObject::getNamedWithReceiver_RJS(target, runtime, name, receiver);
}
return getWithTrap(
runtime,
name.isUniqued() ? runtime->makeHandle(HermesValue::encodeStringValue(
runtime->getStringPrimFromSymbolID(name)))
: runtime->makeHandle(name),
*trapRes,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target,
receiver);
}
CallResult<PseudoHandle<>> JSProxy::getComputed(
Handle<JSObject> selfHandle,
Runtime *runtime,
Handle<> nameValHandle,
Handle<> receiver) {
GCScope gcScope{runtime};
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, Predefined::get);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 7. If trap is undefined, then
if (!*trapRes) {
GCScope gcScope(runtime);