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 23 if (DEBUG_IS_ACTIVE) %DebugPrepareStepInIfStepping(this); 24 return %_GeneratorNext(this, value); 25 } 26 27 function GeneratorObjectThrow(exn) { 28 if (!IS_GENERATOR(this)) { 29 throw MakeTypeError('incompatible_method_receiver', 30 ['[Generator].prototype.throw', this]); 31 } 32 33 return %_GeneratorThrow(this, exn); 34 } 35 36 function GeneratorObjectIterator() { 37 return this; 38 } 39 40 function GeneratorFunctionPrototypeConstructor(x) { 41 if (%_IsConstructCall()) { 42 throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']); 43 } 44 } 45 46 function GeneratorFunctionConstructor(arg1) { // length == 1 47 var source = NewFunctionString(arguments, 'function*'); 48 var global_proxy = %GlobalProxy(global); 49 // Compile the string in the constructor and not a helper so that errors 50 // appear to come from here. 51 var f = %_CallFunction(global_proxy, %CompileString(source, true)); 52 %FunctionMarkNameShouldPrintAsAnonymous(f); 53 return f; 54 } 55 56 57 function SetUpGenerators() { 58 %CheckIsBootstrapping(); 59 60 // Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by 61 // neither Crankshaft nor TurboFan, disable optimization of wrappers here. 62 %NeverOptimizeFunction(GeneratorObjectNext); 63 %NeverOptimizeFunction(GeneratorObjectThrow); 64 65 // Set up non-enumerable functions on the generator prototype object. 66 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; 67 InstallFunctions(GeneratorObjectPrototype, 68 DONT_ENUM | DONT_DELETE | READ_ONLY, 69 ["next", GeneratorObjectNext, 70 "throw", GeneratorObjectThrow]); 71 72 %FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]'); 73 %AddNamedProperty(GeneratorObjectPrototype, symbolIterator, 74 GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY); 75 %AddNamedProperty(GeneratorObjectPrototype, "constructor", 76 GeneratorFunctionPrototype, DONT_ENUM | DONT_DELETE | READ_ONLY); 77 %InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype); 78 %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor); 79 %AddNamedProperty(GeneratorFunctionPrototype, "constructor", 80 GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY); 81 %InternalSetPrototype(GeneratorFunction, $Function); 82 %SetCode(GeneratorFunction, GeneratorFunctionConstructor); 83 } 84 85 SetUpGenerators(); 86