2013-04-11 16:28:19 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-04-11 16:28:19 +00:00
|
|
|
|
2013-04-12 15:52:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
2013-04-11 16:28:19 +00:00
|
|
|
// This file relies on the fact that the following declarations have been made
|
|
|
|
// in runtime.js:
|
|
|
|
// var $Function = global.Function;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2013-06-10 09:26:18 +00:00
|
|
|
// Generator functions and objects are specified by ES6, sections 15.19.3 and
|
|
|
|
// 15.19.4.
|
2013-04-11 16:28:19 +00:00
|
|
|
|
2013-06-10 09:26:18 +00:00
|
|
|
function GeneratorObjectNext(value) {
|
2013-04-17 15:01:25 +00:00
|
|
|
if (!IS_GENERATOR(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['[Generator].prototype.next', this]);
|
|
|
|
}
|
|
|
|
|
2014-11-12 14:28:53 +00:00
|
|
|
var continuation = %GeneratorGetContinuation(this);
|
|
|
|
if (continuation > 0) {
|
|
|
|
// Generator is suspended.
|
|
|
|
if (DEBUG_IS_ACTIVE) %DebugPrepareStepInIfStepping(this);
|
|
|
|
try {
|
|
|
|
return %_GeneratorNext(this, value);
|
|
|
|
} catch (e) {
|
|
|
|
%GeneratorClose(this);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} else if (continuation == 0) {
|
|
|
|
// Generator is already closed.
|
|
|
|
return { value: void 0, done: true };
|
|
|
|
} else {
|
|
|
|
// Generator is running.
|
|
|
|
throw MakeTypeError('generator_running', []);
|
|
|
|
}
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function GeneratorObjectThrow(exn) {
|
2013-04-17 15:01:25 +00:00
|
|
|
if (!IS_GENERATOR(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['[Generator].prototype.throw', this]);
|
|
|
|
}
|
|
|
|
|
2014-11-12 14:28:53 +00:00
|
|
|
var continuation = %GeneratorGetContinuation(this);
|
|
|
|
if (continuation > 0) {
|
|
|
|
// Generator is suspended.
|
|
|
|
try {
|
|
|
|
return %_GeneratorThrow(this, exn);
|
|
|
|
} catch (e) {
|
|
|
|
%GeneratorClose(this);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
} else if (continuation == 0) {
|
|
|
|
// Generator is already closed.
|
|
|
|
throw exn;
|
|
|
|
} else {
|
|
|
|
// Generator is running.
|
|
|
|
throw MakeTypeError('generator_running', []);
|
|
|
|
}
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 08:53:07 +00:00
|
|
|
function GeneratorObjectIterator() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-06-12 12:52:16 +00:00
|
|
|
function GeneratorFunctionPrototypeConstructor(x) {
|
|
|
|
if (%_IsConstructCall()) {
|
|
|
|
throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function GeneratorFunctionConstructor(arg1) { // length == 1
|
2014-11-12 10:06:19 +00:00
|
|
|
return NewFunctionFromString(arguments, 'function*');
|
2013-06-12 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 16:28:19 +00:00
|
|
|
function SetUpGenerators() {
|
|
|
|
%CheckIsBootstrapping();
|
2014-09-08 14:10:33 +00:00
|
|
|
|
|
|
|
// Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by
|
|
|
|
// neither Crankshaft nor TurboFan, disable optimization of wrappers here.
|
|
|
|
%NeverOptimizeFunction(GeneratorObjectNext);
|
|
|
|
%NeverOptimizeFunction(GeneratorObjectThrow);
|
|
|
|
|
|
|
|
// Set up non-enumerable functions on the generator prototype object.
|
2013-04-11 16:28:19 +00:00
|
|
|
var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
|
|
|
|
InstallFunctions(GeneratorObjectPrototype,
|
|
|
|
DONT_ENUM | DONT_DELETE | READ_ONLY,
|
|
|
|
["next", GeneratorObjectNext,
|
2013-04-24 13:00:16 +00:00
|
|
|
"throw", GeneratorObjectThrow]);
|
2014-09-08 14:10:33 +00:00
|
|
|
|
2014-06-12 17:31:54 +00:00
|
|
|
%FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]');
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty(GeneratorObjectPrototype, symbolIterator,
|
2014-06-27 13:48:37 +00:00
|
|
|
GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY);
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty(GeneratorObjectPrototype, "constructor",
|
2015-02-19 11:30:25 +00:00
|
|
|
GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY);
|
2014-10-21 17:21:32 +00:00
|
|
|
%AddNamedProperty(GeneratorObjectPrototype,
|
|
|
|
symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY);
|
2014-08-11 14:00:58 +00:00
|
|
|
%InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype);
|
2014-11-27 16:09:31 +00:00
|
|
|
%AddNamedProperty(GeneratorFunctionPrototype,
|
|
|
|
symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY);
|
2013-06-12 12:52:16 +00:00
|
|
|
%SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty(GeneratorFunctionPrototype, "constructor",
|
2015-02-19 11:30:25 +00:00
|
|
|
GeneratorFunction, DONT_ENUM | READ_ONLY);
|
2014-08-11 14:00:58 +00:00
|
|
|
%InternalSetPrototype(GeneratorFunction, $Function);
|
2013-06-12 12:52:16 +00:00
|
|
|
%SetCode(GeneratorFunction, GeneratorFunctionConstructor);
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetUpGenerators();
|