summaryrefslogtreecommitdiffstats
path: root/js/src/vm/Iterator.cpp
blob: ce7189c49516f33e20f0cb5ceeb11d851e51e2ab (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 http://mozilla.org/MPL/2.0/. */

#include "js/Iterator.h"

#include "js/Conversions.h"
#include "vm/Interpreter.h"
#include "vm/JSAtomState.h"
#include "vm/JSContext.h"
#include "vm/ObjectOperations.h"
#include "vm/SelfHosting.h"
#include "vm/StringType.h"
#include "vm/JSContext-inl.h"  // JSContext::check
#include "vm/JSObject-inl.h"

using namespace js;

namespace JS {

// https://tc39.es/ecma262/#sec-getiterator
// GetIterator(obj [, hint [, method]])
JSObject* GetIteratorObject(JSContext* cx, HandleValue obj, bool isAsync) {
  cx->check(obj);

  FixedInvokeArgs<3> args(cx);
  args[0].set(obj);
  args[1].setBoolean(isAsync);
  args[2].setUndefined();  // method

  RootedValue rval(cx);
  if (!CallSelfHostedFunction(cx, cx->names().GetIterator, UndefinedHandleValue,
                              args, &rval)) {
    return nullptr;
  }

  MOZ_ASSERT(rval.isObject());
  return &rval.toObject();
}

// https://tc39.es/ecma262/#sec-iteratornext
bool IteratorNext(JSContext* cx, HandleObject iteratorRecord,
                  MutableHandleValue result) {
  cx->check(iteratorRecord);

  FixedInvokeArgs<1> args(cx);
  args[0].setObject(*iteratorRecord);
  return CallSelfHostedFunction(cx, cx->names().IteratorNext,
                                UndefinedHandleValue, args, result);
}

// https://tc39.es/ecma262/#sec-iteratorcomplete
bool IteratorComplete(JSContext* cx, HandleObject iterResult, bool* done) {
  cx->check(iterResult);

  RootedValue doneV(cx);
  if (!GetProperty(cx, iterResult, iterResult, cx->names().done, &doneV)) {
    return false;
  }

  *done = ToBoolean(doneV);
  return true;
}

// https://tc39.es/ecma262/#sec-iteratorvalue
bool IteratorValue(JSContext* cx, HandleObject iterResult,
                   MutableHandleValue value) {
  cx->check(iterResult);
  return GetProperty(cx, iterResult, iterResult, cx->names().value, value);
}

bool GetIteratorRecordIterator(JSContext* cx, HandleObject iteratorRecord,
                               MutableHandleValue iterator) {
  cx->check(iteratorRecord);
  return GetProperty(cx, iteratorRecord, iteratorRecord, cx->names().iterator,
                     iterator);
}

// https://tc39.es/ecma262/#sec-getmethod
static bool GetMethod(JSContext* cx, HandleValue v, Handle<PropertyName*> name,
                      MutableHandleValue result) {
  // Step 1. Let func be ? GetV(V, P).
  RootedValue func(cx);
  if (!GetProperty(cx, v, name, &func)) {
    return false;
  }

  // Step 2. If func is either undefined or null, return undefined.
  if (func.isNullOrUndefined()) {
    result.setUndefined();
    return true;
  }

  // Step 3. If IsCallable(func) is false, throw a TypeError exception.
  if (!IsCallable(func)) {
    return ReportIsNotFunction(cx, func, -1);
  }

  // Step 4. Return func.
  result.set(func);
  return true;
}

bool GetReturnMethod(JSContext* cx, HandleValue iterator,
                     MutableHandleValue result) {
  cx->check(iterator);
  // Step 2. Let returnMethod be GetMethod(iterator, "return").
  return GetMethod(cx, iterator, cx->names().return_, result);
}

}  // namespace JS