blob: 9ab7dcb9aab9b78baea82ca749ce129a5350be75 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +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"use strict";
6
7// This file relies on the fact that the following declarations have been made
8// in runtime.js:
9// var $Function = global.Function;
10
11// ----------------------------------------------------------------------------
12
13
14// Generator functions and objects are specified by ES6, sections 15.19.3 and
15// 15.19.4.
16
17function GeneratorObjectNext(value) {
18 if (!IS_GENERATOR(this)) {
19 throw MakeTypeError('incompatible_method_receiver',
20 ['[Generator].prototype.next', this]);
21 }
22
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023 var continuation = %GeneratorGetContinuation(this);
24 if (continuation > 0) {
25 // Generator is suspended.
26 if (DEBUG_IS_ACTIVE) %DebugPrepareStepInIfStepping(this);
27 try {
28 return %_GeneratorNext(this, value);
29 } catch (e) {
30 %GeneratorClose(this);
31 throw e;
32 }
33 } else if (continuation == 0) {
34 // Generator is already closed.
35 return { value: void 0, done: true };
36 } else {
37 // Generator is running.
38 throw MakeTypeError('generator_running', []);
39 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040}
41
42function GeneratorObjectThrow(exn) {
43 if (!IS_GENERATOR(this)) {
44 throw MakeTypeError('incompatible_method_receiver',
45 ['[Generator].prototype.throw', this]);
46 }
47
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048 var continuation = %GeneratorGetContinuation(this);
49 if (continuation > 0) {
50 // Generator is suspended.
51 try {
52 return %_GeneratorThrow(this, exn);
53 } catch (e) {
54 %GeneratorClose(this);
55 throw e;
56 }
57 } else if (continuation == 0) {
58 // Generator is already closed.
59 throw exn;
60 } else {
61 // Generator is running.
62 throw MakeTypeError('generator_running', []);
63 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064}
65
66function GeneratorObjectIterator() {
67 return this;
68}
69
70function GeneratorFunctionPrototypeConstructor(x) {
71 if (%_IsConstructCall()) {
72 throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
73 }
74}
75
76function GeneratorFunctionConstructor(arg1) { // length == 1
Emily Bernierd0a1eb72015-03-24 16:35:39 -040077 return NewFunctionFromString(arguments, 'function*');
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078}
79
80
81function SetUpGenerators() {
82 %CheckIsBootstrapping();
83
84 // Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by
85 // neither Crankshaft nor TurboFan, disable optimization of wrappers here.
86 %NeverOptimizeFunction(GeneratorObjectNext);
87 %NeverOptimizeFunction(GeneratorObjectThrow);
88
89 // Set up non-enumerable functions on the generator prototype object.
90 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
91 InstallFunctions(GeneratorObjectPrototype,
92 DONT_ENUM | DONT_DELETE | READ_ONLY,
93 ["next", GeneratorObjectNext,
94 "throw", GeneratorObjectThrow]);
95
96 %FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]');
97 %AddNamedProperty(GeneratorObjectPrototype, symbolIterator,
98 GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY);
99 %AddNamedProperty(GeneratorObjectPrototype, "constructor",
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400100 GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY);
101 %AddNamedProperty(GeneratorObjectPrototype,
102 symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 %InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400104 %AddNamedProperty(GeneratorFunctionPrototype,
105 symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
107 %AddNamedProperty(GeneratorFunctionPrototype, "constructor",
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400108 GeneratorFunction, DONT_ENUM | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 %InternalSetPrototype(GeneratorFunction, $Function);
110 %SetCode(GeneratorFunction, GeneratorFunctionConstructor);
111}
112
113SetUpGenerators();