Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: src/builtins/builtins-iterator.cc

Issue 2407423002: [modules] Implement @@iterator on namespace objects. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/builtins/builtins-utils.h"
5 #include "src/builtins/builtins.h" 6 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h" 7 #include "src/frames-inl.h"
7 8
8 namespace v8 { 9 namespace v8 {
9 namespace internal { 10 namespace internal {
10 11
11 void Builtins::Generate_IteratorPrototypeIterator( 12 void Builtins::Generate_IteratorPrototypeIterator(
12 CodeStubAssembler* assembler) { 13 CodeStubAssembler* assembler) {
13 assembler->Return(assembler->Parameter(0)); 14 assembler->Return(assembler->Parameter(0));
14 } 15 }
15 16
17 BUILTIN(ModuleNamespaceIterator) {
18 HandleScope scope(isolate);
19 DCHECK_EQ(1, args.length());
20 Handle<Object> receiver = args.at<Object>(0);
21
22 if (!receiver->IsJSModuleNamespace()) {
23 THROW_NEW_ERROR_RETURN_FAILURE(
24 isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
25 isolate->factory()->iterator_symbol(), receiver));
26 }
27 auto ns = Handle<JSModuleNamespace>::cast(receiver);
28
29 Handle<FixedArray> names =
30 KeyAccumulator::GetKeys(ns, KeyCollectionMode::kOwnOnly, SKIP_SYMBOLS)
31 .ToHandleChecked();
32 return *isolate->factory()->NewJSFixedArrayIterator(names);
33 }
34
35 BUILTIN(FixedArrayIteratorNext) {
Benedikt Meurer 2016/10/12 03:24:05 Nice idea!
36 HandleScope scope(isolate);
37 DCHECK_EQ(1, args.length());
38 Handle<Object> receiver = args.at<Object>(0);
39
40 // It is an error if this function is called on anything other than the
41 // particular iterator object for which the function was created.
42 bool error = !receiver->IsJSFixedArrayIterator();
43 if (!error) {
44 StackFrameIterator it(isolate);
45 error = BuiltinExitFrame::cast(it.frame())->function() !=
46 Handle<JSFixedArrayIterator>::cast(receiver)->next();
47 }
neis 2016/10/11 18:36:17 This is awesome, isn't it? Is there any better wa
adamk 2016/10/11 19:42:53 This looks about right to me (this "next" function
Benedikt Meurer 2016/10/12 03:24:05 Adam is right, you need this. But please don't go
neis 2016/10/13 07:37:21 Thanks for pointing me to args.target(), that's ex
48 if (error) {
49 THROW_NEW_ERROR_RETURN_FAILURE(
50 isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
51 isolate->factory()->next_string(), receiver));
52 }
53
54 auto iterator = Handle<JSFixedArrayIterator>::cast(receiver);
55 Handle<Object> value;
56 bool done;
57
58 int index = iterator->index();
59 if (index < iterator->array()->length()) {
60 value = handle(iterator->array()->get(index), isolate);
61 done = false;
62 iterator->set_index(index + 1);
63 } else {
64 value = isolate->factory()->undefined_value();
65 done = true;
66 }
67
68 return *isolate->factory()->NewJSIteratorResult(value, done);
69 }
70
16 } // namespace internal 71 } // namespace internal
17 } // namespace v8 72 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698