blob: 3dcdcc0ffa1aeafee85384f345a4c4bd5587fefa [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5(function(global, utils) {
6
7"use strict";
8
9%CheckIsBootstrapping();
10
11// -------------------------------------------------------------------
12// Imports
13
14var GeneratorFunctionPrototype = utils.ImportNow("GeneratorFunctionPrototype");
15var GeneratorFunction = utils.ImportNow("GeneratorFunction");
16var GlobalFunction = global.Function;
17var MakeTypeError;
18var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
19
20utils.Import(function(from) {
21 MakeTypeError = from.MakeTypeError;
22});
23
24// ----------------------------------------------------------------------------
25
26// Generator functions and objects are specified by ES6, sections 15.19.3 and
27// 15.19.4.
28
29function GeneratorObjectNext(value) {
30 if (!IS_GENERATOR(this)) {
31 throw MakeTypeError(kIncompatibleMethodReceiver,
32 '[Generator].prototype.next', this);
33 }
34
35 var continuation = %GeneratorGetContinuation(this);
36 if (continuation > 0) {
37 // Generator is suspended.
38 DEBUG_PREPARE_STEP_IN_IF_STEPPING(this);
Ben Murdoch097c5b22016-05-18 11:27:45 +010039 return %_GeneratorNext(this, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 } else if (continuation == 0) {
41 // Generator is already closed.
Ben Murdoch097c5b22016-05-18 11:27:45 +010042 return %_CreateIterResultObject(UNDEFINED, true);
43 } else {
44 // Generator is running.
45 throw MakeTypeError(kGeneratorRunning);
46 }
47}
48
49
50function GeneratorObjectReturn(value) {
51 if (!IS_GENERATOR(this)) {
52 throw MakeTypeError(kIncompatibleMethodReceiver,
53 '[Generator].prototype.return', this);
54 }
55
56 var continuation = %GeneratorGetContinuation(this);
57 if (continuation > 0) {
58 // Generator is suspended.
59 DEBUG_PREPARE_STEP_IN_IF_STEPPING(this);
60 return %_GeneratorReturn(this, value);
61 } else if (continuation == 0) {
62 // Generator is already closed.
63 return %_CreateIterResultObject(value, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 } else {
65 // Generator is running.
66 throw MakeTypeError(kGeneratorRunning);
67 }
68}
69
70
71function GeneratorObjectThrow(exn) {
72 if (!IS_GENERATOR(this)) {
73 throw MakeTypeError(kIncompatibleMethodReceiver,
74 '[Generator].prototype.throw', this);
75 }
76
77 var continuation = %GeneratorGetContinuation(this);
78 if (continuation > 0) {
79 // Generator is suspended.
Ben Murdoch097c5b22016-05-18 11:27:45 +010080 DEBUG_PREPARE_STEP_IN_IF_STEPPING(this);
81 return %_GeneratorThrow(this, exn);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 } else if (continuation == 0) {
83 // Generator is already closed.
84 throw exn;
85 } else {
86 // Generator is running.
87 throw MakeTypeError(kGeneratorRunning);
88 }
89}
90
91// ----------------------------------------------------------------------------
92
Ben Murdoch097c5b22016-05-18 11:27:45 +010093// None of the three resume operations (Runtime_GeneratorNext,
94// Runtime_GeneratorReturn, Runtime_GeneratorThrow) is supported by
95// Crankshaft or TurboFan. Disable optimization of wrappers here.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096%NeverOptimizeFunction(GeneratorObjectNext);
Ben Murdoch097c5b22016-05-18 11:27:45 +010097%NeverOptimizeFunction(GeneratorObjectReturn);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098%NeverOptimizeFunction(GeneratorObjectThrow);
99
100// Set up non-enumerable functions on the generator prototype object.
101var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
102utils.InstallFunctions(GeneratorObjectPrototype,
103 DONT_ENUM,
104 ["next", GeneratorObjectNext,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100105 "return", GeneratorObjectReturn,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 "throw", GeneratorObjectThrow]);
107
108%AddNamedProperty(GeneratorObjectPrototype, "constructor",
109 GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY);
110%AddNamedProperty(GeneratorObjectPrototype,
111 toStringTagSymbol, "Generator", DONT_ENUM | READ_ONLY);
112%InternalSetPrototype(GeneratorFunctionPrototype, GlobalFunction.prototype);
113%AddNamedProperty(GeneratorFunctionPrototype,
114 toStringTagSymbol, "GeneratorFunction", DONT_ENUM | READ_ONLY);
115%AddNamedProperty(GeneratorFunctionPrototype, "constructor",
116 GeneratorFunction, DONT_ENUM | READ_ONLY);
117%InternalSetPrototype(GeneratorFunction, GlobalFunction);
118
119})