From cd27c0d67e0e6ccd014e359b93a5f48d562d0334 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 18 Jul 2023 15:06:32 -0400 Subject: [PATCH] LibJS: Throw a TypeError early for undefined methods in GetIterator This is an editorial change in the ECMA-262 spec. See: https://github.com/tc39/ecma262/commit/784fc65 Note we were already doing this for a nicer error message. This is also what lets GetIteratorFromMethod accept the method parameter as a nonnull pointer. --- .../Libraries/LibJS/Runtime/IteratorOperations.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 9f87dcfdf5..b71171f38e 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -49,14 +49,14 @@ ThrowCompletionOr get_iterator(VM& vm, Value object, IteratorHin // i. Let syncMethod be ? GetMethod(obj, @@iterator). auto sync_method = TRY(object.get_method(vm, vm.well_known_symbol_iterator())); - // NOTE: Additional type check to produce a better error message than Call(). + // ii. If syncMethod is undefined, throw a TypeError exception. if (!sync_method) return vm.throw_completion(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects())); - // ii. Let syncIteratorRecord be ? GetIteratorFromMethod(obj, syncMethod). + // iii. Let syncIteratorRecord be ? GetIteratorFromMethod(obj, syncMethod). auto sync_iterator_record = TRY(get_iterator_from_method(vm, object, *sync_method)); - // iii. Return CreateAsyncFromSyncIterator(syncIteratorRecord). + // iv. Return CreateAsyncFromSyncIterator(syncIteratorRecord). return create_async_from_sync_iterator(vm, sync_iterator_record); } } @@ -66,11 +66,11 @@ ThrowCompletionOr get_iterator(VM& vm, Value object, IteratorHin method = TRY(object.get_method(vm, vm.well_known_symbol_iterator())); } - // NOTE: Additional type check to produce a better error message than Call(). + // 3. If method is undefined, throw a TypeError exception. if (!method) return vm.throw_completion(ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, object.to_string_without_side_effects())); - // 3. Return ? GetIteratorFromMethod(obj, method). + // 4. Return ? GetIteratorFromMethod(obj, method). return TRY(get_iterator_from_method(vm, object, *method)); }