-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathproxy.rs
1777 lines (1690 loc) · 68.4 KB
/
proxy.rs
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
// 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 https://mozilla.org/MPL/2.0/.
use core::ops::{Index, IndexMut};
use std::collections::VecDeque;
use abstract_operations::{NonRevokedProxy, validate_non_revoked_proxy};
use data::ProxyHeapData;
use crate::{
ecmascript::{
abstract_operations::{
operations_on_objects::{
call, call_function, construct, create_array_from_list,
create_property_key_list_from_array_like, get_object_method, try_get_object_method,
},
testing_and_comparison::{is_constructor, is_extensible, same_value},
type_conversion::to_boolean,
},
builtins::ArgumentsList,
execution::{Agent, JsResult, agent::ExceptionType},
types::{
BUILTIN_STRING_MEMORY, Function, InternalMethods, InternalSlots, IntoObject, IntoValue,
Object, OrdinaryObject, PropertyDescriptor, PropertyKey, String, Value,
scope_property_keys,
},
},
engine::{
TryResult,
context::{Bindable, GcScope, NoGcScope},
rootable::{HeapRootData, Scopable},
},
heap::{
CreateHeapData, Heap, HeapMarkAndSweep,
indexes::{BaseIndex, ProxyIndex},
},
};
use super::ordinary::is_compatible_property_descriptor;
pub(crate) mod abstract_operations;
pub mod data;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Proxy<'a>(pub(crate) ProxyIndex<'a>);
impl Proxy<'_> {
pub(crate) const fn _def() -> Self {
Self(BaseIndex::from_u32_index(0))
}
pub(crate) const fn get_index(self) -> usize {
self.0.into_index()
}
}
// SAFETY: Property implemented as a lifetime transmute.
unsafe impl Bindable for Proxy<'_> {
type Of<'a> = Proxy<'a>;
#[inline(always)]
fn unbind(self) -> Self::Of<'static> {
unsafe { core::mem::transmute::<Self, Self::Of<'static>>(self) }
}
#[inline(always)]
fn bind<'a>(self, _gc: NoGcScope<'a, '_>) -> Self::Of<'a> {
unsafe { core::mem::transmute::<Self, Self::Of<'a>>(self) }
}
}
impl<'a> IntoValue<'a> for Proxy<'a> {
fn into_value(self) -> Value<'a> {
self.into()
}
}
impl<'a> IntoObject<'a> for Proxy<'a> {
fn into_object(self) -> Object<'a> {
self.into()
}
}
impl<'a> From<Proxy<'a>> for Value<'a> {
fn from(value: Proxy<'a>) -> Self {
Value::Proxy(value)
}
}
impl<'a> From<Proxy<'a>> for Object<'a> {
fn from(value: Proxy<'a>) -> Self {
Object::Proxy(value)
}
}
impl<'a> InternalSlots<'a> for Proxy<'a> {
#[inline(always)]
fn get_backing_object(self, _agent: &Agent) -> Option<OrdinaryObject<'static>> {
unreachable!()
}
fn set_backing_object(self, _agent: &mut Agent, _backing_object: OrdinaryObject<'static>) {
unreachable!()
}
fn create_backing_object(self, _agent: &mut Agent) -> OrdinaryObject<'static> {
unreachable!()
}
fn internal_extensible(self, _agent: &Agent) -> bool {
unreachable!();
}
fn internal_set_extensible(self, _agent: &mut Agent, _value: bool) {
unreachable!();
}
fn internal_prototype(self, _agent: &Agent) -> Option<Object<'static>> {
unreachable!();
}
fn internal_set_prototype(self, _agent: &mut Agent, _prototype: Option<Object>) {
unreachable!();
}
}
impl<'a> InternalMethods<'a> for Proxy<'a> {
fn try_get_prototype_of<'gc>(
self,
_: &mut Agent,
_: NoGcScope<'gc, '_>,
) -> TryResult<Option<Object<'gc>>> {
TryResult::Break(())
}
fn internal_get_prototype_of<'gc>(
self,
agent: &mut Agent,
mut gc: GcScope<'gc, '_>,
) -> JsResult<Option<Object<'gc>>> {
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
target,
mut handler,
} = validate_non_revoked_proxy(agent, self, gc.nogc())?;
// 5. Let trap be ? GetMethod(handler, "getPrototypeOf").
let target = target.scope(agent, gc.nogc());
let trap = if let TryResult::Continue(trap) = try_get_object_method(
agent,
handler,
BUILTIN_STRING_MEMORY.getPrototypeOf.into(),
gc.nogc(),
) {
trap?
} else {
let scoped_handler = handler.scope(agent, gc.nogc());
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.getPrototypeOf.into(),
gc.reborrow(),
)?
.map(Function::unbind)
.map(|t| t.bind(gc.nogc()));
handler = scoped_handler.get(agent).bind(gc.nogc());
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? target.[[GetPrototypeOf]]().
return target.get(agent).internal_get_prototype_of(agent, gc);
};
// 7. Let handlerProto be ? Call(trap, handler, « target »).
let handler_proto = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[target.get(agent).into()])),
gc.reborrow(),
)?
.unbind();
// 8. If handlerProto is not an Object and handlerProto is not null, throw a TypeError exception.
let handler_proto = if handler_proto.is_null() {
None
} else if let Ok(handler_proto) = Object::try_from(handler_proto) {
Some(handler_proto)
} else {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"Handler prototype must be an object or null",
gc.nogc(),
));
};
// 9. Let extensibleTarget be ? IsExtensible(target).
let extensible_target = is_extensible(agent, target.get(agent), gc.reborrow())?;
// 10. If extensibleTarget is true, return handlerProto.
if extensible_target {
return Ok(handler_proto);
}
// 11. Let targetProto be ? target.[[GetPrototypeOf]]().
let target_proto = target
.get(agent)
.internal_get_prototype_of(agent, gc.reborrow())?;
// 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError exception.
if handler_proto != target_proto {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"handlerProto and targetProto are not the same value",
gc.nogc(),
));
}
// 13. Return handlerProto.
Ok(handler_proto)
}
fn try_set_prototype_of(
self,
_: &mut Agent,
_: Option<Object>,
_: NoGcScope,
) -> TryResult<bool> {
TryResult::Break(())
}
/// ### 0.5.2 [[[SetPrototypeOf]] ( V )](https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v)
///
/// The [[SetPrototypeOf]] internal method of a Proxy exotic object O takes
/// argument V (an Object or null) and returns either a normal completion
/// containing a Boolean or a throw completion.
fn internal_set_prototype_of(
self,
agent: &mut Agent,
prototype: Option<Object>,
mut gc: GcScope,
) -> JsResult<bool> {
let nogc = gc.nogc();
let mut prototype = prototype.map(|p| p.bind(nogc));
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
mut target,
mut handler,
} = validate_non_revoked_proxy(agent, self, nogc)?;
let scoped_target = target.scope(agent, nogc);
let mut scoped_prototype = None;
// 5. Let trap be ? GetMethod(handler, "setPrototypeOf").
let trap = if let TryResult::Continue(trap) = try_get_object_method(
agent,
handler,
BUILTIN_STRING_MEMORY.setPrototypeOf.into(),
nogc,
) {
trap?
} else {
scoped_prototype = prototype.map(|p| p.scope(agent, nogc));
let scoped_handler = handler.scope(agent, nogc);
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.setPrototypeOf.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let gc = gc.nogc();
let trap = trap.map(|t| t.bind(gc));
target = scoped_target.get(agent).bind(gc);
handler = scoped_handler.get(agent).bind(gc);
prototype = scoped_prototype.as_ref().map(|p| p.get(agent).bind(gc));
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? target.[[SetPrototypeOf]](V).
return target.unbind().internal_set_prototype_of(
agent,
prototype.map(|p| p.unbind()),
gc,
);
};
let scoped_prototype = if scoped_prototype.is_none() && prototype.is_some() {
prototype.map(|p| p.scope(agent, gc.nogc()))
} else {
scoped_prototype
};
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, V »)).
let argument = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[
target.into_value().unbind(),
prototype.map_or(Value::Null, |p| p.into_value().unbind()),
])),
gc.reborrow(),
)?;
let boolean_trap_result = to_boolean(agent, argument);
// 8. If booleanTrapResult is false, return false.
if !boolean_trap_result {
return Ok(false);
}
// 9. Let extensibleTarget be ? IsExtensible(target).
let extensible_target = is_extensible(agent, scoped_target.get(agent), gc.reborrow())?;
// 10. If extensibleTarget is true, return true.
if extensible_target {
return Ok(true);
}
// 11. Let targetProto be ? target.[[GetPrototypeOf]]().
let target_proto = scoped_target
.get(agent)
.internal_get_prototype_of(agent, gc.reborrow())?;
// 12. If SameValue(V, targetProto) is false, throw a TypeError exception.
if scoped_prototype.map(|p| p.get(agent)) != target_proto {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"'setPrototypeOf' on proxy: trap returned truish for setting a new prototype on the non-extensible proxy target",
gc.nogc(),
));
}
// 13. Return true.
Ok(true)
}
fn try_is_extensible(self, _: &mut Agent, _: NoGcScope) -> TryResult<bool> {
TryResult::Break(())
}
fn internal_is_extensible(self, agent: &mut Agent, mut gc: GcScope) -> JsResult<bool> {
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
target,
mut handler,
} = validate_non_revoked_proxy(agent, self, gc.nogc())?;
// 5. Let trap be ? GetMethod(handler, "isExtensible").
let target = target.scope(agent, gc.nogc());
let trap = if let TryResult::Continue(trap) = try_get_object_method(
agent,
handler,
BUILTIN_STRING_MEMORY.isExtensible.into(),
gc.nogc(),
) {
trap?
} else {
let scoped_handler = handler.scope(agent, gc.nogc());
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.isExtensible.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let trap = trap.map(|t| t.bind(gc.nogc()));
handler = scoped_handler.get(agent).bind(gc.nogc());
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? IsExtensible(target).
return is_extensible(agent, target.get(agent), gc.reborrow());
};
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target »)).
let argument = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[target.get(agent).into()])),
gc.reborrow(),
)?;
let boolean_trap_result = to_boolean(agent, argument);
// 8. Let targetResult be ? IsExtensible(target).
let target_result = is_extensible(agent, target.get(agent), gc.reborrow())?;
// 9. If booleanTrapResult is not targetResult, throw a TypeError exception.
if boolean_trap_result != target_result {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"proxy must report same extensiblitity as target",
gc.nogc(),
));
};
// 10. Return booleanTrapResult.
Ok(boolean_trap_result)
}
fn try_prevent_extensions(self, _: &mut Agent, _: NoGcScope) -> TryResult<bool> {
TryResult::Break(())
}
fn internal_prevent_extensions(self, agent: &mut Agent, mut gc: GcScope) -> JsResult<bool> {
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
target,
mut handler,
} = validate_non_revoked_proxy(agent, self, gc.nogc())?;
// 5. Let trap be ? GetMethod(handler, "preventExtensions").
let target = target.scope(agent, gc.nogc());
let trap = if let TryResult::Continue(trap) = try_get_object_method(
agent,
handler,
BUILTIN_STRING_MEMORY.preventExtensions.into(),
gc.nogc(),
) {
trap?
} else {
let scoped_handler = handler.scope(agent, gc.nogc());
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.preventExtensions.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let trap = trap.map(|f| f.bind(gc.nogc()));
handler = scoped_handler.get(agent).bind(gc.nogc());
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? target.[[PreventExtensions]]().
return target.get(agent).internal_prevent_extensions(agent, gc);
};
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target »)).
let argument = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[target.get(agent).into()])),
gc.reborrow(),
)?;
let boolean_trap_result = to_boolean(agent, argument);
// 8. If booleanTrapResult is true, then
if boolean_trap_result {
// a. Let extensibleTarget be ? IsExtensible(target).
let extensible_target = is_extensible(agent, target.get(agent), gc.reborrow())?;
// b. If extensibleTarget is true, throw a TypeError exception.
if extensible_target {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"proxy can't report an extensible object as non-extensible",
gc.nogc(),
));
}
};
// 9. Return booleanTrapResult.
Ok(boolean_trap_result)
}
fn try_get_own_property(
self,
_: &mut Agent,
_: PropertyKey,
_: NoGcScope,
) -> TryResult<Option<PropertyDescriptor>> {
TryResult::Break(())
}
/// ### 10.5.5 [[[GetOwnProperty]] ( P )](https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p)
///
/// The [[GetOwnProperty]] internal method of a Proxy exotic object O takes
/// argument P (a property key) and returns either a normal completion
/// containing either a Property Descriptor or undefined, or a throw completion.
fn internal_get_own_property(
self,
agent: &mut Agent,
property_key: PropertyKey,
mut gc: GcScope,
) -> JsResult<Option<PropertyDescriptor>> {
let nogc = gc.nogc();
let mut property_key = property_key.bind(nogc);
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
mut target,
mut handler,
} = validate_non_revoked_proxy(agent, self, nogc)?;
let scoped_target = target.scope(agent, nogc);
let mut scoped_property_key = None;
// 5. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor").
let trap = if let TryResult::Continue(trap) = try_get_object_method(
agent,
handler,
BUILTIN_STRING_MEMORY.getOwnPropertyDescriptor.into(),
nogc,
) {
trap?
} else {
let scoped_handler = handler.scope(agent, nogc);
scoped_property_key = Some(property_key.scope(agent, nogc));
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.getOwnPropertyDescriptor.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let gc = gc.nogc();
let trap = trap.map(|f| f.bind(gc));
handler = scoped_handler.get(agent).bind(gc);
target = scoped_target.get(agent).bind(gc);
property_key = scoped_property_key.as_ref().unwrap().get(agent).bind(gc);
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? target.[[GetOwnProperty]](P).
return target
.unbind()
.internal_get_own_property(agent, property_key.unbind(), gc);
};
// 7. Let trapResultObj be ? Call(trap, handler, « target, P »).
let scoped_property_key =
scoped_property_key.unwrap_or_else(|| property_key.scope(agent, gc.nogc()));
let p = property_key.convert_to_value(agent, gc.nogc());
let trap_result_obj = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[target.unbind().into_value(), p.unbind()])),
gc.reborrow(),
)?;
// 8. If trapResultObj is not an Object and trapResultObj is not undefined, throw a TypeError exception.
let trap_result_obj_is_undefined = trap_result_obj.is_undefined();
if !trap_result_obj.is_object() && !trap_result_obj_is_undefined {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"proxy [[GetOwnProperty]] must return an object or undefined",
gc.nogc(),
));
};
let trap_result_obj = trap_result_obj.unbind().scope(agent, gc.nogc());
// 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
let target_desc = scoped_target
.get(agent)
.unbind()
.internal_get_own_property(agent, scoped_property_key.get(agent), gc.reborrow())?;
// 10. If trapResultObj is undefined, then
if trap_result_obj_is_undefined {
if let Some(target_desc) = target_desc {
// b. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
if target_desc.configurable == Some(false) {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't report a non-configurable own property '{}' as non-existent.",
scoped_property_key.get(agent).as_display(agent)
),
gc.nogc(),
));
}
} else {
// a. If targetDesc is undefined, return undefined.
return Ok(None);
}
// c. Let extensibleTarget be ? IsExtensible(target).
let extensible_target =
is_extensible(agent, scoped_target.get(agent).unbind(), gc.reborrow())?;
// d. If extensibleTarget is false, throw a TypeError exception.
if !extensible_target {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't report a extensibleTarget own property '{}' as non-existent.",
scoped_property_key.get(agent).as_display(agent)
),
gc.nogc(),
));
};
// e. Return undefined.
return Ok(None);
};
// 11. Let extensibleTarget be ? IsExtensible(target).
let extensible_target =
is_extensible(agent, scoped_target.get(agent).unbind(), gc.reborrow())?;
// 12. Let resultDesc be ? ToPropertyDescriptor(trapResultObj).
let mut result_desc = PropertyDescriptor::to_property_descriptor(
agent,
trap_result_obj.get(agent),
gc.reborrow(),
)?;
// 13. Perform CompletePropertyDescriptor(resultDesc).
result_desc.complete_property_descriptor()?;
// 14. Let valid be IsCompatiblePropertyDescriptor(extensibleTarget, resultDesc, targetDesc).
let valid = is_compatible_property_descriptor(
agent,
extensible_target,
result_desc.clone(),
target_desc.clone(),
gc.nogc(),
);
// 15. If valid is false, throw a TypeError exception.
if !valid {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"valid",
gc.nogc(),
));
};
// 16. If resultDesc.[[Configurable]] is false, then
if result_desc.configurable == Some(false) {
// a. If targetDesc is undefined or targetDesc.[[Configurable]] is true, then
if target_desc
.as_ref()
.is_none_or(|d| d.configurable == Some(true))
{
// i. Throw a TypeError exception.
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't report a non-existent property '{}' as non-configurable",
scoped_property_key.get(agent).as_display(agent)
),
gc.nogc(),
));
}
let target_desc = target_desc.unwrap();
// b. If resultDesc has a [[Writable]] field and resultDesc.[[Writable]] is false, then
if result_desc.writable == Some(false) {
// i. Assert: targetDesc has a [[Writable]] field.
assert!(target_desc.writable.is_some());
// ii. If targetDesc.[[Writable]] is true, throw a TypeError exception.
if target_desc.writable == Some(true) {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't report existing writable property '{}' as non-writable",
scoped_property_key.get(agent).as_display(agent)
),
gc.nogc(),
));
}
}
};
// 17. Return resultDesc.
Ok(Some(result_desc))
}
fn try_define_own_property(
self,
_: &mut Agent,
_: PropertyKey,
_: PropertyDescriptor,
_: NoGcScope,
) -> TryResult<bool> {
TryResult::Break(())
}
fn internal_define_own_property(
self,
agent: &mut Agent,
property_key: PropertyKey,
property_descriptor: PropertyDescriptor,
mut gc: GcScope,
) -> JsResult<bool> {
let nogc = gc.nogc();
let property_key = property_key.bind(nogc).scope(agent, nogc);
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy { target, handler } = validate_non_revoked_proxy(agent, self, nogc)?;
let scoped_target = target.scope(agent, nogc);
let scoped_handler = handler.scope(agent, nogc);
// 5. Let trap be ? GetMethod(handler, "defineProperty").
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.defineProperty.into(),
gc.reborrow(),
)?;
// 6. If trap is undefined, then
let Some(trap) = trap else {
// a. Return ? target.[[DefineOwnProperty]](P, Desc).
return scoped_target.get(agent).internal_define_own_property(
agent,
property_key.get(agent),
property_descriptor,
gc,
);
};
let trap = trap.unbind().bind(gc.nogc());
// 7. Let descObj be FromPropertyDescriptor(Desc).
let desc_obj = PropertyDescriptor::from_property_descriptor(
property_descriptor.clone().into(),
agent,
gc.nogc(),
);
// 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P, descObj »)).
let p = property_key.get(agent).convert_to_value(agent, gc.nogc());
let argument = call_function(
agent,
trap.unbind(),
scoped_handler.get(agent).into_value(),
Some(ArgumentsList(&[
scoped_target.get(agent).into_value(),
p.unbind(),
desc_obj.map_or(Value::Null, |d| d.into_value().unbind()),
])),
gc.reborrow(),
)?;
let boolean_trap_result = to_boolean(agent, argument);
// 9. If booleanTrapResult is false, return false.
if !boolean_trap_result {
return Ok(false);
};
// 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
let target_desc = scoped_target.get(agent).internal_get_own_property(
agent,
property_key.get(agent),
gc.reborrow(),
)?;
// 11. Let extensibleTarget be ? IsExtensible(target).
let extensible_target = is_extensible(agent, scoped_target.get(agent), gc.reborrow())?;
// 12. If Desc has a [[Configurable]] field and Desc.[[Configurable]] is false, then
let setting_config_false = property_descriptor.configurable == Some(false);
// 14. If targetDesc is undefined, then
let gc = gc.into_nogc();
if target_desc.is_none() {
// a. If extensibleTarget is false, throw a TypeError exception.
if !extensible_target {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't define a new property '{}' on a non-extensible object",
property_key.get(agent).as_display(agent)
),
gc,
));
}
// b. If settingConfigFalse is true, throw a TypeError exception.
if setting_config_false {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't define a non-existent '{}' property as non-configurable",
property_key.get(agent).as_display(agent)
),
gc,
));
}
} else {
// 15. Else,
// a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, targetDesc) is false, throw a TypeError exception.
if !is_compatible_property_descriptor(
agent,
extensible_target,
property_descriptor.clone(),
target_desc.clone(),
gc,
) {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't define an incompatible property descriptor ('{}', proxy can't report an existing non-configurable property as configurable)",
property_key.get(agent).as_display(agent)
),
gc,
));
};
// b. If settingConfigFalse is true and targetDesc.[[Configurable]] is true, throw a TypeError exception.
if setting_config_false {
if let Some(target_desc) = &target_desc {
if target_desc.configurable == Some(true) {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't define an incompatible property descriptor ('{}', proxy can't define an existing configurable property as non-configurable)",
property_key.get(agent).as_display(agent)
),
gc,
));
}
}
}
// c. If IsDataDescriptor(targetDesc) is true, targetDesc.[[Configurable]] is false, and targetDesc.[[Writable]] is true, then
if let Some(target_desc) = target_desc {
if property_descriptor.is_data_descriptor()
&& target_desc.configurable == Some(false)
&& target_desc.writable == Some(true)
{
// i. If Desc has a [[Writable]] field and Desc.[[Writable]] is false, throw a TypeError exception.
if let Some(writable) = property_descriptor.writable {
if !writable {
return Err(agent.throw_exception(
ExceptionType::TypeError,
format!(
"proxy can't define an incompatible property descriptor ('{}', proxy can't define an existing non-configurable writable property as non-writable)",
property_key.get(agent).as_display(agent)
),
gc,
));
}
}
}
}
};
// 16. Return true.
Ok(true)
}
fn try_has_property(self, _: &mut Agent, _: PropertyKey, _: NoGcScope) -> TryResult<bool> {
TryResult::Break(())
}
fn internal_has_property(
self,
agent: &mut Agent,
property_key: PropertyKey,
mut gc: GcScope,
) -> JsResult<bool> {
let nogc = gc.nogc();
let mut property_key = property_key.bind(nogc);
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
mut target,
mut handler,
} = validate_non_revoked_proxy(agent, self, nogc)?;
// 5. Let trap be ? GetMethod(handler, "has").
let scoped_target = target.scope(agent, nogc);
let scoped_property_key = property_key.scope(agent, nogc);
let trap = if let TryResult::Continue(trap) =
try_get_object_method(agent, handler, BUILTIN_STRING_MEMORY.has.into(), nogc)
{
trap?
} else {
let scoped_handler = handler.scope(agent, nogc);
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.has.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let gc = gc.nogc();
let trap = trap.map(|f| f.bind(gc));
handler = scoped_handler.get(agent).bind(gc);
target = scoped_target.get(agent).bind(gc);
property_key = scoped_property_key.get(agent).bind(gc);
trap
};
// 6. If trap is undefined, then
let Some(trap) = trap else {
// Return ? target.[[HasProperty]](P).
return target
.unbind()
.internal_has_property(agent, property_key.unbind(), gc);
};
// 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P »)).
let p = property_key.convert_to_value(agent, gc.nogc());
let argument = call_function(
agent,
trap.unbind(),
handler.into_value().unbind(),
Some(ArgumentsList(&[target.into_value().unbind(), p.unbind()])),
gc.reborrow(),
)?;
let boolean_trap_result = to_boolean(agent, argument);
// 8. If booleanTrapResult is false, then
if !boolean_trap_result {
// a. Let targetDesc be ? target.[[GetOwnProperty]](P).
let target_desc = scoped_target
.get(agent)
.unbind()
.internal_get_own_property(agent, scoped_property_key.get(agent), gc.reborrow())?;
// b. If targetDesc is not undefined, then
if let Some(target_desc) = target_desc {
// i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
if target_desc.configurable == Some(false) {
let property_key = scoped_property_key.get(agent).bind(gc.nogc());
let message = String::from_string(
agent,
format!(
"proxy can't report a non-configurable own property '{}' as non-existent",
property_key.as_display(agent)
),
gc.into_nogc(),
);
return Err(
agent.throw_exception_with_message(ExceptionType::TypeError, message)
);
}
// ii. Let extensibleTarget be ? IsExtensible(target).
// iii. If extensibleTarget is false, throw a TypeError exception.
if !is_extensible(agent, scoped_target.get(agent), gc.reborrow())? {
return Err(agent.throw_exception_with_static_message(
ExceptionType::TypeError,
"proxy can't report an extensible object as non-extensible",
gc.into_nogc(),
));
}
}
};
// 9. Return booleanTrapResult.
Ok(boolean_trap_result)
}
fn try_get<'gc>(
self,
_: &mut Agent,
_: PropertyKey,
_: Value,
_: NoGcScope<'gc, '_>,
) -> TryResult<Value<'gc>> {
TryResult::Break(())
}
/// ### [10.5.8 [[Get]] ( P, Receiver )](https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver)
///
/// The \[\[Get]] internal method of a Proxy exotic object O takes
/// arguments P (a property key) and Receiver (an ECMAScript language
/// value) and returns either a normal completion containing an ECMAScript
/// language value or a throw completion.
///
/// > #### Note
/// > \[\[Get]] for Proxy objects enforces the following invariants:
/// >
/// > The value reported for a property must be the same as the value of
/// > the corresponding target object property if the target object
/// > property is a non-writable, non-configurable own data property.
/// > The value reported for a property must be undefined if the
/// > corresponding target object property is a non-configurable own
/// > accessor property that has undefined as its \[\[Get]] attribute.
fn internal_get<'gc>(
self,
agent: &mut Agent,
property_key: PropertyKey,
receiver: Value,
mut gc: GcScope<'gc, '_>,
) -> JsResult<Value<'gc>> {
let nogc = gc.nogc();
let mut property_key = property_key.bind(nogc);
let mut receiver = receiver.bind(nogc);
// 1. Perform ? ValidateNonRevokedProxy(O).
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
let NonRevokedProxy {
mut target,
mut handler,
} = validate_non_revoked_proxy(agent, self, nogc)?;
// 5. Let trap be ? GetMethod(handler, "get").
let scoped_target = target.scope(agent, nogc);
let scoped_property_key = property_key.scope(agent, nogc);
let trap = if let TryResult::Continue(trap) =
try_get_object_method(agent, handler, BUILTIN_STRING_MEMORY.get.into(), nogc)
{
trap?
} else {
let scoped_handler = handler.scope(agent, nogc);
let scoped_receiver = receiver.scope(agent, nogc);
let trap = get_object_method(
agent,
handler.unbind(),
BUILTIN_STRING_MEMORY.get.into(),
gc.reborrow(),
)?
.map(Function::unbind);
let gc = gc.nogc();