Procházet zdrojové kódy

LibJS: Implement temporal AO SnapshotOwnProperties

Shannon Booth před 1 rokem
rodič
revize
d2710ad73f

+ 17 - 0
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -487,6 +487,23 @@ ThrowCompletionOr<void> Object::copy_data_properties(VM& vm, Value source, HashT
     return {};
 }
 
+// 14.7 SnapshotOwnProperties ( source, proto [ , excludedKeys [ , excludedValues ] ] ), https://tc39.es/proposal-temporal/#sec-snapshotownproperties
+ThrowCompletionOr<NonnullGCPtr<Object>> Object::snapshot_own_properties(VM& vm, GCPtr<Object> prototype, HashTable<PropertyKey> const& excluded_keys, HashTable<Value> const& excluded_values)
+{
+    auto& realm = *vm.current_realm();
+
+    // 1. Let copy be OrdinaryObjectCreate(proto).
+    auto copy = Object::create(realm, prototype);
+
+    // 2. If excludedKeys is not present, set excludedKeys to « ».
+    // 3. If excludedValues is not present, set excludedValues to « ».
+    // 4. Perform ? CopyDataProperties(copy, source, excludedKeys, excludedValues).
+    TRY(copy->copy_data_properties(vm, Value { this }, excluded_keys, excluded_values));
+
+    // 5. Return copy.
+    return copy;
+}
+
 // 7.3.27 PrivateElementFind ( O, P ), https://tc39.es/ecma262/#sec-privateelementfind
 PrivateElement* Object::private_element_find(PrivateName const& name)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Object.h

@@ -119,6 +119,7 @@ public:
     ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const;
     ThrowCompletionOr<MarkedVector<Value>> enumerable_own_property_names(PropertyKind kind) const;
     ThrowCompletionOr<void> copy_data_properties(VM&, Value source, HashTable<PropertyKey> const& excluded_keys, HashTable<JS::Value> const& excluded_values = {});
+    ThrowCompletionOr<NonnullGCPtr<Object>> snapshot_own_properties(VM&, GCPtr<Object> prototype, HashTable<PropertyKey> const& excluded_keys = {}, HashTable<Value> const& excluded_values = {});
 
     PrivateElement* private_element_find(PrivateName const& name);
     ThrowCompletionOr<void> private_field_add(PrivateName const& name, Value value);