1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "js/Iterator.h"
9 #include "js/Conversions.h"
10 #include "vm/Interpreter.h"
11 #include "vm/JSAtomState.h"
12 #include "vm/JSContext.h"
13 #include "vm/ObjectOperations.h"
14 #include "vm/SelfHosting.h"
15 #include "vm/StringType.h"
16 #include "vm/JSObject-inl.h"
22 // https://tc39.es/ecma262/#sec-getiterator
23 // GetIterator(obj [, hint [, method]])
24 JSObject
* GetIteratorObject(JSContext
* cx
, HandleValue obj
, bool isAsync
) {
25 FixedInvokeArgs
<3> args(cx
);
27 args
[1].setBoolean(isAsync
);
28 args
[2].setUndefined(); // method
31 if (!CallSelfHostedFunction(cx
, cx
->names().GetIterator
, UndefinedHandleValue
,
36 MOZ_ASSERT(rval
.isObject());
37 return &rval
.toObject();
40 // https://tc39.es/ecma262/#sec-iteratornext
41 bool IteratorNext(JSContext
* cx
, HandleObject iteratorRecord
,
42 MutableHandleValue result
) {
43 FixedInvokeArgs
<1> args(cx
);
44 args
[0].setObject(*iteratorRecord
);
45 return CallSelfHostedFunction(cx
, cx
->names().IteratorNext
,
46 UndefinedHandleValue
, args
, result
);
49 // https://tc39.es/ecma262/#sec-iteratorcomplete
50 bool IteratorComplete(JSContext
* cx
, HandleObject iterResult
, bool* done
) {
51 RootedValue
doneV(cx
);
52 if (!GetProperty(cx
, iterResult
, iterResult
, cx
->names().done
, &doneV
)) {
56 *done
= ToBoolean(doneV
);
60 // https://tc39.es/ecma262/#sec-iteratorvalue
61 bool IteratorValue(JSContext
* cx
, HandleObject iterResult
,
62 MutableHandleValue value
) {
63 return GetProperty(cx
, iterResult
, iterResult
, cx
->names().value
, value
);
66 bool GetIteratorRecordIterator(JSContext
* cx
, HandleObject iteratorRecord
,
67 MutableHandleValue iterator
) {
68 return GetProperty(cx
, iteratorRecord
, iteratorRecord
, cx
->names().iterator
,
72 static bool GetMethod(JSContext
* cx
, HandleValue v
, Handle
<PropertyName
*> name
,
73 MutableHandleValue result
) {
74 // Step 1. Let func be ? GetV(V, P).
76 if (!GetProperty(cx
, v
, name
, &func
)) {
80 // Step 2. If func is either undefined or null, return undefined.
81 if (func
.isNullOrUndefined()) {
82 result
.setUndefined();
86 // Step 3. If IsCallable(func) is false, throw a TypeError exception.
87 if (!IsCallable(func
)) {
88 return ReportIsNotFunction(cx
, func
, -1);
91 // Step 4. Return func.
96 bool GetReturnMethod(JSContext
* cx
, HandleValue iterator
,
97 MutableHandleValue result
) {
98 // Step 2. Let returnMethod be GetMethod(iterator, "return").
99 return GetMethod(cx
, iterator
, cx
->names().return_
, result
);