Home | History | Annotate | Download | only in src
      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   return %_GeneratorNext(this, value);
     24 }
     25 
     26 function GeneratorObjectThrow(exn) {
     27   if (!IS_GENERATOR(this)) {
     28     throw MakeTypeError('incompatible_method_receiver',
     29                         ['[Generator].prototype.throw', this]);
     30   }
     31 
     32   return %_GeneratorThrow(this, exn);
     33 }
     34 
     35 function GeneratorObjectIterator() {
     36   return this;
     37 }
     38 
     39 function GeneratorFunctionPrototypeConstructor(x) {
     40   if (%_IsConstructCall()) {
     41     throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
     42   }
     43 }
     44 
     45 function GeneratorFunctionConstructor(arg1) {  // length == 1
     46   var source = NewFunctionString(arguments, 'function*');
     47   var global_receiver = %GlobalReceiver(global);
     48   // Compile the string in the constructor and not a helper so that errors
     49   // appear to come from here.
     50   var f = %CompileString(source, true);
     51   if (!IS_FUNCTION(f)) return f;
     52   f = %_CallFunction(global_receiver, f);
     53   %FunctionMarkNameShouldPrintAsAnonymous(f);
     54   return f;
     55 }
     56 
     57 
     58 function SetUpGenerators() {
     59   %CheckIsBootstrapping();
     60   var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
     61   InstallFunctions(GeneratorObjectPrototype,
     62                    DONT_ENUM | DONT_DELETE | READ_ONLY,
     63                    ["next", GeneratorObjectNext,
     64                     "throw", GeneratorObjectThrow]);
     65   %FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]');
     66   %SetProperty(GeneratorObjectPrototype, symbolIterator, GeneratorObjectIterator,
     67       DONT_ENUM | DONT_DELETE | READ_ONLY);
     68   %SetProperty(GeneratorObjectPrototype, "constructor",
     69                GeneratorFunctionPrototype, DONT_ENUM | DONT_DELETE | READ_ONLY);
     70   %SetPrototype(GeneratorFunctionPrototype, $Function.prototype);
     71   %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
     72   %SetProperty(GeneratorFunctionPrototype, "constructor",
     73                GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY);
     74   %SetPrototype(GeneratorFunction, $Function);
     75   %SetCode(GeneratorFunction, GeneratorFunctionConstructor);
     76 }
     77 
     78 SetUpGenerators();
     79