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.
7 // This file relies on the fact that the following declarations have been made
9 // var $Function = global.Function;
11 // ----------------------------------------------------------------------------
14 // Generator functions and objects are specified by ES6, sections 15.19.3 and
17 function GeneratorObjectNext(value) {
18 if (!IS_GENERATOR(this)) {
19 throw MakeTypeError('incompatible_method_receiver',
20 ['[Generator].prototype.next', this]);
23 if (DEBUG_IS_ACTIVE) %DebugPrepareStepInIfStepping(this);
24 return %_GeneratorNext(this, value);
27 function GeneratorObjectThrow(exn) {
28 if (!IS_GENERATOR(this)) {
29 throw MakeTypeError('incompatible_method_receiver',
30 ['[Generator].prototype.throw', this]);
33 return %_GeneratorThrow(this, exn);
36 function GeneratorObjectIterator() {
40 function GeneratorFunctionPrototypeConstructor(x) {
41 if (%_IsConstructCall()) {
42 throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
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);
57 function SetUpGenerators() {
58 %CheckIsBootstrapping();
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);
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]);
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 %AddNamedProperty(GeneratorObjectPrototype,
78 symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY);
79 %InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype);
80 %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
81 %AddNamedProperty(GeneratorFunctionPrototype, "constructor",
82 GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY);
83 %InternalSetPrototype(GeneratorFunction, $Function);
84 %SetCode(GeneratorFunction, GeneratorFunctionConstructor);