Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +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 | (function(global, utils) { |
| 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | %CheckIsBootstrapping(); |
| 10 | |
| 11 | // ------------------------------------------------------------------- |
| 12 | // Imports |
| 13 | |
| 14 | var GeneratorFunctionPrototype = utils.ImportNow("GeneratorFunctionPrototype"); |
| 15 | var GeneratorFunction = utils.ImportNow("GeneratorFunction"); |
| 16 | var GlobalFunction = global.Function; |
| 17 | var MakeTypeError; |
| 18 | var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
| 19 | |
| 20 | utils.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 | |
| 29 | function 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 39 | return %_GeneratorNext(this, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | } else if (continuation == 0) { |
| 41 | // Generator is already closed. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 42 | return %_CreateIterResultObject(UNDEFINED, true); |
| 43 | } else { |
| 44 | // Generator is running. |
| 45 | throw MakeTypeError(kGeneratorRunning); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | function 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 64 | } else { |
| 65 | // Generator is running. |
| 66 | throw MakeTypeError(kGeneratorRunning); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | function 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 80 | DEBUG_PREPARE_STEP_IN_IF_STEPPING(this); |
| 81 | return %_GeneratorThrow(this, exn); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 82 | } 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 93 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | %NeverOptimizeFunction(GeneratorObjectNext); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 97 | %NeverOptimizeFunction(GeneratorObjectReturn); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | %NeverOptimizeFunction(GeneratorObjectThrow); |
| 99 | |
| 100 | // Set up non-enumerable functions on the generator prototype object. |
| 101 | var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; |
| 102 | utils.InstallFunctions(GeneratorObjectPrototype, |
| 103 | DONT_ENUM, |
| 104 | ["next", GeneratorObjectNext, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 105 | "return", GeneratorObjectReturn, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | "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 | }) |