Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / generator.js
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 GeneratorFunctionPrototypeConstructor(x) {
36   if (%_IsConstructCall()) {
37     throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
38   }
39 }
40
41 function GeneratorFunctionConstructor(arg1) {  // length == 1
42   var source = NewFunctionString(arguments, 'function*');
43   var global_receiver = %GlobalReceiver(global);
44   // Compile the string in the constructor and not a helper so that errors
45   // appear to come from here.
46   var f = %_CallFunction(global_receiver, %CompileString(source, true));
47   %FunctionMarkNameShouldPrintAsAnonymous(f);
48   return f;
49 }
50
51
52 function SetUpGenerators() {
53   %CheckIsBootstrapping();
54   var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
55   InstallFunctions(GeneratorObjectPrototype,
56                    DONT_ENUM | DONT_DELETE | READ_ONLY,
57                    ["next", GeneratorObjectNext,
58                     "throw", GeneratorObjectThrow]);
59   %SetProperty(GeneratorObjectPrototype, "constructor",
60                GeneratorFunctionPrototype, DONT_ENUM | DONT_DELETE | READ_ONLY);
61   %SetPrototype(GeneratorFunctionPrototype, $Function.prototype);
62   %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
63   %SetProperty(GeneratorFunctionPrototype, "constructor",
64                GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY);
65   %SetPrototype(GeneratorFunction, $Function);
66   %SetCode(GeneratorFunction, GeneratorFunctionConstructor);
67 }
68
69 SetUpGenerators();