Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 | |
| 17 | function GeneratorObjectNext(value) { |
| 18 | if (!IS_GENERATOR(this)) { |
| 19 | throw MakeTypeError('incompatible_method_receiver', |
| 20 | ['[Generator].prototype.next', this]); |
| 21 | } |
| 22 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 23 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | function GeneratorObjectThrow(exn) { |
| 43 | if (!IS_GENERATOR(this)) { |
| 44 | throw MakeTypeError('incompatible_method_receiver', |
| 45 | ['[Generator].prototype.throw', this]); |
| 46 | } |
| 47 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 48 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | function GeneratorObjectIterator() { |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | function GeneratorFunctionPrototypeConstructor(x) { |
| 71 | if (%_IsConstructCall()) { |
| 72 | throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function GeneratorFunctionConstructor(arg1) { // length == 1 |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 77 | return NewFunctionFromString(arguments, 'function*'); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | function 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 100 | GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY); |
| 101 | %AddNamedProperty(GeneratorObjectPrototype, |
| 102 | symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | %InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 104 | %AddNamedProperty(GeneratorFunctionPrototype, |
| 105 | symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor); |
| 107 | %AddNamedProperty(GeneratorFunctionPrototype, "constructor", |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 108 | GeneratorFunction, DONT_ENUM | READ_ONLY); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | %InternalSetPrototype(GeneratorFunction, $Function); |
| 110 | %SetCode(GeneratorFunction, GeneratorFunctionConstructor); |
| 111 | } |
| 112 | |
| 113 | SetUpGenerators(); |